generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathorgListUtil.test.ts
196 lines (175 loc) · 7.1 KB
/
orgListUtil.test.ts
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
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { $$, expect } from '@salesforce/command/lib/test';
import { AuthInfo, ConfigAggregator, fs, Aliases } from '@salesforce/core';
import { stubMethod } from '@salesforce/ts-sinon';
import { OrgListUtil } from '../../src/shared/orgListUtil';
import * as utils from '../../src/shared/utils';
const orgAuthConfigFields = {
username: '[email protected]',
expirationDate: '2099-03-30T00:00:00.000Z',
devHubUsername: '[email protected]',
accessToken: '123456abc',
refreshToken: 'axb123',
clientSecret: '123455',
};
const expiredAuthConfigFields = {
username: '[email protected]',
devHubUsername: '[email protected]',
accessToken: '121abc',
refreshToken: 'test123',
clientSecret: '121',
};
const expiredAuthConfig = {
getFields: () => expiredAuthConfigFields,
getConnectionOptions: () => ({ accessToken: '00D!XX' }),
isJwt: () => false,
isOauth: () => false,
getUsername: () => expiredAuthConfigFields.username,
};
const orgAuthConfig = {
getFields: () => orgAuthConfigFields,
getConnectionOptions: () => ({ accessToken: '00D!XX' }),
isJwt: () => false,
isOauth: () => false,
getUsername: () => orgAuthConfigFields.username,
};
const devHubConfigFields = {
username: '[email protected]',
isDevHub: true,
};
const devHubConfig = {
getFields: () => devHubConfigFields,
getConnectionOptions: () => ({ accessToken: '00D!XX' }),
isJwt: () => false,
isOauth: () => false,
getUsername: () => devHubConfigFields.username,
};
const fileNames = ['[email protected]', '[email protected]'];
describe('orgListUtil tests', () => {
const spies = new Map();
let readAuthFilesStub: sinon.SinonStub;
let groupOrgsStub: sinon.SinonStub;
let aliasListStub: sinon.SinonStub;
let determineConnectedStatusForNonScratchOrg: sinon.SinonStub;
let retrieveScratchOrgInfoFromDevHubStub: sinon.SinonStub;
describe('readLocallyValidatedMetaConfigsGroupedByOrgType', () => {
afterEach(() => spies.clear());
beforeEach(() => {
readAuthFilesStub = stubMethod($$.SANDBOX, OrgListUtil, 'readAuthFiles').resolves([
orgAuthConfig,
expiredAuthConfig,
devHubConfig,
]);
groupOrgsStub = stubMethod($$.SANDBOX, OrgListUtil, 'groupOrgs').resolves({
nonScratchOrgs: [devHubConfigFields],
scratchOrgs: [orgAuthConfigFields, expiredAuthConfigFields],
});
aliasListStub = stubMethod($$.SANDBOX, Aliases, 'fetch').resolves();
determineConnectedStatusForNonScratchOrg = stubMethod(
$$.SANDBOX,
OrgListUtil,
'determineConnectedStatusForNonScratchOrg'
).resolves({});
retrieveScratchOrgInfoFromDevHubStub = stubMethod(
$$.SANDBOX,
OrgListUtil,
'retrieveScratchOrgInfoFromDevHub'
).resolves([]);
spies.set('reduceScratchOrgInfo', $$.SANDBOX.spy(OrgListUtil, 'reduceScratchOrgInfo'));
$$.SANDBOX.stub(fs, 'readFileSync');
});
afterEach(async () => {
$$.SANDBOX.restore();
});
it('readLocallyValidatedMetaConfigsGroupedByOrgType', async () => {
const flags = {};
await OrgListUtil.readLocallyValidatedMetaConfigsGroupedByOrgType(fileNames, flags);
expect(readAuthFilesStub.calledOnce).to.be.true;
expect(groupOrgsStub.calledOnce).to.be.true;
expect(aliasListStub.calledOnce).to.be.false;
expect(determineConnectedStatusForNonScratchOrg.calledOnce).to.be.true;
expect(retrieveScratchOrgInfoFromDevHubStub.calledOnce).to.be.true;
});
it('skipconnectionstatus', async () => {
const flags = { skipconnectionstatus: true };
await OrgListUtil.readLocallyValidatedMetaConfigsGroupedByOrgType(fileNames, flags);
expect(readAuthFilesStub.calledOnce).to.be.true;
expect(groupOrgsStub.calledOnce).to.be.true;
expect(aliasListStub.calledOnce).to.be.false;
expect(determineConnectedStatusForNonScratchOrg.calledOnce).to.be.false;
});
it('should execute queries to check for org information if --verbose is used', async () => {
const flags = { verbose: true };
await OrgListUtil.readLocallyValidatedMetaConfigsGroupedByOrgType(fileNames, flags);
expect(retrieveScratchOrgInfoFromDevHubStub.calledOnce).to.be.true;
expect(spies.get('reduceScratchOrgInfo').calledOnce).to.be.true;
});
it('execute queries should add information to grouped orgs', async () => {
retrieveScratchOrgInfoFromDevHubStub.restore();
retrieveScratchOrgInfoFromDevHubStub = stubMethod(
$$.SANDBOX,
OrgListUtil,
'retrieveScratchOrgInfoFromDevHub'
).resolves([
{
SignupUsername: '[email protected]',
OrgName: 'Baz',
CreatedDate: '2017-04-11T17:58:43.000+0000',
CreatedBy: 'SRV',
Edition: 'Developer',
ScratchOrg: '00Dxx0000001hcF',
Status: 'Active',
},
]);
const flags = { verbose: true };
const orgGroups = await OrgListUtil.readLocallyValidatedMetaConfigsGroupedByOrgType(fileNames, flags);
expect(retrieveScratchOrgInfoFromDevHubStub.calledOnce).to.be.true;
expect(spies.get('reduceScratchOrgInfo').calledOnce).to.be.true;
expect(orgGroups.scratchOrgs[0].signupUsername).to.equal(orgAuthConfigFields.username);
});
});
describe('groupOrgs', () => {
let contents;
beforeEach(() => {
$$.SANDBOX.stub(AuthInfo, 'create');
$$.SANDBOX.stub(utils, 'getAliasByUsername').withArgs('[email protected]').resolves('gaz');
readAuthFilesStub = stubMethod($$.SANDBOX, OrgListUtil, 'readAuthFiles').resolves([
orgAuthConfig,
expiredAuthConfig,
devHubConfig,
]);
stubMethod($$.SANDBOX, fs, 'stat').resolves({ atime: 'test' });
stubMethod($$.SANDBOX, ConfigAggregator, 'create').resolves({
getConfig: () => {
return {
defaultusername: orgAuthConfigFields.username,
defaultdevhubusername: devHubConfigFields.username,
};
},
});
});
afterEach(async () => {
$$.SANDBOX.restore();
});
it('ensure the auth infos are categorized into scratchOrgs, nonScratchOrgs', async () => {
contents = await OrgListUtil.readAuthFiles(['[email protected]']);
const orgs = await OrgListUtil.groupOrgs(contents);
expect(orgs.scratchOrgs.length).to.equal(2);
expect(orgs.scratchOrgs[0]).to.haveOwnProperty('username').to.equal('[email protected]');
expect(orgs.nonScratchOrgs.length).to.equal(1);
});
it('should omit sensitive information and catergorise active and non-active scracth orgs', async () => {
const authInfos = await OrgListUtil.readAuthFiles(['[email protected]']);
const orgs = await OrgListUtil.groupOrgs(authInfos);
expect(orgs.scratchOrgs[0]).to.not.haveOwnProperty('clientSecret');
expect(orgs.scratchOrgs[1]).to.not.haveOwnProperty('clientSecret');
expect(orgs.scratchOrgs[0]).to.not.haveOwnProperty('refreshToken');
expect(orgs.scratchOrgs[1]).to.not.haveOwnProperty('refreshToken');
});
});
});