|
| 1 | +import { jest } from '@jest/globals'; |
| 2 | +import type { Request, Response, NextFunction } from 'express'; |
| 3 | + |
| 4 | +const mockGetDJsInCurrentShow = jest.fn<() => Promise<{ id: string }[]>>(); |
| 5 | + |
| 6 | +jest.mock('../../../apps/backend/services/flowsheet.service', () => ({ |
| 7 | + getDJsInCurrentShow: mockGetDJsInCurrentShow, |
| 8 | +})); |
| 9 | + |
| 10 | +import { showMemberMiddleware } from '../../../apps/backend/middleware/checkShowMember'; |
| 11 | + |
| 12 | +function createMockReqResNext(userId: string) { |
| 13 | + const req = { |
| 14 | + auth: { id: userId }, |
| 15 | + } as unknown as Request; |
| 16 | + |
| 17 | + const res = { |
| 18 | + status: jest.fn().mockReturnThis(), |
| 19 | + json: jest.fn().mockReturnThis(), |
| 20 | + locals: {}, |
| 21 | + } as unknown as Response; |
| 22 | + |
| 23 | + const next = jest.fn() as unknown as NextFunction; |
| 24 | + |
| 25 | + return { req, res, next }; |
| 26 | +} |
| 27 | + |
| 28 | +describe('showMemberMiddleware', () => { |
| 29 | + it('rejects a DJ who is not in the current show', async () => { |
| 30 | + mockGetDJsInCurrentShow.mockResolvedValue([ |
| 31 | + { id: 'dj-alice' }, |
| 32 | + { id: 'dj-bob' }, |
| 33 | + ]); |
| 34 | + |
| 35 | + const { req, res, next } = createMockReqResNext('dj-charlie'); |
| 36 | + |
| 37 | + await showMemberMiddleware(req, res, next); |
| 38 | + |
| 39 | + expect(res.status).toHaveBeenCalledWith(400); |
| 40 | + expect(res.json).toHaveBeenCalledWith({ |
| 41 | + message: 'Bad Request: DJ not a member of show', |
| 42 | + }); |
| 43 | + expect(next).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('allows a DJ who is in the current show', async () => { |
| 47 | + mockGetDJsInCurrentShow.mockResolvedValue([ |
| 48 | + { id: 'dj-alice' }, |
| 49 | + { id: 'dj-bob' }, |
| 50 | + ]); |
| 51 | + |
| 52 | + const { req, res, next } = createMockReqResNext('dj-alice'); |
| 53 | + |
| 54 | + await showMemberMiddleware(req, res, next); |
| 55 | + |
| 56 | + expect(next).toHaveBeenCalled(); |
| 57 | + expect(res.status).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('rejects when there are no DJs in the current show', async () => { |
| 61 | + mockGetDJsInCurrentShow.mockResolvedValue([]); |
| 62 | + |
| 63 | + const { req, res, next } = createMockReqResNext('dj-alice'); |
| 64 | + |
| 65 | + await showMemberMiddleware(req, res, next); |
| 66 | + |
| 67 | + expect(res.status).toHaveBeenCalledWith(400); |
| 68 | + expect(next).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns 500 when getDJsInCurrentShow throws', async () => { |
| 72 | + mockGetDJsInCurrentShow.mockRejectedValue(new Error('DB connection lost')); |
| 73 | + |
| 74 | + const { req, res, next } = createMockReqResNext('dj-alice'); |
| 75 | + |
| 76 | + await showMemberMiddleware(req, res, next); |
| 77 | + |
| 78 | + expect(res.status).toHaveBeenCalledWith(500); |
| 79 | + expect(res.json).toHaveBeenCalledWith({ |
| 80 | + message: 'Internal server error checking show membership', |
| 81 | + }); |
| 82 | + expect(next).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | +}); |
0 commit comments