|
| 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 GetCommandExecutionCommand = require('../../../src/commands/cloudmanager/commerce/get-command-execution') |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + resetCurrentOrgId() |
| 20 | +}) |
| 21 | + |
| 22 | +test('get-command (commerce) - missing arg', async () => { |
| 23 | + expect.assertions(4) |
| 24 | + |
| 25 | + const runResultOne = GetCommandExecutionCommand.run([]) |
| 26 | + await expect(runResultOne instanceof Promise).toBeTruthy() |
| 27 | + await expect(runResultOne).rejects.toSatisfy( |
| 28 | + (err) => err.message.indexOf('Missing 2 required args') === 0, |
| 29 | + ) |
| 30 | + const runResultTwo = GetCommandExecutionCommand.run(['12345']) |
| 31 | + await expect(runResultTwo instanceof Promise).toBeTruthy() |
| 32 | + await expect(runResultTwo).rejects.toSatisfy( |
| 33 | + (err) => err.message.indexOf('Missing 1 required arg') === 0, |
| 34 | + ) |
| 35 | +}) |
| 36 | + |
| 37 | +test('get-command (commerce) - missing config', async () => { |
| 38 | + expect.assertions(2) |
| 39 | + |
| 40 | + const runResult = GetCommandExecutionCommand.run(['--programId', '5', '10', '20']) |
| 41 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 42 | + await expect(runResult).rejects.toSatisfy(err => err.message === '[CloudManagerCLI:NO_IMS_CONTEXT] Unable to find IMS context aio-cli-plugin-cloudmanager.') |
| 43 | +}) |
| 44 | + |
| 45 | +test('get-command (commerce)', async () => { |
| 46 | + setCurrentOrgId('good') |
| 47 | + mockSdk.getCommerceCommandExecution = jest.fn(() => { |
| 48 | + return Promise.resolve({ |
| 49 | + id: 100, |
| 50 | + type: 'bin/magento', |
| 51 | + command: 'maintenance:status', |
| 52 | + options: [], |
| 53 | + |
| 54 | + startedAt: '2021-08-17T17:03:18.858+0000', |
| 55 | + completedAt: '2021-08-17T17:03:43.000+0000', |
| 56 | + name: 'magento-cli-952', |
| 57 | + status: 'COMPLETED', |
| 58 | + environmentId: 177253, |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + // expect.assertions(9) |
| 63 | + |
| 64 | + const runResult = GetCommandExecutionCommand.run(['--programId', '5', '10', '100']) |
| 65 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 66 | + await runResult |
| 67 | + await expect(init.mock.calls.length).toEqual(1) |
| 68 | + await expect(init).toHaveBeenCalledWith( |
| 69 | + 'good', |
| 70 | + 'test-client-id', |
| 71 | + 'fake-token', |
| 72 | + 'https://cloudmanager.adobe.io', |
| 73 | + ) |
| 74 | + await expect(mockSdk.getCommerceCommandExecution.mock.calls.length).toEqual(1) |
| 75 | + await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledWith('5', '10', '100') |
| 76 | + await expect(cli.action.start.mock.calls[0][0]).toEqual('Getting Status for Command ID# 100') |
| 77 | + await expect(cli.action.start.mock.calls[1][0]).toEqual('Status for Command ID# 100') |
| 78 | + await expect(cli.table.mock.calls[0][0]).toEqual([{ command: 'maintenance:status', completedAt: '2021-08-17T17:03:43.000+0000', id: 100, startedAt: '2021-08-17T17:03:18.858+0000', startedBy: '[email protected]', status: 'COMPLETED', type: 'bin/magento' }]) |
| 79 | + await expect(cli.action.stop).toHaveBeenCalled() |
| 80 | +}) |
| 81 | + |
| 82 | +test('get-command (commerce) - api error', async () => { |
| 83 | + setCurrentOrgId('good') |
| 84 | + mockSdk.getCommerceCommandExecution = jest.fn(() => |
| 85 | + Promise.reject(new Error('Command failed.')), |
| 86 | + ) |
| 87 | + const runResult = GetCommandExecutionCommand.run(['--programId', '5', '10', '20']) |
| 88 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 89 | + await expect(runResult).rejects.toEqual(new Error('Command failed.')) |
| 90 | +}) |
0 commit comments