@adobe/aio-lib-web 7.2.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.
@@ -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('calls getS3Credentials and empties folder', async () => {
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
- auth: 'password'
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(getS3Credentials).toHaveBeenCalledWith(config)
66
- expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
67
- expect(mockRemoteStorageInstance.folderExists).toHaveBeenCalledWith('somefolder/')
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
- auth: 'password'
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(getS3Credentials).toHaveBeenCalledWith(config)
91
- expect(RemoteStorage).toHaveBeenCalledWith('fakecreds')
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
- })