Skip to content

Commit a6198bd

Browse files
author
timmydoza
authored
Merge pull request #49 from twilio-labs/log-token-server-editor
Make token server editable in Functions
2 parents 685d828 + 5b4d775 commit a6198bd

File tree

9 files changed

+105
-32
lines changed

9 files changed

+105
-32
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 0.6.0
4+
5+
### Enhancements
6+
7+
- This version will print a URL to the terminal where the user can view and edit the deployed token server function in the Twilio console. By default, the token server is editable in the Twilio console, but this can be disabled with the `--no-ui-editable` flag.
8+
- Upgraded @twilio/cli-core from 5.9.0 to 5.9.1
9+
- Upgraded moment from 2.27.0 to 2.28.0
10+
- Upgraded @twilio-labs/serverless-api from 4.0.1 to 4.0.2
11+
312
## 0.5.0
413

514
### Enhancements

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ OPTIONS
229229
--authentication=(passcode) (required) Type of authentication to use
230230
--override Override an existing App deployment
231231
--room-type=(group|group-small|peer-to-peer|go) [default: group] Set room type
232+
--[no-]ui-editable Specifies whether the app's files and variables can be edited in the Twilio console.
232233
233234
DESCRIPTION
234235
This command publishes two components as a Twilio Function: an application token
@@ -251,6 +252,7 @@ EXAMPLES
251252
Passcode: xxx xxx xxxx xxxx
252253
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
253254
Room Type: group
255+
Edit your token server at: https://www.twilio.com/console/functions/editor/...
254256
255257
# Deploy an application token server with the React app
256258
$ twilio rtc:apps:video:deploy --authentication passcode --app-directory /path/to/app
@@ -259,6 +261,7 @@ EXAMPLES
259261
Passcode: xxx xxx xxxx xxxx
260262
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
261263
Room Type: group
264+
Edit your token server at: https://www.twilio.com/console/functions/editor/...
262265
263266
# Override an existing app with a fresh deployment
264267
# Please note that this will remove a previously deployed web application if no
@@ -269,13 +272,15 @@ EXAMPLES
269272
Passcode: yyy yyy yyyy yyyy
270273
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
271274
Room Type: group
275+
Edit your token server at: https://www.twilio.com/console/functions/editor/...
272276
273277
# Deploy an application token server with a specific room type
274278
$ twilio rtc:apps:video:deploy --authentication passcode --room-type peer-to-peer
275279
deploying app... done
276280
Passcode: xxx xxx xxxx xxxx
277281
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
278282
Room Type: peer-to-peer
283+
Edit your token server at: https://www.twilio.com/console/functions/editor/...
279284
```
280285

281286
## `twilio rtc:apps:video:view`
@@ -295,6 +300,7 @@ EXAMPLE
295300
Web App URL: https://video-app-1111-dev.twil.io?passcode=xxxxxxxxxxxxxx
296301
Passcode: xxx xxx xxxx xxxx
297302
Room Type: group
303+
Edit your token server at: https://www.twilio.com/console/functions/editor/...
298304
```
299305

300306
<!-- commandsstop -->

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@twilio-labs/plugin-rtc",
3-
"version": "0.5.1",
3+
"version": "0.6.0",
44
"description": "A Twilio-CLI plugin for real-time communication apps",
55
"main": "index.js",
66
"publishConfig": {

src/commands/rtc/apps/video/deploy.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ DeployCommand.flags = Object.assign(
4848
required: false,
4949
default: 'group',
5050
}),
51+
'ui-editable': flags.boolean({
52+
required: false,
53+
default: true,
54+
description: "Specifies whether the app's files and variables can be edited in the Twilio console.",
55+
allowNo: true,
56+
}),
5157
},
5258
TwilioClientCommand.flags
5359
);
@@ -76,15 +82,17 @@ $ twilio rtc:apps:video:deploy --authentication passcode
7682
deploying app... done
7783
Passcode: xxx xxx xxxx xxxx
7884
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
79-
Room Type: group`,
85+
Room Type: group
86+
Edit your token server at: https://www.twilio.com/console/functions/editor/...`,
8087
`
8188
# Deploy an application token server with the React app
8289
$ twilio rtc:apps:video:deploy --authentication passcode --app-directory /path/to/app
8390
deploying app... done
8491
Web App URL: https://video-app-xxxx-xxxx-dev.twil.io?passcode=xxxxxxxxxxxxxx
8592
Passcode: xxx xxx xxxx xxxx
8693
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
87-
Room Type: group`,
94+
Room Type: group
95+
Edit your token server at: https://www.twilio.com/console/functions/editor/...`,
8896
`
8997
# Override an existing app with a fresh deployment
9098
# Please note that this will remove a previously deployed web application if no
@@ -94,14 +102,16 @@ Removed app with Passcode: xxx xxx xxxx xxxx
94102
deploying app... done
95103
Passcode: yyy yyy yyyy yyyy
96104
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
97-
Room Type: group`,
105+
Room Type: group
106+
Edit your token server at: https://www.twilio.com/console/functions/editor/...`,
98107
`
99108
# Deploy an application token server with a specific room type
100109
$ twilio rtc:apps:video:deploy --authentication passcode --room-type peer-to-peer
101110
deploying app... done
102111
Passcode: xxx xxx xxxx xxxx
103112
Expires: Mon Mar 09 2020 16:36:23 GMT-0600
104-
Room Type: peer-to-peer`,
113+
Room Type: peer-to-peer
114+
Edit your token server at: https://www.twilio.com/console/functions/editor/...`,
105115
];
106116

107117
module.exports = DeployCommand;

src/commands/rtc/apps/video/view.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ ViewCommand.description = 'View a Programmable Video app';
1515
ViewCommand.examples = [
1616
`$ twilio rtc:apps:video:view
1717
Web App URL: https://video-app-1111-dev.twil.io?passcode=1111111111
18-
Passcode: 1111111111`,
18+
Passcode: 1111111111
19+
Edit your token server at: https://www.twilio.com/console/functions/editor/...`,
1920
];
2021

2122
module.exports = ViewCommand;

src/helpers.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ async function getAppInfo() {
7979

8080
const assets = await appInstance.assets.list();
8181

82+
const functions = await appInstance.functions.list();
83+
const tokenServerFunction = functions.find(fn => fn.friendlyName === 'token');
84+
8285
const passcodeVar = variables.find(v => v.key === 'API_PASSCODE');
8386
const expiryVar = variables.find(v => v.key === 'API_PASSCODE_EXPIRY');
8487
const roomTypeVar = variables.find(v => v.key === 'ROOM_TYPE');
@@ -96,6 +99,8 @@ async function getAppInfo() {
9699
passcode: fullPasscode,
97100
hasAssets: Boolean(assets.length),
98101
roomType,
102+
environmentSid: environment.sid,
103+
functionSid: tokenServerFunction.sid,
99104
};
100105
}
101106

@@ -117,6 +122,10 @@ async function displayAppInfo() {
117122
if (appInfo.roomType) {
118123
console.log(`Room Type: ${appInfo.roomType}`);
119124
}
125+
126+
console.log(
127+
`Edit your token server at: https://www.twilio.com/console/functions/editor/${appInfo.sid}/environment/${appInfo.environmentSid}/function/${appInfo.functionSid}`
128+
);
120129
}
121130

122131
async function deploy() {
@@ -181,7 +190,10 @@ TWILIO_API_SECRET = the secret for the API Key`);
181190
}
182191

183192
try {
184-
await serverlessClient.deployProject(deployOptions);
193+
const { serviceSid } = await serverlessClient.deployProject(deployOptions);
194+
await this.twilioClient.serverless
195+
.services(serviceSid)
196+
.update({ includeCredentials: true, uiEditable: this.flags['ui-editable'] });
185197
cli.action.stop();
186198
} catch (e) {
187199
console.error('Something went wrong', e);

test/e2e/e2e.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('the RTC Twilio-CLI Plugin', () => {
122122
await ViewCommand.run([]);
123123
stdout.stop();
124124
expect(stdout.output).toMatch(
125-
/Web App URL: .+\nPasscode: \d{3} \d{3} \d{4} \d{4}\nExpires: .+\nRoom Type: group/
125+
/Web App URL: .+\nPasscode: \d{3} \d{3} \d{4} \d{4}\nExpires: .+\nRoom Type: group\nEdit your token server at: https:\/\/www.twilio.com\/console\/functions\/editor\/ZS\w{32}\/environment\/ZE\w{32}\/function\/ZH\w{32}/
126126
);
127127
});
128128
});
@@ -248,7 +248,9 @@ describe('the RTC Twilio-CLI Plugin', () => {
248248
stdout.start();
249249
await ViewCommand.run([]);
250250
stdout.stop();
251-
expect(stdout.output).toMatch(/Passcode: \d{3} \d{3} \d{4} \d{4}\nExpires: .+\nRoom Type: go/);
251+
expect(stdout.output).toMatch(
252+
/Passcode: \d{3} \d{3} \d{4} \d{4}\nExpires: .+\nRoom Type: go\nEdit your token server at: https:\/\/www.twilio.com\/console\/functions\/editor\/ZS\w{32}\/environment\/ZE\w{32}\/function\/ZH\w{32}/
253+
);
252254
expect(stdout.output).not.toMatch(/Web App URL:/);
253255
});
254256
});

test/helpers/helpers.test.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { getListOfFunctionsAndAssets } = require('@twilio-labs/serverless-api/dis
1313
const path = require('path');
1414
const { stdout } = require('stdout-stderr');
1515

16-
const mockDeployProject = jest.fn(() => Promise.resolve());
16+
const mockDeployProject = jest.fn(() => Promise.resolve({ serviceSid: 'mockServiceSid' }));
1717

1818
jest.mock('@twilio-labs/serverless-api', () => ({
1919
TwilioServerlessApiClient: function() {
@@ -38,10 +38,14 @@ jest.mock('@twilio-labs/serverless-api/dist/utils/fs', () => ({
3838
function getMockTwilioInstance(options) {
3939
const mockTwilioClient = {
4040
serverless: {},
41+
username: options.username,
42+
password: options.password,
4143
};
4244

4345
const mockAppInstance = {
4446
assets: { list: () => Promise.resolve(options.hasAssets ? [{}] : []) },
47+
functions: {},
48+
update: jest.fn(() => Promise.resolve()),
4549
};
4650

4751
mockAppInstance.environments = jest.fn(() => ({
@@ -55,8 +59,9 @@ function getMockTwilioInstance(options) {
5559
},
5660
}));
5761
mockAppInstance.environments.list = () =>
58-
Promise.resolve([{ sid: 'env', domainName: `${APP_NAME}-1234-5678-dev.twil.io` }]);
59-
mockTwilioClient.serverless.services = jest.fn(() => Promise.resolve(mockAppInstance));
62+
Promise.resolve([{ sid: 'environmentSid', domainName: `${APP_NAME}-1234-5678-dev.twil.io` }]);
63+
mockAppInstance.functions.list = () => Promise.resolve([{ sid: 'tokenFunctionSid', friendlyName: 'token' }]);
64+
mockTwilioClient.serverless.services = jest.fn(() => mockAppInstance);
6065
mockTwilioClient.serverless.services.list = () =>
6166
Promise.resolve([
6267
{
@@ -178,6 +183,8 @@ describe('the getAppInfo function', () => {
178183
});
179184
expect(result).toEqual({
180185
expiry: 'Wed May 20 2020 18:40:00 GMT+0000',
186+
environmentSid: 'environmentSid',
187+
functionSid: 'tokenFunctionSid',
181188
hasAssets: false,
182189
passcode: '12345612345678',
183190
sid: 'appSid',
@@ -192,6 +199,8 @@ describe('the getAppInfo function', () => {
192199
});
193200
expect(result).toEqual({
194201
expiry: 'Wed May 20 2020 18:40:00 GMT+0000',
202+
environmentSid: 'environmentSid',
203+
functionSid: 'tokenFunctionSid',
195204
hasAssets: true,
196205
passcode: '12345612345678',
197206
sid: 'appSid',
@@ -220,6 +229,7 @@ describe('the displayAppInfo function', () => {
220229
"Passcode: 123 456 1234 5678
221230
Expires: Wed May 20 2020 18:40:00 GMT+0000
222231
Room Type: group
232+
Edit your token server at: https://www.twilio.com/console/functions/editor/appSid/environment/environmentSid/function/tokenFunctionSid
223233
"
224234
`);
225235
});
@@ -233,6 +243,7 @@ describe('the displayAppInfo function', () => {
233243
Passcode: 123 456 1234 5678
234244
Expires: Wed May 20 2020 18:40:00 GMT+0000
235245
Room Type: group
246+
Edit your token server at: https://www.twilio.com/console/functions/editor/appSid/environment/environmentSid/function/tokenFunctionSid
236247
"
237248
`);
238249
});
@@ -242,19 +253,16 @@ describe('the displayAppInfo function', () => {
242253
twilioClient: getMockTwilioInstance({ exists: false }),
243254
});
244255
expect(stdout.output).toMatchInlineSnapshot(`
245-
"There is no deployed app
246-
"
247-
`);
256+
"There is no deployed app
257+
"
258+
`);
248259
});
249260
});
250261

251262
describe('the deploy function', () => {
252263
it('should set serviceSid when appInfo exists', async () => {
253264
await deploy.call({
254-
twilioClient: {
255-
username: '',
256-
password: '',
257-
},
265+
twilioClient: getMockTwilioInstance({ username: '', password: '' }),
258266
appInfo: {
259267
sid: '1234',
260268
},
@@ -267,16 +275,41 @@ describe('the deploy function', () => {
267275

268276
it('should set serviceName when appInfo doesnt exist', async () => {
269277
await deploy.call({
270-
twilioClient: {
271-
username: '',
272-
password: '',
273-
},
278+
twilioClient: getMockTwilioInstance({ username: '', password: '' }),
274279
flags: {},
275280
});
276281
expect(mockDeployProject.mock.calls[0][0].serviceSid).toBe(undefined);
277282
expect(mockDeployProject.mock.calls[0][0].serviceName).toMatch(new RegExp(`${APP_NAME}-(\\d{4})`));
278283
});
279284

285+
it('should set ui-editable to false when the flag is false', async () => {
286+
const mockTwilioClient = getMockTwilioInstance({ username: '', password: '' });
287+
await deploy.call({
288+
twilioClient: mockTwilioClient,
289+
flags: {
290+
'ui-editable': false,
291+
},
292+
});
293+
expect(mockTwilioClient.serverless.services().update).toHaveBeenCalledWith({
294+
includeCredentials: true,
295+
uiEditable: false,
296+
});
297+
});
298+
299+
it('should set ui-editable to true when the flag is true', async () => {
300+
const mockTwilioClient = getMockTwilioInstance({ username: '', password: '' });
301+
await deploy.call({
302+
twilioClient: mockTwilioClient,
303+
flags: {
304+
'ui-editable': true,
305+
},
306+
});
307+
expect(mockTwilioClient.serverless.services().update).toHaveBeenCalledWith({
308+
includeCredentials: true,
309+
uiEditable: true,
310+
});
311+
});
312+
280313
it('should display an error when the API key is not provided', () => {
281314
return expect(
282315
deploy.call({
@@ -288,17 +321,17 @@ describe('the deploy function', () => {
288321
flags: {},
289322
})
290323
).rejects.toMatchInlineSnapshot(`
291-
[Error: No API Key found.
324+
[Error: No API Key found.
292325
293-
Please login to the Twilio CLI to create an API key:
326+
Please login to the Twilio CLI to create an API key:
294327
295-
twilio login
328+
twilio login
296329
297-
Alternatively, the Twilio CLI can use credentials stored in these environment variables:
330+
Alternatively, the Twilio CLI can use credentials stored in these environment variables:
298331
299-
TWILIO_ACCOUNT_SID = your Account SID from twil.io/console
300-
TWILIO_API_KEY = an API Key created at twil.io/get-api-key
301-
TWILIO_API_SECRET = the secret for the API Key]
302-
`);
332+
TWILIO_ACCOUNT_SID = your Account SID from twil.io/console
333+
TWILIO_API_KEY = an API Key created at twil.io/get-api-key
334+
TWILIO_API_SECRET = the secret for the API Key]
335+
`);
303336
});
304337
});

0 commit comments

Comments
 (0)