Skip to content

Commit 81ec0e3

Browse files
author
Tim Mendoza
committed
Fix issue where middleware was not uploaded when an app-directory was not provided
Also, changed logic in getAppInfo so web app URL is only display when web assets exist
1 parent 3638ef5 commit 81ec0e3

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/helpers.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ async function getAssets(folder) {
4646
});
4747

4848
const indexHTML = assets.find(asset => asset.name.includes('index.html'));
49-
const authHandlerFn = fs.readFileSync(path.join(__dirname, './serverless/middleware/auth.js'));
5049

5150
const allAssets = assets.concat([
5251
{
@@ -59,15 +58,22 @@ async function getAssets(folder) {
5958
path: '/login',
6059
name: '/login',
6160
},
61+
]);
62+
63+
return allAssets;
64+
}
65+
66+
function getMiddleware() {
67+
const authHandlerFn = fs.readFileSync(path.join(__dirname, './serverless/middleware/auth.js'));
68+
69+
return [
6270
{
6371
name: 'auth-handler',
6472
path: '/auth-handler.js',
6573
content: authHandlerFn,
6674
access: 'private',
6775
},
68-
]);
69-
70-
return allAssets;
76+
];
7177
}
7278

7379
async function findApp() {
@@ -106,7 +112,7 @@ async function getAppInfo() {
106112
expiry: moment(Number(expiry)).toString(),
107113
sid: app.sid,
108114
passcode: fullPasscode,
109-
hasAssets: Boolean(assets.length),
115+
hasWebAssets: Boolean(assets.find(asset => asset.friendlyName.includes('index.html'))),
110116
roomType,
111117
environmentSid: environment.sid,
112118
functionSid: tokenServerFunction.sid,
@@ -121,7 +127,7 @@ async function displayAppInfo() {
121127
return;
122128
}
123129

124-
if (appInfo.hasAssets) {
130+
if (appInfo.hasWebAssets) {
125131
console.log(`Web App URL: ${appInfo.url}`);
126132
}
127133

@@ -144,6 +150,8 @@ async function deploy() {
144150
assetsFolderNames: [],
145151
});
146152

153+
assets.push(...getMiddleware());
154+
147155
if (this.twilioClient.username === this.twilioClient.accountSid) {
148156
// When twilioClient.username equals twilioClient.accountSid, it means that the user
149157
// authenticated with the Twilio CLI by providing their Account SID and Auth Token
@@ -213,6 +221,7 @@ module.exports = {
213221
displayAppInfo,
214222
findApp,
215223
getAssets,
224+
getMiddleware,
216225
getAppInfo,
217226
getPasscode,
218227
getRandomInt,

test/helpers/helpers.test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
getPasscode,
99
getRandomInt,
1010
verifyAppDirectory,
11+
getMiddleware,
1112
} = require('../../src/helpers');
1213
const { getListOfFunctionsAndAssets } = require('@twilio-labs/serverless-api/dist/utils/fs');
1314
const path = require('path');
@@ -43,7 +44,7 @@ function getMockTwilioInstance(options) {
4344
};
4445

4546
const mockAppInstance = {
46-
assets: { list: () => Promise.resolve(options.hasAssets ? [{}] : []) },
47+
assets: { list: () => Promise.resolve(options.hasWebAssets ? [{ friendlyName: 'index.html' }] : []) },
4748
functions: {},
4849
update: jest.fn(() => Promise.resolve()),
4950
};
@@ -134,9 +135,11 @@ describe('the getAssets function', () => {
134135
])
135136
);
136137
});
138+
});
137139

140+
describe('the getMiddleware function', () => {
138141
it('should add the auth-handler.js as a private asset', async () => {
139-
expect(await getAssets('mockFolder')).toEqual(
142+
expect(await getMiddleware('mockFolder')).toEqual(
140143
expect.arrayContaining([
141144
{
142145
name: 'auth-handler',
@@ -200,23 +203,23 @@ describe('the getAppInfo function', () => {
200203
expiry: 'Wed May 20 2020 18:40:00 GMT+0000',
201204
environmentSid: 'environmentSid',
202205
functionSid: 'tokenFunctionSid',
203-
hasAssets: false,
206+
hasWebAssets: false,
204207
passcode: '12345612345678',
205208
sid: 'appSid',
206209
url: `https://${APP_NAME}-1234-5678-dev.twil.io?passcode=12345612345678`,
207210
roomType: 'group',
208211
});
209212
});
210213

211-
it('should return the correct information when there are assets', async () => {
214+
it('should return the correct information when there are web assets', async () => {
212215
const result = await getAppInfo.call({
213-
twilioClient: getMockTwilioInstance({ exists: true, hasAssets: true }),
216+
twilioClient: getMockTwilioInstance({ exists: true, hasWebAssets: true }),
214217
});
215218
expect(result).toEqual({
216219
expiry: 'Wed May 20 2020 18:40:00 GMT+0000',
217220
environmentSid: 'environmentSid',
218221
functionSid: 'tokenFunctionSid',
219-
hasAssets: true,
222+
hasWebAssets: true,
220223
passcode: '12345612345678',
221224
sid: 'appSid',
222225
url: `https://${APP_NAME}-1234-5678-dev.twil.io?passcode=12345612345678`,
@@ -251,7 +254,7 @@ describe('the displayAppInfo function', () => {
251254

252255
it('should display the correct information when there are assets', async () => {
253256
await displayAppInfo.call({
254-
twilioClient: getMockTwilioInstance({ exists: true, hasAssets: true }),
257+
twilioClient: getMockTwilioInstance({ exists: true, hasWebAssets: true }),
255258
});
256259
expect(stdout.output).toMatchInlineSnapshot(`
257260
"Web App URL: https://${APP_NAME}-1234-5678-dev.twil.io?passcode=12345612345678

0 commit comments

Comments
 (0)