Advertisement
Heida

Untitled

Feb 19th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. Authorize test:
  2. import jwt from 'jsonwebtoken'
  3. import config from '../../../config'
  4. import middleware from '../../../middleware'
  5. import { User } from '../../../database/models'
  6.  
  7. const { authorize } = middleware
  8.  
  9. test('Should call sendFailureResponse if user is not authorized', async () => {
  10. const req = {
  11. currentRecipe: {
  12. userId:1
  13. },
  14. authUser: {
  15. id:2
  16. },
  17. }
  18.  
  19. const res = {
  20. sendFailureResponse: jest.fn()
  21. }
  22.  
  23. const next = jest.fn()
  24.  
  25. await authorize(req, res, next)
  26.  
  27. expect(res.sendFailureResponse).toHaveBeenCalledWith({
  28. message: "Unauthorized."
  29. }, 401)
  30. expect(next).toHaveBeenCalledTimes(0)
  31. })
  32.  
  33. Has recipe test:
  34. import middleware from '../../../middleware'
  35. import { runInNewContext } from 'vm';
  36.  
  37. const { hasRecipe } = middleware
  38.  
  39. test ('The hasRecipe calls the sendFailureResponse function if the validation fails', async () => {
  40. const req = {
  41. params: {
  42. recipeId: '',
  43. id: ''
  44. }
  45. }
  46.  
  47. //console.log (req)
  48.  
  49. const res = {
  50. sendFailureResponse: jest.fn()
  51. }
  52.  
  53. const next = jest.fn()
  54.  
  55. await hasRecipe(req, res, next)
  56. expect(res.sendFailureResponse).toHaveBeenCalledWith({
  57. message:
  58. 'Recipe not found.'
  59.  
  60. }, 404)
  61.  
  62. expect(next).toHaveBeenCalledTimes(0)
  63. })
  64.  
  65. Signin test:
  66. import middleware from '../../../middleware'
  67. import { runInNewContext } from 'vm';
  68.  
  69. const { signinUserValidator } = middleware
  70.  
  71. test ('The signinUserValidator calls the next function if the validation is successful', async () => {
  72. const req = {
  73. body: {
  74. name: 'bahdcoder',
  75. email: 'bahdcoder@gmail.com',
  76. password: 'password'
  77. }
  78. }
  79.  
  80. const res = {
  81. sendFailurResponse() {}
  82. }
  83.  
  84. const next = jest.fn()
  85.  
  86. await signinUserValidator(req, res, next)
  87. expect(next).toHaveBeenCalled()
  88. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement