Skip to content

Commit b2def3e

Browse files
Aaron AlanizTim Mendoza
andauthored
AHOYAPPS-489 Update override behavior (#19)
* Enable overriding an existing token server without delete * Add support for overriding assets * Add test to validate override after app deploy * Address PR feedback * Remove superfluous definition * Add unit tests for deploy function Co-authored-by: Tim Mendoza <[email protected]>
1 parent 966354c commit b2def3e

File tree

4 files changed

+111
-14
lines changed

4 files changed

+111
-14
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@ class DeployCommand extends TwilioClientCommand {
1515
}
1616
}
1717

18-
const appInfo = await getAppInfo.call(this);
18+
this.appInfo = await getAppInfo.call(this);
1919

20-
if (appInfo) {
21-
if (this.flags.override) {
22-
await this.twilioClient.serverless.services(appInfo.sid).remove();
23-
console.log(`Removed app with Passcode: ${appInfo.passcode}`);
24-
} else {
25-
console.log('A Video app is already deployed. Use the --override flag to override the existing deployment.');
26-
await displayAppInfo.call(this);
27-
return;
28-
}
20+
if (this.appInfo && !this.flags.override) {
21+
console.log('A Video app is already deployed. Use the --override flag to override the existing deployment.');
22+
await displayAppInfo.call(this);
23+
return;
2924
}
3025
await deploy.call(this);
3126
await displayAppInfo.call(this);
3227
}
3328
}
34-
3529
DeployCommand.flags = Object.assign(
3630
{
3731
'app-directory': flags.string({

src/helpers.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ async function getAssets(folder) {
4848
assets.push({
4949
...indexHTML,
5050
path: '/',
51+
name: '/',
5152
});
5253
assets.push({
5354
...indexHTML,
5455
path: '/login',
56+
name: '/login',
5557
});
5658

5759
return assets;
@@ -130,7 +132,6 @@ async function deploy() {
130132
API_PASSCODE_EXPIRY: expiryTime,
131133
},
132134
pkgJson: {},
133-
serviceName: APP_NAME,
134135
functionsEnv: 'dev',
135136
functions: [
136137
{
@@ -143,6 +144,12 @@ async function deploy() {
143144
assets: assets,
144145
};
145146

147+
if (this.appInfo && this.appInfo.sid) {
148+
deployOptions.serviceSid = this.appInfo.sid;
149+
} else {
150+
deployOptions.serviceName = APP_NAME;
151+
}
152+
146153
try {
147154
await serverlessClient.deployProject(deployOptions);
148155
cli.action.stop();

test/e2e/e2e.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,41 @@ describe('the RTC Twilio-CLI Plugin', () => {
148148
stdout.stop();
149149
expect(stdout.output).toContain('A Video app is already deployed. Use the --override flag to override the existing deployment.');
150150
});
151+
152+
it('should redeploy the app when --override flag is passed', async () => {
153+
stdout.start();
154+
await DeployCommand.run([
155+
'--authentication',
156+
'passcode',
157+
'--override',
158+
'--app-directory',
159+
path.join(__dirname, '../test-assets'),
160+
]);
161+
stdout.stop();
162+
const updatedPasscode = getPasscode(stdout.output);
163+
const testURL = getURL(stdout.output);
164+
const testWebAppURL = getWebAppURL(stdout.output);
165+
expect(updatedPasscode).not.toEqual(passcode)
166+
expect(testURL).toEqual(URL)
167+
const { text } = await superagent.get(testWebAppURL + '/login');
168+
expect(text).toEqual('<html>test</html>');
169+
});
170+
171+
it('should redeploy the token server when no app-directory is set and when --override flag is true', async () => {
172+
stdout.start();
173+
await DeployCommand.run([
174+
'--authentication',
175+
'passcode',
176+
'--override',
177+
]);
178+
stdout.stop();
179+
const updatedPasscode = getPasscode(stdout.output);
180+
const testURL = getURL(stdout.output);
181+
const testWebAppURL = getWebAppURL(stdout.output);
182+
expect(updatedPasscode).not.toEqual(passcode)
183+
expect(testURL).toEqual(URL)
184+
superagent.get(`${testWebAppURL}`).catch(e => expect(e.status).toBe(404));
185+
});
151186
});
152187
});
153188

@@ -202,6 +237,26 @@ describe('the RTC Twilio-CLI Plugin', () => {
202237
it('should return a 404 from "/"', () => {
203238
superagent.get(`${URL}`).catch(e => expect(e.status).toBe(404));
204239
});
240+
241+
it('should redeploy the token server when --override flag is passed', async () => {
242+
stdout.start();
243+
await DeployCommand.run([
244+
'--authentication',
245+
'passcode',
246+
'--override',
247+
]);
248+
stdout.stop();
249+
250+
const updatedPasscode = getPasscode(stdout.output);
251+
const testURL = getURL(stdout.output);
252+
expect(updatedPasscode).not.toEqual(passcode)
253+
expect(testURL).toEqual(URL)
254+
255+
const { body } = await superagent
256+
.post(`${testURL}/token`)
257+
.send({ passcode: updatedPasscode, room_name: 'test-room', user_identity: 'test user' });
258+
expect(jwt.decode(body.token).grants).toEqual({ identity: 'test user', video: { room: 'test-room' } });
259+
});
205260
});
206261
});
207262
});

test/helpers/helpers.test.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { APP_NAME } = require('../../src/constants');
22
const {
3+
deploy,
34
displayAppInfo,
45
findApp,
56
getAppInfo,
@@ -12,6 +13,16 @@ const { getListOfFunctionsAndAssets } = require('@twilio-labs/serverless-api/dis
1213
const path = require('path');
1314
const { stdout } = require('stdout-stderr');
1415

16+
const mockDeployProject = jest.fn(() => Promise.resolve());
17+
18+
jest.mock('@twilio-labs/serverless-api', () => ({
19+
TwilioServerlessApiClient: function() {
20+
return {
21+
deployProject: mockDeployProject,
22+
};
23+
},
24+
}));
25+
1526
jest.mock('@twilio-labs/serverless-api/dist/utils/fs', () => ({
1627
getListOfFunctionsAndAssets: jest.fn(() => ({
1728
assets: [
@@ -98,12 +109,12 @@ describe('the getAssets function', () => {
98109
content: 'mockHTMLcontent',
99110
},
100111
{
101-
name: 'index.html',
112+
name: '/',
102113
path: '/',
103114
content: 'mockHTMLcontent',
104115
},
105116
{
106-
name: 'index.html',
117+
name: '/login',
107118
path: '/login',
108119
content: 'mockHTMLcontent',
109120
},
@@ -225,3 +236,33 @@ describe('the displayAppInfo function', () => {
225236
`);
226237
});
227238
});
239+
240+
describe('the deploy function', () => {
241+
it('should set serviceSid when appInfo exists', async () => {
242+
await deploy.call({
243+
twilioClient: {
244+
username: '',
245+
password: '',
246+
},
247+
appInfo: {
248+
sid: '1234',
249+
},
250+
flags: {},
251+
});
252+
253+
expect(mockDeployProject.mock.calls[0][0].serviceSid).toBe('1234');
254+
expect(mockDeployProject.mock.calls[0][0].serviceName).toBe(undefined);
255+
});
256+
257+
it('should set serviceName when appInfo doesnt exist', async () => {
258+
await deploy.call({
259+
twilioClient: {
260+
username: '',
261+
password: '',
262+
},
263+
flags: {},
264+
});
265+
expect(mockDeployProject.mock.calls[0][0].serviceSid).toBe(undefined);
266+
expect(mockDeployProject.mock.calls[0][0].serviceName).toBe(APP_NAME);
267+
});
268+
});

0 commit comments

Comments
 (0)