Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import supertest from 'supertest';
- import app from '../../../index';
- import {User} from '../../../database/models';
- import bcrypt from 'bcrypt';
- describe('user login testing', function()
- {
- beforeEach(async function()
- {
- /* Remove users which may have been created in previous test runs. */
- await User.destroy({ where:{} });
- });
- test('user can login and get a jwt', async function()
- {
- const fakeUser =
- {
- name: 'bahdcoder',
- email: 'bahdcoder@gmail.com',
- password: 'password'
- };
- await User.create(
- {
- name: fakeUser.name,
- email: fakeUser.email,
- password: bcrypt.hashSync(fakeUser.password, 1)
- });
- const response = await supertest(app).post('/api/v1/users/signin').send(
- {
- email: fakeUser.email,
- password: fakeUser.password
- });
- expect(response.status).toBe(200);
- expect(response.body.data.access_token).toBeTruthy();
- expect(response.body.data.user.email).toBe(fakeUser.email);
- });
- /*
- ASSIGNMENT 4: Note on testing:
- In each test I ran 3 setups:
- 1. Invalid data as specified to PASS the test.
- 2. Valid data but no user credentials to trigger an error from the middleware.
- 3. Create test user as above and get a successful endpoint request.
- I opted for toContain() to allow a PASS with multiple validation errors so long as 1 of them was
- the one I was testing for. It worked with both arrays (validator) and single strings (middleware).
- */
- test('ASS4: error return from missing password', async function()
- {
- const response = await supertest(app).post('/api/v1/users/signin').send(
- {
- email: 'bahdcoder@gmail.com'
- });
- expect(response.status).toBe(422);
- expect(response.body.status).toBe('fail');
- expect(response.body.data.errors).toBeDefined();
- expect(response.body.data.errors).toContain('The password is required.');
- });
- test('ASS4: error return from missing email', async function()
- {
- const response = await supertest(app).post('/api/v1/users/signin').send(
- {
- password:'password'
- });
- expect(response.status).toBe(422);
- expect(response.body.status).toBe('fail');
- expect(response.body.data.errors).toBeDefined();
- expect(response.body.data.errors).toContain('The email is required.');
- });
- test('ASS4: error return from malformed email', async function()
- {
- const response = await supertest(app).post('/api/v1/users/signin').send(
- {
- email: 'bahdcodergmail.com',
- password:'password'
- });
- expect(response.status).toBe(422);
- expect(response.body.status).toBe('fail');
- expect(response.body.data.errors).toBeDefined();
- expect(response.body.data.errors).toContain('The email must be a valid email address.');
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement