Advertisement
Guest User

Assignment 4

a guest
Oct 7th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import supertest from 'supertest';
  2. import app from '../../../index';
  3. import {User} from '../../../database/models';
  4. import bcrypt from 'bcrypt';
  5.  
  6. describe('user login testing', function()
  7. {
  8.     beforeEach(async function()
  9.     {
  10.         /* Remove users which may have been created in previous test runs. */
  11.         await User.destroy({ where:{} });
  12.     });
  13.  
  14.     test('user can login and get a jwt', async function()
  15.     {
  16.         const fakeUser =
  17.         {
  18.             name: 'bahdcoder',
  19.             email: 'bahdcoder@gmail.com',
  20.             password: 'password'
  21.         };
  22.         await User.create(
  23.         {
  24.             name: fakeUser.name,
  25.             email: fakeUser.email,
  26.             password: bcrypt.hashSync(fakeUser.password, 1)
  27.         });
  28.         const response = await supertest(app).post('/api/v1/users/signin').send(
  29.         {
  30.             email: fakeUser.email,
  31.             password: fakeUser.password
  32.         });
  33.  
  34.         expect(response.status).toBe(200);
  35.         expect(response.body.data.access_token).toBeTruthy();
  36.         expect(response.body.data.user.email).toBe(fakeUser.email);
  37.     });
  38.  
  39.  
  40.     /*
  41.         ASSIGNMENT 4: Note on testing:
  42.  
  43.         In each test I ran 3 setups:
  44.         1. Invalid data as specified to PASS the test.
  45.         2. Valid data but no user credentials to trigger an error from the middleware.
  46.         3. Create test user as above and get a successful endpoint request.
  47.  
  48.         I opted for toContain() to allow a PASS with multiple validation errors so long as 1 of them was
  49.         the one I was testing for.  It worked with both arrays (validator) and single strings (middleware).
  50.     */
  51.  
  52.     test('ASS4: error return from missing password', async function()
  53.     {
  54.         const response = await supertest(app).post('/api/v1/users/signin').send(
  55.         {
  56.             email: 'bahdcoder@gmail.com'
  57.         });
  58.  
  59.         expect(response.status).toBe(422);
  60.         expect(response.body.status).toBe('fail');
  61.         expect(response.body.data.errors).toBeDefined();
  62.         expect(response.body.data.errors).toContain('The password is required.');
  63.     });
  64.  
  65.     test('ASS4: error return from missing email', async function()
  66.     {
  67.         const response = await supertest(app).post('/api/v1/users/signin').send(
  68.         {
  69.             password:'password'
  70.         });
  71.  
  72.         expect(response.status).toBe(422);
  73.         expect(response.body.status).toBe('fail');
  74.         expect(response.body.data.errors).toBeDefined();
  75.         expect(response.body.data.errors).toContain('The email is required.');
  76.     });
  77.  
  78.     test('ASS4: error return from malformed email', async function()
  79.     {
  80.         const response = await supertest(app).post('/api/v1/users/signin').send(
  81.         {
  82.             email: 'bahdcodergmail.com',
  83.             password:'password'
  84.         });
  85.  
  86.         expect(response.status).toBe(422);
  87.         expect(response.body.status).toBe('fail');
  88.         expect(response.body.data.errors).toBeDefined();
  89.         expect(response.body.data.errors).toContain('The email must be a valid email address.');
  90.     });
  91. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement