-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.test.js
More file actions
307 lines (277 loc) · 12.2 KB
/
Copy pathindex.test.js
File metadata and controls
307 lines (277 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const ConsoleCommand = require('../../../src/commands/console/index')
const mockLogger = require('@adobe/aio-lib-core-logging')
const config = require('@adobe/aio-lib-core-config')
const { Help } = require('@oclif/core')
const yaml = require('js-yaml')
const { CONFIG_KEYS, API_KEYS } = require('../../../src/config')
const { PROD_ENV, STAGE_ENV, DEFAULT_ENV } = require('@adobe/aio-lib-env')
jest.mock('@adobe/aio-lib-ims')
describe('ConsoleCommand', () => {
let command
let ORIGINAL_AIO_CLI_ENV
beforeAll(() => {
ORIGINAL_AIO_CLI_ENV = process.env.AIO_CLI_ENV
})
afterAll(() => {
process.env.AIO_CLI_ENV = ORIGINAL_AIO_CLI_ENV
})
beforeEach(() => {
delete process.env.AIO_CLI_ENV
mockLogger.mockReset()
jest.clearAllMocks()
command = new ConsoleCommand([])
command.log = jest.fn()
})
test('exports', () => {
expect(typeof ConsoleCommand).toEqual('function')
})
test('description', () => {
expect(ConsoleCommand.description).toBeDefined()
})
test('flags', async () => {
expect(ConsoleCommand.flags.help.type).toBe('boolean')
})
describe('initSDK', () => {
test('exists', async () => {
expect(command.initSdk).toBeInstanceOf(Function)
})
test('env STAGE_ENV', async () => {
process.env.AIO_CLI_ENV = STAGE_ENV
await command.initSdk()
expect(command.cliEnv).toEqual(STAGE_ENV)
expect(command.apiKey).toEqual(API_KEYS[STAGE_ENV])
expect(command.consoleCLI.sdkClient.env).toEqual(STAGE_ENV)
})
test('env PROD_ENV', async () => {
process.env.AIO_CLI_ENV = PROD_ENV
await command.initSdk()
expect(command.cliEnv).toEqual(PROD_ENV)
expect(command.apiKey).toEqual(API_KEYS[PROD_ENV])
expect(command.consoleCLI.sdkClient.env).toEqual(PROD_ENV)
})
test('env unknown', async () => {
process.env.AIO_CLI_ENV = 'unknown-env'
await command.initSdk()
expect(command.cliEnv).toEqual(DEFAULT_ENV)
expect(command.apiKey).toEqual(API_KEYS[DEFAULT_ENV])
expect(command.consoleCLI.sdkClient.env).toEqual(DEFAULT_ENV)
})
test('env not set', async () => {
delete process.env.AIO_CLI_ENV
await command.initSdk()
expect(command.cliEnv).toEqual(DEFAULT_ENV)
expect(command.apiKey).toEqual(API_KEYS[DEFAULT_ENV])
expect(command.consoleCLI.sdkClient.env).toEqual(DEFAULT_ENV)
})
})
describe('run', () => {
test('exists', async () => {
expect(command.run).toBeInstanceOf(Function)
})
test('returns help file for console command', () => {
command.config = {}
const spy = jest.spyOn(Help.prototype, 'showHelp').mockReturnValue(true)
return command.run().then(() => {
expect(spy).toHaveBeenCalledWith(['console', '--help'])
})
})
})
describe('helper methods', () => {
describe('printConsoleConfig', () => {
test('no selected org', () => {
config.get.mockImplementation((key) => null)
command.printConsoleConfig()
expect(command.log).toHaveBeenCalledTimes(4)
expect(command.log).toHaveBeenCalledWith('You are currently in:')
expect(command.log).toHaveBeenCalledWith('1. Org: <no org selected>')
expect(command.log).toHaveBeenCalledWith('2. Project: <no project selected>')
expect(command.log).toHaveBeenCalledWith('3. Workspace: <no workspace selected>')
})
test('only selected org and project', () => {
config.get.mockImplementation(key => {
if (key === `${CONFIG_KEYS.CONSOLE}.org.name`) {
return 'THE_ORG'
}
if (key === `${CONFIG_KEYS.CONSOLE}.project.title`) {
return 'THE_PROJECT'
}
return null
})
command.printConsoleConfig()
expect(command.log).toHaveBeenCalledTimes(4)
expect(command.log).toHaveBeenCalledWith('You are currently in:')
expect(command.log).toHaveBeenCalledWith('1. Org: THE_ORG')
expect(command.log).toHaveBeenCalledWith('2. Project: THE_PROJECT')
expect(command.log).toHaveBeenCalledWith('3. Workspace: <no workspace selected>')
})
test('selected org, project and workspace', () => {
config.get.mockImplementation(key => {
if (key === `${CONFIG_KEYS.CONSOLE}.org.name`) {
return 'THE_ORG'
}
if (key === `${CONFIG_KEYS.CONSOLE}.project.title`) {
return 'THE_PROJECT'
}
if (key === `${CONFIG_KEYS.CONSOLE}.workspace.name`) {
return 'THE_WORKSPACE'
}
return null
})
command.printConsoleConfig()
expect(command.log).toHaveBeenCalledTimes(4)
expect(command.log).toHaveBeenCalledWith('You are currently in:')
expect(command.log).toHaveBeenCalledWith('1. Org: THE_ORG')
expect(command.log).toHaveBeenCalledWith('2. Project: THE_PROJECT')
expect(command.log).toHaveBeenCalledWith('3. Workspace: THE_WORKSPACE')
})
test('selected org, project and workspace in json format', () => {
config.get.mockImplementation(key => {
if (key === `${CONFIG_KEYS.CONSOLE}.org.name`) {
return 'THE_ORG'
}
if (key === `${CONFIG_KEYS.CONSOLE}.project.title`) {
return 'THE_PROJECT'
}
if (key === `${CONFIG_KEYS.CONSOLE}.workspace.name`) {
return 'THE_WORKSPACE'
}
return null
})
command.printConsoleConfig({ alternativeFormat: 'json' })
expect(command.log).toHaveBeenCalledTimes(1)
expect(command.log).toHaveBeenCalledWith(JSON.stringify({ org: 'THE_ORG', project: 'THE_PROJECT', workspace: 'THE_WORKSPACE' }, null, 2))
})
test('no selected in json format', () => {
config.get.mockImplementation(key => {
return undefined
})
command.printConsoleConfig({ alternativeFormat: 'json' })
expect(command.log).toHaveBeenCalledTimes(1)
expect(command.log).toHaveBeenCalledWith('{}')
})
test('selected org, project and workspace in yml format', () => {
config.get.mockImplementation(key => {
if (key === `${CONFIG_KEYS.CONSOLE}.org.name`) {
return 'THE_ORG'
}
if (key === `${CONFIG_KEYS.CONSOLE}.project.title`) {
return 'THE_PROJECT'
}
if (key === `${CONFIG_KEYS.CONSOLE}.workspace.name`) {
return 'THE_WORKSPACE'
}
return null
})
command.printConsoleConfig({ alternativeFormat: 'yml' })
expect(command.log).toHaveBeenCalledTimes(1)
expect(command.log).toHaveBeenCalledWith(yaml.dump({ org: 'THE_ORG', project: 'THE_PROJECT', workspace: 'THE_WORKSPACE' }))
})
test('no selected in yml format', () => {
config.get.mockImplementation(key => {
return undefined
})
command.printConsoleConfig({ alternativeFormat: 'yml' })
expect(command.log).toHaveBeenCalledTimes(1)
expect(command.log).toHaveBeenCalledWith('{}\n')
})
})
test('setConfig', () => {
const command = new ConsoleCommand([])
command.setConfig('test', 'value')
expect(config.set).toHaveBeenCalledWith(`${CONFIG_KEYS.CONSOLE}.test`, 'value')
})
test('getConfig', () => {
const command = new ConsoleCommand([])
command.getConfig('test')
expect(config.get).toHaveBeenCalledWith(`${CONFIG_KEYS.CONSOLE}.test`)
})
test('getConfig - nokey', () => {
const command = new ConsoleCommand([])
command.getConfig()
expect(config.get).toHaveBeenCalledWith(`${CONFIG_KEYS.CONSOLE}`)
})
test('clearConfig', () => {
const command = new ConsoleCommand([])
command.clearConfig()
expect(config.delete).toHaveBeenCalledWith(CONFIG_KEYS.CONSOLE)
})
})
describe('ensureDevTermAccepted', () => {
let mockConsoleCLI
beforeEach(() => {
mockConsoleCLI = {
checkDevTermsForOrg: jest.fn().mockResolvedValue(true),
getDevTermsForOrg: jest.fn().mockResolvedValue({ text: 'Developer Terms text' }),
acceptDevTermsForOrg: jest.fn().mockResolvedValue(true),
prompt: { promptConfirm: jest.fn().mockResolvedValue(true) }
}
command.error = jest.fn((msg) => { throw new Error(msg) })
command.log = jest.fn()
})
test('no-op when terms already accepted', async () => {
mockConsoleCLI.checkDevTermsForOrg.mockResolvedValue(true)
await command.ensureDevTermAccepted(mockConsoleCLI, 'org-1')
expect(mockConsoleCLI.checkDevTermsForOrg).toHaveBeenCalledWith('org-1')
expect(mockConsoleCLI.getDevTermsForOrg).not.toHaveBeenCalled()
expect(mockConsoleCLI.prompt.promptConfirm).not.toHaveBeenCalled()
expect(mockConsoleCLI.acceptDevTermsForOrg).not.toHaveBeenCalled()
expect(command.log).not.toHaveBeenCalled()
})
test('errors fast when terms not accepted and skipPrompts=true', async () => {
mockConsoleCLI.checkDevTermsForOrg.mockResolvedValue(false)
await expect(command.ensureDevTermAccepted(mockConsoleCLI, 'org-1', true))
.rejects.toThrow(/Developer Terms of Service have not been accepted/)
expect(mockConsoleCLI.getDevTermsForOrg).not.toHaveBeenCalled()
expect(mockConsoleCLI.prompt.promptConfirm).not.toHaveBeenCalled()
})
test('errors when user declines the terms', async () => {
mockConsoleCLI.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLI.prompt.promptConfirm.mockResolvedValue(false)
await expect(command.ensureDevTermAccepted(mockConsoleCLI, 'org-1'))
.rejects.toThrow('The Developer Terms of Service were declined')
expect(mockConsoleCLI.getDevTermsForOrg).toHaveBeenCalled()
expect(mockConsoleCLI.acceptDevTermsForOrg).not.toHaveBeenCalled()
})
test('accepts terms programmatically when user confirms', async () => {
mockConsoleCLI.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLI.prompt.promptConfirm.mockResolvedValue(true)
mockConsoleCLI.acceptDevTermsForOrg.mockResolvedValue(true)
await command.ensureDevTermAccepted(mockConsoleCLI, 'org-1')
expect(mockConsoleCLI.acceptDevTermsForOrg).toHaveBeenCalledWith('org-1')
expect(command.log).toHaveBeenCalledWith(expect.stringContaining('successfully accepted'))
})
test('errors when accept call returns false', async () => {
mockConsoleCLI.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLI.prompt.promptConfirm.mockResolvedValue(true)
mockConsoleCLI.acceptDevTermsForOrg.mockResolvedValue(false)
await expect(command.ensureDevTermAccepted(mockConsoleCLI, 'org-1'))
.rejects.toThrow('The Developer Terms of Service could not be accepted')
})
test('includes the terms URL in the prompt', async () => {
mockConsoleCLI.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLI.getDevTermsForOrg.mockResolvedValue({ text: '\n Developer Terms text \n' })
await command.ensureDevTermAccepted(mockConsoleCLI, 'org-1')
const promptArg = mockConsoleCLI.prompt.promptConfirm.mock.calls[0][0]
expect(promptArg).toContain('https://www.adobe.com/go/developer-terms')
expect(promptArg).toContain('Developer Terms text')
expect(promptArg).toMatch(/^Developer Terms text\n\nYou have not accepted/)
})
test('handles missing terms text in the prompt', async () => {
mockConsoleCLI.checkDevTermsForOrg.mockResolvedValue(false)
mockConsoleCLI.getDevTermsForOrg.mockResolvedValue({})
await command.ensureDevTermAccepted(mockConsoleCLI, 'org-1')
const promptArg = mockConsoleCLI.prompt.promptConfirm.mock.calls[0][0]
expect(promptArg).toContain('You have not accepted the Developer Terms of Service.')
})
})
})