Skip to content

Commit 6f1bde3

Browse files
committed
Added unit tests for metrics in the controller layer.
1 parent e1c088b commit 6f1bde3

File tree

7 files changed

+624
-12
lines changed

7 files changed

+624
-12
lines changed

src/controllers/admin/adminController.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export const loginAdmin = async (req: Request, res: Response) => {
77

88
try {
99
const { admin, token } = await adminService.loginAdmin(email, password);
10-
return res.status(200).json({ message: "Admin logged in successfully.", token, admin });
10+
return res.status(200).json({ message: "Admin logged in successfully", token, admin });
1111
} catch (error: any) {
12-
if (error.message === "Admin not found.") {
12+
if (error.message === "Admin not found") {
1313
return res.status(404).json({ message: error.message });
1414
}
15-
if (error.message === "Invalid credentials.") {
15+
if (error.message === "Invalid credentials") {
1616
return res.status(401).json({ message: error.message });
1717
}
1818

@@ -28,9 +28,9 @@ export const registerAdmin = async (req: Request, res: Response) => {
2828

2929
try {
3030
const admin = await adminService.registerAdmin(email, password);
31-
return res.status(201).json({ message: "Admin registered successfully.", admin });
31+
return res.status(201).json({ message: "Admin registered successfully", admin });
3232
} catch (error: any) {
33-
if (error.message === "Admin already exists.") {
33+
if (error.message === "Admin already exists") {
3434
return res.status(400).json({ message: error.message });
3535
}
3636

@@ -45,14 +45,14 @@ export const getAdminInfo = async (req: Request, res: Response) => {
4545
const token = req.headers.authorization?.split(" ")[1];
4646

4747
if (!token) {
48-
return res.status(401).json({ message: "Authorization token missing." });
48+
return res.status(401).json({ message: "Authorization token missing" });
4949
}
5050

5151
try {
5252
const admin = await adminService.getAdminInfoByToken(token);
5353
return res.status(200).json(admin);
5454
} catch (error: any) {
55-
if (error.message === "Admin not found.") {
55+
if (error.message === "Admin not found") {
5656
return res.status(404).json({ message: error.message });
5757
}
5858

@@ -68,9 +68,9 @@ export const activateAdmin = async (req: Request, res: Response) => {
6868

6969
try {
7070
const admin = await adminService.activateAdminById(id);
71-
return res.status(200).json({ message: "Admin activated successfully.", admin });
71+
return res.status(200).json({ message: "Admin activated successfully", admin });
7272
} catch (error: any) {
73-
if (error.message === "Admin not found.") {
73+
if (error.message === "Admin not found") {
7474
return res.status(404).json({ message: error.message });
7575
}
7676

@@ -85,9 +85,9 @@ export const deactivateAdmin = async (req: Request, res: Response) => {
8585
const { id } = req.params;
8686
try {
8787
const admin = await adminService.deactivateAdminById(id);
88-
return res.status(200).json({ message: "Admin deactivated successfully.", admin });
88+
return res.status(200).json({ message: "Admin deactivated successfully", admin });
8989
} catch (error: any) {
90-
if (error.message === "Admin not found.") {
90+
if (error.message === "Admin not found") {
9191
return res.status(404).json({ message: error.message });
9292
}
9393

src/services/admin.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ export const loginAdmin = async (
2020
return { admin, token };
2121
};
2222

23-
export const registerAdmin = async (email: string, password: string): Promise<IAdmin> => {
23+
export const registerAdmin = async (
24+
email: string,
25+
password: string
26+
): Promise<IAdmin> => {
2427
const existingAdmin = await Admin.findOne({ email });
2528
if (existingAdmin) {
2629
throw new Error("Admin already exists.");
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
import { loginAdmin, registerAdmin, getAdminInfo, activateAdmin } from '../../../src/controllers/admin/adminController';
2+
import * as adminService from '../../../src/services/admin.service';
3+
4+
jest.mock('../../../src/services/admin.service', () => ({
5+
loginAdmin: jest.fn(),
6+
registerAdmin: jest.fn(),
7+
getAdminInfoByToken: jest.fn(),
8+
activateAdminById: jest.fn()
9+
}));
10+
11+
describe('AdminController', () => {
12+
let req: any;
13+
let res: any;
14+
15+
describe('loginAdmin function', () => {
16+
beforeEach(() => {
17+
req = {
18+
body: {
19+
20+
password: 'test1@2#3'
21+
}
22+
};
23+
res = {
24+
status: jest.fn().mockReturnThis(),
25+
json: jest.fn(),
26+
};
27+
});
28+
29+
it('should return appropriate [200] when the admin is successfully logged in', async () => {
30+
const expectedAdminData = {
31+
admin: {
32+
email: req.body.email,
33+
username: 'testAdmin',
34+
role: 'superadmin'
35+
},
36+
token: 'testAdminToken'
37+
};
38+
39+
(adminService.loginAdmin as jest.Mock).mockResolvedValue(expectedAdminData);
40+
41+
await loginAdmin(req, res);
42+
43+
expect(adminService.loginAdmin).toHaveBeenCalledWith(
44+
req.body.email,
45+
req.body.password
46+
);
47+
expect(res.status).toHaveBeenCalledWith(200);
48+
expect(res.json).toHaveBeenCalledWith({
49+
message: 'Admin logged in successfully',
50+
admin: expectedAdminData.admin,
51+
token: expectedAdminData.token
52+
});
53+
});
54+
55+
it('should return appropriate [404] if admin is not found', async () => {
56+
const mockError = new Error('Admin not found');
57+
58+
(adminService.loginAdmin as jest.Mock).mockRejectedValue(mockError);
59+
60+
await loginAdmin(req, res);
61+
62+
expect(adminService.loginAdmin).toHaveBeenCalledWith(
63+
req.body.email,
64+
req.body.password
65+
);
66+
expect(res.status).toHaveBeenCalledWith(404);
67+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
68+
});
69+
70+
it('should return appropriate [401] if admin credentials are invalid', async () => {
71+
const mockError = new Error('Invalid credentials');
72+
73+
(adminService.loginAdmin as jest.Mock).mockRejectedValue(mockError);
74+
75+
await loginAdmin(req, res);
76+
77+
expect(adminService.loginAdmin).toHaveBeenCalledWith(
78+
req.body.email,
79+
req.body.password
80+
);
81+
expect(res.status).toHaveBeenCalledWith(401);
82+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
83+
});
84+
85+
it('should return appropriate [500] for an internal server error', async () => {
86+
const mockError = new Error('An error occurred while logging in admin; please try again later');
87+
88+
(adminService.loginAdmin as jest.Mock).mockRejectedValue(mockError);
89+
90+
await loginAdmin(req, res);
91+
92+
expect(adminService.loginAdmin).toHaveBeenCalledWith(
93+
req.body.email,
94+
req.body.password
95+
);
96+
expect(res.status).toHaveBeenCalledWith(500);
97+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
98+
});
99+
});
100+
101+
describe('registerAdmin function', () => {
102+
beforeEach(() => {
103+
req = {
104+
body: {
105+
106+
password: 'test1@2#3'
107+
}
108+
};
109+
res = {
110+
status: jest.fn().mockReturnThis(),
111+
json: jest.fn(),
112+
};
113+
});
114+
115+
it('should return appropriate [200] when admin registration is successful', async () => {
116+
const expectedAdminData = {
117+
admin: {
118+
email: req.body.email,
119+
username: 'testAdmin',
120+
role: 'superadmin'
121+
}
122+
};
123+
124+
(adminService.registerAdmin as jest.Mock).mockResolvedValue(expectedAdminData);
125+
126+
await registerAdmin(req, res);
127+
128+
expect(adminService.registerAdmin).toHaveBeenCalledWith(
129+
req.body.email,
130+
req.body.password
131+
);
132+
expect(res.status).toHaveBeenCalledWith(201);
133+
expect(res.json).toHaveBeenCalledWith({
134+
message: 'Admin registered successfully',
135+
admin: expectedAdminData
136+
});
137+
});
138+
139+
it('should return appropriate [400] if registered admin already exists', async () => {
140+
const mockError = new Error('Admin already exists');
141+
142+
(adminService.registerAdmin as jest.Mock).mockRejectedValue(mockError);
143+
144+
await registerAdmin(req, res);
145+
146+
expect(adminService.registerAdmin).toHaveBeenCalledWith(
147+
req.body.email,
148+
req.body.password
149+
);
150+
expect(res.status).toHaveBeenCalledWith(400);
151+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
152+
});
153+
154+
it('should return appropriate [500] for an internal server error', async () => {
155+
const mockError = new Error('An error occurred while registering admin; please try again later');
156+
157+
(adminService.registerAdmin as jest.Mock).mockRejectedValue(mockError);
158+
159+
await registerAdmin(req, res);
160+
161+
expect(adminService.registerAdmin).toHaveBeenCalledWith(
162+
req.body.email,
163+
req.body.password
164+
);
165+
expect(res.status).toHaveBeenCalledWith(500);
166+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
167+
});
168+
});
169+
170+
describe('getAdminInfo function', () => {
171+
beforeEach(() => {
172+
req = {
173+
headers: {
174+
authorization: 'Bearer t0K3n12345'
175+
}
176+
};
177+
res = {
178+
status: jest.fn().mockReturnThis(),
179+
json: jest.fn(),
180+
};
181+
});
182+
183+
it('should return appropriate [200] when admin info is successfully retrieved', async () => {
184+
const expectedAdminData = {
185+
admin: {
186+
187+
username: 'testAdmin',
188+
role: 'superadmin'
189+
}
190+
};
191+
192+
(adminService.getAdminInfoByToken as jest.Mock).mockResolvedValue(expectedAdminData);
193+
194+
await getAdminInfo(req, res);
195+
196+
expect(adminService.getAdminInfoByToken).toHaveBeenCalledWith(
197+
req.headers.authorization.split(" ")[1]
198+
);
199+
expect(res.status).toHaveBeenCalledWith(200);
200+
expect(res.json).toHaveBeenCalledWith(expectedAdminData);
201+
});
202+
203+
it('should return appropriate [404] if admin is not found', async () => {
204+
const mockError = new Error('Admin not found');
205+
206+
(adminService.getAdminInfoByToken as jest.Mock).mockRejectedValue(mockError);
207+
208+
await getAdminInfo(req, res);
209+
210+
expect(adminService.getAdminInfoByToken).toHaveBeenCalledWith(
211+
req.headers.authorization.split(" ")[1]
212+
);
213+
expect(res.status).toHaveBeenCalledWith(404);
214+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
215+
});
216+
217+
it('should return appropriate [500] for an internal server error', async () => {
218+
const mockError = new Error('An error occurred while fetching admin info; please try again later');
219+
220+
(adminService.getAdminInfoByToken as jest.Mock).mockRejectedValue(mockError);
221+
222+
await getAdminInfo(req, res);
223+
224+
expect(adminService.getAdminInfoByToken).toHaveBeenCalledWith(
225+
req.headers.authorization.split(" ")[1]
226+
);
227+
expect(res.status).toHaveBeenCalledWith(500);
228+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
229+
});
230+
});
231+
232+
describe('activateAdmin function', () => {
233+
beforeEach(() => {
234+
req = {
235+
params: {
236+
id: 'testAdminId'
237+
}
238+
};
239+
res = {
240+
status: jest.fn().mockReturnThis(),
241+
json: jest.fn(),
242+
};
243+
});
244+
245+
it('should return appropriate [200] when admin activation is successful', async () => {
246+
const expectedAdminData = {
247+
admin: {
248+
249+
username: 'testAdmin',
250+
role: 'superadmin'
251+
}
252+
};
253+
254+
(adminService.activateAdminById as jest.Mock).mockResolvedValue(expectedAdminData);
255+
256+
await activateAdmin(req, res);
257+
258+
expect(adminService.activateAdminById).toHaveBeenCalledWith(req.params.id);
259+
expect(res.status).toHaveBeenCalledWith(200);
260+
expect(res.json).toHaveBeenCalledWith({
261+
message: 'Admin activated successfully',
262+
admin: expectedAdminData
263+
});
264+
});
265+
266+
it('should return appropriate [404] if admin is not found', async () => {
267+
const mockError = new Error('Admin not found');
268+
269+
(adminService.activateAdminById as jest.Mock).mockRejectedValue(mockError);
270+
271+
await activateAdmin(req, res);
272+
273+
expect(adminService.activateAdminById).toHaveBeenCalledWith(req.params.id);
274+
expect(res.status).toHaveBeenCalledWith(404);
275+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
276+
});
277+
278+
it('should return appropriate [500] for an internal server error', async () => {
279+
const mockError = new Error('An error occurred while activating admin; please try again later');
280+
281+
(adminService.activateAdminById as jest.Mock).mockRejectedValue(mockError);
282+
283+
await activateAdmin(req, res);
284+
285+
expect(adminService.activateAdminById).toHaveBeenCalledWith(req.params.id);
286+
expect(res.status).toHaveBeenCalledWith(500);
287+
expect(res.json).toHaveBeenCalledWith({ message: mockError.message });
288+
});
289+
});
290+
});

0 commit comments

Comments
 (0)