Advertisement
Guest User

Assignment 3

a guest
Oct 2nd, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Recipe,User} from '../../../database/models';
  2. import middleware from '../../../middleware';
  3.  
  4. const {hasRecipe, authorize, signinUserValidator} = middleware;
  5.  
  6. describe('hasRecipe middleware testing', function()
  7. {
  8.     test('Calls next() if recipe is supplied', async function()
  9.     {
  10.         await Recipe.destroy({where:{}});
  11.        
  12.         const recipe = await Recipe.create
  13.         ({
  14.             title: "Grandma Hatey's Apple Poopers",
  15.             description: '10% less salt than a lethal dose.'
  16.         });
  17.         const request =
  18.         {
  19.             params: {id: recipe.dataValues.id}
  20.         };
  21.         var response =
  22.         {
  23.             sendFailureResponse: jest.fn()
  24.         }
  25.         const next = jest.fn();
  26.  
  27.         await hasRecipe(request, response, next);
  28.  
  29.         expect(next).toHaveBeenCalled();
  30.     });
  31.     test('calls sendFailureResponse() if recipe is not supplied', async function()
  32.     {
  33.         await Recipe.destroy({where:{}});
  34.  
  35.         const recipe = await Recipe.create
  36.         ({
  37.             title: "Grandma Hatey's Apple Poopers",
  38.             description: '10% less salt than a lethal dose.'
  39.         });
  40.         const request =
  41.         {
  42.             params:
  43.             {
  44.                 wrongField: recipe.dataValues.id,
  45.                 recipeId: null,
  46.                 id: null
  47.             }
  48.         };
  49.         var response =
  50.         {
  51.             sendFailureResponse: jest.fn()
  52.         }
  53.         const next = jest.fn();
  54.  
  55.         await hasRecipe(request, response, next);
  56.  
  57.         expect(response.sendFailureResponse).toHaveBeenCalledWith({ message: 'Recipe not found.' }, 404);
  58.     });
  59.     test('throws Exception if recipe is not found', async function()
  60.     {
  61.         await Recipe.destroy({where:{}});
  62.         const recipe = await Recipe.create
  63.         ({
  64.             title: "Grandma Hatey's Apple Poopers",
  65.             description: '10% less salt than a lethal dose.'
  66.         });
  67.         const request =
  68.         {
  69.             params:
  70.             {
  71.                 recipeId: recipe.dataValues.id,
  72.                 id: null
  73.             }
  74.         };
  75.         var response =
  76.         {
  77.             sendFailureResponse: jest.fn()
  78.         }
  79.         const next = jest.fn();
  80.  
  81.         /* Just for fun, destroy the created recipe. */
  82.         await Recipe.destroy({where:{}});
  83.  
  84.  
  85.         await hasRecipe(request, response, next);
  86.  
  87.         /* Changed params in call from exception handler to distingush between cases. */
  88.         expect(response.sendFailureResponse).toHaveBeenCalledWith({ message: 'Recipe not in DB.' }, 401);
  89.     });
  90. });
  91.  
  92. describe('authorize middleware testing', function()
  93. {
  94.     test('Request for recipe other than from designated owner calls sendFailureResponse()', async function()
  95.     {
  96.         /*
  97.             Create 2 users.
  98.             Create a recipe belonging to 1 of them.
  99.             Have the other user try to access it.
  100.         */
  101.         await User.destroy({where:{}});
  102.         const user1 = await User.create
  103.         ({
  104.             name: 'bahdcoder',
  105.             email: 'bahdcoder@gmail.com',
  106.             password: 'password'
  107.         });
  108.         const user2 = await User.create
  109.         ({
  110.             name: 'Grandma Hatey',
  111.             email: 'grandma@hatey.com',
  112.             password: 'ihateyou'
  113.         });
  114.         await Recipe.destroy({where:{}});
  115.         const recipe = await Recipe.create
  116.         ({
  117.             title: "Grandma Hatey's Apple Poopers",
  118.             description: '10% less salt than a lethal dose.',
  119.             userId: user2.id
  120.         });
  121.         const request =
  122.         {
  123.             currentRecipe: recipe,
  124.             authUser: user1
  125.         };
  126.         var response =
  127.         {
  128.             sendFailureResponse: jest.fn()
  129.         }
  130.         const next = jest.fn();
  131.  
  132.  
  133.         authorize(request, response, next);
  134.  
  135.         expect(response.sendFailureResponse).toHaveBeenCalled();
  136.     });
  137.     test('Request for recipe from designated owner calls next()', async function()
  138.     {
  139.         await User.destroy({where:{}});
  140.         const user1 = await User.create
  141.         ({
  142.             name: 'bahdcoder',
  143.             email: 'bahdcoder@gmail.com',
  144.             password: 'password'
  145.         });
  146.         const user2 = await User.create
  147.         ({
  148.             name: 'Grandma Hatey',
  149.             email: 'grandma@hatey.com',
  150.             password: 'ihateyou'
  151.         });
  152.         await Recipe.destroy({where:{}});
  153.         const recipe = await Recipe.create
  154.         ({
  155.             title: "Grandma Hatey's Apple Poopers",
  156.             description: '10% less salt than a lethal dose.',
  157.             userId: user2.id
  158.         });
  159.         const request =
  160.         {
  161.             currentRecipe: recipe,
  162.             authUser: user2
  163.         };
  164.         var response =
  165.         {
  166.             sendFailureResponse: jest.fn()
  167.         }
  168.         const next = jest.fn();
  169.  
  170.  
  171.         authorize(request, response, next);
  172.  
  173.         expect(next).toHaveBeenCalled();
  174.     });
  175. });
  176.  
  177. describe('signinUserValidator middleware testing', function()
  178. {
  179.     /* Reprising some tests from signin.spec.js */
  180.     test('signinUserValidator calls next() when receiving a valid user', function()
  181.     {
  182.         const request =
  183.         {
  184.             body:
  185.             {
  186.                 name: 'bahdcoder',
  187.                 email: 'bahdcoder@gmail.com',
  188.                 password: 'password'
  189.             }
  190.         };
  191.         const response =
  192.         {
  193.             sendFailureResponse: jest.fn()
  194.         };
  195.         const next = jest.fn();
  196.  
  197.         signinUserValidator(request, response, next);
  198.  
  199.         expect(next).toHaveBeenCalled();
  200.     });
  201.     test('signinUserValidator calls sendFailureResponse() when receiving an invalid user', function()
  202.     {
  203.         const request =
  204.         {
  205.             body:
  206.             {
  207.                 name: 'bahdcoder',
  208.                 email: 'bahdcoder@gmail',
  209.                 password: 'password'
  210.             }
  211.         };
  212.         const response =
  213.         {
  214.             sendFailureResponse: jest.fn()
  215.         };
  216.         const next = jest.fn();
  217.  
  218.         signinUserValidator(request, response, next);
  219.  
  220.         expect(response.sendFailureResponse).toHaveBeenCalled();
  221.     });
  222. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement