|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 { |
| 8 | + Lifecycle, |
| 9 | + Messages, |
| 10 | + Org, |
| 11 | + SandboxEvents, |
| 12 | + SandboxProcessObject, |
| 13 | + AuthFields, |
| 14 | + SandboxRequestCacheEntry, |
| 15 | + SfError, |
| 16 | +} from '@salesforce/core'; |
| 17 | +import { stubMethod } from '@salesforce/ts-sinon'; |
| 18 | +import * as sinon from 'sinon'; |
| 19 | +import { expect, config } from 'chai'; |
| 20 | +import { OrgAccessor } from '@salesforce/core/lib/stateAggregator'; |
| 21 | +import { stubSfCommandUx, stubSpinner, stubUx } from '@salesforce/sf-plugins-core'; |
| 22 | +import ResumeSandbox from '../../../src/commands/org/resume/sandbox'; |
| 23 | + |
| 24 | +config.truncateThreshold = 0; |
| 25 | + |
| 26 | +const prodOrgUsername = '[email protected]'; |
| 27 | +const sandboxName = 'TestSbx'; |
| 28 | +const fakeOrg: AuthFields = { |
| 29 | + orgId: '00Dsomefakeorg1', |
| 30 | + instanceUrl: 'https://some.fake.org', |
| 31 | + username: prodOrgUsername, |
| 32 | +}; |
| 33 | + |
| 34 | +const sandboxProcessObj: SandboxProcessObject = { |
| 35 | + Id: '0GR4p000000U8EMXXX', |
| 36 | + Status: 'Completed', |
| 37 | + SandboxName: sandboxName, |
| 38 | + SandboxInfoId: '0GQ4p000000U6sKXXX', |
| 39 | + LicenseType: 'DEVELOPER', |
| 40 | + CreatedDate: '2021-12-07T16:20:21.000+0000', |
| 41 | + CopyProgress: 100, |
| 42 | + SandboxOrganization: '00D2f0000008XXX', |
| 43 | + SourceId: '123', |
| 44 | + Description: 'sandbox description', |
| 45 | + ApexClassId: '123', |
| 46 | + EndDate: '2021-12-07T16:38:47.000+0000', |
| 47 | +}; |
| 48 | + |
| 49 | +const sandboxRequestData: SandboxRequestCacheEntry = { |
| 50 | + prodOrgUsername, |
| 51 | + sandboxRequest: {}, |
| 52 | + sandboxProcessObject: { |
| 53 | + SandboxName: sandboxName, |
| 54 | + }, |
| 55 | + setDefault: false, |
| 56 | +}; |
| 57 | + |
| 58 | +describe('[org resume sandbox]', () => { |
| 59 | + Messages.importMessagesDirectory(__dirname); |
| 60 | + const messages = Messages.loadMessages('@salesforce/plugin-org', 'sandboxbase'); |
| 61 | + |
| 62 | + const sandbox = sinon.createSandbox(); |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + stubMethod(sandbox, OrgAccessor.prototype, 'read').resolves(fakeOrg); |
| 66 | + stubMethod(sandbox, OrgAccessor.prototype, 'write').resolves(fakeOrg); |
| 67 | + sfCommandUxStubs = stubSfCommandUx(sandbox); |
| 68 | + stubUx(sandbox); |
| 69 | + stubSpinner(sandbox); |
| 70 | + }); |
| 71 | + |
| 72 | + let sfCommandUxStubs: ReturnType<typeof stubSfCommandUx>; |
| 73 | + |
| 74 | + it('will warn when multiple sandboxes are in a resumable state', async () => { |
| 75 | + stubMethod(sandbox, ResumeSandbox.prototype, 'getSandboxRequestConfig').resolves(); |
| 76 | + stubMethod(sandbox, ResumeSandbox.prototype, 'buildSandboxRequestCacheEntry').returns(sandboxRequestData); |
| 77 | + stubMethod(sandbox, ResumeSandbox.prototype, 'createResumeSandboxRequest').returns({ |
| 78 | + SandboxName: sandboxName, |
| 79 | + }); |
| 80 | + stubMethod(sandbox, Org, 'create').resolves(Org.prototype); |
| 81 | + stubMethod(sandbox, Org.prototype, 'getUsername').returns(prodOrgUsername); |
| 82 | + const inProgSandboxProcessObj = Object.assign({}, sandboxProcessObj, { |
| 83 | + Status: 'In Progress', |
| 84 | + Id: '0GR4p000000U8EMZZZ', |
| 85 | + CopyProgress: 25, |
| 86 | + CreatedDate: '2022-12-07T16:20:21.000+0000', |
| 87 | + }); |
| 88 | + stubMethod(sandbox, Org.prototype, 'resumeSandbox').callsFake(async () => { |
| 89 | + await Lifecycle.getInstance().emit(SandboxEvents.EVENT_MULTIPLE_SBX_PROCESSES, [ |
| 90 | + inProgSandboxProcessObj, |
| 91 | + sandboxProcessObj, |
| 92 | + ]); |
| 93 | + throw new SfError('sbx create not complete', 'sandboxCreateNotComplete'); |
| 94 | + }); |
| 95 | + |
| 96 | + try { |
| 97 | + await ResumeSandbox.run(['-o', prodOrgUsername, '--name', sandboxName]); |
| 98 | + expect(false, 'ResumeSandbox should have thrown sandboxCreateNotComplete'); |
| 99 | + } catch (err: unknown) { |
| 100 | + const warningMsg = messages.getMessage('warning.MultipleMatchingSandboxProcesses', [ |
| 101 | + sandboxName, |
| 102 | + sandboxProcessObj.Id, |
| 103 | + sandboxProcessObj.Status, |
| 104 | + inProgSandboxProcessObj.Id, |
| 105 | + sandboxProcessObj.Id, |
| 106 | + prodOrgUsername, |
| 107 | + ]); |
| 108 | + expect(sfCommandUxStubs.warn.calledWith(warningMsg)).to.be.true; |
| 109 | + const error = err as SfError; |
| 110 | + expect(error.name).to.equal('sandboxCreateNotComplete'); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + afterEach(() => { |
| 115 | + sandbox.restore(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments