Skip to content

Commit

Permalink
fix: explain v51 setPassword changes on command errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jan 28, 2021
1 parent 18412a1 commit 2dd3ecc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion messages/password.generate.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
"flags": {
"onBehalfOf": "comma-separated list of usernames or aliases to assign the password to"
},
"noSelfSetError": "Create a scratch org with the enableSetPasswordInApi org security setting set to TRUE and try again.",
"noSelfSetErrorV50": "Create a scratch org with the enableSetPasswordInApi org security setting set to TRUE and try again.",

"noSelfSetError": "Beginning in version 51, enableSetPasswordInApi is a feature in your scratch org definition file. Settings.securitySettings.passwordPolicies.enableSetPasswordInApi is deprecated.",
"noSelfSetErrorAction": "Update your scratch org definition file and try creating the org again",
"scratchFeaturesUrl": "see https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_scratch_orgs_def_file_config_values.htm",
"success": "Successfully set the password \"%s\" for user %s.",
"successMultiple": "Successfully set passwords:%s",
"viewWithCommand": "You can see the password again by running \"sfdx force:user:display -u %s\"."
Expand Down
19 changes: 16 additions & 3 deletions src/commands/force/user/password/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class UserPasswordGenerateCommand extends SfdxCommand {
private passwordData: PasswordData[] = [];

public async run(): Promise<PasswordData[]> {
this.usernames = this.flags.onbehalfof ?? this.org.getUsername();
this.usernames = this.flags.onbehalfof ?? [this.org.getUsername()];

for (const username of this.usernames) {
try {
Expand All @@ -56,8 +56,21 @@ export class UserPasswordGenerateCommand extends SfdxCommand {
});
await authInfo.save();
} catch (e) {
if (e.message.includes('Cannot set password for self')) {
throw SfdxError.create('@salesforce/plugin-user', 'password.generate', 'noSelfSetError');
if (
e.message.includes('Cannot set password for self') ||
e.message.includes('The requested Resource does not exist')
) {
// we don't have access to the apiVersion from what happened in the try, so until v51 is r2, we have to check versions the hard way
const authInfo: AuthInfo = await AuthInfo.create({ username });
const connection: Connection = await Connection.create({ authInfo });
const org = await Org.create({ connection });
if (parseInt(await org.retrieveMaxApiVersion(), 10) >= 51) {
throw new SfdxError(messages.getMessage('noSelfSetError'), 'noSelfSetError', [
messages.getMessage('noSelfSetErrorAction'),
messages.getMessage('scratchFeaturesUrl'),
]);
}
throw new SfdxError(messages.getMessage('noSelfSetErrorV50'), 'noSelfSetError');
}
throw SfdxError.wrap(e);
}
Expand Down
1 change: 1 addition & 0 deletions test/commands/user/password/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('force:user:password:generate', () => {
stubMethod($$.SANDBOX, Connection, 'create').callsFake(async () => Connection.prototype);
stubMethod($$.SANDBOX, Org, 'create').callsFake(async () => Org.prototype);
stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns('[email protected]');
stubMethod($$.SANDBOX, Org.prototype, 'retrieveMaxApiVersion').returns('51.0');
stubMethod($$.SANDBOX, User, 'create').callsFake(async () => User.prototype);
stubMethod($$.SANDBOX, User.prototype, 'retrieve').resolves({
id: '0052D0000043PawWWR',
Expand Down

0 comments on commit 2dd3ecc

Please sign in to comment.