diff --git a/messages/display.json b/messages/display.json index 23017d32..43e19bdf 100644 --- a/messages/display.json +++ b/messages/display.json @@ -1,4 +1,6 @@ { "description": "displays information about a user of a scratch org", - "examples": ["sfdx force:user:display", "sfdx force:user:display -u me@my.org --json"] + "examples": ["sfdx force:user:display", "sfdx force:user:display -u me@my.org --json"], + "accessTokenError": "This command does not accept an access token for a username", + "accessTokenAction": "Use a username or an alias" } diff --git a/src/commands/force/user/display.ts b/src/commands/force/user/display.ts index 2d13350e..35b9f2ee 100644 --- a/src/commands/force/user/display.ts +++ b/src/commands/force/user/display.ts @@ -7,7 +7,7 @@ import * as os from 'os'; import { SfdxCommand } from '@salesforce/command'; -import { Aliases, AuthFields, AuthInfo, Connection, Logger, Messages } from '@salesforce/core'; +import { Aliases, AuthFields, AuthInfo, Connection, Logger, Messages, SfdxError, sfdc } from '@salesforce/core'; import { getString } from '@salesforce/ts-types'; Messages.importMessagesDirectory(__dirname); @@ -34,7 +34,11 @@ export class UserDisplayCommand extends SfdxCommand { public async run(): Promise { this.logger = await Logger.child(this.constructor.name); - + if (sfdc.matchesAccessToken(this.flags.targetusername)) { + throw new SfdxError(messages.getMessage('accessTokenError'), 'accessTokenError', [ + messages.getMessage('accessTokenAction'), + ]); + } const username: string = this.org.getUsername(); const userAuthDataArray: AuthInfo[] = await this.org.readUserAuthFiles(); // userAuthDataArray contains all of the Org's users AuthInfo, we just need the default or -u, which is in the username variable diff --git a/test/commands/user/display.test.ts b/test/commands/user/display.test.ts index 717787ce..c0e25a46 100644 --- a/test/commands/user/display.test.ts +++ b/test/commands/user/display.test.ts @@ -114,4 +114,20 @@ describe('force:user:display', () => { const result = JSON.parse(ctx.stdout).result; expect(result).to.deep.equal(expected); }); + + test + .stdout() + .command([ + 'force:user:display', + '--json', + '--targetusername', + '00D54000000GxYk!ARwAQOZEBxYvKxKlNJfvJEGyj7fNj6TA61Fn5RxJzaYm79hR9IYjx2x147a2GH2DGtne21DW.g_8DD0rzNF.COIAXcmq0FfJ', + '--targetdevhubusername', + 'devhub@test.com', + ]) + .it('should throw when username is an accessToken', (ctx) => { + const response = JSON.parse(ctx.stdout); + expect(response.status).to.equal(1); + expect(response.name).to.equal('accessTokenError'); + }); });