|
| 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 { Messages, Org, SfdxProject } from '@salesforce/core'; |
| 8 | +import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon'; |
| 9 | +import { IConfig } from '@oclif/config'; |
| 10 | +import * as sinon from 'sinon'; |
| 11 | +import { expect } from '@salesforce/command/lib/test'; |
| 12 | +import { UX } from '@salesforce/command'; |
| 13 | +import { Delete } from '../../../../src/commands/force/org/delete'; |
| 14 | + |
| 15 | +Messages.importMessagesDirectory(__dirname); |
| 16 | +const messages = Messages.loadMessages('@salesforce/plugin-org', 'delete'); |
| 17 | + |
| 18 | +describe('org:delete', () => { |
| 19 | + const sandbox = sinon.createSandbox(); |
| 20 | + const username = '[email protected]'; |
| 21 | + const orgId = '00D54000000KDltEAG'; |
| 22 | + const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox)); |
| 23 | + |
| 24 | + // stubs |
| 25 | + let resolveProjectConfigStub: sinon.SinonStub; |
| 26 | + let uxConfirmStub: sinon.SinonStub; |
| 27 | + let uxLogStub: sinon.SinonStub; |
| 28 | + let cmd: TestDelete; |
| 29 | + |
| 30 | + class TestDelete extends Delete { |
| 31 | + public async runIt() { |
| 32 | + await this.init(); |
| 33 | + return this.run(); |
| 34 | + } |
| 35 | + public setOrg(org: Org) { |
| 36 | + this.org = org; |
| 37 | + } |
| 38 | + public setProject(project: SfdxProject) { |
| 39 | + this.project = project; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + const runDeleteCommand = async ( |
| 44 | + params: string[], |
| 45 | + options: { isSandbox?: boolean; deleteScratchOrg?: string } = { isSandbox: false } |
| 46 | + ) => { |
| 47 | + cmd = new TestDelete(params, oclifConfigStub); |
| 48 | + stubMethod(sandbox, cmd, 'assignProject').callsFake(() => { |
| 49 | + const sfdxProjectStub = fromStub( |
| 50 | + stubInterface<SfdxProject>(sandbox, { |
| 51 | + resolveProjectConfig: resolveProjectConfigStub, |
| 52 | + }) |
| 53 | + ); |
| 54 | + cmd.setProject(sfdxProjectStub); |
| 55 | + }); |
| 56 | + stubMethod(sandbox, cmd, 'assignOrg').callsFake(() => { |
| 57 | + const orgStubOptions = { |
| 58 | + getSandboxOrgConfigField: () => {}, |
| 59 | + delete: () => {}, |
| 60 | + getUsername: () => username, |
| 61 | + getOrgId: () => orgId, |
| 62 | + isSandbox: () => options.isSandbox, |
| 63 | + }; |
| 64 | + |
| 65 | + if (options.deleteScratchOrg) { |
| 66 | + orgStubOptions.delete = () => { |
| 67 | + const e = new Error(); |
| 68 | + e.name = options.deleteScratchOrg; |
| 69 | + throw e; |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + const orgStub = fromStub(stubInterface<Org>(sandbox, orgStubOptions)); |
| 74 | + cmd.setOrg(orgStub); |
| 75 | + }); |
| 76 | + uxConfirmStub = stubMethod(sandbox, UX.prototype, 'confirm'); |
| 77 | + uxLogStub = stubMethod(sandbox, UX.prototype, 'log'); |
| 78 | + |
| 79 | + return cmd.runIt(); |
| 80 | + }; |
| 81 | + |
| 82 | + it('will prompt before attempting to delete', async () => { |
| 83 | + const res = await runDeleteCommand([]); |
| 84 | + expect(uxConfirmStub.calledOnce).to.equal(true); |
| 85 | + expect(uxConfirmStub.firstCall.args[0]).to.equal(messages.getMessage('confirmDelete', ['scratch', username])); |
| 86 | + expect(res).to.deep.equal({ orgId, username }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('will determine sandbox vs scratch org and delete sandbox', async () => { |
| 90 | + const res = await runDeleteCommand([], { isSandbox: true }); |
| 91 | + expect(uxConfirmStub.calledOnce).to.equal(true); |
| 92 | + expect(uxConfirmStub.firstCall.args[0]).to.equal(messages.getMessage('confirmDelete', ['sandbox', username])); |
| 93 | + expect(res).to.deep.equal({ orgId, username }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('will NOT prompt before deleting scratch org when flag is provided', async () => { |
| 97 | + const res = await runDeleteCommand(['--noprompt']); |
| 98 | + expect(uxConfirmStub.called).to.equal(false); |
| 99 | + expect(uxLogStub.callCount).to.equal(1); |
| 100 | + expect(uxLogStub.firstCall.args[0]).to.equal(messages.getMessage('deleteOrgCommandSuccess', [username])); |
| 101 | + expect(res).to.deep.equal({ orgId, username }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('will NOT prompt before deleting sandbox when flag is provided', async () => { |
| 105 | + const res = await runDeleteCommand(['--noprompt'], { isSandbox: true }); |
| 106 | + expect(uxConfirmStub.called).to.equal(false); |
| 107 | + expect(uxLogStub.callCount).to.equal(1); |
| 108 | + expect(uxLogStub.firstCall.args[0]).to.equal(messages.getMessage('commandSandboxSuccess', [username])); |
| 109 | + expect(res).to.deep.equal({ orgId, username }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('will catch the ScratchOrgNotFound and wrap correctly', async () => { |
| 113 | + const res = await runDeleteCommand(['--noprompt'], { deleteScratchOrg: 'ScratchOrgNotFound' }); |
| 114 | + expect(uxConfirmStub.called).to.equal(false); |
| 115 | + expect(res).to.deep.equal({ orgId, username }); |
| 116 | + expect(uxLogStub.firstCall.args[0]).to.equal(messages.getMessage('deleteOrgConfigOnlyCommandSuccess', [username])); |
| 117 | + }); |
| 118 | + |
| 119 | + it('will catch the SandboxNotFound and wrap correctly', async () => { |
| 120 | + const res = await runDeleteCommand(['--noprompt'], { |
| 121 | + deleteScratchOrg: 'SandboxNotFound', |
| 122 | + isSandbox: true, |
| 123 | + }); |
| 124 | + expect(uxConfirmStub.called).to.equal(false); |
| 125 | + expect(res).to.deep.equal({ orgId, username }); |
| 126 | + expect(uxLogStub.firstCall.args[0]).to.equal(messages.getMessage('sandboxConfigOnlySuccess', [username])); |
| 127 | + }); |
| 128 | + |
| 129 | + afterEach(() => { |
| 130 | + sandbox.restore(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments