diff --git a/src/commands/force/user/create.ts b/src/commands/force/user/create.ts index a7042541..c7a54b93 100644 --- a/src/commands/force/user/create.ts +++ b/src/commands/force/user/create.ts @@ -10,7 +10,7 @@ import { AuthInfo, Connection, DefaultUserFields, - GlobalInfo, + StateAggregator, Logger, Messages, REQUIRED_FIELDS, @@ -144,9 +144,9 @@ export class UserCreateCommand extends SfdxCommand { // Set the alias if specified if (this.flags.setalias && typeof this.flags.setalias === 'string') { - const globalInfo = await GlobalInfo.getInstance(); - globalInfo.aliases.set(this.flags.setalias, fields.username); - await globalInfo.write(); + const stateAggregator = await StateAggregator.getInstance(); + stateAggregator.aliases.set(this.flags.setalias, fields.username); + await stateAggregator.aliases.write(); } fields.id = this.authInfo.getFields().userId; diff --git a/src/commands/force/user/display.ts b/src/commands/force/user/display.ts index 282d8b66..89c094d8 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 { AuthFields, AuthInfo, Connection, Logger, Messages, SfError, sfdc, GlobalInfo } from '@salesforce/core'; +import { AuthFields, AuthInfo, Connection, Logger, Messages, SfError, sfdc, StateAggregator } from '@salesforce/core'; import { getString } from '@salesforce/ts-types'; Messages.importMessagesDirectory(__dirname); @@ -85,8 +85,8 @@ export class UserDisplayCommand extends SfdxCommand { username, }; - const globalInfo = await GlobalInfo.getInstance(); - const alias = globalInfo.aliases.get(username); + const stateAggregator = await StateAggregator.getInstance(); + const alias = stateAggregator.aliases.get(username); if (alias) { result.alias = alias; } diff --git a/src/commands/force/user/list.ts b/src/commands/force/user/list.ts index 490f77de..8288b38f 100644 --- a/src/commands/force/user/list.ts +++ b/src/commands/force/user/list.ts @@ -6,7 +6,7 @@ */ import * as os from 'os'; import { SfdxCommand } from '@salesforce/command'; -import { Messages, Connection, GlobalInfo } from '@salesforce/core'; +import { Messages, Connection, StateAggregator } from '@salesforce/core'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/plugin-user', 'list'); @@ -43,7 +43,7 @@ export class UserListCommand extends SfdxCommand { this.buildUserInfos(), this.buildProfileInfos(), this.org.readUserAuthFiles(), - (await GlobalInfo.getInstance()).aliases, + (await StateAggregator.getInstance()).aliases, ]); const authList: AuthList[] = userAuthData.map((authData) => { diff --git a/src/commands/force/user/password/generate.ts b/src/commands/force/user/password/generate.ts index 0c9238a3..f82cdbec 100644 --- a/src/commands/force/user/password/generate.ts +++ b/src/commands/force/user/password/generate.ts @@ -6,7 +6,7 @@ */ import * as os from 'os'; import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command'; -import { AuthInfo, Connection, GlobalInfo, Messages, Org, SfError, User } from '@salesforce/core'; +import { AuthInfo, Connection, StateAggregator, Messages, Org, SfError, User } from '@salesforce/core'; import { PasswordConditions } from '@salesforce/core/lib/org/user'; import { asNumber } from '@salesforce/ts-types'; Messages.importMessagesDirectory(__dirname); @@ -60,7 +60,7 @@ export class UserPasswordGenerateCommand extends SfdxCommand { try { // Convert any aliases to usernames // fetch will return undefined if there's no Alias for that name - const username = (await GlobalInfo.getInstance()).aliases.resolveUsername(aliasOrUsername); + const username = (await StateAggregator.getInstance()).aliases.resolveUsername(aliasOrUsername); const authInfo: AuthInfo = await AuthInfo.create({ username }); const connection: Connection = await Connection.create({ authInfo }); diff --git a/src/commands/force/user/permset/assign.ts b/src/commands/force/user/permset/assign.ts index 7e4aa7ed..e8fdd1d0 100644 --- a/src/commands/force/user/permset/assign.ts +++ b/src/commands/force/user/permset/assign.ts @@ -7,7 +7,7 @@ import * as os from 'os'; import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command'; -import { Connection, GlobalInfo, Messages, Org, SfError, User } from '@salesforce/core'; +import { Connection, StateAggregator, Messages, Org, SfError, User } from '@salesforce/core'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/plugin-user', 'permset.assign'); @@ -54,7 +54,7 @@ export class UserPermsetAssignCommand extends SfdxCommand { for (const aliasOrUsername of aliasOrUsernames) { // Attempt to convert any aliases to usernames. Not found alias will be **assumed** to be a username - const username = (await GlobalInfo.getInstance()).aliases.resolveUsername(aliasOrUsername); + const username = (await StateAggregator.getInstance()).aliases.resolveUsername(aliasOrUsername); const user: User = await User.create({ org }); // get userId of whomever the permset will be assigned to via query to avoid AuthInfo if remote user const queryResult = await connection.singleRecordQuery<{ Id: string }>( diff --git a/src/commands/force/user/permsetlicense/assign.ts b/src/commands/force/user/permsetlicense/assign.ts index 92e73b61..c0d5d6a9 100644 --- a/src/commands/force/user/permsetlicense/assign.ts +++ b/src/commands/force/user/permsetlicense/assign.ts @@ -7,7 +7,7 @@ import * as os from 'os'; import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command'; -import { GlobalInfo, Messages, SfError } from '@salesforce/core'; +import { StateAggregator, Messages, SfError } from '@salesforce/core'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/plugin-user', 'permsetlicense.assign'); @@ -54,7 +54,6 @@ export class UserPermsetLicenseAssignCommand extends SfdxCommand { const usernamesOrAliases = (this.flags.onbehalfof as string[]) ?? [this.org.getUsername()]; this.logger.debug(`will assign permset to users: ${usernamesOrAliases.join(', ')}`); const pslName = this.flags.name as string; - const conn = this.org.getConnection(); try { this.pslId = ( @@ -95,7 +94,7 @@ export class UserPermsetLicenseAssignCommand extends SfdxCommand { usernameOrAlias: string; }): Promise { // Convert any aliases to usernames - const resolvedUsername = (await GlobalInfo.getInstance()).aliases.resolveUsername(usernameOrAlias); + const resolvedUsername = (await StateAggregator.getInstance()).aliases.resolveUsername(usernameOrAlias); try { const AssigneeId = ( diff --git a/test/commands/user/create.test.ts b/test/commands/user/create.test.ts index 80d0edb1..fa7f27e5 100644 --- a/test/commands/user/create.test.ts +++ b/test/commands/user/create.test.ts @@ -9,7 +9,7 @@ import { $$, expect, test } from '@salesforce/command/lib/test'; import * as fse from 'fs-extra'; -import { AuthInfo, Connection, DefaultUserFields, GlobalInfo, Logger, Org, User, UserFields } from '@salesforce/core'; +import { AuthInfo, Connection, DefaultUserFields, Logger, Org, User, UserFields } from '@salesforce/core'; import { stubMethod } from '@salesforce/ts-sinon'; import { Config } from '@oclif/core'; import UserCreateCommand from '../../../src/commands/force/user/create'; @@ -118,9 +118,8 @@ describe('force:user:create', () => { stubMethod($$.SANDBOX, Logger.prototype, 'debug'); stubMethod($$.SANDBOX, fse, 'readJson').resolves(readsFile); } - stubMethod($$.SANDBOX, GlobalInfo, 'getInstance').resolves({ - aliases: { resolveUsername: () => 'testAlias' }, - }); + + $$.stubAliases({ testAlias: '1605130295132_test-j6asqt5qoprs@example.com' }); } test .do(async () => { diff --git a/test/commands/user/display.test.ts b/test/commands/user/display.test.ts index bb2da00a..f012eb6e 100644 --- a/test/commands/user/display.test.ts +++ b/test/commands/user/display.test.ts @@ -6,7 +6,7 @@ */ import { $$, expect, test } from '@salesforce/command/lib/test'; -import { Connection, GlobalInfo, Org } from '@salesforce/core'; +import { Connection, Org } from '@salesforce/core'; import { stubMethod } from '@salesforce/ts-sinon'; const username = 'defaultusername@test.com'; @@ -53,9 +53,7 @@ describe('force:user:display', () => { ]); } - stubMethod($$.SANDBOX, GlobalInfo, 'getInstance').resolves({ - aliases: { get: () => 'testAlias' }, - }); + $$.stubAliases({ testAlias: 'defaultusername@test.com' }); } test diff --git a/test/commands/user/list.test.ts b/test/commands/user/list.test.ts index 650175dc..11daa14a 100644 --- a/test/commands/user/list.test.ts +++ b/test/commands/user/list.test.ts @@ -6,7 +6,7 @@ */ import { $$, expect, test } from '@salesforce/command/lib/test'; -import { Connection, GlobalInfo, Org } from '@salesforce/core'; +import { Connection, Org } from '@salesforce/core'; import { stubMethod } from '@salesforce/ts-sinon'; const user1 = 'defaultusername@test.com'; @@ -70,9 +70,7 @@ describe('force:user:list', () => { ]); stubMethod($$.SANDBOX, Org.prototype, 'getOrgId').returns('abc123'); stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns(user1); - stubMethod($$.SANDBOX, GlobalInfo, 'getInstance').resolves({ - aliases: { get: (arg: string) => (arg === user1 ? 'testAlias' : undefined) }, - }); + $$.stubAliases({ testAlias: user1 }); stubMethod($$.SANDBOX, Connection.prototype, 'query') .withArgs('SELECT username, profileid, id FROM User') .resolves({ diff --git a/test/commands/user/password/generate.test.ts b/test/commands/user/password/generate.test.ts index 73c1c352..bddce7b6 100644 --- a/test/commands/user/password/generate.test.ts +++ b/test/commands/user/password/generate.test.ts @@ -6,7 +6,7 @@ */ import { $$, expect, test } from '@salesforce/command/lib/test'; -import { AuthInfo, Connection, Org, User, Messages, GlobalInfo } from '@salesforce/core'; +import { AuthInfo, Connection, Org, User, Messages } from '@salesforce/core'; import { StubbedType, stubInterface, stubMethod } from '@salesforce/ts-sinon'; import { MockTestOrgData } from '@salesforce/core/lib/testSetup'; import { SecureBuffer } from '@salesforce/core/lib/crypto/secureBuffer'; @@ -43,9 +43,8 @@ describe('force:user:password:generate', () => { } else { stubMethod($$.SANDBOX, User.prototype, 'assignPassword').resolves(); } - stubMethod($$.SANDBOX, GlobalInfo, 'getInstance').resolves({ - aliases: { resolveUsername: (arg: string) => (arg === 'testUser1@test.com' ? 'testAlias' : arg) }, - }); + + $$.stubAliases({ testAlias: 'testUser1@test.com' }); } test @@ -55,10 +54,9 @@ describe('force:user:password:generate', () => { .stdout() .command(['force:user:password:generate', '--json', '--onbehalfof', 'testUser1@test.com, testUser2@test.com']) .it('should generate a new password for the user', (ctx) => { - // testUser1@test.com is aliased to testUser const expected = [ { - username: 'testAlias', + username: 'testUser1@test.com', password: 'abc', }, { diff --git a/test/commands/user/permset/assign.test.ts b/test/commands/user/permset/assign.test.ts index 8ac61a2c..bbcc0e7b 100644 --- a/test/commands/user/permset/assign.test.ts +++ b/test/commands/user/permset/assign.test.ts @@ -6,7 +6,7 @@ */ import { $$, expect, test } from '@salesforce/command/lib/test'; -import { Connection, GlobalInfo, Org, User } from '@salesforce/core'; +import { Connection, Org, User } from '@salesforce/core'; import { stubMethod } from '@salesforce/ts-sinon'; describe('force:user:permset:assign', () => { @@ -23,9 +23,7 @@ describe('force:user:permset:assign', () => { } else { stubMethod($$.SANDBOX, User.prototype, 'assignPermissionSets').resolves(); } - stubMethod($$.SANDBOX, GlobalInfo, 'getInstance').resolves({ - aliases: { resolveUsername: (arg: string) => (arg === 'testAlias' ? 'testAlias' : 'testUser2@test.com') }, - }); + $$.stubAliases({ testAlias: 'testUser2@test.com' }); } test diff --git a/test/commands/user/permsetlicense/assign.test.ts b/test/commands/user/permsetlicense/assign.test.ts index 43a9477a..1da98579 100644 --- a/test/commands/user/permsetlicense/assign.test.ts +++ b/test/commands/user/permsetlicense/assign.test.ts @@ -4,9 +4,8 @@ * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ -import * as Sinon from 'sinon'; import { $$, expect, test } from '@salesforce/command/lib/test'; -import { AuthInfo, Connection, GlobalInfo, Org } from '@salesforce/core'; +import { AuthInfo, Connection, Org } from '@salesforce/core'; import { StubbedType, stubInterface, stubMethod } from '@salesforce/ts-sinon'; import { MockTestOrgData } from '@salesforce/core/lib/testSetup'; @@ -28,16 +27,15 @@ describe('force:user:permsetlicense:assign', () => { stubMethod($$.SANDBOX, Org, 'create').callsFake(async () => Org.prototype); stubMethod($$.SANDBOX, Org.prototype, 'getConnection').returns(Connection.prototype); stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns(defaultUsername); - stubMethod($$.SANDBOX, GlobalInfo, 'getInstance').resolves({ - aliases: { resolveUsername: (arg: string) => (arg === username1 ? 'testAlias' : username2) }, - }); + + $$.stubAliases({ testAlias: username1 }); stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery') // matcher for all user queries - .withArgs(Sinon.match((arg: string) => arg.startsWith('select Id from User'))) - .resolves({ - Id: '0051234567890123', - }) + .withArgs(`select Id from User where Username = '${username1}'`) + .resolves({ Id: '0051234567890123' }) + .withArgs(`select Id from User where Username = '${username2}'`) + .resolves({ Id: '0051234567890123' }) .withArgs(`select Id from PermissionSetLicense where DeveloperName = '${goodPSL}' or MasterLabel = '${goodPSL}'`) .resolves({ Id: '0PL46000000gHHsGAM', @@ -68,10 +66,9 @@ describe('force:user:permsetlicense:assign', () => { goodPSL, ]) .it('should assign the one permset to both users', (ctx) => { - // testUser1@test.com is aliased to testUser const expected = [ { - name: 'testAlias', + name: username1, value: goodPSL, }, { @@ -79,6 +76,7 @@ describe('force:user:permsetlicense:assign', () => { value: goodPSL, }, ]; + const result = JSON.parse(ctx.stdout).result; expect(result.failures).to.deep.equal([]); expect(result.successes).to.deep.equal(expected);