feat: Add role-based access control tests and update server
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
bfd432d094
commit
342342423c
2 changed files with 37 additions and 0 deletions
|
|
@ -17,6 +17,11 @@ app.use(express.json());
|
|||
|
||||
app.get('/health', (_req, res) => res.json({ status: 'ok' }));
|
||||
|
||||
// Test route for role middleware
|
||||
app.get('/test-role', (_req, res) => {
|
||||
res.status(200).json({ message: 'Test route for role middleware' });
|
||||
});
|
||||
|
||||
// Metrics endpoint
|
||||
app.get('/metrics', (_req, res) => {
|
||||
const uptime = process.uptime();
|
||||
|
|
|
|||
32
backend/tests/roles.test.js
Normal file
32
backend/tests/roles.test.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
const request = require('supertest');
|
||||
const app = require('../src/server');
|
||||
const { requireRole } = require('../middleware/role.middleware');
|
||||
|
||||
describe('Role Middleware', () => {
|
||||
// Test for a route that requires 'admin' role
|
||||
it('should deny access to users without admin role', async () => {
|
||||
const response = await request(app)
|
||||
.get('/admin/users')
|
||||
.set('Authorization', 'Bearer invalid-token');
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
// Test for a route that requires 'moderator' role
|
||||
it('should deny access to users without moderator role', async () => {
|
||||
const response = await request(app)
|
||||
.get('/moderator/reports')
|
||||
.set('Authorization', 'Bearer invalid-token');
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
// Test for a route that requires 'user' role
|
||||
it('should deny access to users without user role', async () => {
|
||||
const response = await request(app)
|
||||
.get('/profile')
|
||||
.set('Authorization', 'Bearer invalid-token');
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue