11const { handler } = require ( '../src/video-token-server' ) ;
22const jwt = require ( 'jsonwebtoken' ) ;
3+ const { set } = require ( 'lodash' ) ;
34
45const callback = jest . fn ( ) ;
56
7+ const mockCreateFunction = jest . fn ( ) ;
8+
9+ const mockTwilioClient = set ( { } , 'video.rooms.create' , mockCreateFunction ) ;
10+
611const 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
1522describe ( '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