Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // hasRecipe
- import middleware from '../../../middleware';
- import { Recipe } from '../../../database/models';
- const { hasRecipe } = middleware;
- describe('The hasRecipe middleware class', () => {
- test('the hasRecipe calls the next function if the validation is successful', async () => {
- const recipe = await Recipe.create({});
- const req = {
- params: {
- id: recipe.id,
- },
- };
- const res = {};
- const next = jest.fn();
- await hasRecipe(req, res, next);
- expect(next).toHaveBeenCalled();
- });
- test('the hasRecipe calls the sendFailureResponse function if no recipe is found', async () => {
- const req = {
- params: {
- id: 455558,
- },
- };
- const res = {
- sendFailureResponse: jest.fn(),
- };
- const next = jest.fn();
- await hasRecipe(req, res, next);
- expect(res.sendFailureResponse).toHaveBeenCalledWith({
- message: 'Recipe not found.',
- }, 404);
- });
- });
- // authorize middleware
- import middleware from '../../../middleware';
- const { authorize } = middleware;
- describe('The authorize middleware class', () => {
- test('the authorize calls the next function if the validation is successful', async () => {
- const req = {
- currentRecipe: {
- userId: 12,
- },
- authUser: {
- id: 12,
- },
- };
- const res = {};
- const next = jest.fn();
- await authorize(req, res, next);
- expect(next).toHaveBeenCalled();
- });
- test('throws an error if the validation fails', async () => {
- const req = {
- currentRecipe: {
- userId: 12,
- },
- authUser: {
- id: 11,
- },
- };
- const res = {
- sendFailureResponse: jest.fn(),
- };
- const next = jest.fn();
- await authorize(req, res, next);
- expect(res.sendFailureResponse).toHaveBeenCalledWith({ message: 'Unauthorized.' }, 401);
- expect(next).toHaveBeenCalledTimes(0);
- });
- });
- // signin middleware
- import middleware from '../../../middleware';
- import { User } from '../../../database/models';
- const { signinUserValidator } = middleware;
- describe('The signin middleware class', () => {
- test('the signinUserValidator calls the next function if the validation is successful', () => {
- User.destroy({ where: {} });
- const req = {
- body: {
- email: 'bahdcoder@gmail.com',
- password: 'bahdcoder',
- },
- };
- const res = {
- sendFailureResponse () {},
- };
- const next = jest.fn();
- signinUserValidator(req, res, next);
- expect(next).toHaveBeenCalled();
- });
- test('the signinUserValidator calls the sendFailureResponse function if the validation fails', () => {
- const req = {
- body: {
- password: 'bahd',
- },
- };
- const res = {
- sendFailureResponse: jest.fn(),
- };
- const next = jest.fn();
- signinUserValidator(req, res, next);
- expect(res.sendFailureResponse).toHaveBeenCalledWith({
- errors: [
- 'The email is required.',
- ],
- }, 422);
- expect(next).toHaveBeenCalledTimes(0);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement