diff --git a/src/tools/auth0/handlers/organizations.ts b/src/tools/auth0/handlers/organizations.ts index f6b81f920..98aa8ae0e 100644 --- a/src/tools/auth0/handlers/organizations.ts +++ b/src/tools/auth0/handlers/organizations.ts @@ -145,12 +145,20 @@ export default class OrganizationsHandler extends DefaultHandler { delete organization.discovery_domains; } - const { data: created } = await this.client.organizations.create(organization); + const created = await this.client.organizations.create(organization); + + if (!created.id) { + throw new Error( + `Organization "${organization.name}" was created but the response did not include an ID. Skipping connection/grant association.` + ); + } + + const createdId = created.id; if (typeof org.connections !== 'undefined' && org.connections.length > 0) { await Promise.all( org.connections.map((conn) => - this.client.organizations.enabledConnections.add(created.id, conn) + this.client.organizations.enabledConnections.add(createdId, conn) ) ); } @@ -159,7 +167,7 @@ export default class OrganizationsHandler extends DefaultHandler { await Promise.all( org.client_grants.map((organizationClientGrants) => this.createOrganizationClientGrants( - created.id, + createdId, this.getClientGrantIDByClientName(organizationClientGrants.client_id) ) ) @@ -173,13 +181,13 @@ export default class OrganizationsHandler extends DefaultHandler { generator: ( discoveryDomain: Management.CreateOrganizationDiscoveryDomainRequestContent ) => - this.createOrganizationDiscoveryDomain(created.id, { + this.createOrganizationDiscoveryDomain(createdId, { domain: discoveryDomain?.domain, status: discoveryDomain?.status, use_for_organization_discovery: discoveryDomain?.use_for_organization_discovery, }).catch((err) => { throw new Error( - `Problem creating discovery domain ${discoveryDomain?.domain} for organization ${created.id}\n${err}` + `Problem creating discovery domain ${discoveryDomain?.domain} for organization ${createdId}\n${err}` ); }), }) diff --git a/test/tools/auth0/handlers/organizations.tests.js b/test/tools/auth0/handlers/organizations.tests.js index 6124fd29e..9256d5f2a 100644 --- a/test/tools/auth0/handlers/organizations.tests.js +++ b/test/tools/auth0/handlers/organizations.tests.js @@ -156,7 +156,7 @@ describe('#organizations handler', () => { expect(data.display_name).to.equal('Acme'); expect(data.connections).to.equal(undefined); data.id = 'fake'; - return Promise.resolve({ data }); + return Promise.resolve(data); }, update: () => Promise.resolve([]), delete: () => Promise.resolve([]), @@ -275,7 +275,7 @@ describe('#organizations handler', () => { }, }); data.id = 'fake'; - return Promise.resolve({ data }); + return Promise.resolve(data); }, update: () => Promise.resolve({ data: [] }), delete: () => Promise.resolve({ data: [] }), @@ -893,7 +893,9 @@ describe('#organizations handler', () => { it('should delete organizations', async () => { const auth0 = { organizations: { - create: () => Promise.resolve([]), + create: () => { + throw new Error('create should not be called when deleting organizations'); + }, update: () => Promise.resolve([]), delete: (orgId) => { expect(orgId).to.equal(sampleOrg.id); @@ -923,7 +925,7 @@ describe('#organizations handler', () => { }; const handler = new organizations.default({ client: pageClient(auth0), config }); const stageFn = Object.getPrototypeOf(handler).processChanges; - await stageFn.apply(handler, [{ organizations: [{}] }]); + await stageFn.apply(handler, [{ organizations: [] }]); }); it('should create organization with discovery domains', async () => { @@ -935,7 +937,7 @@ describe('#organizations handler', () => { expect(data.name).to.equal('acme'); expect(data.discovery_domains).to.equal(undefined); data.id = 'fake'; - return Promise.resolve({ data }); + return Promise.resolve(data); }, update: () => Promise.resolve({ data: [] }), delete: () => Promise.resolve({ data: [] }),