Advertisement
efcp

Untitled

Jul 6th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // hasRecipe
  2.  
  3. import middleware from '../../../middleware';
  4. import { Recipe } from '../../../database/models';
  5.  
  6. const { hasRecipe } = middleware;
  7.  
  8. describe('The hasRecipe middleware class', () => {
  9.     test('the hasRecipe calls the next function if the validation is successful', async () => {
  10.  
  11.         const recipe = await Recipe.create({});
  12.  
  13.         const req = {
  14.             params: {
  15.                 id: recipe.id,
  16.             },
  17.         };
  18.  
  19.         const res = {};
  20.  
  21.         const next = jest.fn();
  22.  
  23.         await hasRecipe(req, res, next);
  24.  
  25.         expect(next).toHaveBeenCalled();
  26.     });
  27.  
  28.     test('the hasRecipe calls the sendFailureResponse function if no recipe is found', async () => {
  29.  
  30.         const req = {
  31.             params: {
  32.                 id: 455558,
  33.             },
  34.         };
  35.  
  36.         const res = {
  37.             sendFailureResponse: jest.fn(),
  38.         };
  39.  
  40.         const next = jest.fn();
  41.  
  42.         await hasRecipe(req, res, next);
  43.  
  44.         expect(res.sendFailureResponse).toHaveBeenCalledWith({
  45.             message: 'Recipe not found.',
  46.         }, 404);
  47.     });
  48. });
  49.  
  50.  
  51. // authorize middleware
  52.  
  53. import middleware from '../../../middleware';
  54.  
  55. const { authorize } = middleware;
  56.  
  57. describe('The authorize middleware class', () => {
  58.     test('the authorize calls the next function if the validation is successful', async () => {
  59.         const req = {
  60.             currentRecipe: {
  61.                 userId: 12,
  62.             },
  63.             authUser: {
  64.                 id: 12,
  65.             },
  66.         };
  67.  
  68.         const res = {};
  69.  
  70.         const next = jest.fn();
  71.  
  72.         await authorize(req, res, next);
  73.  
  74.         expect(next).toHaveBeenCalled();
  75.     });
  76.  
  77.     test('throws an error if the validation fails', async () => {
  78.         const req = {
  79.             currentRecipe: {
  80.                 userId: 12,
  81.             },
  82.             authUser: {
  83.                 id: 11,
  84.             },
  85.         };
  86.  
  87.         const res = {
  88.             sendFailureResponse: jest.fn(),
  89.         };
  90.  
  91.         const next = jest.fn();
  92.  
  93.         await authorize(req, res, next);
  94.  
  95.         expect(res.sendFailureResponse).toHaveBeenCalledWith({ message: 'Unauthorized.' }, 401);
  96.         expect(next).toHaveBeenCalledTimes(0);
  97.     });
  98. });
  99.  
  100.  
  101. // signin middleware
  102.  
  103. import middleware from '../../../middleware';
  104. import { User } from '../../../database/models';
  105.  
  106. const { signinUserValidator } = middleware;
  107.  
  108. describe('The signin middleware class', () => {
  109.     test('the signinUserValidator calls the next function if the validation is successful', () => {
  110.         User.destroy({ where: {} });
  111.         const req = {
  112.             body: {
  113.                 email: 'bahdcoder@gmail.com',
  114.                 password: 'bahdcoder',
  115.             },
  116.         };
  117.  
  118.         const res = {
  119.             sendFailureResponse () {},
  120.         };
  121.  
  122.         const next = jest.fn();
  123.  
  124.         signinUserValidator(req, res, next);
  125.         expect(next).toHaveBeenCalled();
  126.     });
  127.  
  128.     test('the signinUserValidator calls the sendFailureResponse function if the validation fails', () => {
  129.         const req = {
  130.             body: {
  131.                 password: 'bahd',
  132.             },
  133.         };
  134.  
  135.         const res = {
  136.             sendFailureResponse: jest.fn(),
  137.         };
  138.  
  139.         const next = jest.fn();
  140.  
  141.         signinUserValidator(req, res, next);
  142.  
  143.         expect(res.sendFailureResponse).toHaveBeenCalledWith({
  144.             errors: [
  145.                 'The email is required.',
  146.             ],
  147.         }, 422);
  148.         expect(next).toHaveBeenCalledTimes(0);
  149.     });
  150. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement