1- const { handler } = require ( '../src/video- token-server ' ) ;
1+ const { handler } = require ( '../../../ src/serverless/functions/ token' ) ;
22const jwt = require ( 'jsonwebtoken' ) ;
33const { set } = require ( 'lodash' ) ;
44
@@ -8,64 +8,23 @@ const mockCreateFunction = jest.fn();
88
99const mockTwilioClient = set ( { } , 'video.rooms.create' , mockCreateFunction ) ;
1010
11+ Date . now = ( ) => 5 ;
12+
1113const 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
2221describe ( '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' } ,
0 commit comments