-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dc5b66
commit cd5f274
Showing
2 changed files
with
76 additions
and
13 deletions.
There are no files selected for viewing
79 changes: 71 additions & 8 deletions
79
server/controllers/userController/authUser/authUser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters