From 917acb64fa231242762daee3046e146ce18afe64 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 10 Aug 2024 11:16:27 -0400 Subject: [PATCH 1/3] CHE-202 Refactored deleteUserByEmail to use custom error handlers --- .../deleteUserByEmail.test.ts | 0 .../deleteUserByEmail/deleteUserByEmail.ts | 19 ++++++------------- 2 files changed, 6 insertions(+), 13 deletions(-) create mode 100644 server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts diff --git a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts index f9b5fa6..c1f3165 100644 --- a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts +++ b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts @@ -1,5 +1,6 @@ import User from '../../../models/userModel'; import { Request, Response, NextFunction } from 'express'; +import { NotFoundError } from '../../../errors'; import { UserType } from '../../../types/user'; // ENDPOINT DELETE api/users/:email @@ -8,21 +9,13 @@ import { UserType } from '../../../types/user'; const deleteUserByEmail = async (req: Request, res: Response, next: NextFunction) => { const { email } = req.params; - try { - const user: UserType | null = await User.findOneAndDelete({ email }); + const user: UserType | null = await User.findOneAndDelete({ email }); - if (!user) { - return res.status(404).json({ msg: 'User not found!' }); //TODO Move to global error handler - } - - return res.status(200).json({ msg: 'User successfully deleted!' }); - } catch (error) { - return next({ - log: 'Express error in getUserByEmail Middleware', - status: 500, - message: { err: 'An error occurred during removal' }, - }); + if (!user) { + throw new NotFoundError(); } + + return res.status(200).json({ msg: 'User successfully deleted!' }); }; export default deleteUserByEmail; From 9d729af2a0be77ac368e2a6e2ff68ea0ad2af255 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 10 Aug 2024 11:42:50 -0400 Subject: [PATCH 2/3] CHE-202 Test suite complete --- .../deleteUserByEmail.test.ts | 75 +++++++++++++++++++ server/routes/userRoutes.ts | 8 +- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts index e69de29..aee1d99 100644 --- a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts +++ b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts @@ -0,0 +1,75 @@ +import app from '../../../app'; +import request, { Response } from 'supertest'; +import User from '../../../models/userModel'; + +const testEmail = 'john.doe@codehammers.com'; +const nonExistentEmail = 'nonexistent@codehammers.com'; +const testPassword = 'password123'; + +const createUser = async () => { + const user = await User.create({ + firstName: 'John', + lastName: 'Doe', + email: testEmail, + password: testPassword, + }); + return user; +}; + +const loginAndGetCookie = async () => { + const response = await request(app) + .post('/api/users/login') + .send({ email: testEmail, password: testPassword }); + return response.headers['set-cookie']; +}; + +describe('Tests for userController.deleteUserByEmail', () => { + const baseUrl = '/api/users'; + + describe('Delete User By Email Failure Tests', () => { + let authCookie: string; + + beforeEach(async () => { + await User.deleteMany(); + await createUser(); + authCookie = await loginAndGetCookie(); + }); + + it('🧪 Fails if user is not found', async () => { + const response = await request(app) + .delete(`${baseUrl}/${nonExistentEmail}`) + .set('Cookie', authCookie) + .send(); + + expect(response.status).toEqual(404); + expect(response.body[0].message).toEqual('Not Found'); + }); + }); + + describe('Delete User By Email Success Tests', () => { + let successResponse: Response; + let authCookie: string; + + beforeEach(async () => { + await User.deleteMany(); + const user = await createUser(); + console.log('user: ', user); + console.log('user email: ', user.email); + console.log('test email: ', testEmail); + authCookie = await loginAndGetCookie(); + }); + + it('🧪 Deletes the user successfully with a 200 status', async () => { + successResponse = await request(app) + .delete(`${baseUrl}/${testEmail}`) + .set('Cookie', authCookie) + .send(); + + expect(successResponse.status).toEqual(200); + expect(successResponse.body.msg).toEqual('User successfully deleted!'); + + const user = await User.findOne({ email: testEmail }); + expect(user).toBeNull(); + }); + }); +}); diff --git a/server/routes/userRoutes.ts b/server/routes/userRoutes.ts index 8525eed..ea0df63 100644 --- a/server/routes/userRoutes.ts +++ b/server/routes/userRoutes.ts @@ -1,11 +1,7 @@ import express from 'express'; import { protect } from '../middleware/authMiddleware'; import registerUser from '../controllers/userController/registerUser/registerUser'; -import { - authUser, - getUserById, - // deleteUserByEmail, -} from '../controllers/userController'; +import { authUser, getUserById, deleteUserByEmail } from '../controllers/userController'; const router = express.Router(); @@ -15,6 +11,6 @@ router.post('/register', registerUser); router.use(protect); /* Require Auth for ALL routes below */ router.get('/:userId', getUserById); //TODO Disabled until admin auth is created -//router.delete("/:email", deleteUserByEmail); +router.delete('/:email', deleteUserByEmail); export default router; From c665114a6cf96e97a080afd2c22777e286afb530 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 10 Aug 2024 11:48:48 -0400 Subject: [PATCH 3/3] CHE-202 Linting fix --- .../userController/deleteUserByEmail/deleteUserByEmail.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts index c1f3165..fc79d40 100644 --- a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts +++ b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts @@ -1,12 +1,12 @@ import User from '../../../models/userModel'; -import { Request, Response, NextFunction } from 'express'; +import { Request, Response } from 'express'; import { NotFoundError } from '../../../errors'; import { UserType } from '../../../types/user'; // ENDPOINT DELETE api/users/:email // PURPOSE Delete user by email // ACCESS Private -const deleteUserByEmail = async (req: Request, res: Response, next: NextFunction) => { +const deleteUserByEmail = async (req: Request, res: Response) => { const { email } = req.params; const user: UserType | null = await User.findOneAndDelete({ email });