|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import * as fs from 'fs'; |
| 8 | +import { Org, Aliases, Config, ConfigAggregator, Lifecycle, SandboxEvents } from '@salesforce/core'; |
| 9 | +import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon'; |
| 10 | +import * as sinon from 'sinon'; |
| 11 | +import { expect, IConfig } from '@salesforce/command/lib/test'; |
| 12 | +import { UX } from '@salesforce/command'; |
| 13 | +import { OrgCloneCommand } from '../../../../src/commands/force/org/clone'; |
| 14 | + |
| 15 | +describe('org:clone', () => { |
| 16 | + const sandbox = sinon.createSandbox(); |
| 17 | + const sanboxname = 'my-sandbox'; |
| 18 | + const sandboxalias = 'my-sandbox-alias'; |
| 19 | + const authUserName = 'my-user'; |
| 20 | + const sandboxProcessObj = { |
| 21 | + attributes: { |
| 22 | + type: 'SandboxProcess', |
| 23 | + url: '/services/data/v54.0/tooling/sobjects/SandboxProcess/0GR4p000000HQG4GAO', |
| 24 | + }, |
| 25 | + Id: '0GR4p000000HQG4GAO', |
| 26 | + Status: 'Completed', |
| 27 | + SandboxName: sanboxname, |
| 28 | + SandboxInfoId: '0GQ4p000000HOL2GAO', |
| 29 | + LicenseType: 'DEVELOPER', |
| 30 | + CreatedDate: '2022-03-02T15:30:32.000+0000', |
| 31 | + CopyProgress: 100, |
| 32 | + SandboxOrganization: '00D2f0000008gzD', |
| 33 | + SourceId: null, |
| 34 | + Description: null, |
| 35 | + ApexClassId: '0GQ4p000000HOL2KAO', |
| 36 | + EndDate: '2022-03-02T15:45:16.000+0000', |
| 37 | + }; |
| 38 | + const resultObject = { |
| 39 | + sandboxProcessObj, |
| 40 | + sandboxRes: { |
| 41 | + authUserName, |
| 42 | + authCode: 'my-auth-code', |
| 43 | + instanceUrl: 'https://my-instance.com', |
| 44 | + loginUrl: 'https://my-login.com', |
| 45 | + }, |
| 46 | + }; |
| 47 | + const defFile = { |
| 48 | + SourceSandboxName: 'mySandbox', |
| 49 | + licenseType: 'Developer', |
| 50 | + sandboxName: 'newSandbox', |
| 51 | + }; |
| 52 | + const statusEvent = { |
| 53 | + sandboxProcessObj, |
| 54 | + interval: 1, |
| 55 | + retries: 0, |
| 56 | + waitingOnAuth: false, |
| 57 | + }; |
| 58 | + const oclifConfigStub = fromStub(stubInterface<IConfig.IConfig>(sandbox)); |
| 59 | + |
| 60 | + // stubs |
| 61 | + let uxTableStub: sinon.SinonStub; |
| 62 | + let uxStyledHeaderStub: sinon.SinonStub; |
| 63 | + let uxLogStub: sinon.SinonStub; |
| 64 | + let cmd: TestOrgCloneCommand; |
| 65 | + let aliasSetStub: sinon.SinonStub; |
| 66 | + let configSetStub: sinon.SinonStub; |
| 67 | + let configWriteStub: sinon.SinonStub; |
| 68 | + let onStub: sinon.SinonStub; |
| 69 | + let readFileSyncStub: sinon.SinonStub; |
| 70 | + let configAggregatorStub; |
| 71 | + |
| 72 | + class TestOrgCloneCommand extends OrgCloneCommand { |
| 73 | + public async runIt() { |
| 74 | + await this.init(); |
| 75 | + return this.run(); |
| 76 | + } |
| 77 | + public setOrg(org: Org) { |
| 78 | + this.org = org; |
| 79 | + } |
| 80 | + public setConfigAggregator(configAggregator: ConfigAggregator) { |
| 81 | + this.configAggregator = configAggregator; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const runCloneCommand = async (params: string[]) => { |
| 86 | + cmd = new TestOrgCloneCommand(params, oclifConfigStub); |
| 87 | + stubMethod(sandbox, cmd, 'assignOrg').callsFake(() => { |
| 88 | + const orgStubOptions = { |
| 89 | + cloneSandbox: sandbox.stub().callsFake(async () => { |
| 90 | + return sandboxProcessObj; |
| 91 | + }), |
| 92 | + }; |
| 93 | + const orgStub = fromStub(stubInterface<Org>(sandbox, orgStubOptions)); |
| 94 | + cmd.setOrg(orgStub); |
| 95 | + configSetStub = sandbox.stub().returns(true); |
| 96 | + configWriteStub = sandbox.stub().resolves(true); |
| 97 | + const configAggregatorStubOptions = { |
| 98 | + getGlobalConfig: () => ({ |
| 99 | + set: configSetStub, |
| 100 | + write: configWriteStub, |
| 101 | + }), |
| 102 | + }; |
| 103 | + configAggregatorStub = fromStub(stubInterface<ConfigAggregator>(sandbox, configAggregatorStubOptions)); |
| 104 | + cmd.setConfigAggregator(configAggregatorStub); |
| 105 | + }); |
| 106 | + onStub = sandbox |
| 107 | + .stub() |
| 108 | + .callsArgWith(1, sandboxProcessObj) |
| 109 | + .callsArgWith(1, statusEvent) |
| 110 | + .callsArgWith(1, resultObject); |
| 111 | + stubMethod(sandbox, Lifecycle, 'getInstance').returns({ |
| 112 | + on: onStub, |
| 113 | + }); |
| 114 | + aliasSetStub = stubMethod(sandbox, Aliases.prototype, 'set').returns(sandboxalias); |
| 115 | + uxTableStub = stubMethod(sandbox, UX.prototype, 'table'); |
| 116 | + uxLogStub = stubMethod(sandbox, UX.prototype, 'log'); |
| 117 | + uxStyledHeaderStub = stubMethod(sandbox, UX.prototype, 'styledHeader'); |
| 118 | + readFileSyncStub = stubMethod(sandbox, fs, 'readFileSync').returns(JSON.stringify(defFile)); |
| 119 | + return cmd.runIt(); |
| 120 | + }; |
| 121 | + |
| 122 | + it('will return sandbox process object', async () => { |
| 123 | + const res = await runCloneCommand(['-t', 'sandbox', '-u', 'DevHub', '-f', 'sandbox-def.json']); |
| 124 | + expect(uxStyledHeaderStub.calledOnce).to.be.true; |
| 125 | + expect(uxTableStub.firstCall.args[0].length).to.be.equal(12); |
| 126 | + expect(readFileSyncStub.calledOnce).to.be.true; |
| 127 | + expect(uxLogStub.callCount).to.be.equal(3); |
| 128 | + expect(aliasSetStub.callCount).to.be.equal(0); |
| 129 | + expect(configSetStub.callCount).to.be.equal(0); |
| 130 | + expect(configWriteStub.callCount).to.be.equal(0); |
| 131 | + expect(onStub.firstCall.firstArg).to.be.equal(SandboxEvents.EVENT_ASYNC_RESULT); |
| 132 | + expect(onStub.secondCall.firstArg).to.be.equal(SandboxEvents.EVENT_STATUS); |
| 133 | + expect(onStub.thirdCall.firstArg).to.be.equal(SandboxEvents.EVENT_RESULT); |
| 134 | + expect(onStub.callCount).to.be.equal(3); |
| 135 | + expect(res).to.deep.equal(sandboxProcessObj); |
| 136 | + }); |
| 137 | + |
| 138 | + it('will set alias and default username', async () => { |
| 139 | + const res = await runCloneCommand([ |
| 140 | + '-t', |
| 141 | + 'sandbox', |
| 142 | + '-u', |
| 143 | + 'DevHub', |
| 144 | + '-f', |
| 145 | + 'sandbox-def.json', |
| 146 | + '-a', |
| 147 | + sandboxalias, |
| 148 | + '-s', |
| 149 | + ]); |
| 150 | + expect(uxStyledHeaderStub.calledOnce).to.be.true; |
| 151 | + expect(uxTableStub.firstCall.args[0].length).to.be.equal(12); |
| 152 | + expect(readFileSyncStub.calledOnce).to.be.true; |
| 153 | + expect(uxLogStub.callCount).to.be.equal(3); |
| 154 | + expect(aliasSetStub.callCount).to.be.equal(1); |
| 155 | + expect(aliasSetStub.firstCall.args[0]).to.be.equal(sandboxalias); |
| 156 | + expect(aliasSetStub.firstCall.args[1]).to.be.equal(authUserName); |
| 157 | + expect(configSetStub.firstCall.args[0]).to.be.equal(Config.DEFAULT_USERNAME); |
| 158 | + expect(configSetStub.firstCall.args[1]).to.be.equal(authUserName); |
| 159 | + expect(onStub.firstCall.firstArg).to.be.equal(SandboxEvents.EVENT_ASYNC_RESULT); |
| 160 | + expect(onStub.secondCall.firstArg).to.be.equal(SandboxEvents.EVENT_STATUS); |
| 161 | + expect(onStub.thirdCall.firstArg).to.be.equal(SandboxEvents.EVENT_RESULT); |
| 162 | + expect(onStub.callCount).to.be.equal(3); |
| 163 | + expect(configWriteStub.calledOnce).to.be.true; |
| 164 | + expect(res).to.deep.equal(sandboxProcessObj); |
| 165 | + }); |
| 166 | + |
| 167 | + afterEach(() => { |
| 168 | + sandbox.restore(); |
| 169 | + }); |
| 170 | +}); |
0 commit comments