Skip to content

Commit

Permalink
fix: code review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Jan 27, 2021
1 parent 4b0b78d commit cb90a86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
45 changes: 28 additions & 17 deletions src/commands/force/user/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ export class UserCreateCommand extends SfdxCommand {
private failures: FailureMsg[] = [];
private authInfo: AuthInfo;

/**
* removes fields that cause errors in sfdx-core's userCreate method
*
* @param fields a list of combined fields from varargs and the config file
* @private
*/
private static stripInvalidAPIFields(fields: UserFields & Dictionary<string>): UserFields {
const copy = Object.assign({}, fields);
// remove invalid fields for userCreate()
delete copy.permsets;
delete copy.generatepassword;
delete copy.generatePassword;
delete copy.profileName;
return copy as UserFields;
}

public async run(): Promise<UserFields> {
this.logger = await Logger.child(this.constructor.name);
const defaultUserFields: DefaultUserFields = await DefaultUserFields.create({
Expand All @@ -68,26 +84,14 @@ export class UserCreateCommand extends SfdxCommand {

// merge defaults with provided values with cli > file > defaults
const fields = await this.aggregateFields(defaultUserFields.getFields());
// because fields is type UserFields & Dictionary<string> we can access these
const permsets: string = fields.permsets;
const generatepassword: string = fields.generatepassword;
const profileName = fields.profileName;

// extract the fields and then delete, createUser doesn't expect a permsets or generatepassword
delete fields.permsets;
delete fields.generatepassword;
delete fields.generatePassword;
delete fields.profileName;

try {
this.authInfo = await this.user.createUser(fields);
fields.permsets = permsets;
fields.profileName = profileName;
if (fields.profileName) await this.authInfo.save({ userProfileName: fields.profileName });
this.authInfo = await this.user.createUser(UserCreateCommand.stripInvalidAPIFields(fields));
} catch (e) {
await this.catchCreateUser(e, fields);
}

if (fields.profileName) await this.authInfo.save({ userProfileName: fields.profileName });

// Assign permission sets to the created user
if (fields.permsets) {
try {
Expand All @@ -111,7 +115,7 @@ export class UserCreateCommand extends SfdxCommand {
}

// Generate and set a password if specified
if (generatepassword === 'true' || generatepassword) {
if (fields.generatepassword === 'true') {
try {
const password = User.generatePasswordUtf8();
await this.user.assignPassword(this.authInfo, password);
Expand Down Expand Up @@ -191,7 +195,14 @@ export class UserCreateCommand extends SfdxCommand {
}

// the file schema is camelCase while the cli arg is no capitialization
if (defaultFields['generatePassword'] || defaultFields['generatepassword']) {
// the flag and file can pass as a string or a proper boolean
// we can check only for existence because it could be set to false or 'false'
if (
defaultFields['generatePassword'] === 'true' ||
defaultFields['generatepassword'] === 'true' ||
defaultFields['generatePassword'] === true ||
defaultFields['generatepassword'] === true
) {
// standardize on 'generatepassword'
defaultFields['generatepassword'] = 'true';
}
Expand Down
7 changes: 5 additions & 2 deletions test/commands/user/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('force:user:create', () => {
'[email protected]',
'--targetdevhubusername',
'[email protected]',
"permissionSets='permCLI, permCLI2'",
"permsets='permCLI, permCLI2'",
'generatepassword=true',
'profileName=profileFromArgs',
])
Expand All @@ -132,7 +132,8 @@ describe('force:user:create', () => {
lastName: 'User',
localeSidKey: 'en_US',
orgId: 'abc123',
permissionSets: "'permCLI, permCLI2'",
generatepassword: 'true',
permsets: "'permCLI, permCLI2'",
profileId: '12345678',
profileName: 'profileFromArgs',
timeZoneSidKey: 'America/Los_Angeles',
Expand Down Expand Up @@ -243,6 +244,7 @@ describe('force:user:create', () => {
orgId: 'abc123',
permsets: ['test1', 'test2'],
profileId: '00e2D000000bNexWWR',
generatepassword: 'false',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
};
Expand Down Expand Up @@ -282,6 +284,7 @@ describe('force:user:create', () => {
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
generatepassword: 'false',
profileName: "'Chatter Free User'",
orgId: 'abc123',
// note the new profileId 12345678 -> Chatter Free User from var args
Expand Down

0 comments on commit cb90a86

Please sign in to comment.