Skip to content

Commit f9ec85e

Browse files
committed
Add test cases for admin controller
1 parent acbd4f6 commit f9ec85e

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

backend/__tests__/adminController.test.js

+115
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,119 @@ describe('User Controller with Auth Middleware', () => {
107107
expect(res.body).toHaveProperty('message', 'Access denied. Admins only.');
108108
});
109109
});
110+
111+
describe('Delete User', () => {
112+
let userToDelete;
113+
114+
beforeEach(async () => {
115+
userToDelete = new User({ email: '[email protected]', password: 'password123', isAdmin: false });
116+
await userToDelete.save();
117+
});
118+
119+
it('should allow admin to delete a user', async () => {
120+
const res = await request(app)
121+
.delete(`/users/${userToDelete._id}`)
122+
.set('Authorization', `Bearer ${adminToken}`);
123+
124+
expect(res.statusCode).toBe(200);
125+
expect(res.body).toHaveProperty('message', 'User deleted successfully');
126+
});
127+
128+
it('should deny delete access to non-admin users', async () => {
129+
const res = await request(app)
130+
.delete(`/users/${userToDelete._id}`)
131+
.set('Authorization', `Bearer ${userToken}`);
132+
133+
expect(res.statusCode).toBe(403);
134+
expect(res.body).toHaveProperty('message', 'Access denied. Admins only.');
135+
});
136+
137+
it('should return 404 if the user to be deleted does not exist', async () => {
138+
const nonExistentId = new mongoose.Types.ObjectId();
139+
const res = await request(app)
140+
.delete(`/users/${nonExistentId}`)
141+
.set('Authorization', `Bearer ${adminToken}`);
142+
143+
expect(res.statusCode).toBe(404);
144+
expect(res.body).toHaveProperty('message', 'User not found');
145+
});
146+
});
147+
148+
describe('Update User', () => {
149+
let userToUpdate;
150+
151+
beforeEach(async () => {
152+
userToUpdate = new User({ email: '[email protected]', password: 'password123', isAdmin: false });
153+
await userToUpdate.save();
154+
});
155+
156+
it('should allow admin to update a user', async () => {
157+
const res = await request(app)
158+
.put(`/users/${userToUpdate._id}`)
159+
.set('Authorization', `Bearer ${adminToken}`)
160+
.send({ email: '[email protected]', password: 'newpassword123', isAdmin: true });
161+
162+
expect(res.statusCode).toBe(200);
163+
expect(res.body.email).toBe('[email protected]');
164+
expect(res.body.isAdmin).toBe(true);
165+
});
166+
167+
it('should deny update access to non-admin users', async () => {
168+
const res = await request(app)
169+
.put(`/users/${userToUpdate._id}`)
170+
.set('Authorization', `Bearer ${userToken}`)
171+
.send({ email: '[email protected]', password: 'newpassword123', isAdmin: true });
172+
173+
expect(res.statusCode).toBe(403);
174+
expect(res.body).toHaveProperty('message', 'Access denied. Admins only.');
175+
});
176+
177+
it('should return 404 if the user to be updated does not exist', async () => {
178+
const nonExistentId = new mongoose.Types.ObjectId();
179+
const res = await request(app)
180+
.put(`/users/${nonExistentId}`)
181+
.set('Authorization', `Bearer ${adminToken}`)
182+
.send({ email: '[email protected]', password: 'newpassword123', isAdmin: true });
183+
184+
expect(res.statusCode).toBe(404);
185+
expect(res.body).toHaveProperty('message', 'User not found');
186+
});
187+
});
188+
189+
describe('Get All Users', () => {
190+
beforeEach(async () => {
191+
// Create multiple users to test retrieval
192+
await User.insertMany([
193+
{ email: '[email protected]', password: 'password123', isAdmin: false },
194+
{ email: '[email protected]', password: 'password123', isAdmin: false },
195+
{ email: '[email protected]', password: 'password123', isAdmin: true }
196+
]);
197+
});
198+
199+
it('should allow admin to get all users', async () => {
200+
const res = await request(app)
201+
.get('/users')
202+
.set('Authorization', `Bearer ${adminToken}`);
203+
204+
expect(res.statusCode).toBe(200);
205+
expect(res.body.length).toBeGreaterThan(0);
206+
expect(res.body).toEqual(
207+
expect.arrayContaining([
208+
expect.objectContaining({ email: '[email protected]' }),
209+
expect.objectContaining({ email: '[email protected]' }),
210+
expect.objectContaining({ email: '[email protected]' })
211+
])
212+
);
213+
});
214+
215+
it('should deny access to non-admin users when getting all users', async () => {
216+
const res = await request(app)
217+
.get('/users')
218+
.set('Authorization', `Bearer ${userToken}`);
219+
220+
expect(res.statusCode).toBe(403);
221+
expect(res.body).toHaveProperty('message', 'Access denied. Admins only.');
222+
});
223+
});
224+
110225
});

0 commit comments

Comments
 (0)