Advertisement
GCrispino

Section 4 - Assignment 4

Feb 6th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import supertest from "supertest";
  2. import faker from "faker";
  3. import bcrypt from "bcrypt";
  4. import app from "../../../index";
  5. import { User } from "../../../database/models";
  6.  
  7. describe("The user login", () => {
  8.   test("The user can login and get a jwt", async () => {
  9.     const password = "1234567";
  10.     const fakeUser = {
  11.       name: "testuser",
  12.       email: faker.internet.email(),
  13.       password: await bcrypt.hash(password, 1)
  14.     };
  15.  
  16.     await User.create(fakeUser);
  17.  
  18.     const response = await supertest(app)
  19.       .post("/api/v1/users/signin")
  20.       .send({ ...fakeUser, password })
  21.       .expect(200);
  22.  
  23.     expect(response.body.status).toBe("success");
  24.     expect(response.body.data.user.email).toBe(fakeUser.email);
  25.     expect(response.body.data.access_token).toBeTruthy();
  26.   });
  27.  
  28.   test("Should return validation error if email is not provided", async () => {
  29.     const fakeUser = {
  30.       name: "testuser",
  31.       password: "1234567"
  32.     };
  33.  
  34.     const response = await supertest(app)
  35.       .post("/api/v1/users/signin")
  36.       .send(fakeUser)
  37.       .expect(422);
  38.  
  39.     expect(response.body.status).toBe("fail");
  40.     expect(response.body.data).toEqual({ errors: ["The email is required."] });
  41.   });
  42.  
  43.   test("Should return validation error if email is not valid", async () => {
  44.     const fakeUser = {
  45.       name: "testuser",
  46.       email: "bad@email",
  47.       password: "1234567"
  48.     };
  49.  
  50.     const response = await supertest(app)
  51.       .post("/api/v1/users/signin")
  52.       .send(fakeUser)
  53.       .expect(422);
  54.  
  55.     expect(response.body.status).toBe("fail");
  56.     expect(response.body.data).toEqual({
  57.       errors: ["The email must be a valid email address."]
  58.     });
  59.   });
  60.  
  61.   test("Should return validation error if password is not provided", async () => {
  62.     const fakeUser = {
  63.       name: "testuser",
  64.       email: faker.internet.email()
  65.     };
  66.  
  67.     const response = await supertest(app)
  68.       .post("/api/v1/users/signin")
  69.       .send(fakeUser)
  70.       .expect(422);
  71.  
  72.     expect(response.body.status).toBe("fail");
  73.     expect(response.body.data).toEqual({
  74.       errors: ["The password is required."]
  75.     });
  76.   });
  77.  
  78.   test("Should return validation errors if password and email are not provided", async () => {
  79.     const fakeUser = {};
  80.  
  81.     const response = await supertest(app)
  82.       .post("/api/v1/users/signin")
  83.       .send(fakeUser)
  84.       .expect(422);
  85.  
  86.     expect(response.body.status).toBe("fail");
  87.     expect(response.body.data).toEqual({
  88.       errors: ["The password is required.", "The email is required."]
  89.     });
  90.   });
  91. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement