|
| 1 | +const User = require('../models/User.model'); |
| 2 | +const { updateUserRole } = require('../controllers/admin.users.controller'); |
| 3 | + |
| 4 | +jest.mock('../models/User.model', () => ({ |
| 5 | + findById: jest.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +describe('Admin user role management', () => { |
| 9 | + let req; |
| 10 | + let res; |
| 11 | + let next; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks(); |
| 15 | + |
| 16 | + req = { |
| 17 | + params: { id: '507f1f77bcf86cd799439011' }, |
| 18 | + body: { role: 'admin' }, |
| 19 | + userId: '507f1f77bcf86cd799439012', |
| 20 | + }; |
| 21 | + |
| 22 | + res = { |
| 23 | + status: jest.fn().mockReturnThis(), |
| 24 | + json: jest.fn().mockReturnThis(), |
| 25 | + }; |
| 26 | + |
| 27 | + next = jest.fn(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('allows an admin to promote another user', async () => { |
| 31 | + const user = { |
| 32 | + id: '507f1f77bcf86cd799439011', |
| 33 | + email: 'alice@example.com', |
| 34 | + role: 'user', |
| 35 | + save: jest.fn().mockResolvedValue(undefined), |
| 36 | + }; |
| 37 | + |
| 38 | + User.findById.mockResolvedValue(user); |
| 39 | + |
| 40 | + await updateUserRole(req, res, next); |
| 41 | + |
| 42 | + expect(User.findById).toHaveBeenCalledWith('507f1f77bcf86cd799439011'); |
| 43 | + expect(user.role).toBe('admin'); |
| 44 | + expect(user.save).toHaveBeenCalledTimes(1); |
| 45 | + expect(res.status).toHaveBeenCalledWith(200); |
| 46 | + expect(res.json).toHaveBeenCalledWith({ |
| 47 | + success: true, |
| 48 | + statusCode: 200, |
| 49 | + message: 'User role updated successfully', |
| 50 | + data: { |
| 51 | + id: '507f1f77bcf86cd799439011', |
| 52 | + email: 'alice@example.com', |
| 53 | + role: 'admin', |
| 54 | + }, |
| 55 | + }); |
| 56 | + expect(next).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('rejects invalid role values', async () => { |
| 60 | + req.body.role = 'superadmin'; |
| 61 | + |
| 62 | + await updateUserRole(req, res, next); |
| 63 | + |
| 64 | + expect(User.findById).not.toHaveBeenCalled(); |
| 65 | + expect(res.status).toHaveBeenCalledWith(400); |
| 66 | + expect(res.json).toHaveBeenCalledWith({ |
| 67 | + success: false, |
| 68 | + statusCode: 400, |
| 69 | + message: 'Role must be either user or admin', |
| 70 | + data: {}, |
| 71 | + }); |
| 72 | + expect(next).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('blocks admins from downgrading their own role', async () => { |
| 76 | + req.userId = '507f1f77bcf86cd799439011'; |
| 77 | + req.body.role = 'user'; |
| 78 | + |
| 79 | + await updateUserRole(req, res, next); |
| 80 | + |
| 81 | + expect(User.findById).not.toHaveBeenCalled(); |
| 82 | + expect(res.status).toHaveBeenCalledWith(403); |
| 83 | + expect(res.json).toHaveBeenCalledWith({ |
| 84 | + success: false, |
| 85 | + statusCode: 403, |
| 86 | + message: 'You cannot downgrade your own role', |
| 87 | + data: {}, |
| 88 | + }); |
| 89 | + expect(next).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns 404 when the user does not exist', async () => { |
| 93 | + User.findById.mockResolvedValue(null); |
| 94 | + |
| 95 | + await updateUserRole(req, res, next); |
| 96 | + |
| 97 | + expect(User.findById).toHaveBeenCalledWith('507f1f77bcf86cd799439011'); |
| 98 | + expect(res.status).toHaveBeenCalledWith(404); |
| 99 | + expect(res.json).toHaveBeenCalledWith({ |
| 100 | + success: false, |
| 101 | + statusCode: 404, |
| 102 | + message: 'User not found', |
| 103 | + data: {}, |
| 104 | + }); |
| 105 | + expect(next).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | +}); |
0 commit comments