Skip to content

Commit 3cea55a

Browse files
chore: fixed aliased default username, added tests
1 parent 87accd4 commit 3cea55a

File tree

5 files changed

+122
-14
lines changed

5 files changed

+122
-14
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -- CLEAN
2-
2+
tmp/
33
# use yarn by default, so ignore npm
44
package-lock.json
55

@@ -33,4 +33,3 @@ node_modules
3333
# os specific files
3434
.DS_Store
3535
.idea
36-
/tmp/

src/commands/force/user/create.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
User,
1919
UserFields,
2020
} from '@salesforce/core';
21+
import { QueryResult } from 'jsforce';
2122
import { getString, Dictionary, isArray } from '@salesforce/ts-types';
2223
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
2324

@@ -176,9 +177,10 @@ export class UserCreateCommand extends SfdxCommand {
176177
if (defaultFields['profileName']) {
177178
const name: string = defaultFields['profileName'] || 'Standard User';
178179
this.logger.debug(`Querying org for profile name [${name}]`);
179-
const profileQuery = `SELECT id FROM profile WHERE name='${name}'`;
180-
const response = await this.org.getConnection().query(profileQuery);
181-
defaultFields.profileId = getString(response, 'records[0].Id');
180+
const response: QueryResult<{ Id: string }> = await this.org
181+
.getConnection()
182+
.query(`SELECT id FROM profile WHERE name='${name}'`);
183+
defaultFields.profileId = response.records[0].Id;
182184
delete defaultFields['profileName'];
183185
}
184186

src/commands/force/user/list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export class UserListCommand extends SfdxCommand {
4545
// if they passed in a alias and it maps to something we have an Alias.
4646
const alias = aliases.getKeysByValue(authData.getUsername())[0];
4747
const configAgg = ConfigAggregator.getInstance();
48-
const defaultUser = configAgg.getLocalConfig().get('defaultusername');
48+
const defaultUserOrAlias = configAgg.getLocalConfig().get('defaultusername');
4949
return {
50-
defaultMarker: defaultUser === username ? '(A)' : '',
50+
defaultMarker: defaultUserOrAlias === username || defaultUserOrAlias === alias ? '(A)' : '',
5151
alias: alias || '',
5252
username,
5353
profileName: profileInfos[userInfos[username].ProfileId],

test/commands/user/create.test.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/* eslint-disable @typescript-eslint/ban-ts-ignore */
99

1010
import { $$, expect, test } from '@salesforce/command/lib/test';
11-
import { Aliases, Connection, DefaultUserFields, fs, Org, User } from '@salesforce/core';
11+
import { Aliases, Connection, DefaultUserFields, fs, Logger, Org, User } from '@salesforce/core';
1212
import { stubMethod } from '@salesforce/ts-sinon';
1313
import { IConfig } from '@oclif/config';
1414
import UserCreateCommand from '../../../src/commands/force/user/create';
@@ -61,7 +61,7 @@ describe('force:user:create', () => {
6161
});
6262
});
6363

64-
async function prepareStubs(throws: { license?: boolean; duplicate?: boolean } = {}, readsFile = false) {
64+
async function prepareStubs(throws: { license?: boolean; duplicate?: boolean } = {}, readsFile?) {
6565
stubMethod($$.SANDBOX, Org.prototype, 'getConnection').callsFake(() => Connection.prototype);
6666
stubMethod($$.SANDBOX, DefaultUserFields, 'create').resolves({
6767
getFields: () => {
@@ -94,7 +94,11 @@ describe('force:user:create', () => {
9494
}
9595

9696
if (readsFile) {
97-
stubMethod($$.SANDBOX, fs, 'readJson').resolves({ generatepassword: true });
97+
stubMethod($$.SANDBOX, Connection.prototype, 'query').resolves({
98+
records: [{ Id: '12345678' }],
99+
});
100+
stubMethod($$.SANDBOX, Logger.prototype, 'debug');
101+
stubMethod($$.SANDBOX, fs, 'readJson').resolves(readsFile);
98102
}
99103
}
100104

@@ -131,7 +135,7 @@ describe('force:user:create', () => {
131135

132136
test
133137
.do(async () => {
134-
await prepareStubs({}, true);
138+
await prepareStubs({}, { generatepassword: true });
135139
})
136140
.stdout()
137141
.command([
@@ -165,6 +169,48 @@ describe('force:user:create', () => {
165169
expect(result).to.deep.equal(expected);
166170
});
167171

172+
test
173+
.do(async () => {
174+
await prepareStubs({}, { generatepassword: true, profileName: 'System Administrator' });
175+
})
176+
.stdout()
177+
.command([
178+
'force:user:create',
179+
'--json',
180+
'--definitionfile',
181+
'parent/child/file.json',
182+
'--targetusername',
183+
'testUser1@test.com',
184+
'--targetdevhubusername',
185+
'devhub@test.com',
186+
'email=me@my.org',
187+
'generatepassword=false',
188+
"profileName='Chatter Free User'",
189+
])
190+
// we set generatepassword=false in the varargs, in the definitionfile we have generatepassword=true, so we SHOULD NOT generate a password
191+
// we should override the profileName with 'Chatter Free User'
192+
.it(
193+
'will merge fields from the cli args, and the definitionfile correctly, preferring cli args, cli args > file > default',
194+
(ctx) => {
195+
const expected = {
196+
alias: 'testAlias',
197+
email: 'me@my.org',
198+
emailEncodingKey: 'UTF-8',
199+
id: '0052D0000043PawWWR',
200+
languageLocaleKey: 'en_US',
201+
lastName: 'User',
202+
localeSidKey: 'en_US',
203+
orgId: 'abc123',
204+
// note the new profileId 12345678 -> Chatter Free User from var args
205+
profileId: '12345678',
206+
timeZoneSidKey: 'America/Los_Angeles',
207+
username: '1605130295132_test-j6asqt5qoprs@example.com',
208+
};
209+
const result = JSON.parse(ctx.stdout).result;
210+
expect(result).to.deep.equal(expected);
211+
}
212+
);
213+
168214
test
169215
.do(async () => {
170216
await prepareStubs({ license: true }, false);

test/commands/user/list.test.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
*/
77

88
import { $$, expect, test } from '@salesforce/command/lib/test';
9-
import { Aliases, Connection, Org } from '@salesforce/core';
9+
import { Aliases, ConfigAggregator, Connection, Org } from '@salesforce/core';
1010
import { stubMethod } from '@salesforce/ts-sinon';
1111

1212
describe('force:user:list', () => {
13-
async function prepareStubs() {
13+
async function prepareStubs(defaultUser?: boolean) {
1414
stubMethod($$.SANDBOX, Org.prototype, 'getConnection').callsFake(() => Connection.prototype);
1515
stubMethod($$.SANDBOX, Org.prototype, 'readUserAuthFiles').returns([
1616
{
@@ -29,6 +29,10 @@ describe('force:user:list', () => {
2929
]);
3030
stubMethod($$.SANDBOX, Org.prototype, 'getOrgId').returns('abc123');
3131
stubMethod($$.SANDBOX, Aliases.prototype, 'getKeysByValue').returns(['testAlias']);
32+
if (defaultUser) {
33+
const cfa = ConfigAggregator.getInstance();
34+
stubMethod($$.SANDBOX, cfa, 'getLocalConfig').returns({ get: () => 'testAlias' });
35+
}
3236
stubMethod($$.SANDBOX, Connection.prototype, 'query')
3337
.withArgs('SELECT username, profileid, id FROM User')
3438
.resolves({
@@ -60,6 +64,31 @@ describe('force:user:list', () => {
6064
});
6165
}
6266

67+
test
68+
.do(async () => {
69+
await prepareStubs();
70+
})
71+
.stdout()
72+
.command(['force:user:list', '--json', '--targetusername', 'testUser', '--targetdevhubusername', 'devhub@test.com'])
73+
.it('should display the correct information from the default user', (ctx) => {
74+
// testUser1@test.com is aliased to testUser
75+
const expected = [
76+
{
77+
defaultMarker: '',
78+
alias: 'testAlias',
79+
username: 'testuser@test.com',
80+
profileName: 'Analytics Cloud Integration User',
81+
orgId: 'abc123',
82+
accessToken: 'accessToken',
83+
instanceUrl: 'instanceURL',
84+
loginUrl: 'login.test.com',
85+
userId: '0052D0000043PbGQAU',
86+
},
87+
];
88+
const result = JSON.parse(ctx.stdout).result;
89+
expect(result).to.deep.equal(expected);
90+
});
91+
6392
test
6493
.do(async () => {
6594
await prepareStubs();
@@ -73,7 +102,7 @@ describe('force:user:list', () => {
73102
'--targetdevhubusername',
74103
'devhub@test.com',
75104
])
76-
.it('should display the correct information from the default user', (ctx) => {
105+
.it('should display the correct information from the default user with alias', (ctx) => {
77106
// testUser1@test.com is aliased to testUser
78107
const expected = [
79108
{
@@ -91,4 +120,36 @@ describe('force:user:list', () => {
91120
const result = JSON.parse(ctx.stdout).result;
92121
expect(result).to.deep.equal(expected);
93122
});
123+
124+
test
125+
.do(async () => {
126+
await prepareStubs(true);
127+
})
128+
.stdout()
129+
.command([
130+
'force:user:list',
131+
'--json',
132+
'--targetusername',
133+
'testUser1@test.com',
134+
'--targetdevhubusername',
135+
'devhub@test.com',
136+
])
137+
.it('should display the correct information from the default user with alias and it default', (ctx) => {
138+
// testUser1@test.com is aliased to testUser
139+
const expected = [
140+
{
141+
defaultMarker: '(A)',
142+
alias: 'testAlias',
143+
username: 'testuser@test.com',
144+
profileName: 'Analytics Cloud Integration User',
145+
orgId: 'abc123',
146+
accessToken: 'accessToken',
147+
instanceUrl: 'instanceURL',
148+
loginUrl: 'login.test.com',
149+
userId: '0052D0000043PbGQAU',
150+
},
151+
];
152+
const result = JSON.parse(ctx.stdout).result;
153+
expect(result).to.deep.equal(expected);
154+
});
94155
});

0 commit comments

Comments
 (0)