Skip to content

Commit

Permalink
CHE-84 Test Suite complete
Browse files Browse the repository at this point in the history
  • Loading branch information
brok3turtl3 committed Aug 8, 2024
1 parent 7dc5b66 commit cd5f274
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
79 changes: 71 additions & 8 deletions server/controllers/userController/authUser/authUser.test.ts
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]';
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');
});
});
});
10 changes: 5 additions & 5 deletions server/controllers/userController/authUser/authUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')]);
Expand All @@ -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) {
Expand Down

0 comments on commit cd5f274

Please sign in to comment.