Skip to content

Commit 5de7681

Browse files
feat(app:config:import): add app:config:import commerce command fixes #473 (#475)
Co-authored-by: Justin Edelson <[email protected]>
1 parent 583439f commit 5de7681

File tree

2 files changed

+147
-0
lines changed
  • src/commands/cloudmanager/commerce/bin-magento/app/config
  • test/commands/commerce/bin-magento/app/config

2 files changed

+147
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2021 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 BaseCommerceCliCommand = require('../../../../../../base-commerce-cli-command')
14+
const { getProgramId } = require('../../../../../../cloudmanager-helpers')
15+
const commonFlags = require('../../../../../../common-flags')
16+
const commonArgs = require('../../../../../../common-args')
17+
18+
class AppConfigImportCommand extends BaseCommerceCliCommand {
19+
async run () {
20+
const { args, flags } = this.parse(AppConfigImportCommand)
21+
22+
const programId = getProgramId(flags)
23+
24+
const result = await this.runSync(programId, args.environmentId,
25+
{
26+
type: 'bin/magento',
27+
command: 'app:config:import',
28+
options: ['-n'],
29+
},
30+
1000, 'app:config:import')
31+
32+
return result
33+
}
34+
}
35+
36+
AppConfigImportCommand.description = 'commerce config import'
37+
38+
AppConfigImportCommand.flags = {
39+
...commonFlags.global,
40+
...commonFlags.programId,
41+
}
42+
43+
AppConfigImportCommand.args = [
44+
commonArgs.environmentId,
45+
]
46+
47+
module.exports = AppConfigImportCommand
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2021 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 { cli } = require('cli-ux')
14+
const { init, mockSdk } = require('@adobe/aio-lib-cloudmanager')
15+
const { resetCurrentOrgId, setCurrentOrgId } = require('@adobe/aio-lib-ims')
16+
const AppConfigImportCommand = require('../../../../../../src/commands/cloudmanager/commerce/bin-magento/app/config/import')
17+
18+
beforeEach(() => {
19+
resetCurrentOrgId()
20+
})
21+
22+
test('app:config:import - missing environmentId', async () => {
23+
expect.assertions(2)
24+
25+
const runResult = AppConfigImportCommand.run([])
26+
await expect(runResult instanceof Promise).toBeTruthy()
27+
await expect(runResult).rejects.toThrow(/^Missing 1 required arg/)
28+
})
29+
30+
test('app:config:import - missing IMS Context', async () => {
31+
expect.assertions(2)
32+
33+
const runResult = AppConfigImportCommand.run(['--programId', '3', '60'])
34+
await expect(runResult instanceof Promise).toBeTruthy()
35+
await expect(runResult).rejects.toThrow('[CloudManagerCLI:NO_IMS_CONTEXT] Unable to find IMS context aio-cli-plugin-cloudmanager.')
36+
})
37+
38+
test('app:config:import - api error', async () => {
39+
setCurrentOrgId('valid-org-id')
40+
mockSdk.postCommerceCommandExecution = jest.fn(() =>
41+
Promise.reject(new Error('Command failed.')),
42+
)
43+
mockSdk.getCommerceCommandExecution = jest.fn()
44+
const runResult = AppConfigImportCommand.run(['--programId', '3', '60'])
45+
await expect(runResult instanceof Promise).toBeTruthy()
46+
await expect(runResult).rejects.toEqual(new Error('Command failed.'))
47+
})
48+
49+
test('app:config:import - success', async () => {
50+
let counter = 0
51+
setCurrentOrgId('valid-org-id')
52+
mockSdk.postCommerceCommandExecution = jest.fn(() =>
53+
Promise.resolve({
54+
id: '6000',
55+
}),
56+
)
57+
mockSdk.getCommerceCommandExecution = jest.fn(() => {
58+
counter++
59+
if (counter === 1) {
60+
return Promise.resolve({
61+
status: 'PENDING',
62+
message: 'running config import',
63+
})
64+
} else if (counter < 3) {
65+
return Promise.resolve({
66+
status: 'RUNNING',
67+
message: 'running config import',
68+
})
69+
}
70+
return Promise.resolve({
71+
status: 'COMPLETE',
72+
message: 'done',
73+
})
74+
})
75+
76+
expect.assertions(11)
77+
78+
const runResult = AppConfigImportCommand.run(['--programId', '3', '60'])
79+
await expect(runResult instanceof Promise).toBeTruthy()
80+
await runResult
81+
await expect(init.mock.calls.length).toEqual(1)
82+
await expect(init).toHaveBeenCalledWith(
83+
'valid-org-id',
84+
'test-client-id',
85+
'fake-token',
86+
'https://cloudmanager.adobe.io',
87+
)
88+
await expect(mockSdk.postCommerceCommandExecution.mock.calls.length).toEqual(1)
89+
await expect(mockSdk.postCommerceCommandExecution).toHaveBeenCalledWith('3', '60', {
90+
type: 'bin/magento',
91+
command: 'app:config:import',
92+
options: ['-n'],
93+
})
94+
await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledWith('3', '60', '6000')
95+
await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledTimes(3)
96+
await expect(cli.action.start.mock.calls[0][0]).toEqual('Starting app:config:import')
97+
await expect(cli.action.start.mock.calls[1][0]).toEqual('Starting app:config:import')
98+
await expect(cli.action.start.mock.calls[2][0]).toEqual('Running app:config:import')
99+
await expect(cli.action.stop.mock.calls[0][0]).toEqual('done')
100+
})

0 commit comments

Comments
 (0)