Skip to content

Commit

Permalink
feat: added setpassedusernameunique flag #827
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienTaillon committed Feb 15, 2021
1 parent 7ff1637 commit 2e28714
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
11 changes: 10 additions & 1 deletion command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
{
"command": "force:user:create",
"plugin": "@salesforce/plugin-user",
"flags": ["apiversion", "definitionfile", "json", "loglevel", "setalias", "targetdevhubusername", "targetusername"]
"flags": [
"apiversion",
"definitionfile",
"json",
"loglevel",
"setalias",
"targetdevhubusername",
"targetusername",
"setpassedusernameunique"
]
},
{
"command": "force:user:display",
Expand Down
3 changes: 2 additions & 1 deletion messages/create.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
],
"flags": {
"alias": "set an alias for the created username to reference within the CLI",
"definitionfile": "file path to a user definition"
"definitionfile": "file path to a user definition",
"setpassedusernameunique": "if a username is passed, set it unique by appending the org id"
},
"licenseLimitExceeded": "There are no available user licenses for the user profile \"%s\".",
"duplicateUsername": "The username \"%s\" already exists in this or another Salesforce org. Usernames must be unique across all Salesforce orgs.",
Expand Down
12 changes: 12 additions & 0 deletions src/commands/force/user/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class UserCreateCommand extends SfdxCommand {
char: 'f',
description: messages.getMessage('flags.definitionfile'),
}),
setpassedusernameunique: flags.boolean({
char: 's',
description: messages.getMessage('flags.setpassedusernameunique'),
}),
};
public logger: Logger;
public org: Org;
Expand Down Expand Up @@ -190,6 +194,9 @@ export class UserCreateCommand extends SfdxCommand {
}

private async aggregateFields(defaultFields: UserFields): Promise<UserFields & Dictionary<string>> {
// username can be overrided both in the file or varargs, save it to check if it was changed somewhere
const defaultUsername = defaultFields['username'];

// start with the default fields, then add the fields from the file, then (possibly overwritting) add the fields from the cli varargs param
if (this.flags.definitionfile) {
const content = (await fs.readJson(this.flags.definitionfile)) as UserFields;
Expand All @@ -214,6 +221,11 @@ export class UserCreateCommand extends SfdxCommand {
});
}

// check if "username" was passed along with "setpassedusernameunique" flag, if so append org id
if (this.flags.setpassedusernameunique && defaultFields['username'] !== defaultUsername) {
defaultFields['username'] = `${defaultFields['username']}.${this.org.getOrgId().toLowerCase()}`;
}

// check if "profileName" was passed, this needs to become a profileId before calling User.create
if (defaultFields['profileName']) {
const name = (defaultFields['profileName'] ?? 'Standard User') as string;
Expand Down
76 changes: 76 additions & 0 deletions test/commands/user/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,80 @@ describe('force:user:create', () => {
'The username "[email protected]" already exists in this or another Salesforce org. Usernames must be unique across all Salesforce orgs.'
);
});

test
.do(async () => {
await prepareStubs({}, false);
})
.stdout()
.command([
'force:user:create',
'--json',
'--targetusername',
'[email protected]',
'--targetdevhubusername',
'[email protected]',
'[email protected]',
'--setpassedusernameunique',
])
.it('will append the org id to the passed username if the setpassedusernameunique is used', (ctx) => {
const expected = {
orgId: 'abc123',
permissionSetAssignments: [],
fields: {
alias: 'testAlias',
email: '[email protected]',
emailencodingkey: 'UTF-8',
id: '0052D0000043PawWWR',
languagelocalekey: 'en_US',
lastname: 'User',
localesidkey: 'en_US',
profileid: '00e2D000000bNexWWR',
timezonesidkey: 'America/Los_Angeles',
username: '[email protected]',
},
};
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
expect(authInfoStub.callCount).to.be.equal(0);
});

test
.do(async () => {
await prepareStubs({}, false);
})
.stdout()
.command([
'force:user:create',
'--json',
'--targetusername',
'[email protected]',
'--targetdevhubusername',
'[email protected]',
'--setpassedusernameunique',
])
.it(
'will not append the org id to the username if the setpassedusernameunique is used but username was generated by the CLI',
(ctx) => {
const expected = {
orgId: 'abc123',
permissionSetAssignments: [],
fields: {
alias: 'testAlias',
email: '[email protected]',
emailencodingkey: 'UTF-8',
id: '0052D0000043PawWWR',
languagelocalekey: 'en_US',
lastname: 'User',
localesidkey: 'en_US',
profileid: '00e2D000000bNexWWR',
timezonesidkey: 'America/Los_Angeles',
username: '[email protected]',
},
};
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
expect(authInfoStub.callCount).to.be.equal(0);
}
);
});

0 comments on commit 2e28714

Please sign in to comment.