Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/tools/auth0/handlers/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of silently returning here, should we not throw the error?
As the current behaviour creates partial state(org exists, connection missing) with no error propagation

);
}

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)
)
);
}
Expand All @@ -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)
)
)
Expand All @@ -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}`
);
}),
})
Expand Down
12 changes: 7 additions & 5 deletions test/tools/auth0/handlers/organizations.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]),
Expand Down Expand Up @@ -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: [] }),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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: [] }),
Expand Down