From cd5f274db69e209312a5c2be3eb819622f071403 Mon Sep 17 00:00:00 2001 From: Brok3Turtl3 Date: Thu, 8 Aug 2024 16:43:23 -0400 Subject: [PATCH] CHE-84 Test Suite complete --- .../userController/authUser/authUser.test.ts | 79 +++++++++++++++++-- .../userController/authUser/authUser.ts | 10 +-- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/server/controllers/userController/authUser/authUser.test.ts b/server/controllers/userController/authUser/authUser.test.ts index 7ceae5b..af440ed 100644 --- a/server/controllers/userController/authUser/authUser.test.ts +++ b/server/controllers/userController/authUser/authUser.test.ts @@ -1,30 +1,93 @@ import app from '../../../app'; import request, { Response } from 'supertest'; import User from '../../../models/userModel'; - -import { ValidationError, RequestValidationError } from '../../../errors'; -import { IUser } from '../../../types/user'; +import { NotAuthorizedError } from '../../../errors'; const testEmail = 'tester@codehammers.com'; const testPassword = 'ilovetesting'; -// TODO -/*eslint jest/no-disabled-tests: "off"*/ +const createUser = async () => { + const user = await User.create({ + firstName: 'Test', + lastName: 'User', + email: testEmail, + password: testPassword, + }); + return user; +}; describe('Tests for userController.authUser', () => { + const baseUrl = '/api/users/login'; describe('Auth Failure Tests', () => { - it('Fails if invalid email is provided', async () => { + beforeEach(async () => { + await User.deleteMany(); + }); + + it('🧪 Fails if invalid email is provided', async () => { const response = await request(app) - .post('/api/users/login') + .post(baseUrl) .send({ email: 'invalid-email', password: testPassword }); expect(response.status).toEqual(400); expect(response.body[0].message).toEqual('Please enter a valid email'); expect(response.body[0].field).toEqual('email'); }); + + it('🧪 Fails if no email is provided', async () => { + const response = await request(app).post(baseUrl).send({ password: testPassword }); + + expect(response.status).toEqual(400); + expect(response.body[0].message).toEqual('You must enter an email'); + expect(response.body[0].field).toEqual('email'); + }); + + it('🧪 Fails if no password is provided', async () => { + const response = await request(app).post(baseUrl).send({ email: testEmail }); + + expect(response.status).toEqual(400); + expect(response.body[0].message).toEqual('You must enter a password'); + expect(response.body[0].field).toEqual('password'); + }); + + it('🧪 Fails if user does not exist', async () => { + const response = await request(app) + .post(baseUrl) + .send({ email: testEmail, password: testPassword }); + + expect(response.status).toEqual(401); + expect(response.body).toEqual(new NotAuthorizedError().serializeErrors()); + }); + + it('🧪 Fails if password is incorrect', async () => { + await createUser(); + const response = await request(app) + .post(baseUrl) + .send({ email: testEmail, password: 'wrongpassword' }); + + expect(response.status).toEqual(401); + expect(response.body).toEqual(new NotAuthorizedError().serializeErrors()); + }); }); describe('Auth Success Tests', () => { - xit('', async () => {}); + let successResponse: Response; + + beforeEach(async () => { + await User.deleteMany(); + await createUser(); + successResponse = await request(app) + .post(baseUrl) + .send({ email: testEmail, password: testPassword }); + }); + + it('🧪 Authenticates and sends back the user with a 200 status', async () => { + expect(successResponse.status).toEqual(200); + expect(successResponse.body.email).toEqual(testEmail); + }); + + it('🧪 Sends back a cookie with a token', async () => { + const cookie = successResponse.get('Set-Cookie') as string[]; + expect(cookie[0].split('=')[0]).toEqual('token'); + }); }); }); diff --git a/server/controllers/userController/authUser/authUser.ts b/server/controllers/userController/authUser/authUser.ts index 4cf9a80..ef7eaf9 100644 --- a/server/controllers/userController/authUser/authUser.ts +++ b/server/controllers/userController/authUser/authUser.ts @@ -10,11 +10,6 @@ import { UserType } from '../../../types/user'; const authUser = async (req: Request, res: Response, next: NextFunction) => { const { email, password } = req.body; - const isValidEmail = email.match(/[\w\d.]+@[a-z]+.[\w]+$/gim); - if (!isValidEmail) { - throw new RequestValidationError([new ValidationError('Please enter a valid email', 'email')]); - } - if (!email || !password) { if (!email) throw new RequestValidationError([new ValidationError('You must enter an email', 'email')]); @@ -24,6 +19,11 @@ const authUser = async (req: Request, res: Response, next: NextFunction) => { ]); } + const isValidEmail = email.match(/[\w\d.]+@[a-z]+.[\w]+$/gim); + if (!isValidEmail) { + throw new RequestValidationError([new ValidationError('Please enter a valid email', 'email')]); + } + const user: UserType | null = await User.findOne({ email }); if (!user) {