Skip to content

Commit c5de2dd

Browse files
author
Tim Mendoza
committed
Add tests for verify_passcode and fix tests for token
1 parent 6f3d199 commit c5de2dd

File tree

3 files changed

+79
-52
lines changed

3 files changed

+79
-52
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const verifyPasscode = jest.requireActual('../../../src/serverless/assets/verify_passcode');
2+
3+
describe('the verify_passcode asset', () => {
4+
it('should return an "unauthorized" error when the passcode is incorrect', () => {
5+
Date.now = () => 5;
6+
const mockCallback = jest.fn();
7+
verifyPasscode(
8+
{ API_PASSCODE: '123456', API_PASSCODE_EXPIRY: '10', DOMAIN_NAME: 'video-app-1234-5678-dev.twil.io' },
9+
{ passcode: '9876543210' },
10+
mockCallback
11+
);
12+
13+
expect(mockCallback).toHaveBeenCalledWith(null, {
14+
body: {
15+
error: {
16+
message: 'passcode incorrect',
17+
explanation: 'The passcode used to validate application users is incorrect.',
18+
},
19+
},
20+
headers: { 'Content-Type': 'application/json' },
21+
statusCode: 401,
22+
});
23+
});
24+
25+
it('should return an "expired" error when the current time is past the API_PASSCODE_EXPIRY time', () => {
26+
Date.now = () => 15;
27+
const mockCallback = jest.fn();
28+
29+
verifyPasscode(
30+
{ API_PASSCODE: '123456', API_PASSCODE_EXPIRY: '10', DOMAIN_NAME: 'video-app-1234-5678-dev.twil.io' },
31+
{ passcode: '12345612345678', user_identity: 'test identity' },
32+
mockCallback
33+
);
34+
35+
expect(mockCallback).toHaveBeenCalledWith(null, {
36+
body: {
37+
error: {
38+
message: 'passcode expired',
39+
explanation:
40+
'The passcode used to validate application users has expired. Re-deploy the application to refresh the passcode.',
41+
},
42+
},
43+
headers: { 'Content-Type': 'application/json' },
44+
statusCode: 401,
45+
});
46+
});
47+
48+
it('should not call the callback function when the passcode is correct and not expired', () => {
49+
Date.now = () => 5;
50+
const mockCallback = jest.fn();
51+
verifyPasscode(
52+
{ API_PASSCODE: '123456', API_PASSCODE_EXPIRY: '10', DOMAIN_NAME: 'video-app-1234-5678-dev.twil.io' },
53+
{ passcode: '12345612345678' },
54+
mockCallback
55+
);
56+
57+
expect(mockCallback).not.toHaveBeenCalled();
58+
});
59+
});
Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { handler } = require('../src/video-token-server');
1+
const { handler } = require('../../../src/serverless/functions/token');
22
const jwt = require('jsonwebtoken');
33
const { set } = require('lodash');
44

@@ -8,64 +8,23 @@ const mockCreateFunction = jest.fn();
88

99
const mockTwilioClient = set({}, 'video.rooms.create', mockCreateFunction);
1010

11+
Date.now = () => 5;
12+
1113
const mockContext = {
1214
ACCOUNT_SID: 'AC1234',
1315
TWILIO_API_KEY_SID: 'SK1234',
1416
TWILIO_API_KEY_SECRET: 'api_secret',
15-
API_PASSCODE: '123456',
16-
API_PASSCODE_EXPIRY: '10',
17-
DOMAIN_NAME: 'video-app-1234-5678-dev.twil.io',
1817
ROOM_TYPE: 'group',
1918
getTwilioClient: () => mockTwilioClient,
2019
};
2120

2221
describe('the video-token-server', () => {
2322
beforeEach(() => {
24-
Date.now = () => 5;
2523
mockCreateFunction.mockImplementation(() => Promise.resolve());
2624
});
2725

28-
it('should return an "unauthorized" error when the passcode is incorrect', () => {
29-
handler(mockContext, { passcode: '9876543210', user_identity: 'test identity' }, callback);
30-
31-
expect(callback).toHaveBeenCalledWith(null, {
32-
body: {
33-
error: {
34-
message: 'passcode incorrect',
35-
explanation: 'The passcode used to validate application users is incorrect.',
36-
},
37-
},
38-
headers: { 'Content-Type': 'application/json' },
39-
statusCode: 401,
40-
});
41-
});
42-
43-
it('should return an "expired" error when the current time is past the API_PASSCODE_EXPIRY time', () => {
44-
Date.now = () => 15;
45-
46-
handler(mockContext, { passcode: '12345612345678', user_identity: 'test identity' }, callback);
47-
48-
expect(callback).toHaveBeenCalledWith(null, {
49-
body: {
50-
error: {
51-
message: 'passcode expired',
52-
explanation:
53-
'The passcode used to validate application users has expired. Re-deploy the application to refresh the passcode.',
54-
},
55-
},
56-
headers: { 'Content-Type': 'application/json' },
57-
statusCode: 401,
58-
});
59-
});
60-
6126
it('should return an "invalid parameter" error when the create_room parameter is not a boolean', async () => {
62-
Date.now = () => 5;
63-
64-
await handler(
65-
mockContext,
66-
{ passcode: '12345612345678', user_identity: 'test identity', create_room: 'no thanks' },
67-
callback
68-
);
27+
await handler(mockContext, { user_identity: 'test identity', create_room: 'no thanks' }, callback);
6928

7029
expect(callback).toHaveBeenCalledWith(null, {
7130
body: {
@@ -80,7 +39,7 @@ describe('the video-token-server', () => {
8039
});
8140

8241
it('should return a "missing user_identity" error when the "user_identity" parameter is not supplied', () => {
83-
handler(mockContext, { passcode: '12345612345678' }, callback);
42+
handler(mockContext, {}, callback);
8443

8544
expect(callback).toHaveBeenCalledWith(null, {
8645
body: {
@@ -95,7 +54,7 @@ describe('the video-token-server', () => {
9554
});
9655

9756
it('should return a token when no room_name is supplied', async () => {
98-
await handler(mockContext, { passcode: '12345612345678', user_identity: 'test identity' }, callback);
57+
await handler(mockContext, { user_identity: 'test identity' }, callback);
9958

10059
expect(callback).toHaveBeenCalledWith(null, {
10160
body: { token: expect.any(String), room_type: 'group' },
@@ -118,11 +77,7 @@ describe('the video-token-server', () => {
11877

11978
describe('when passcode, room_name, and user_identity parameters are supplied', () => {
12079
it('should return a valid token', async () => {
121-
await handler(
122-
mockContext,
123-
{ passcode: '12345612345678', room_name: 'test-room', user_identity: 'test-user' },
124-
callback
125-
);
80+
await handler(mockContext, { room_name: 'test-room', user_identity: 'test-user' }, callback);
12681

12782
expect(callback).toHaveBeenCalledWith(null, {
12883
body: { token: expect.any(String), room_type: 'group' },

test/setupTests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ class Response {
2020

2121
global.Twilio = require('twilio');
2222
global.Twilio.Response = Response;
23+
24+
const verifyPasscodePath = `${__dirname}/../src/serverless/assets/verify_passcode.js`;
25+
26+
global.Runtime = {
27+
getAssets: () => ({
28+
'/auth-handler.js': {
29+
path: verifyPasscodePath,
30+
},
31+
}),
32+
};
33+
34+
// 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`, () => () => {});

0 commit comments

Comments
 (0)