-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdelete.test.ts
More file actions
249 lines (229 loc) · 11.3 KB
/
delete.test.ts
File metadata and controls
249 lines (229 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AuthInfo, Messages, Org, SfError } from '@salesforce/core';
import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup';
import { SinonStub } from 'sinon';
import { config, expect } from 'chai';
import { stubPrompter, stubSfCommandUx } from '@salesforce/sf-plugins-core';
import { SandboxAccessor } from '../../../node_modules/@salesforce/core/lib/stateAggregator/accessors/sandboxAccessor.js';
import DeleteSandbox from '../../../src/commands/org/delete/sandbox.js';
import DeleteScratch from '../../../src/commands/org/delete/scratch.js';
config.truncateThreshold = 0;
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const sbxOrgMessages = Messages.loadMessages('@salesforce/plugin-org', 'delete_sandbox');
const scratchOrgMessages = Messages.loadMessages('@salesforce/plugin-org', 'delete_scratch');
describe('org delete', () => {
const $$ = new TestContext();
const testOrg = new MockTestOrgData();
const testHub = new MockTestOrgData();
testOrg.devHubUsername = testHub.username;
// stubs
let prompterStubs: ReturnType<typeof stubPrompter>;
let sfCommandUxStubs: ReturnType<typeof stubSfCommandUx>;
let orgDeleteStub: SinonStub;
beforeEach(async () => {
await $$.stubAuths(testOrg, testHub);
prompterStubs = stubPrompter($$.SANDBOX);
sfCommandUxStubs = stubSfCommandUx($$.SANDBOX);
orgDeleteStub = $$.SANDBOX.stub(Org.prototype, 'delete').resolves();
});
describe('sandbox', () => {
it('will throw an error when no org provided', async () => {
await $$.stubConfig({});
try {
await DeleteSandbox.run();
expect.fail('should have thrown MissingUsernameError');
} catch (e) {
const err = e as SfError;
expect(err.name).to.equal('MissingUsernameError');
expect(err.message).to.equal(sbxOrgMessages.getMessage('error.missingUsername'));
}
});
it('will throw an error when org cannot be found', async () => {
const username = 'nonexistant';
await $$.stubConfig({ 'target-org': username });
const error = new SfError('no auth file found', 'NamedOrgNotFoundError');
$$.SANDBOX.stub(AuthInfo, 'create').throws(error);
try {
await DeleteSandbox.run(['--target-org', username]);
expect.fail('should have thrown NamedOrgNotFoundError');
} catch (e) {
const err = e as SfError;
expect(err.name).to.equal('NamedOrgNotFoundError');
const errorActions = err.actions ?? [];
expect(errorActions[0]).to.equal(`Ensure the alias or username for the ${username} org is correct.`);
expect(errorActions[1]).to.equal(`Ensure the ${username} org has been authenticated with the CLI.`);
}
});
it('will throw an error when the org is not identified as a sandbox', async () => {
$$.SANDBOX.stub(SandboxAccessor.prototype, 'hasFile').resolves(false);
await $$.stubConfig({ 'target-org': testOrg.username });
try {
await DeleteSandbox.run(['--target-org', testOrg.username]);
expect.fail('should have thrown UnknownSandboxError');
} catch (e) {
const err = e as SfError;
expect(err.name).to.equal('UnknownSandboxError');
const expectedActions = sbxOrgMessages.getMessage('error.unknownSandbox.actions');
const errorActions = err.actions ?? [];
expect(errorActions[0]).to.equal(expectedActions);
}
});
it('will prompt before attempting to delete by username', async () => {
$$.SANDBOX.stub(SandboxAccessor.prototype, 'hasFile').resolves(true);
await $$.stubConfig({ 'target-org': testOrg.username });
const res = await DeleteSandbox.run([]);
expect(prompterStubs.confirm.callCount).to.equal(1);
expect(prompterStubs.confirm.firstCall.args[0]).to.deep.equal({
message: sbxOrgMessages.getMessage('prompt.confirm', [testOrg.username]),
});
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
});
it('will prompt before attempting to delete by alias', async () => {
$$.SANDBOX.stub(SandboxAccessor.prototype, 'hasFile').resolves(true);
await $$.stubConfig({ 'target-org': 'myAlias' });
$$.stubAliases({ myAlias: testOrg.username });
const res = await DeleteSandbox.run([]);
expect(prompterStubs.confirm.callCount).to.equal(1);
expect(prompterStubs.confirm.firstCall.args[0]).to.deep.equal({
message: sbxOrgMessages.getMessage('prompt.confirm', [testOrg.username]),
});
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
});
it('will NOT prompt before deleting sandbox when flag is provided', async () => {
$$.SANDBOX.stub(SandboxAccessor.prototype, 'hasFile').resolves(true);
const res = await DeleteSandbox.run(['--no-prompt', '--target-org', testOrg.username]);
expect(prompterStubs.confirm.callCount).to.equal(0);
expect(sfCommandUxStubs.logSuccess.callCount).to.equal(1);
expect(sfCommandUxStubs.logSuccess.getCalls().flatMap((call) => call.args)).to.deep.include(
sbxOrgMessages.getMessage('success', [testOrg.username])
);
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
});
it('will catch the SandboxNotFound and wrap correctly', async () => {
$$.SANDBOX.stub(SandboxAccessor.prototype, 'hasFile').resolves(true);
orgDeleteStub.restore();
$$.SANDBOX.stub(Org.prototype, 'delete').throws(new SfError('bah!', 'SandboxNotFound'));
const res = await DeleteSandbox.run(['--no-prompt', '--target-org', testOrg.username]);
expect(prompterStubs.confirm.called).to.equal(false);
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
expect(
sfCommandUxStubs.logSuccess.getCalls().flatMap((call) => call.args),
JSON.stringify(sfCommandUxStubs.logSuccess.getCalls().flatMap((call) => call.args))
).to.deep.include(sbxOrgMessages.getMessage('success.Idempotent', [testOrg.username]));
});
it('will throw a clean SfError when the user lacks Manage Sandboxes permission', async () => {
$$.SANDBOX.stub(SandboxAccessor.prototype, 'hasFile').resolves(true);
orgDeleteStub.restore();
const insufficientAccessError = Object.assign(new Error('INSUFFICIENT_ACCESS_OR_READONLY'), {
errorCode: 'INSUFFICIENT_ACCESS_OR_READONLY',
});
$$.SANDBOX.stub(Org.prototype, 'delete').throws(insufficientAccessError);
try {
await DeleteSandbox.run(['--no-prompt', '--target-org', testOrg.username]);
expect.fail('should have thrown InsufficientAccessError');
} catch (e) {
const err = e as SfError;
expect(err.name).to.equal('InsufficientAccessError');
expect(err.message).to.equal(sbxOrgMessages.getMessage('error.insufficientAccess'));
}
});
});
describe('scratch', () => {
it('will throw an error when no org provided', async () => {
await $$.stubConfig({});
try {
await DeleteScratch.run();
expect.fail('should have thrown MissingUsernameError');
} catch (e) {
const err = e as SfError;
expect(err.name).to.equal('MissingUsernameError');
expect(err.message).to.equal(scratchOrgMessages.getMessage('error.missingUsername'));
}
});
it('will prompt before attempting to delete by username', async () => {
$$.SANDBOX.stub(AuthInfo.prototype, 'getFields').returns({
orgId: testOrg.orgId,
isScratch: true,
});
await $$.stubConfig({ 'target-org': testOrg.username });
const res = await DeleteScratch.run([]);
expect(prompterStubs.confirm.callCount).to.equal(1);
expect(prompterStubs.confirm.firstCall.args[0]).to.deep.equal({
message: scratchOrgMessages.getMessage('prompt.confirm', [testOrg.username]),
});
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
});
it('will throw an error when the org is not identified as a scratch', async () => {
$$.SANDBOX.stub(AuthInfo.prototype, 'getFields').returns({
orgId: testOrg.orgId,
isScratch: false,
});
await $$.stubConfig({ 'target-org': testOrg.username });
try {
await DeleteScratch.run(['--target-org', testOrg.username]);
expect.fail('should have thrown UnknownScratchError');
} catch (e) {
const err = e as SfError;
expect(err.name).to.equal('UnknownScratchError');
}
});
it('will prompt before attempting to delete by alias', async () => {
$$.SANDBOX.stub(AuthInfo.prototype, 'getFields').returns({
orgId: testOrg.orgId,
isScratch: true,
});
await $$.stubConfig({ 'target-org': 'myAlias' });
$$.stubAliases({ myAlias: testOrg.username });
const res = await DeleteScratch.run(['--target-org', 'myAlias']);
expect(prompterStubs.confirm.callCount).to.equal(1);
expect(prompterStubs.confirm.firstCall.args[0]).to.deep.equal({
message: scratchOrgMessages.getMessage('prompt.confirm', [testOrg.username]),
});
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
});
it('will NOT prompt before deleting scratch org when flag is provided', async () => {
await $$.stubConfig({ 'target-org': testOrg.username });
$$.SANDBOX.stub(AuthInfo.prototype, 'getFields').returns({
orgId: testOrg.orgId,
isScratch: true,
});
orgDeleteStub.restore();
$$.SANDBOX.stub(Org.prototype, 'delete').throws(new SfError('bah!', 'DomainNotFoundError'));
const res = await DeleteScratch.run(['--no-prompt', '--target-org', testOrg.username]);
expect(prompterStubs.confirm.calledOnce).to.equal(false);
expect(sfCommandUxStubs.logSuccess.callCount).to.equal(1);
expect(sfCommandUxStubs.logSuccess.getCalls().flatMap((call) => call.args)).to.deep.include(
scratchOrgMessages.getMessage('success', [testOrg.username])
);
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
});
it('will catch the ScratchOrgNotFound and wrap correctly', async () => {
$$.SANDBOX.stub(AuthInfo.prototype, 'getFields').returns({
orgId: testOrg.orgId,
isScratch: true,
});
orgDeleteStub.restore();
$$.SANDBOX.stub(Org.prototype, 'delete').throws(new SfError('bah!', 'ScratchOrgNotFound'));
const res = await DeleteScratch.run(['--no-prompt', '--target-org', testOrg.username]);
expect(prompterStubs.confirm.calledOnce).to.equal(false);
expect(res).to.deep.equal({ orgId: testOrg.orgId, username: testOrg.username });
expect(sfCommandUxStubs.logSuccess.getCalls().flatMap((call) => call.args)).to.deep.include(
scratchOrgMessages.getMessage('success.Idempotent', [testOrg.username])
);
});
});
});