Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Recipe,User} from '../../../database/models';
- import middleware from '../../../middleware';
- const {hasRecipe, authorize, signinUserValidator} = middleware;
- describe('hasRecipe middleware testing', function()
- {
- test('Calls next() if recipe is supplied', async function()
- {
- await Recipe.destroy({where:{}});
- const recipe = await Recipe.create
- ({
- title: "Grandma Hatey's Apple Poopers",
- description: '10% less salt than a lethal dose.'
- });
- const request =
- {
- params: {id: recipe.dataValues.id}
- };
- var response =
- {
- sendFailureResponse: jest.fn()
- }
- const next = jest.fn();
- await hasRecipe(request, response, next);
- expect(next).toHaveBeenCalled();
- });
- test('calls sendFailureResponse() if recipe is not supplied', async function()
- {
- await Recipe.destroy({where:{}});
- const recipe = await Recipe.create
- ({
- title: "Grandma Hatey's Apple Poopers",
- description: '10% less salt than a lethal dose.'
- });
- const request =
- {
- params:
- {
- wrongField: recipe.dataValues.id,
- recipeId: null,
- id: null
- }
- };
- var response =
- {
- sendFailureResponse: jest.fn()
- }
- const next = jest.fn();
- await hasRecipe(request, response, next);
- expect(response.sendFailureResponse).toHaveBeenCalledWith({ message: 'Recipe not found.' }, 404);
- });
- test('throws Exception if recipe is not found', async function()
- {
- await Recipe.destroy({where:{}});
- const recipe = await Recipe.create
- ({
- title: "Grandma Hatey's Apple Poopers",
- description: '10% less salt than a lethal dose.'
- });
- const request =
- {
- params:
- {
- recipeId: recipe.dataValues.id,
- id: null
- }
- };
- var response =
- {
- sendFailureResponse: jest.fn()
- }
- const next = jest.fn();
- /* Just for fun, destroy the created recipe. */
- await Recipe.destroy({where:{}});
- await hasRecipe(request, response, next);
- /* Changed params in call from exception handler to distingush between cases. */
- expect(response.sendFailureResponse).toHaveBeenCalledWith({ message: 'Recipe not in DB.' }, 401);
- });
- });
- describe('authorize middleware testing', function()
- {
- test('Request for recipe other than from designated owner calls sendFailureResponse()', async function()
- {
- /*
- Create 2 users.
- Create a recipe belonging to 1 of them.
- Have the other user try to access it.
- */
- await User.destroy({where:{}});
- const user1 = await User.create
- ({
- name: 'bahdcoder',
- email: 'bahdcoder@gmail.com',
- password: 'password'
- });
- const user2 = await User.create
- ({
- name: 'Grandma Hatey',
- email: 'grandma@hatey.com',
- password: 'ihateyou'
- });
- await Recipe.destroy({where:{}});
- const recipe = await Recipe.create
- ({
- title: "Grandma Hatey's Apple Poopers",
- description: '10% less salt than a lethal dose.',
- userId: user2.id
- });
- const request =
- {
- currentRecipe: recipe,
- authUser: user1
- };
- var response =
- {
- sendFailureResponse: jest.fn()
- }
- const next = jest.fn();
- authorize(request, response, next);
- expect(response.sendFailureResponse).toHaveBeenCalled();
- });
- test('Request for recipe from designated owner calls next()', async function()
- {
- await User.destroy({where:{}});
- const user1 = await User.create
- ({
- name: 'bahdcoder',
- email: 'bahdcoder@gmail.com',
- password: 'password'
- });
- const user2 = await User.create
- ({
- name: 'Grandma Hatey',
- email: 'grandma@hatey.com',
- password: 'ihateyou'
- });
- await Recipe.destroy({where:{}});
- const recipe = await Recipe.create
- ({
- title: "Grandma Hatey's Apple Poopers",
- description: '10% less salt than a lethal dose.',
- userId: user2.id
- });
- const request =
- {
- currentRecipe: recipe,
- authUser: user2
- };
- var response =
- {
- sendFailureResponse: jest.fn()
- }
- const next = jest.fn();
- authorize(request, response, next);
- expect(next).toHaveBeenCalled();
- });
- });
- describe('signinUserValidator middleware testing', function()
- {
- /* Reprising some tests from signin.spec.js */
- test('signinUserValidator calls next() when receiving a valid user', function()
- {
- const request =
- {
- body:
- {
- name: 'bahdcoder',
- email: 'bahdcoder@gmail.com',
- password: 'password'
- }
- };
- const response =
- {
- sendFailureResponse: jest.fn()
- };
- const next = jest.fn();
- signinUserValidator(request, response, next);
- expect(next).toHaveBeenCalled();
- });
- test('signinUserValidator calls sendFailureResponse() when receiving an invalid user', function()
- {
- const request =
- {
- body:
- {
- name: 'bahdcoder',
- email: 'bahdcoder@gmail',
- password: 'password'
- }
- };
- const response =
- {
- sendFailureResponse: jest.fn()
- };
- const next = jest.fn();
- signinUserValidator(request, response, next);
- expect(response.sendFailureResponse).toHaveBeenCalled();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement