diff --git a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.test.ts new file mode 100644 index 0000000..aee1d99 --- /dev/null +++ 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/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts index f9b5fa6..fc79d40 100644 --- a/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts +++ b/server/controllers/userController/deleteUserByEmail/deleteUserByEmail.ts @@ -1,28 +1,21 @@ 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; - 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; 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;