Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { User } from '../../../database/models'
- import authorize from '../../../middleware/authorize'
- describe('The authorize module', () => {
- test('next function called if req.currentRecipe.userId === req.authUser.id', async () => {
- await User.destroy({ where: {} })
- const user = await User.create({
- name: 'bahdcoder',
- email: 'bahdcoder@gmail.com',
- password: 'password'
- })
- const req = {
- currentRecipe: {
- userId: user.id
- },
- authUser: {
- id: user.id
- }
- }
- const next = jest.fn()
- const res = {
- sendFailureResponse: jest.fn()
- }
- await authorize(req, res, next)
- expect(next).toHaveBeenCalled()
- })
- test('error if req.currentRecipe.userId !== req.authUser.id', async () => {
- await User.destroy({ where: {} })
- const user = await User.create({
- name: 'bahdcoder',
- email: 'bahdcoder@gmail.com',
- password: 'password'
- })
- const req = {
- currentRecipe: {
- userId: user.id
- },
- authUser: {
- id: ''
- }
- }
- const next = jest.fn()
- const res = {
- sendFailureResponse: jest.fn()
- }
- await authorize(req, res, next)
- expect(res.sendFailureResponse).toHaveBeenCalledWith({
- message: 'Unauthorized.'
- }, 401)
- expect(next).not.toHaveBeenCalled()
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement