|
| 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