|
| 1 | +const { handler } = require('../../../src/serverless/functions/recordingrules'); |
| 2 | +const twilio = require('twilio'); |
| 3 | + |
| 4 | +const mockUpdateFn = jest.fn(() => Promise.resolve('mockSuccessResponse')); |
| 5 | + |
| 6 | +const mockClient = jest.fn(() => ({ |
| 7 | + video: { |
| 8 | + rooms: jest.fn(() => ({ |
| 9 | + recordingRules: { |
| 10 | + update: mockUpdateFn, |
| 11 | + }, |
| 12 | + })), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('twilio'); |
| 17 | +twilio.mockImplementation(mockClient); |
| 18 | + |
| 19 | +describe('the recordingrules function', () => { |
| 20 | + it('should correctly respond when a room update is successful', async () => { |
| 21 | + const mockCallback = jest.fn(); |
| 22 | + |
| 23 | + await handler( |
| 24 | + { ACCOUNT_SID: '1234', AUTH_TOKEN: '2345' }, |
| 25 | + { room_sid: 'mockRoomSid', rules: 'mockRules' }, |
| 26 | + mockCallback |
| 27 | + ); |
| 28 | + |
| 29 | + expect(mockClient).toHaveBeenCalledWith('1234', '2345'); |
| 30 | + expect(mockCallback).toHaveBeenCalledWith(null, { |
| 31 | + body: 'mockSuccessResponse', |
| 32 | + headers: { 'Content-Type': 'application/json' }, |
| 33 | + statusCode: 200, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should correctly respond when a room update is not successful', async () => { |
| 38 | + const mockCallback = jest.fn(); |
| 39 | + const mockError = { message: 'mockErrorMesage', code: 123 }; |
| 40 | + mockUpdateFn.mockImplementationOnce(() => Promise.reject(mockError)); |
| 41 | + |
| 42 | + await handler( |
| 43 | + { ACCOUNT_SID: '1234', AUTH_TOKEN: '2345' }, |
| 44 | + { room_sid: 'mockRoomSid', rules: 'mockRules' }, |
| 45 | + mockCallback |
| 46 | + ); |
| 47 | + |
| 48 | + expect(mockCallback).toHaveBeenCalledWith(null, { |
| 49 | + body: { error: mockError }, |
| 50 | + headers: { 'Content-Type': 'application/json' }, |
| 51 | + statusCode: 500, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return a "missing room_sid" error when the room_sid is absent', async () => { |
| 56 | + const mockCallback = jest.fn(); |
| 57 | + |
| 58 | + await handler({ ACCOUNT_SID: '1234', AUTH_TOKEN: '2345' }, { rules: 'mockRules' }, mockCallback); |
| 59 | + |
| 60 | + expect(mockCallback).toHaveBeenCalledWith(null, { |
| 61 | + body: { |
| 62 | + error: { |
| 63 | + message: 'missing room_sid', |
| 64 | + explanation: 'The room_sid parameter is missing.', |
| 65 | + }, |
| 66 | + }, |
| 67 | + headers: { 'Content-Type': 'application/json' }, |
| 68 | + statusCode: 400, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return a "missing rules" error when the rules array is absent', async () => { |
| 73 | + const mockCallback = jest.fn(); |
| 74 | + |
| 75 | + await handler({ ACCOUNT_SID: '1234', AUTH_TOKEN: '2345' }, { room_sid: 'mockSid' }, mockCallback); |
| 76 | + |
| 77 | + expect(mockCallback).toHaveBeenCalledWith(null, { |
| 78 | + body: { |
| 79 | + error: { |
| 80 | + message: 'missing rules', |
| 81 | + explanation: 'The rules parameter is missing.', |
| 82 | + }, |
| 83 | + }, |
| 84 | + headers: { 'Content-Type': 'application/json' }, |
| 85 | + statusCode: 400, |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments