Skip to content

Commit

Permalink
fix: user:create matches smoke test json
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Feb 5, 2021
1 parent 898886e commit b8f9380
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 71 deletions.
30 changes: 23 additions & 7 deletions src/commands/force/user/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ interface FailureMsg {
message: string;
}

const permsetsStringToArray = (fieldsPermsets: string | string[]): string[] => {
if (!fieldsPermsets) return [];
return isArray<string>(fieldsPermsets)
? fieldsPermsets
: fieldsPermsets.split(',').map((item) => item.replace("'", '').trim());
};

export class UserCreateCommand extends SfdxCommand {
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessage('examples').split(os.EOL);
Expand All @@ -61,7 +68,7 @@ export class UserCreateCommand extends SfdxCommand {
private authInfo: AuthInfo;

/**
* removes fields that cause errors in salesforce api's within sfdx-core's createUser method
* removes fields that cause errors in salesforce APIs within sfdx-core's createUser method
*
* @param fields a list of combined fields from varargs and the config file
* @private
Expand All @@ -76,7 +83,7 @@ export class UserCreateCommand extends SfdxCommand {
return copy as UserFields;
}

public async run(): Promise<UserFields> {
public async run(): Promise<UserCreateOutput> {
this.logger = await Logger.child(this.constructor.name);
const defaultUserFields: DefaultUserFields = await DefaultUserFields.create({
templateUser: this.org.getUsername(),
Expand All @@ -98,9 +105,7 @@ export class UserCreateCommand extends SfdxCommand {
try {
// permsets can be passed from cli args or file we need to create an array of permset names either way it's passed
// it will either be a comma separated string, or an array, force it into an array
const permsetArray: string[] = isArray<string>(fields.permsets)
? fields.permsets
: fields.permsets.trim().split(',');
const permsetArray = permsetsStringToArray(fields.permsets);

await this.user.assignPermissionSets(this.authInfo.getFields().userId, permsetArray);
this.successes.push({
Expand Down Expand Up @@ -147,7 +152,12 @@ export class UserCreateCommand extends SfdxCommand {

this.print(fields);

return Object.assign({ orgId: this.org.getOrgId() }, fields);
const { permsets, ...fieldsWithoutPermsets } = fields;
return {
orgId: this.org.getOrgId(),
permissionSetAssignments: permsetsStringToArray(permsets),
fields: { ...fieldsWithoutPermsets },
};
}

private async catchCreateUser(respBody: Error, fields: UserFields): Promise<void> {
Expand Down Expand Up @@ -202,7 +212,7 @@ export class UserCreateCommand extends SfdxCommand {
defaultFields.profileId = response.records[0].Id;
}

// the file schema is camelCase and boolean while the cli arg is no capitialization and a string
// the file schema is camelCase and boolean while the cli arg is no capitalization and a string
// we will add logic to capture camelcase in varargs just in case
if (
defaultFields['generatepassword'] === 'true' ||
Expand Down Expand Up @@ -258,3 +268,9 @@ export class UserCreateCommand extends SfdxCommand {
}

export default UserCreateCommand;

interface UserCreateOutput {
orgId: string;
permissionSetAssignments: string[];
fields: UserFields;
}
141 changes: 77 additions & 64 deletions test/commands/user/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ describe('force:user:create', () => {
lastName: 'User',
timeZoneSidKey: 'America/Los_Angeles',
});

expect(res).to.deep.equal({
alias: 'testAlias',
email: '[email protected]',
Expand Down Expand Up @@ -84,6 +83,8 @@ describe('force:user:create', () => {
});
stubMethod($$.SANDBOX, Aliases, 'fetch').resolves('testAlias');
stubMethod($$.SANDBOX, User, 'create').callsFake(() => User.prototype);

stubMethod($$.SANDBOX, User.prototype, 'assignPermissionSets').resolves();
stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns(username);
stubMethod($$.SANDBOX, Org.prototype, 'getOrgId').returns('abc123');
authInfoStub = stubMethod($$.SANDBOX, AuthInfo.prototype, 'save').resolves();
Expand Down Expand Up @@ -124,21 +125,23 @@ describe('force:user:create', () => {
])
.it('will handle a merge multiple permsets and profileNames from args and file (permsets from args)', (ctx) => {
const expected = {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
orgId: 'abc123',
generatepassword: 'true',
generatePassword: true,
permsets: "'permCLI, permCLI2'",
profileId: '12345678',
profileName: 'profileFromArgs',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
permissionSetAssignments: ['permCLI', 'permCLI2'],
fields: {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
generatepassword: 'true',
generatePassword: true,
profileId: '12345678',
profileName: 'profileFromArgs',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
},
};
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
Expand All @@ -164,19 +167,21 @@ describe('force:user:create', () => {
])
.it('will handle a merge multiple permsets and profileNames from args and file (permsets from file)', (ctx) => {
const expected = {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
orgId: 'abc123',
permsets: ['perm1', 'perm2'],
profileId: '12345678',
profileName: 'profileFromArgs',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
permissionSetAssignments: ['perm1', 'perm2'],
fields: {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
profileId: '12345678',
profileName: 'profileFromArgs',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
},
};
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
Expand All @@ -198,17 +203,20 @@ describe('force:user:create', () => {
])
.it('default create creates user exactly from DefaultUserFields', (ctx) => {
const expected = {
alias: 'testAlias',
email: username,
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
orgId: 'abc123',
profileId: '00e2D000000bNexWWR',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
permissionSetAssignments: [],
fields: {
alias: 'testAlias',
email: username,
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);
Expand All @@ -235,20 +243,22 @@ describe('force:user:create', () => {
// we set generatepassword=false in the varargs, in the definitionfile we have generatepassword=true, so we SHOULD NOT generate a password
.it('will merge fields from the cli args, and the definitionfile correctly, preferring cli args', (ctx) => {
const expected = {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
orgId: 'abc123',
permsets: ['test1', 'test2'],
profileId: '00e2D000000bNexWWR',
generatePassword: false,
generatepassword: 'false',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
permissionSetAssignments: ['test1', 'test2'],
fields: {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
profileId: '00e2D000000bNexWWR',
generatePassword: false,
generatepassword: 'false',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
},
};
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
Expand Down Expand Up @@ -279,21 +289,24 @@ describe('force:user:create', () => {
'will merge fields from the cli args, and the definitionfile correctly, preferring cli args, cli args > file > default',
(ctx) => {
const expected = {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
generatePassword: false,
generatepassword: 'false',
profileName: "'Chatter Free User'",
orgId: 'abc123',
// note the new profileId 12345678 -> Chatter Free User from var args
profileId: '12345678',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
permissionSetAssignments: [],
fields: {
alias: 'testAlias',
email: '[email protected]',
emailEncodingKey: 'UTF-8',
id: '0052D0000043PawWWR',
languageLocaleKey: 'en_US',
lastName: 'User',
localeSidKey: 'en_US',
generatePassword: false,
generatepassword: 'false',
profileName: "'Chatter Free User'",
// note the new profileId 12345678 -> Chatter Free User from var args
profileId: '12345678',
timeZoneSidKey: 'America/Los_Angeles',
username: '[email protected]',
},
};
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
Expand Down

0 comments on commit b8f9380

Please sign in to comment.