@adobe/aio-lib-web 7.3.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/remote-storage.js +219 -75
- package/package.json +5 -3
- package/src/deploy-web.js +28 -9
- package/src/undeploy-web.js +18 -6
- package/test/jest.setup.js +28 -0
- package/test/lib/remote-storage.test.js +952 -554
- package/test/src/deploy-web.test.js +197 -79
- package/test/src/undeploy-web.test.js +88 -19
- package/lib/getS3Creds.js +0 -42
- package/test/lib/getS3Creds.test.js +0 -73
|
@@ -15,10 +15,6 @@ const deployWeb = require('../../src/deploy-web')
|
|
|
15
15
|
const fs = require('fs-extra')
|
|
16
16
|
jest.mock('fs-extra')
|
|
17
17
|
|
|
18
|
-
jest.mock('../../lib/getS3Creds')
|
|
19
|
-
const getS3Credentials = require('../../lib/getS3Creds')
|
|
20
|
-
getS3Credentials.mockResolvedValue('fakecreds')
|
|
21
|
-
|
|
22
18
|
const mockRemoteStorageInstance = {
|
|
23
19
|
emptyFolder: jest.fn(),
|
|
24
20
|
folderExists: jest.fn(),
|
|
@@ -37,7 +33,6 @@ describe('deploy-web', () => {
|
|
|
37
33
|
mockRemoteStorageInstance.emptyFolder.mockReset()
|
|
38
34
|
mockRemoteStorageInstance.folderExists.mockReset()
|
|
39
35
|
mockRemoteStorageInstance.uploadDir.mockReset()
|
|
40
|
-
getS3Credentials.mockClear()
|
|
41
36
|
|
|
42
37
|
global.cleanFs(vol)
|
|
43
38
|
})
|
|
@@ -48,10 +43,34 @@ describe('deploy-web', () => {
|
|
|
48
43
|
await expect(deployWeb({ app: { hasFrontEnd: false } })).rejects.toThrow('cannot deploy web')
|
|
49
44
|
})
|
|
50
45
|
|
|
46
|
+
test('throws if no auth token', async () => {
|
|
47
|
+
const config = {
|
|
48
|
+
app: {
|
|
49
|
+
hasFrontend: true
|
|
50
|
+
},
|
|
51
|
+
ow: {
|
|
52
|
+
namespace: 'ns',
|
|
53
|
+
auth_handler: {
|
|
54
|
+
getAuthHeader: jest.fn().mockResolvedValue(null)
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
web: {
|
|
58
|
+
distProd: 'dist'
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
await expect(deployWeb(config)).rejects.toThrow('cannot deploy web, Authorization is required')
|
|
62
|
+
})
|
|
63
|
+
|
|
51
64
|
test('throws if src dir does not exist', async () => {
|
|
52
65
|
const config = {
|
|
53
66
|
s3: {
|
|
54
|
-
|
|
67
|
+
folder: 'somefolder'
|
|
68
|
+
},
|
|
69
|
+
ow: {
|
|
70
|
+
namespace: 'ns',
|
|
71
|
+
auth_handler: {
|
|
72
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
73
|
+
}
|
|
55
74
|
},
|
|
56
75
|
app: {
|
|
57
76
|
hasFrontend: true
|
|
@@ -66,7 +85,13 @@ describe('deploy-web', () => {
|
|
|
66
85
|
test('throws if src dir is not a directory', async () => {
|
|
67
86
|
const config = {
|
|
68
87
|
s3: {
|
|
69
|
-
|
|
88
|
+
folder: 'somefolder'
|
|
89
|
+
},
|
|
90
|
+
ow: {
|
|
91
|
+
namespace: 'ns',
|
|
92
|
+
auth_handler: {
|
|
93
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
94
|
+
}
|
|
70
95
|
},
|
|
71
96
|
app: {
|
|
72
97
|
hasFrontend: true
|
|
@@ -80,13 +105,20 @@ describe('deploy-web', () => {
|
|
|
80
105
|
await expect(deployWeb(config)).rejects.toThrow('missing files in dist')
|
|
81
106
|
})
|
|
82
107
|
|
|
83
|
-
test('throws if
|
|
84
|
-
const
|
|
108
|
+
test('throws if hostname or namespace is missing', async () => {
|
|
109
|
+
const baseConfig = {
|
|
85
110
|
s3: {
|
|
86
|
-
|
|
111
|
+
folder: 'somefolder'
|
|
112
|
+
},
|
|
113
|
+
ow: {
|
|
114
|
+
namespace: 'ns',
|
|
115
|
+
auth_handler: {
|
|
116
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
117
|
+
}
|
|
87
118
|
},
|
|
88
119
|
app: {
|
|
89
|
-
hasFrontend: true
|
|
120
|
+
hasFrontend: true,
|
|
121
|
+
hostname: 'host'
|
|
90
122
|
},
|
|
91
123
|
web: {
|
|
92
124
|
distProd: 'dist'
|
|
@@ -94,21 +126,56 @@ describe('deploy-web', () => {
|
|
|
94
126
|
}
|
|
95
127
|
fs.existsSync.mockReturnValue(true)
|
|
96
128
|
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
97
|
-
fs.readdirSync.mockReturnValue({ length:
|
|
98
|
-
|
|
129
|
+
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
130
|
+
|
|
131
|
+
await expect(deployWeb({
|
|
132
|
+
...baseConfig,
|
|
133
|
+
app: { hasFrontend: true }
|
|
134
|
+
})).rejects.toThrow('config.app.hostname and config.ow.namespace are required')
|
|
135
|
+
|
|
136
|
+
await expect(deployWeb({
|
|
137
|
+
...baseConfig,
|
|
138
|
+
ow: { auth_handler: baseConfig.ow.auth_handler }
|
|
139
|
+
})).rejects.toThrow('config.app.hostname and config.ow.namespace are required')
|
|
99
140
|
})
|
|
100
141
|
|
|
101
|
-
test('
|
|
142
|
+
test('throws if hostname is invalid', async () => {
|
|
102
143
|
const config = {
|
|
144
|
+
s3: {
|
|
145
|
+
folder: 'somefolder'
|
|
146
|
+
},
|
|
103
147
|
ow: {
|
|
104
148
|
namespace: 'ns',
|
|
105
|
-
|
|
149
|
+
auth_handler: {
|
|
150
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
151
|
+
}
|
|
106
152
|
},
|
|
153
|
+
app: {
|
|
154
|
+
hasFrontend: true,
|
|
155
|
+
hostname: 'bad host!'
|
|
156
|
+
},
|
|
157
|
+
web: {
|
|
158
|
+
distProd: 'dist'
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
fs.existsSync.mockReturnValue(true)
|
|
162
|
+
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
163
|
+
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
164
|
+
|
|
165
|
+
await expect(deployWeb(config)).rejects.toThrow('config.app.hostname is invalid')
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('throws if namespace is invalid', async () => {
|
|
169
|
+
const config = {
|
|
107
170
|
s3: {
|
|
108
|
-
credsCacheFile: 'file',
|
|
109
|
-
tvmUrl: 'url',
|
|
110
171
|
folder: 'somefolder'
|
|
111
172
|
},
|
|
173
|
+
ow: {
|
|
174
|
+
namespace: 'bad/ns',
|
|
175
|
+
auth_handler: {
|
|
176
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
177
|
+
}
|
|
178
|
+
},
|
|
112
179
|
app: {
|
|
113
180
|
hasFrontend: true,
|
|
114
181
|
hostname: 'host'
|
|
@@ -120,23 +187,43 @@ describe('deploy-web', () => {
|
|
|
120
187
|
fs.existsSync.mockReturnValue(true)
|
|
121
188
|
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
122
189
|
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
123
|
-
|
|
124
|
-
expect(
|
|
125
|
-
expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
|
|
126
|
-
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, null)
|
|
127
|
-
expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
|
|
128
|
-
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
190
|
+
|
|
191
|
+
await expect(deployWeb(config)).rejects.toThrow('config.ow.namespace is invalid')
|
|
129
192
|
})
|
|
130
193
|
|
|
131
|
-
test('
|
|
194
|
+
test('throws if src dir is empty', async () => {
|
|
132
195
|
const config = {
|
|
196
|
+
s3: {
|
|
197
|
+
folder: 'somefolder'
|
|
198
|
+
},
|
|
133
199
|
ow: {
|
|
134
200
|
namespace: 'ns',
|
|
135
|
-
|
|
201
|
+
auth_handler: {
|
|
202
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
app: {
|
|
206
|
+
hasFrontend: true
|
|
207
|
+
},
|
|
208
|
+
web: {
|
|
209
|
+
distProd: 'dist'
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
fs.existsSync.mockReturnValue(true)
|
|
213
|
+
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
214
|
+
fs.readdirSync.mockReturnValue({ length: 0 })
|
|
215
|
+
await expect(deployWeb(config)).rejects.toThrow('missing files in dist')
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('uploads files', async () => {
|
|
219
|
+
const config = {
|
|
220
|
+
ow: {
|
|
221
|
+
namespace: 'ns',
|
|
222
|
+
auth_handler: {
|
|
223
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
224
|
+
}
|
|
136
225
|
},
|
|
137
226
|
s3: {
|
|
138
|
-
credsCacheFile: 'file',
|
|
139
|
-
tvmUrl: 'url',
|
|
140
227
|
folder: 'somefolder'
|
|
141
228
|
},
|
|
142
229
|
app: {
|
|
@@ -150,25 +237,20 @@ describe('deploy-web', () => {
|
|
|
150
237
|
fs.existsSync.mockReturnValue(true)
|
|
151
238
|
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
152
239
|
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
mockRemoteStorageInstance.uploadDir.
|
|
156
|
-
await expect(deployWeb(config, mockLogger)).resolves.toEqual('https://ns.host/index.html')
|
|
157
|
-
expect(getS3Credentials).toHaveBeenCalledWith(config)
|
|
158
|
-
expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
|
|
159
|
-
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, expect.any(Function))
|
|
160
|
-
expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
|
|
161
|
-
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
240
|
+
await expect(deployWeb(config)).resolves.toEqual('https://ns.host/index.html')
|
|
241
|
+
expect(RemoteStorage).toHaveBeenCalledWith()
|
|
242
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('Bearer token', 'dist', '', config, null)
|
|
162
243
|
})
|
|
163
244
|
|
|
164
|
-
test('
|
|
245
|
+
test('uploads files with log func', async () => {
|
|
165
246
|
const config = {
|
|
166
247
|
ow: {
|
|
167
248
|
namespace: 'ns',
|
|
168
|
-
|
|
249
|
+
auth_handler: {
|
|
250
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
251
|
+
}
|
|
169
252
|
},
|
|
170
253
|
s3: {
|
|
171
|
-
creds: 'somecreds',
|
|
172
254
|
folder: 'somefolder'
|
|
173
255
|
},
|
|
174
256
|
app: {
|
|
@@ -181,26 +263,23 @@ describe('deploy-web', () => {
|
|
|
181
263
|
}
|
|
182
264
|
fs.existsSync.mockReturnValue(true)
|
|
183
265
|
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
184
|
-
const mockLogger = jest.fn()
|
|
185
266
|
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
267
|
+
const mockLogger = jest.fn()
|
|
268
|
+
// for func coverage
|
|
269
|
+
mockRemoteStorageInstance.uploadDir.mockImplementation((a, b, c, d, func) => func('dist/somefile'))
|
|
189
270
|
await expect(deployWeb(config, mockLogger)).resolves.toEqual('https://ns.host/index.html')
|
|
190
|
-
expect(
|
|
191
|
-
expect(
|
|
192
|
-
expect(
|
|
193
|
-
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
194
|
-
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('dist', 'somefolder', config, expect.any(Function))
|
|
195
|
-
// empty dir!
|
|
196
|
-
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('somefolder/')
|
|
271
|
+
expect(RemoteStorage).toHaveBeenCalledWith()
|
|
272
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('Bearer token', 'dist', '', config, expect.any(Function))
|
|
273
|
+
expect(mockLogger).toHaveBeenCalledWith('deploying somefile')
|
|
197
274
|
})
|
|
198
275
|
|
|
199
|
-
test('
|
|
276
|
+
test('clears existing deployment before uploading to avoid stale files', async () => {
|
|
200
277
|
const config = {
|
|
201
278
|
ow: {
|
|
202
279
|
namespace: 'ns',
|
|
203
|
-
|
|
280
|
+
auth_handler: {
|
|
281
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
282
|
+
}
|
|
204
283
|
},
|
|
205
284
|
s3: {
|
|
206
285
|
folder: 'somefolder'
|
|
@@ -216,27 +295,75 @@ describe('deploy-web', () => {
|
|
|
216
295
|
fs.existsSync.mockReturnValue(true)
|
|
217
296
|
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
218
297
|
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
219
|
-
|
|
220
298
|
mockRemoteStorageInstance.folderExists.mockResolvedValue(true)
|
|
221
299
|
|
|
222
|
-
await
|
|
223
|
-
|
|
224
|
-
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('
|
|
225
|
-
expect(mockRemoteStorageInstance.
|
|
226
|
-
|
|
227
|
-
expect(mockRemoteStorageInstance.emptyFolder).
|
|
300
|
+
await deployWeb(config)
|
|
301
|
+
|
|
302
|
+
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('Bearer token', '/', config)
|
|
303
|
+
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('Bearer token', '/', config)
|
|
304
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith('Bearer token', 'dist', '', config, null)
|
|
305
|
+
expect(mockRemoteStorageInstance.emptyFolder.mock.invocationCallOrder[0]).toBeLessThan(
|
|
306
|
+
mockRemoteStorageInstance.uploadDir.mock.invocationCallOrder[0]
|
|
307
|
+
)
|
|
228
308
|
})
|
|
229
309
|
|
|
230
|
-
test('
|
|
310
|
+
test('throws if config has no ow auth_handler', async () => {
|
|
311
|
+
const config = {
|
|
312
|
+
app: { hasFrontend: true },
|
|
313
|
+
web: { distProd: 'dist' }
|
|
314
|
+
}
|
|
315
|
+
await expect(deployWeb(config)).rejects.toThrow('cannot deploy web, Authorization is required')
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
describe('byo s3 credentials', () => {
|
|
319
|
+
const byoConfig = () => ({
|
|
320
|
+
ow: { namespace: 'ns' },
|
|
321
|
+
s3: {
|
|
322
|
+
folder: 'somefolder',
|
|
323
|
+
creds: { accessKeyId: 'fake', secretAccessKey: 'fake', params: { Bucket: 'bucket' } }
|
|
324
|
+
},
|
|
325
|
+
app: { hasFrontend: true, hostname: 'host' },
|
|
326
|
+
web: { distProd: 'dist' }
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
beforeEach(() => {
|
|
330
|
+
fs.existsSync.mockReturnValue(true)
|
|
331
|
+
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
332
|
+
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
test('uploads to the s3 folder without requiring an auth token', async () => {
|
|
336
|
+
const config = byoConfig()
|
|
337
|
+
mockRemoteStorageInstance.folderExists.mockResolvedValue(false)
|
|
338
|
+
|
|
339
|
+
await expect(deployWeb(config)).resolves.toEqual('https://ns.host/index.html')
|
|
340
|
+
|
|
341
|
+
expect(RemoteStorage).toHaveBeenCalledWith(config.s3.creds)
|
|
342
|
+
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith(undefined, 'somefolder/', config)
|
|
343
|
+
expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
|
|
344
|
+
expect(mockRemoteStorageInstance.uploadDir).toHaveBeenCalledWith(undefined, 'dist', 'somefolder', config, null)
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
test('empties the existing s3 folder before uploading', async () => {
|
|
348
|
+
const config = byoConfig()
|
|
349
|
+
mockRemoteStorageInstance.folderExists.mockResolvedValue(true)
|
|
350
|
+
|
|
351
|
+
await deployWeb(config)
|
|
352
|
+
|
|
353
|
+
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith(undefined, 'somefolder/', config)
|
|
354
|
+
})
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
test('logs overwrite warning when an existing deployment will be replaced', async () => {
|
|
231
358
|
const config = {
|
|
232
359
|
ow: {
|
|
233
360
|
namespace: 'ns',
|
|
234
|
-
|
|
361
|
+
auth_handler: {
|
|
362
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
363
|
+
}
|
|
235
364
|
},
|
|
236
365
|
s3: {
|
|
237
|
-
|
|
238
|
-
tvmUrl: 'url',
|
|
239
|
-
folder: 'nsfolder'
|
|
366
|
+
folder: 'somefolder'
|
|
240
367
|
},
|
|
241
368
|
app: {
|
|
242
369
|
hasFrontend: true,
|
|
@@ -246,23 +373,14 @@ describe('deploy-web', () => {
|
|
|
246
373
|
distProd: 'dist'
|
|
247
374
|
}
|
|
248
375
|
}
|
|
249
|
-
const emptyFolder = jest.fn()
|
|
250
|
-
const folderExists = jest.fn(() => true)
|
|
251
|
-
RemoteStorage.mockImplementation(() => {
|
|
252
|
-
return {
|
|
253
|
-
emptyFolder,
|
|
254
|
-
folderExists,
|
|
255
|
-
uploadDir: jest.fn()
|
|
256
|
-
}
|
|
257
|
-
})
|
|
258
376
|
fs.existsSync.mockReturnValue(true)
|
|
259
377
|
fs.lstatSync.mockReturnValue({ isDirectory: () => true })
|
|
260
|
-
const mockLogger = jest.fn()
|
|
261
378
|
fs.readdirSync.mockReturnValue({ length: 1 })
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
379
|
+
mockRemoteStorageInstance.folderExists.mockResolvedValue(true)
|
|
380
|
+
const mockLogger = jest.fn()
|
|
381
|
+
|
|
382
|
+
await deployWeb(config, mockLogger)
|
|
383
|
+
|
|
384
|
+
expect(mockLogger).toHaveBeenCalledWith('warning: an existing deployment will be overwritten')
|
|
267
385
|
})
|
|
268
386
|
})
|
|
@@ -12,10 +12,6 @@ governing permissions and limitations under the License.
|
|
|
12
12
|
|
|
13
13
|
const undeployWeb = require('../../src/undeploy-web')
|
|
14
14
|
|
|
15
|
-
jest.mock('../../lib/getS3Creds')
|
|
16
|
-
const getS3Credentials = require('../../lib/getS3Creds')
|
|
17
|
-
getS3Credentials.mockResolvedValue('fakecreds')
|
|
18
|
-
|
|
19
15
|
const mockRemoteStorageInstance = {
|
|
20
16
|
emptyFolder: jest.fn(),
|
|
21
17
|
folderExists: jest.fn()
|
|
@@ -32,7 +28,6 @@ describe('undeploy-web', () => {
|
|
|
32
28
|
RemoteStorage.mockClear()
|
|
33
29
|
mockRemoteStorageInstance.emptyFolder.mockReset()
|
|
34
30
|
mockRemoteStorageInstance.folderExists.mockReset()
|
|
35
|
-
getS3Credentials.mockClear()
|
|
36
31
|
})
|
|
37
32
|
|
|
38
33
|
test('throws if config does not have an app, or frontEnd', async () => {
|
|
@@ -41,15 +36,30 @@ describe('undeploy-web', () => {
|
|
|
41
36
|
await expect(undeployWeb({ app: { hasFrontEnd: false } })).rejects.toThrow('cannot undeploy web')
|
|
42
37
|
})
|
|
43
38
|
|
|
44
|
-
test('
|
|
39
|
+
test('throws if no auth token', async () => {
|
|
40
|
+
const config = {
|
|
41
|
+
ow: {
|
|
42
|
+
namespace: 'ns',
|
|
43
|
+
auth_handler: {
|
|
44
|
+
getAuthHeader: jest.fn().mockResolvedValue(null)
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
app: {
|
|
48
|
+
hasFrontend: true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
await expect(undeployWeb(config)).rejects.toThrow('cannot undeploy web, Authorization is required')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('calls folderExists and empties folder', async () => {
|
|
45
55
|
const config = {
|
|
46
56
|
ow: {
|
|
47
57
|
namespace: 'ns',
|
|
48
|
-
|
|
58
|
+
auth_handler: {
|
|
59
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
60
|
+
}
|
|
49
61
|
},
|
|
50
62
|
s3: {
|
|
51
|
-
creds: 'fakes3creds',
|
|
52
|
-
tvmUrl: 'url',
|
|
53
63
|
folder: 'somefolder'
|
|
54
64
|
},
|
|
55
65
|
app: {
|
|
@@ -61,22 +71,22 @@ describe('undeploy-web', () => {
|
|
|
61
71
|
}
|
|
62
72
|
}
|
|
63
73
|
mockRemoteStorageInstance.folderExists.mockResolvedValue(true)
|
|
74
|
+
mockRemoteStorageInstance.emptyFolder.mockResolvedValue(true)
|
|
64
75
|
await undeployWeb(config)
|
|
65
|
-
expect(
|
|
66
|
-
expect(
|
|
67
|
-
expect(mockRemoteStorageInstance.
|
|
68
|
-
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('somefolder/')
|
|
76
|
+
expect(RemoteStorage).toHaveBeenCalledWith()
|
|
77
|
+
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('Bearer token', '/', config)
|
|
78
|
+
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith('Bearer token', '/', config)
|
|
69
79
|
})
|
|
70
80
|
|
|
71
81
|
test('throws if remoteStorage folder does not exist', async () => {
|
|
72
82
|
const config = {
|
|
73
83
|
ow: {
|
|
74
84
|
namespace: 'ns',
|
|
75
|
-
|
|
85
|
+
auth_handler: {
|
|
86
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
87
|
+
}
|
|
76
88
|
},
|
|
77
89
|
s3: {
|
|
78
|
-
credsCacheFile: 'file',
|
|
79
|
-
tvmUrl: 'url',
|
|
80
90
|
folder: 'somefolder'
|
|
81
91
|
},
|
|
82
92
|
app: {
|
|
@@ -86,10 +96,69 @@ describe('undeploy-web', () => {
|
|
|
86
96
|
distProd: 'dist'
|
|
87
97
|
}
|
|
88
98
|
}
|
|
99
|
+
mockRemoteStorageInstance.folderExists.mockResolvedValue(false)
|
|
89
100
|
await expect(undeployWeb(config)).rejects.toThrow('cannot undeploy static files')
|
|
90
|
-
expect(
|
|
91
|
-
expect(
|
|
92
|
-
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
|
|
101
|
+
expect(RemoteStorage).toHaveBeenCalledWith()
|
|
102
|
+
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('Bearer token', '/', config)
|
|
93
103
|
expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
|
|
94
104
|
})
|
|
105
|
+
|
|
106
|
+
test('throws if config has no ow auth_handler', async () => {
|
|
107
|
+
const config = { app: { hasFrontend: true } }
|
|
108
|
+
await expect(undeployWeb(config)).rejects.toThrow('cannot undeploy web, Authorization is required')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
describe('byo s3 credentials', () => {
|
|
112
|
+
const byoConfig = () => ({
|
|
113
|
+
ow: { namespace: 'ns' },
|
|
114
|
+
s3: {
|
|
115
|
+
folder: 'somefolder',
|
|
116
|
+
creds: { accessKeyId: 'fake', secretAccessKey: 'fake', params: { Bucket: 'bucket' } }
|
|
117
|
+
},
|
|
118
|
+
app: { hasFrontend: true }
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('empties the s3 folder without an auth token and ignores the empty result', async () => {
|
|
122
|
+
const config = byoConfig()
|
|
123
|
+
mockRemoteStorageInstance.folderExists.mockResolvedValue(true)
|
|
124
|
+
mockRemoteStorageInstance.emptyFolder.mockResolvedValue(undefined)
|
|
125
|
+
|
|
126
|
+
await expect(undeployWeb(config)).resolves.toBeUndefined()
|
|
127
|
+
|
|
128
|
+
expect(RemoteStorage).toHaveBeenCalledWith(config.s3.creds)
|
|
129
|
+
expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith(undefined, 'somefolder/', config)
|
|
130
|
+
expect(mockRemoteStorageInstance.emptyFolder).toHaveBeenCalledWith(undefined, 'somefolder/', config)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('throws with the s3 folder name if there is no deployment', async () => {
|
|
134
|
+
mockRemoteStorageInstance.folderExists.mockResolvedValue(false)
|
|
135
|
+
|
|
136
|
+
await expect(undeployWeb(byoConfig())).rejects.toThrow('there is no deployment for somefolder')
|
|
137
|
+
expect(mockRemoteStorageInstance.emptyFolder).not.toHaveBeenCalled()
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('throws if remoteStorage emptyFolder fails', async () => {
|
|
142
|
+
const config = {
|
|
143
|
+
ow: {
|
|
144
|
+
namespace: 'ns',
|
|
145
|
+
auth_handler: {
|
|
146
|
+
getAuthHeader: jest.fn().mockResolvedValue('Bearer token')
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
s3: {
|
|
150
|
+
folder: 'somefolder'
|
|
151
|
+
},
|
|
152
|
+
app: {
|
|
153
|
+
hasFrontend: true
|
|
154
|
+
},
|
|
155
|
+
web: {
|
|
156
|
+
distProd: 'dist'
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
mockRemoteStorageInstance.folderExists.mockResolvedValue(true)
|
|
160
|
+
mockRemoteStorageInstance.emptyFolder.mockResolvedValue(false)
|
|
161
|
+
|
|
162
|
+
await expect(undeployWeb(config)).rejects.toThrow('cannot undeploy static files, failed to delete deployment files')
|
|
163
|
+
})
|
|
95
164
|
})
|
package/lib/getS3Creds.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
const TvmClient = require('@adobe/aio-lib-core-tvm')
|
|
13
|
-
|
|
14
|
-
const getS3Credentials = async (config) => {
|
|
15
|
-
if (
|
|
16
|
-
// byo
|
|
17
|
-
!(config.s3 && config.s3.creds) &&
|
|
18
|
-
// ootb
|
|
19
|
-
!(config.ow && config.ow.namespace && config.ow.auth)
|
|
20
|
-
) {
|
|
21
|
-
throw new Error('Please check your .env file to ensure your credentials are correct. You can also use "aio app use" to load/refresh your credentials')
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (config.s3 && config.s3.creds) {
|
|
25
|
-
return config.s3.creds
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const client = await TvmClient.init({
|
|
29
|
-
ow: {
|
|
30
|
-
namespace: config.ow.namespace,
|
|
31
|
-
auth: config.ow.auth
|
|
32
|
-
},
|
|
33
|
-
// can be undefined => defaults in TvmClient
|
|
34
|
-
apiUrl: config.s3 && config.s3.tvmUrl,
|
|
35
|
-
cacheFile: config.s3 && config.s3.credsCacheFile
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
const creds = await client.getAwsS3Credentials()
|
|
39
|
-
return creds
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
module.exports = getS3Credentials
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const getS3Credentials = require('../../lib/getS3Creds')
|
|
14
|
-
|
|
15
|
-
const fakeReturnedTvmCreds = { fake: 'tvmcreds' } // from __mocks__
|
|
16
|
-
const mockTVM = require('@adobe/aio-lib-core-tvm')
|
|
17
|
-
|
|
18
|
-
describe('getS3Credentials', () => {
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
mockTVM.init.mockClear()
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test('throw when missing required args', async () => {
|
|
24
|
-
const expectedErrorMessage =
|
|
25
|
-
'Please check your .env file to ensure your credentials are correct.'
|
|
26
|
-
|
|
27
|
-
await expect(getS3Credentials({}))
|
|
28
|
-
.rejects.toThrow(expectedErrorMessage)
|
|
29
|
-
|
|
30
|
-
await expect(getS3Credentials({ ow: { namespace: 'ns' }, s3: {} }))
|
|
31
|
-
.rejects.toThrow(expectedErrorMessage)
|
|
32
|
-
|
|
33
|
-
await expect(getS3Credentials({ ow: { auth: 'auth' } }))
|
|
34
|
-
.rejects.toThrow(expectedErrorMessage)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test('returns s3.creds if defined', async () => {
|
|
38
|
-
const fakeCreds = { fake: 's3creds' }
|
|
39
|
-
await expect(getS3Credentials({ ow: { namespace: 'ns', auth: 'auth' }, s3: { creds: fakeCreds } }))
|
|
40
|
-
.resolves.toEqual(fakeCreds)
|
|
41
|
-
expect(mockTVM.init).not.toHaveBeenCalled()
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test('gets credentials from tvm', async () => {
|
|
45
|
-
await expect(getS3Credentials({ ow: { namespace: 'ns', auth: 'auth' } }))
|
|
46
|
-
.resolves.toEqual(fakeReturnedTvmCreds)
|
|
47
|
-
expect(mockTVM.init).toHaveBeenCalledWith({
|
|
48
|
-
apiUrl: undefined,
|
|
49
|
-
cacheFile: undefined,
|
|
50
|
-
ow: { auth: 'auth', namespace: 'ns' }
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test('gets credentials from tvm with custom tvmurl', async () => {
|
|
55
|
-
await expect(getS3Credentials({ ow: { namespace: 'ns', auth: 'auth' }, s3: { tvmUrl: 'custom' } }))
|
|
56
|
-
.resolves.toEqual(fakeReturnedTvmCreds)
|
|
57
|
-
expect(mockTVM.init).toHaveBeenCalledWith({
|
|
58
|
-
apiUrl: 'custom',
|
|
59
|
-
cacheFile: undefined,
|
|
60
|
-
ow: { auth: 'auth', namespace: 'ns' }
|
|
61
|
-
})
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test('gets credentials from tvm with custom credsCacheFile', async () => {
|
|
65
|
-
await expect(getS3Credentials({ ow: { namespace: 'ns', auth: 'auth' }, s3: { credsCacheFile: 'custom' } }))
|
|
66
|
-
.resolves.toEqual(fakeReturnedTvmCreds)
|
|
67
|
-
expect(mockTVM.init).toHaveBeenCalledWith({
|
|
68
|
-
apiUrl: undefined,
|
|
69
|
-
cacheFile: 'custom',
|
|
70
|
-
ow: { auth: 'auth', namespace: 'ns' }
|
|
71
|
-
})
|
|
72
|
-
})
|
|
73
|
-
})
|