feat: Add integration tests for role middleware
Some checks are pending
Docker Test / test (push) Waiting to run
Some checks are pending
Docker Test / test (push) Waiting to run
This commit is contained in:
parent
6e973936a7
commit
387aec880f
1 changed files with 25 additions and 39 deletions
|
|
@ -1,83 +1,69 @@
|
|||
const request = require('supertest');
|
||||
const app = require('../app');
|
||||
const { requireRole } = require('../middleware/role.middleware');
|
||||
|
||||
describe('Role Middleware', () => {
|
||||
// Mock a user with a specific role for testing
|
||||
const mockUserWithRole = (role) => {
|
||||
return {
|
||||
role: role,
|
||||
id: 'test-user-id'
|
||||
};
|
||||
};
|
||||
|
||||
// Test that the middleware allows access to users with correct roles
|
||||
test('should allow access to users with correct roles', () => {
|
||||
const mockReq = {
|
||||
user: mockUserWithRole('admin')
|
||||
const req = {
|
||||
user: { role: 'admin' }
|
||||
};
|
||||
const mockRes = {
|
||||
const res = {
|
||||
status: jest.fn().mockReturnThis(),
|
||||
json: jest.fn()
|
||||
};
|
||||
const mockNext = jest.fn();
|
||||
const next = jest.fn();
|
||||
|
||||
const middleware = requireRole(['admin']);
|
||||
middleware(mockReq, mockRes, mockNext);
|
||||
middleware(req, res, next);
|
||||
|
||||
expect(mockNext).toHaveBeenCalled();
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
// Test that the middleware denies access to users with incorrect roles
|
||||
test('should deny access to users with incorrect roles', () => {
|
||||
const mockReq = {
|
||||
user: mockUserWithRole('user')
|
||||
const req = {
|
||||
user: { role: 'user' }
|
||||
};
|
||||
const mockRes = {
|
||||
const res = {
|
||||
status: jest.fn().mockReturnThis(),
|
||||
json: jest.fn()
|
||||
};
|
||||
const mockNext = jest.fn();
|
||||
const next = jest.fn();
|
||||
|
||||
const middleware = requireRole(['admin']);
|
||||
middleware(mockReq, mockRes, mockNext);
|
||||
middleware(req, res, next);
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(403);
|
||||
expect(mockRes.json).toHaveBeenCalledWith({ error: 'Forbidden' });
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'Forbidden' });
|
||||
});
|
||||
|
||||
// Test that the middleware denies access to users without roles
|
||||
test('should deny access to users without roles', () => {
|
||||
const mockReq = {
|
||||
const req = {
|
||||
user: null
|
||||
};
|
||||
const mockRes = {
|
||||
const res = {
|
||||
status: jest.fn().mockReturnThis(),
|
||||
json: jest.fn()
|
||||
};
|
||||
const mockNext = jest.fn();
|
||||
const next = jest.fn();
|
||||
|
||||
const middleware = requireRole(['admin']);
|
||||
middleware(mockReq, mockRes, mockNext);
|
||||
middleware(req, res, next);
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(401);
|
||||
expect(mockRes.json).toHaveBeenCalledWith({ error: 'Unauthorized' });
|
||||
expect(res.status).toHaveBeenCalledWith(401);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'Unauthorized' });
|
||||
});
|
||||
|
||||
// Test that the middleware allows access to users with one of multiple required roles
|
||||
test('should allow access to users with one of multiple required roles', () => {
|
||||
const mockReq = {
|
||||
user: mockUserWithRole('moderator')
|
||||
const req = {
|
||||
user: { role: 'moderator' }
|
||||
};
|
||||
const mockRes = {
|
||||
const res = {
|
||||
status: jest.fn().mockReturnThis(),
|
||||
json: jest.fn()
|
||||
};
|
||||
const mockNext = jest.fn();
|
||||
const next = jest.fn();
|
||||
|
||||
const middleware = requireRole(['admin', 'moderator']);
|
||||
middleware(mockReq, mockRes, mockNext);
|
||||
middleware(req, res, next);
|
||||
|
||||
expect(mockNext).toHaveBeenCalled();
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue