From c2898a136f890517efb01995c07f084ed76f6b79 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Dec 2024 22:11:34 +0200 Subject: [PATCH 1/2] moved the line with the execution of the GET request after validation or the request was executed by the admin --- src/routes/user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/user.js b/src/routes/user.js index 9306a98d..8c560837 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -26,7 +26,6 @@ router.param('offerId', idValidation) router.use('/:id/cooperations', isEntityValid({ params }), cooperationRouter) router.use('/:id/offers', isEntityValid({ params }), offerRouter) -router.get('/', asyncWrapper(userController.getUsers)) router.get( '/:id', isEntityValid({ params }), @@ -44,6 +43,7 @@ router.patch( ) router.use(restrictTo(ADMIN)) +router.get('/', asyncWrapper(userController.getUsers)) router.patch('/:id/change-status', isEntityValid({ params }), asyncWrapper(userController.updateStatus)) router.delete('/:id', isEntityValid({ params }), asyncWrapper(userController.blockUser)) From 9977ac392d967ffc36d618d0f9ed57ffea8a3f3e Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 28 Dec 2024 22:05:03 +0200 Subject: [PATCH 2/2] added new tests for the GET request and edited the old ones --- src/test/integration/controllers/user.spec.js | 162 +++++++++--------- 1 file changed, 85 insertions(+), 77 deletions(-) diff --git a/src/test/integration/controllers/user.spec.js b/src/test/integration/controllers/user.spec.js index e3d5520c..834b5a88 100644 --- a/src/test/integration/controllers/user.spec.js +++ b/src/test/integration/controllers/user.spec.js @@ -125,83 +125,6 @@ describe('User controller', () => { expectError(401, UNAUTHORIZED, response) }) - - it('should GET all users', async () => { - const response = await app.get(endpointUrl).set('Cookie', [`accessToken=${accessToken}`]) - - expect(response.statusCode).toBe(200) - expect(Array.isArray(response.body.items)).toBeTruthy() - expect(response.body.items[response.body.items.length - 1]).toMatchObject({ - totalReviews: expect.objectContaining({ - student: expect.any(Number), - tutor: expect.any(Number) - }), - averageRating: expect.objectContaining({ - student: expect.any(Number), - tutor: expect.any(Number) - }), - status: expect.objectContaining({ - student: expect.any(String), - tutor: expect.any(String), - admin: expect.any(String) - }), - _id: expect.any(String), - role: expect.any(Array), - firstName: testUser.firstName, - lastName: testUser.lastName, - email: testUser.email, - mainSubjects: expect.objectContaining({ - tutor: expect.any(Array), - student: expect.any(Array) - }), - lastLogin: expect.any(String), - createdAt: expect.any(String), - updatedAt: expect.any(String) - }) - }) - - it('should GET all users which match query', async () => { - const query = { - email: testUser.email - } - - const response = await app - .get(endpointUrl) - .query(query) - .set('Cookie', [`accessToken=${accessToken}`]) - - expect(response.status).toBe(200) - expect(Array.isArray(response.body.items)).toBeTruthy() - expect(response.body.items.length).toBe(1) - expect(response.body.items[0]).toMatchObject({ - totalReviews: expect.objectContaining({ - student: expect.any(Number), - tutor: expect.any(Number) - }), - averageRating: expect.objectContaining({ - student: expect.any(Number), - tutor: expect.any(Number) - }), - status: expect.objectContaining({ - student: expect.any(String), - tutor: expect.any(String), - admin: expect.any(String) - }), - _id: expect.any(String), - role: expect.any(Array), - firstName: testUser.firstName, - lastName: testUser.lastName, - email: testUser.email, - mainSubjects: expect.objectContaining({ - student: expect.any(Array), - tutor: expect.any(Array) - }), - lastLogin: expect.any(String), - createdAt: expect.any(String), - updatedAt: expect.any(String) - }) - expect(response.body.count).toBe(1) - }) }) describe(`GET ${endpointUrl}:id`, () => { @@ -571,6 +494,91 @@ describe('User controller', () => { await app.post(logoutEndpoint) }) + describe(`GET ${endpointUrl}: users`, () => { + it('should GET all users', async () => { + const response = await app.get(endpointUrl).set('Cookie', [`accessToken=${accessToken}`]) + + expect(response.statusCode).toBe(200) + expect(Array.isArray(response.body.items)).toBeTruthy() + expect(response.body.items[response.body.items.length - 1]).toMatchObject({ + totalReviews: expect.objectContaining({ + student: expect.any(Number), + tutor: expect.any(Number) + }), + averageRating: expect.objectContaining({ + student: expect.any(Number), + tutor: expect.any(Number) + }), + status: expect.objectContaining({ + student: expect.any(String), + tutor: expect.any(String), + admin: expect.any(String) + }), + _id: expect.any(String), + role: expect.any(Array), + firstName: adminUser.firstName, + lastName: adminUser.lastName, + email: adminUser.email, + mainSubjects: expect.objectContaining({ + tutor: expect.any(Array), + student: expect.any(Array) + }), + lastLogin: expect.any(String), + createdAt: expect.any(String), + updatedAt: expect.any(String) + }) + }) + + it('should throw FORBIDDEN', async () => { + accessToken = await testUserAuthentication(app) + const response = await app.get(endpointUrl).set('Cookie', [`accessToken=${accessToken}`]) + expect(response.statusCode).toBe(403) + }) + + it('should GET all users which match query', async () => { + const query = { + email: adminUser.email + } + + const response = await app + .get(endpointUrl) + .query(query) + .set('Cookie', [`accessToken=${accessToken}`]) + + expect(response.status).toBe(200) + expect(Array.isArray(response.body.items)).toBeTruthy() + expect(response.body.items.length).toBe(1) + expect(response.body.items[0]).toMatchObject({ + totalReviews: expect.objectContaining({ + student: expect.any(Number), + tutor: expect.any(Number) + }), + averageRating: expect.objectContaining({ + student: expect.any(Number), + tutor: expect.any(Number) + }), + status: expect.objectContaining({ + student: expect.any(String), + tutor: expect.any(String), + admin: expect.any(String) + }), + _id: expect.any(String), + role: expect.any(Array), + firstName: adminUser.firstName, + lastName: adminUser.lastName, + email: adminUser.email, + mainSubjects: expect.objectContaining({ + student: expect.any(Array), + tutor: expect.any(Array) + }), + lastLogin: expect.any(String), + createdAt: expect.any(String), + updatedAt: expect.any(String) + }) + expect(response.body.count).toBe(1) + }) + }) + describe(`UPDATE ${endpointUrl}:id/change-status`, () => { const changeStatusPath = '/change-status' const mockedStatus = { tutor: STATUS_ENUM[0] }