Skip to content

Commit 29d715e

Browse files
author
Tim Mendoza
committed
Update tests for video-token-server
1 parent 1e48a84 commit 29d715e

File tree

4 files changed

+99
-37
lines changed

4 files changed

+99
-37
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@oclif/plugin-help": "^2",
2323
"@twilio-labs/serverless-api": "^4.0.1",
2424
"@twilio/cli-core": "^5.8.0",
25+
"lodash": "^4.17.20",
2526
"moment": "^2.24.0",
2627
"nanoid": "^3.1.12"
2728
},

src/video-token-server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ module.exports.handler = async (context, event, callback) => {
6161
try {
6262
await client.video.rooms.create({ uniqueName: room_name, type: ROOM_TYPE });
6363
} catch (e) {
64+
// Ignore 53113 error (room already exists). See: https://www.twilio.com/docs/api/errors/53113
6465
if (e.code !== 53113) {
65-
response.setStatusCode(401);
66+
response.setStatusCode(500);
6667
response.setBody({
6768
error: {
6869
message: 'error creating room',

test/video-token-server.test.js

Lines changed: 93 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
const { handler } = require('../src/video-token-server');
22
const jwt = require('jsonwebtoken');
3+
const { set } = require('lodash');
34

45
const callback = jest.fn();
56

7+
const mockCreateFunction = jest.fn();
8+
9+
const mockTwilioClient = set({}, 'video.rooms.create', mockCreateFunction);
10+
611
const mockContext = {
712
ACCOUNT_SID: 'AC1234',
813
TWILIO_API_KEY_SID: 'SK1234',
914
TWILIO_API_KEY_SECRET: 'api_secret',
1015
API_PASSCODE: '123456',
1116
API_PASSCODE_EXPIRY: '10',
1217
DOMAIN_NAME: 'video-app-1234-5678-dev.twil.io',
18+
ROOM_TYPE: 'group',
19+
getTwilioClient: () => mockTwilioClient,
1320
};
1421

1522
describe('the video-token-server', () => {
16-
it('should return an "unauthorized" error when the passcode is incorrect', () => {
23+
beforeEach(() => {
1724
Date.now = () => 5;
25+
mockCreateFunction.mockImplementation(() => Promise.resolve());
26+
});
1827

28+
it('should return an "unauthorized" error when the passcode is incorrect', () => {
1929
handler(mockContext, { passcode: '9876543210', user_identity: 'test identity' }, callback);
2030

2131
expect(callback).toHaveBeenCalledWith(null, {
@@ -49,8 +59,6 @@ describe('the video-token-server', () => {
4959
});
5060

5161
it('should return a "missing user_identity" error when the "user_identity" parameter is not supplied', () => {
52-
Date.now = () => 5;
53-
5462
handler(mockContext, { passcode: '12345612345678' }, callback);
5563

5664
expect(callback).toHaveBeenCalledWith(null, {
@@ -65,13 +73,11 @@ describe('the video-token-server', () => {
6573
});
6674
});
6775

68-
it('should return a token when no room_name is supplied', () => {
69-
Date.now = () => 5;
70-
71-
handler(mockContext, { passcode: '12345612345678', user_identity: 'test identity' }, callback);
76+
it('should return a token when no room_name is supplied', async () => {
77+
await handler(mockContext, { passcode: '12345612345678', user_identity: 'test identity' }, callback);
7278

7379
expect(callback).toHaveBeenCalledWith(null, {
74-
body: { token: expect.any(String) },
80+
body: { token: expect.any(String), room_type: 'group' },
7581
headers: { 'Content-Type': 'application/json' },
7682
statusCode: 200,
7783
});
@@ -89,47 +95,101 @@ describe('the video-token-server', () => {
8995
});
9096
});
9197

92-
it('should return a valid token when passcode, room_name, and user_identity parameters are supplied', () => {
93-
Date.now = () => 5;
94-
handler(mockContext, { passcode: '12345612345678', room_name: 'test-room', user_identity: 'test-user' }, callback);
98+
describe('when passcode, room_name, and user_identity parameters are supplied', () => {
99+
it('should return a valid token', async () => {
100+
await handler(
101+
mockContext,
102+
{ passcode: '12345612345678', room_name: 'test-room', user_identity: 'test-user' },
103+
callback
104+
);
95105

96-
expect(callback).toHaveBeenCalledWith(null, {
97-
body: { token: expect.any(String) },
98-
headers: { 'Content-Type': 'application/json' },
99-
statusCode: 200,
100-
});
106+
expect(callback).toHaveBeenCalledWith(null, {
107+
body: { token: expect.any(String), room_type: 'group' },
108+
headers: { 'Content-Type': 'application/json' },
109+
statusCode: 200,
110+
});
101111

102-
expect(jwt.verify(callback.mock.calls[0][1].body.token, 'api_secret')).toEqual({
103-
exp: 14400,
104-
grants: {
105-
identity: 'test-user',
106-
video: {
107-
room: 'test-room',
112+
expect(jwt.verify(callback.mock.calls[0][1].body.token, 'api_secret')).toEqual({
113+
exp: 14400,
114+
grants: {
115+
identity: 'test-user',
116+
video: {
117+
room: 'test-room',
118+
},
108119
},
109-
},
110-
iat: 0,
111-
iss: 'SK1234',
112-
jti: 'SK1234-0',
113-
sub: 'AC1234',
120+
iat: 0,
121+
iss: 'SK1234',
122+
jti: 'SK1234-0',
123+
sub: 'AC1234',
124+
});
114125
});
115-
});
116126

117-
describe('when using an old form URL "video-app-XXXX-dev.twil.io', () => {
118-
it('should return a valid token when passcode, room_name, and user_identity parameters are supplied', () => {
119-
Date.now = () => 5;
120-
handler(
127+
it('should return a valid token when passcode when using an old form URL "video-app-XXXX-dev.twil.io', async () => {
128+
await handler(
121129
{ ...mockContext, DOMAIN_NAME: 'video-app-1234-dev.twil.io' },
122130
{ passcode: '1234561234', room_name: 'test-room', user_identity: 'test-user' },
123131
callback
124132
);
125133

126134
expect(callback).toHaveBeenCalledWith(null, {
127-
body: { token: expect.any(String) },
135+
body: { token: expect.any(String), room_type: 'group' },
128136
headers: { 'Content-Type': 'application/json' },
129137
statusCode: 200,
130138
});
131139

132140
expect(jwt.verify(callback.mock.calls[0][1].body.token, 'api_secret')).toBeTruthy();
133141
});
142+
143+
it('should create a new room and return a valid token when passcode', async () => {
144+
await handler(
145+
mockContext,
146+
{ passcode: '12345612345678', room_name: 'test-room', user_identity: 'test-user' },
147+
callback
148+
);
149+
150+
expect(mockCreateFunction).toHaveBeenCalledWith({ type: 'group', uniqueName: 'test-room' });
151+
expect(callback).toHaveBeenCalledWith(null, {
152+
body: { token: expect.any(String), room_type: 'group' },
153+
headers: { 'Content-Type': 'application/json' },
154+
statusCode: 200,
155+
});
156+
});
157+
158+
it('should return a valid token when passcode when the room already exists', async () => {
159+
mockCreateFunction.mockImplementation(() => Promise.reject({ code: 53113 }));
160+
161+
await handler(
162+
mockContext,
163+
{ passcode: '12345612345678', room_name: 'test-room', user_identity: 'test-user' },
164+
callback
165+
);
166+
167+
expect(callback).toHaveBeenCalledWith(null, {
168+
body: { token: expect.any(String), room_type: 'group' },
169+
headers: { 'Content-Type': 'application/json' },
170+
statusCode: 200,
171+
});
172+
});
173+
174+
it('should return an error when there is a problem creating the room', async () => {
175+
mockCreateFunction.mockImplementationOnce(() => Promise.reject({ code: 12345 }));
176+
177+
await handler(
178+
mockContext,
179+
{ passcode: '12345612345678', room_name: 'test-room', user_identity: 'test-user' },
180+
callback
181+
);
182+
183+
expect(callback).toHaveBeenCalledWith(null, {
184+
body: {
185+
error: {
186+
explanation: 'Something went wrong when creating a room.',
187+
message: 'error creating room',
188+
},
189+
},
190+
headers: { 'Content-Type': 'application/json' },
191+
statusCode: 500,
192+
});
193+
});
134194
});
135195
});

0 commit comments

Comments
 (0)