-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.test.ts
More file actions
52 lines (39 loc) · 1.51 KB
/
admin.test.ts
File metadata and controls
52 lines (39 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
jest.mock('../service/stats.service', () => ({
getStats: jest.fn(),
}));
import request from 'supertest';
import express from 'express';
import * as StatsService from '../service/stats.service';
const adminRouter = require('../routes/admin.route').default;
const app = express();
app.use(express.json());
app.use('/admin', adminRouter);
describe('Admin Controller - API Stats', () => {
const ADMIN_KEY = 'super-secret-admin-key';
beforeAll(() => {
process.env.ADMIN_SECRET_KEY = ADMIN_KEY;
});
beforeEach(() => {
jest.clearAllMocks();
});
it('should return 401 if x-admin-key header is missing', async () => {
const res = await request(app).get('/admin/stats');
expect(res.status).toBe(401);
expect(res.body.success).toBe(false);
});
it('should return 401 if x-admin-key is incorrect', async () => {
const res = await request(app).get('/admin/stats').set('x-admin-key', 'wrong-password-key');
expect(res.status).toBe(401);
expect(res.body.success).toBe(false);
});
it('should return 200 and statistics when x-admin-key is valid', async () => {
const mockStats = { 'GET /': 10, 'POST /login': 2 };
(StatsService.getStats as jest.Mock).mockReturnValue(mockStats);
const res = await request(app).get('/admin/stats').set('x-admin-key', ADMIN_KEY);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.message).toBe('Statistics retrieved successfully');
expect(res.body.data).toEqual(mockStats);
expect(StatsService.getStats).toHaveBeenCalledTimes(1);
});
});