From 2e28714f4faa09ddb6e741c15f12730d616109c4 Mon Sep 17 00:00:00 2001 From: Fabien Taillon Date: Mon, 15 Feb 2021 14:14:52 +0100 Subject: [PATCH] feat: added setpassedusernameunique flag #827 --- command-snapshot.json | 11 ++++- messages/create.json | 3 +- src/commands/force/user/create.ts | 12 +++++ test/commands/user/create.test.ts | 76 +++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/command-snapshot.json b/command-snapshot.json index 1dbdd472..39bb3f1d 100644 --- a/command-snapshot.json +++ b/command-snapshot.json @@ -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", diff --git a/messages/create.json b/messages/create.json index 65ce47c9..1cb8e486 100644 --- a/messages/create.json +++ b/messages/create.json @@ -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.", diff --git a/src/commands/force/user/create.ts b/src/commands/force/user/create.ts index 577f9bcf..b8f9b98f 100644 --- a/src/commands/force/user/create.ts +++ b/src/commands/force/user/create.ts @@ -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; @@ -190,6 +194,9 @@ export class UserCreateCommand extends SfdxCommand { } private async aggregateFields(defaultFields: UserFields): Promise> { + // 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; @@ -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; diff --git a/test/commands/user/create.test.ts b/test/commands/user/create.test.ts index c3c6cc31..1361049b 100644 --- a/test/commands/user/create.test.ts +++ b/test/commands/user/create.test.ts @@ -354,4 +354,80 @@ describe('force:user:create', () => { 'The username "1605130295132_test-j6asqt5qoprs@example.com" 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', + 'testUser1@test.com', + '--targetdevhubusername', + 'devhub@test.com', + 'username=user@cliFlag.com', + '--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: 'defaultusername@test.com', + emailencodingkey: 'UTF-8', + id: '0052D0000043PawWWR', + languagelocalekey: 'en_US', + lastname: 'User', + localesidkey: 'en_US', + profileid: '00e2D000000bNexWWR', + timezonesidkey: 'America/Los_Angeles', + username: 'user@cliFlag.com.abc123', + }, + }; + 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', + 'testUser1@test.com', + '--targetdevhubusername', + 'devhub@test.com', + '--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: 'defaultusername@test.com', + emailencodingkey: 'UTF-8', + id: '0052D0000043PawWWR', + languagelocalekey: 'en_US', + lastname: 'User', + localesidkey: 'en_US', + profileid: '00e2D000000bNexWWR', + timezonesidkey: 'America/Los_Angeles', + username: '1605130295132_test-j6asqt5qoprs@example.com', + }, + }; + const result = JSON.parse(ctx.stdout).result; + expect(result).to.deep.equal(expected); + expect(authInfoStub.callCount).to.be.equal(0); + } + ); });