Skip to content

Commit 93efc81

Browse files
author
Tim Mendoza
committed
Add parameter validation and tests to recordingrules route
1 parent c5de2dd commit 93efc81

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

src/serverless/functions/recordingrules.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,37 @@ module.exports.handler = async (context, event, callback) => {
99
const authHandler = require(Runtime.getAssets()['/auth-handler.js'].path);
1010
authHandler(context, event, callback);
1111

12-
const client = twilio(context.ACCOUNT_SID, context.AUTH_TOKEN);
13-
1412
let response = new Twilio.Response();
1513
response.appendHeader('Content-Type', 'application/json');
1614

15+
const { room_sid, rules } = event;
16+
17+
if (typeof room_sid === 'undefined') {
18+
response.setStatusCode(400);
19+
response.setBody({
20+
error: {
21+
message: 'missing room_sid',
22+
explanation: 'The room_sid parameter is missing.',
23+
},
24+
});
25+
return callback(null, response);
26+
}
27+
28+
if (typeof rules === 'undefined') {
29+
response.setStatusCode(400);
30+
response.setBody({
31+
error: {
32+
message: 'missing rules',
33+
explanation: 'The rules parameter is missing.',
34+
},
35+
});
36+
return callback(null, response);
37+
}
38+
39+
const client = twilio(context.ACCOUNT_SID, context.AUTH_TOKEN);
40+
1741
try {
18-
const recordingRulesResponse = await client.video
19-
.rooms(event.room_sid)
20-
.recordingRules.update({ rules: event.rules });
42+
const recordingRulesResponse = await client.video.rooms(room_sid).recordingRules.update({ rules });
2143
response.setStatusCode(200);
2244
response.setBody(recordingRulesResponse);
2345
} catch (err) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
});

test/setupTests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ global.Runtime = {
3232
};
3333

3434
// Mocking this as a no-op since this function is tested in 'tests/serverless/assets/verify_passcode.ts'.
35-
jest.mock(`${__dirname}/../src/serverless/assets/verify_passcode.js`, () => () => {});
35+
jest.doMock(verifyPasscodePath, () => () => {});

0 commit comments

Comments
 (0)