2026-03-17 19:07:03 +00:00
|
|
|
import request from 'supertest';
|
|
|
|
|
import app from '../app.js';
|
|
|
|
|
import { requireRole } from '../middleware/role.middleware.js';
|
2026-03-17 10:09:15 +00:00
|
|
|
|
2026-03-17 17:06:42 +00:00
|
|
|
describe('Role-based Access Control', () => {
|
2026-03-17 19:07:03 +00:00
|
|
|
// Test that the middleware is properly exported
|
2026-03-17 11:07:20 +00:00
|
|
|
test('requireRole should be a function', () => {
|
|
|
|
|
expect(typeof requireRole).toBe('function');
|
2026-03-16 21:07:16 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-17 19:07:03 +00:00
|
|
|
// Mock user authentication for testing
|
|
|
|
|
const mockUser = (role) => {
|
|
|
|
|
return {
|
|
|
|
|
role: role,
|
|
|
|
|
id: 'test-user-id'
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Test that middleware allows access to users with correct roles
|
|
|
|
|
test('should allow access to users with correct roles', () => {
|
|
|
|
|
const req = {
|
|
|
|
|
user: mockUser('admin')
|
|
|
|
|
};
|
|
|
|
|
const res = {
|
2026-03-17 18:07:07 +00:00
|
|
|
status: jest.fn().mockReturnThis(),
|
|
|
|
|
json: jest.fn()
|
|
|
|
|
};
|
2026-03-17 19:07:03 +00:00
|
|
|
const next = jest.fn();
|
2026-03-17 18:07:07 +00:00
|
|
|
|
2026-03-17 17:06:42 +00:00
|
|
|
const middleware = requireRole(['admin']);
|
2026-03-17 19:07:03 +00:00
|
|
|
middleware(req, res, next);
|
2026-03-17 18:07:07 +00:00
|
|
|
|
2026-03-17 19:07:03 +00:00
|
|
|
expect(next).toHaveBeenCalled();
|
2026-03-17 17:06:42 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-17 19:07:03 +00:00
|
|
|
// Test that middleware denies access to users with incorrect roles
|
|
|
|
|
test('should deny access to users with incorrect roles', () => {
|
|
|
|
|
const req = {
|
|
|
|
|
user: mockUser('user')
|
|
|
|
|
};
|
|
|
|
|
const res = {
|
2026-03-17 18:07:07 +00:00
|
|
|
status: jest.fn().mockReturnThis(),
|
|
|
|
|
json: jest.fn()
|
|
|
|
|
};
|
2026-03-17 19:07:03 +00:00
|
|
|
const next = jest.fn();
|
2026-03-17 18:07:07 +00:00
|
|
|
|
|
|
|
|
const middleware = requireRole(['admin']);
|
2026-03-17 19:07:03 +00:00
|
|
|
middleware(req, res, next);
|
2026-03-17 18:07:07 +00:00
|
|
|
|
2026-03-17 19:07:03 +00:00
|
|
|
expect(res.status).toHaveBeenCalledWith(403);
|
|
|
|
|
expect(res.json).toHaveBeenCalledWith({ error: 'Insufficient permissions' });
|
2026-03-17 18:07:07 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-17 19:07:03 +00:00
|
|
|
// Test that middleware denies access to unauthenticated users
|
|
|
|
|
test('should deny access to unauthenticated users', () => {
|
|
|
|
|
const req = {
|
|
|
|
|
user: null
|
|
|
|
|
};
|
|
|
|
|
const res = {
|
2026-03-17 18:07:07 +00:00
|
|
|
status: jest.fn().mockReturnThis(),
|
|
|
|
|
json: jest.fn()
|
|
|
|
|
};
|
2026-03-17 19:07:03 +00:00
|
|
|
const next = jest.fn();
|
2026-03-17 18:07:07 +00:00
|
|
|
|
2026-03-17 17:06:42 +00:00
|
|
|
const middleware = requireRole(['admin']);
|
2026-03-17 19:07:03 +00:00
|
|
|
middleware(req, res, next);
|
|
|
|
|
|
|
|
|
|
expect(res.status).toHaveBeenCalledWith(401);
|
|
|
|
|
expect(res.json).toHaveBeenCalledWith({ error: 'Authentication required' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Test that middleware allows access to users with one of multiple roles
|
|
|
|
|
test('should allow access to users with one of multiple roles', () => {
|
|
|
|
|
const req = {
|
|
|
|
|
user: mockUser('moderator')
|
|
|
|
|
};
|
|
|
|
|
const res = {
|
|
|
|
|
status: jest.fn().mockReturnThis(),
|
|
|
|
|
json: jest.fn()
|
|
|
|
|
};
|
|
|
|
|
const next = jest.fn();
|
|
|
|
|
|
|
|
|
|
const middleware = requireRole(['admin', 'moderator']);
|
|
|
|
|
middleware(req, res, next);
|
2026-03-17 18:07:07 +00:00
|
|
|
|
2026-03-17 19:07:03 +00:00
|
|
|
expect(next).toHaveBeenCalled();
|
2026-03-16 21:07:16 +00:00
|
|
|
});
|
|
|
|
|
});
|