Skip to content

Commit c27fc96

Browse files
Merge pull request #14 from salesforcecli/wr/userRegression
fix: standardized capitalization issues with config file
2 parents e2bc3fa + b217ce2 commit c27fc96

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

src/commands/force/user/create.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
User,
1919
UserFields,
2020
} from '@salesforce/core';
21-
import { get, Dictionary, isArray } from '@salesforce/ts-types';
21+
import { getString, Dictionary, isArray } from '@salesforce/ts-types';
2222
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
2323

2424
Messages.importMessagesDirectory(__dirname);
@@ -136,13 +136,13 @@ export class UserCreateCommand extends SfdxCommand {
136136
private async catchCreateUser(respBody: Error, fields: UserFields): Promise<void> {
137137
// For Gacks, the error message is on response.body[0].message but for handled errors
138138
// the error message is on response.body.Errors[0].description.
139-
const errMessage = (get(respBody, 'message') as string) || 'Unknown Error';
139+
const errMessage = getString(respBody, 'message') || 'Unknown Error';
140140
const conn: Connection = this.org.getConnection();
141141

142142
// Provide a more user friendly error message for certain server errors.
143143
if (errMessage.includes('LICENSE_LIMIT_EXCEEDED')) {
144144
const res = await conn.query(`SELECT name FROM profile WHERE id='${fields.profileId}'`);
145-
const profileName = get(res, 'records[0].Name') as string;
145+
const profileName = getString(res, 'records[0].Name');
146146
throw SfdxError.create('@salesforce/plugin-user', 'create', 'licenseLimitExceeded', [profileName]);
147147
} else if (errMessage.includes('DUPLICATE_USERNAME')) {
148148
throw SfdxError.create('@salesforce/plugin-user', 'create', 'duplicateUsername', [fields.username]);
@@ -151,19 +151,24 @@ export class UserCreateCommand extends SfdxCommand {
151151
}
152152
}
153153

154+
private lowerFirstLetter(word: string): string {
155+
return word[0].toLowerCase() + word.substr(1);
156+
}
157+
154158
private async aggregateFields(defaultFields: UserFields): Promise<UserFields & Dictionary<string>> {
155159
// start with the default fields, then add the fields from the file, then (possibly overwritting) add the fields from the cli varargs param
156160
if (this.flags.definitionfile) {
157161
const content = await fs.readJson(this.flags.definitionfile);
158162
Object.keys(content).forEach((key) => {
159-
defaultFields[key] = content[key];
163+
// cast entries to lowercase to standardize
164+
defaultFields[this.lowerFirstLetter(key)] = content[key];
160165
});
161166
}
162167

163168
if (this.varargs) {
164169
Object.keys(this.varargs).forEach((key) => {
165170
if (defaultFields[key]) {
166-
defaultFields[key] = this.varargs[key];
171+
defaultFields[this.lowerFirstLetter(key)] = this.varargs[key];
167172
}
168173
});
169174
}
@@ -174,7 +179,7 @@ export class UserCreateCommand extends SfdxCommand {
174179
this.logger.debug(`Querying org for profile name [${name}]`);
175180
const profileQuery = `SELECT id FROM profile WHERE name='${name}'`;
176181
const response = await this.org.getConnection().query(profileQuery);
177-
defaultFields.profileId = get(response, 'records[0].Id') as string;
182+
defaultFields.profileId = getString(response, 'records[0].Id');
178183
delete defaultFields['profileName'];
179184
}
180185

src/commands/force/user/display.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as os from 'os';
99
import { SfdxCommand } from '@salesforce/command';
1010
import { Aliases, AuthFields, AuthInfo, Connection, Logger, Messages } from '@salesforce/core';
11-
import { get } from '@salesforce/ts-types';
11+
import { getString } from '@salesforce/ts-types';
1212

1313
Messages.importMessagesDirectory(__dirname);
1414
const messages = Messages.loadMessages('@salesforce/plugin-user', 'display');
@@ -50,24 +50,24 @@ export class UserDisplayCommand extends SfdxCommand {
5050
// the user executing this command may not have access to the Profile sObject.
5151
if (!profileName) {
5252
const PROFILE_NAME_QUERY = `SELECT name FROM Profile WHERE Id IN (SELECT ProfileId FROM User WHERE username='${username}')`;
53-
profileName = get(await conn.query(PROFILE_NAME_QUERY), 'records[0].Name') as string;
53+
profileName = getString(await conn.query(PROFILE_NAME_QUERY), 'records[0].Name');
5454
}
5555
} catch (err) {
5656
profileName = 'unknown';
5757
this.logger.debug(
58-
`Query for the profile name failed for username: ${username} with message: ${get(err, 'message') as string}`
58+
`Query for the profile name failed for username: ${username} with message: ${getString(err, 'message')}`
5959
);
6060
}
6161

6262
try {
6363
if (!userId) {
6464
const USER_QUERY = `SELECT id FROM User WHERE username='${username}'`;
65-
userId = get(await conn.query(USER_QUERY), 'records[0].Id') as string;
65+
userId = getString(await conn.query(USER_QUERY), 'records[0].Id');
6666
}
6767
} catch (err) {
6868
userId = 'unknown';
6969
this.logger.debug(
70-
`Query for the user ID failed for username: ${username} with message: ${get(err, 'message') as string}`
70+
`Query for the user ID failed for username: ${username} with message: ${getString(err, 'message')}`
7171
);
7272
}
7373

test/commands/user/create.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,62 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8+
/* eslint-disable @typescript-eslint/ban-ts-ignore */
9+
810
import { $$, expect, test } from '@salesforce/command/lib/test';
911
import { Aliases, Connection, DefaultUserFields, fs, Org, User } from '@salesforce/core';
1012
import { stubMethod } from '@salesforce/ts-sinon';
13+
import { IConfig } from '@oclif/config';
14+
import UserCreateCommand from '../../../src/commands/force/user/create';
1115

1216
const username = 'defaultusername@test.com';
1317

1418
describe('force:user:create', () => {
19+
it('will properly merge fields regardless of capitalization', async () => {
20+
// notice the varied capitalization
21+
stubMethod($$.SANDBOX, fs, 'readJson').resolves({
22+
id: '0052D0000043PawWWR',
23+
Username: '1605130295132_test-j6asqt5qoprs@example.com',
24+
Alias: 'testAlias',
25+
Email: username,
26+
EmailEncodingKey: 'UTF-8',
27+
LanguageLocaleKey: 'en_US',
28+
localeSidKey: 'en_US',
29+
ProfileId: '00e2D000000bNexWWR',
30+
LastName: 'User',
31+
timeZoneSidKey: 'America/Los_Angeles',
32+
});
33+
34+
const createCommand = new UserCreateCommand(['-f', 'userConfig.json'], {} as IConfig);
35+
36+
// @ts-ignore
37+
createCommand.flags = { definitionfile: 'testing' };
38+
39+
// @ts-ignore private method
40+
const res = await createCommand.aggregateFields({
41+
id: '00555000006lCspAAE',
42+
emailEncodingKey: 'UTF-8',
43+
languageLocaleKey: 'en_US',
44+
localeSidKey: 'en_US',
45+
profileId: '00e55000000fvDdAAI',
46+
lastName: 'User',
47+
timeZoneSidKey: 'America/Los_Angeles',
48+
});
49+
50+
expect(res).to.deep.equal({
51+
alias: 'testAlias',
52+
email: 'defaultusername@test.com',
53+
emailEncodingKey: 'UTF-8',
54+
id: '0052D0000043PawWWR',
55+
languageLocaleKey: 'en_US',
56+
lastName: 'User',
57+
localeSidKey: 'en_US',
58+
profileId: '00e2D000000bNexWWR',
59+
timeZoneSidKey: 'America/Los_Angeles',
60+
username: '1605130295132_test-j6asqt5qoprs@example.com',
61+
});
62+
});
63+
1564
async function prepareStubs(throws: { license?: boolean; duplicate?: boolean } = {}, readsFile = false) {
1665
stubMethod($$.SANDBOX, Org.prototype, 'getConnection').callsFake(() => Connection.prototype);
1766
stubMethod($$.SANDBOX, DefaultUserFields, 'create').resolves({

0 commit comments

Comments
 (0)