From 99315fabff15bd52c680b20409142a042a61751b Mon Sep 17 00:00:00 2001 From: Steve Hetzel Date: Thu, 11 Jan 2024 10:46:20 -0700 Subject: [PATCH] fix: handle missing createdOrgInstance field --- src/commands/org/create/user.ts | 5 ++--- test/commands/create.test.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/commands/org/create/user.ts b/src/commands/org/create/user.ts index daff41a6..9e98e7ee 100644 --- a/src/commands/org/create/user.ts +++ b/src/commands/org/create/user.ts @@ -7,7 +7,6 @@ import { EOL } from 'node:os'; import fs from 'node:fs'; - import { AuthInfo, Connection, @@ -34,7 +33,7 @@ import { } from '@salesforce/sf-plugins-core'; import { Interfaces } from '@oclif/core'; -Messages.importMessagesDirectoryFromMetaUrl(import.meta.url) +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); const messages = Messages.loadMessages('@salesforce/plugin-user', 'create'); type SuccessMsg = { @@ -320,7 +319,7 @@ const getValidatedConnection = async (targetOrg: Org, apiVersion?: string): Prom if ( conn.getAuthInfo().isJwt() && // hyperforce sandbox instances end in S like USA254S - targetOrg.getField(Org.Fields.CREATED_ORG_INSTANCE).endsWith('S') + targetOrg.getField(Org.Fields.CREATED_ORG_INSTANCE)?.endsWith('S') ) { throw messages.createError('error.jwtHyperforce'); } diff --git a/test/commands/create.test.ts b/test/commands/create.test.ts index 79625ce7..ebed34f2 100644 --- a/test/commands/create.test.ts +++ b/test/commands/create.test.ts @@ -19,6 +19,8 @@ const username = 'defaultusername@test.com'; const originalUserId = '0052D0000043PawWWR'; const newUserId = '0052D0000044PawWWR'; +const createdOrgInstanceMissing = 'MISSING'; + describe('org:create:user', () => { const $$ = new TestContext(); @@ -121,7 +123,9 @@ describe('org:create:user', () => { $$.SANDBOX.stub(Org.prototype, 'getOrgId').returns(testOrg.orgId); $$.SANDBOX.stub(Org.prototype, 'getField') .withArgs(Org.Fields.CREATED_ORG_INSTANCE) - .returns(throws.createdOrgInstance ?? testOrg.orgId); + .returns( + throws.createdOrgInstance === createdOrgInstanceMissing ? undefined : throws.createdOrgInstance ?? testOrg.orgId + ); $$.SANDBOX.stub(Org.prototype, 'determineIfScratch').resolves(throws.nonScratch ? false : true); if (throws.license) { @@ -351,6 +355,30 @@ describe('org:create:user', () => { const result = await createCommand.run(); expect(result).to.deep.equal(expected); }); + + it('works if JWT but missing createdOrgInstance auth field', async () => { + await prepareStubs({ isJWT: true, createdOrgInstance: createdOrgInstanceMissing }, true); + const expected = { + orgId: testOrg.orgId, + permissionSetAssignments: [], + fields: { + alias: 'testAlias', + email: username, + emailencodingkey: 'UTF-8', + id: newUserId, + languagelocalekey: 'en_US', + lastname: 'User', + localesidkey: 'en_US', + profileid: '12345678', + profilename: 'profileFromArgs', + timezonesidkey: 'America/Los_Angeles', + username: '1605130295132_test-j6asqt5qoprs@example.com', + }, + }; + const createCommand = new CreateUserCommand(['--json', '--target-org', testOrg.username], {} as Config); + const result = await createCommand.run(); + expect(result).to.deep.equal(expected); + }); }); describe('exceptions', () => {