|
| 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 MaintenanceDisableCommand = require('../../../../../src/commands/cloudmanager/commerce/bin-magento/maintenance/disable') |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + resetCurrentOrgId() |
| 20 | +}) |
| 21 | + |
| 22 | +test('maintenance:disable - missing arg', async () => { |
| 23 | + expect.assertions(2) |
| 24 | + |
| 25 | + const runResult = MaintenanceDisableCommand.run([]) |
| 26 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 27 | + await expect(runResult).rejects.toThrow(/^Missing 1 required arg/) |
| 28 | +}) |
| 29 | + |
| 30 | +test('maintenance:disable - missing config', async () => { |
| 31 | + expect.assertions(2) |
| 32 | + |
| 33 | + const runResult = MaintenanceDisableCommand.run(['--programId', '5', '10']) |
| 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('maintenance:disable', async () => { |
| 39 | + let counter = 0 |
| 40 | + setCurrentOrgId('good') |
| 41 | + mockSdk.postCommerceCommandExecution = jest.fn(() => |
| 42 | + Promise.resolve({ |
| 43 | + id: '5000', |
| 44 | + }), |
| 45 | + ) |
| 46 | + mockSdk.getCommerceCommandExecution = jest.fn(() => { |
| 47 | + counter++ |
| 48 | + if (counter === 1) { |
| 49 | + return Promise.resolve({ |
| 50 | + status: 'PENDING', |
| 51 | + message: 'running maintenance disable', |
| 52 | + }) |
| 53 | + } else if (counter < 3) { |
| 54 | + return Promise.resolve({ |
| 55 | + status: 'RUNNING', |
| 56 | + message: 'running maintenance disable', |
| 57 | + }) |
| 58 | + } |
| 59 | + return Promise.resolve({ |
| 60 | + status: 'COMPLETE', |
| 61 | + message: 'maintenance disabled', |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + expect.assertions(11) |
| 66 | + |
| 67 | + const runResult = MaintenanceDisableCommand.run(['--programId', '5', '10']) |
| 68 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 69 | + await runResult |
| 70 | + await expect(init.mock.calls.length).toEqual(1) |
| 71 | + await expect(init).toHaveBeenCalledWith( |
| 72 | + 'good', |
| 73 | + 'test-client-id', |
| 74 | + 'fake-token', |
| 75 | + 'https://cloudmanager.adobe.io', |
| 76 | + ) |
| 77 | + await expect(mockSdk.postCommerceCommandExecution.mock.calls.length).toEqual(1) |
| 78 | + await expect(mockSdk.postCommerceCommandExecution).toHaveBeenCalledWith('5', '10', { |
| 79 | + type: 'bin/magento', |
| 80 | + command: 'maintenance:disable', |
| 81 | + }) |
| 82 | + await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledWith('5', '10', '5000') |
| 83 | + await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledTimes(3) |
| 84 | + await expect(cli.action.start.mock.calls[0][0]).toEqual('Starting maintenance:disable') |
| 85 | + await expect(cli.action.start.mock.calls[1][0]).toEqual('Starting maintenance:disable') |
| 86 | + await expect(cli.action.start.mock.calls[2][0]).toEqual('Running maintenance:disable') |
| 87 | + await expect(cli.action.stop.mock.calls[0][0]).toEqual('maintenance disabled') |
| 88 | +}) |
| 89 | + |
| 90 | +test('maintenance:disable - api error', async () => { |
| 91 | + setCurrentOrgId('good') |
| 92 | + mockSdk.postCommerceCommandExecution = jest.fn(() => |
| 93 | + Promise.reject(new Error('Command failed.')), |
| 94 | + ) |
| 95 | + mockSdk.getCommerceCommandExecution = jest.fn() |
| 96 | + const runResult = MaintenanceDisableCommand.run(['--programId', '5', '10']) |
| 97 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 98 | + await expect(runResult).rejects.toEqual(new Error('Command failed.')) |
| 99 | +}) |
0 commit comments