Skip to content

Commit 2df9a76

Browse files
authored
fix: organization creation response handling and add ID validation (#1365)
* feat: Enhance organization creation and testing - src/tools/auth0/handlers/organizations.ts: Add error handling for missing organization ID on creation. - src/tools/auth0/handlers/organizations.ts: Refactor to use createdId for connection and grant associations. - test/tools/auth0/handlers/organizations.tests.js: Simplify promise resolution in test cases. * fix: Improve error handling in OrganizationsHandler - src/tools/auth0/handlers/organizations.ts: Replace log error with throw for missing organization ID * fix: Prevent create method from being called during organization deletion - test/tools/auth0/handlers/organizations.tests.js: throw error in create method when deleting organizations - test/tools/auth0/handlers/organizations.tests.js: update stage function to handle empty organizations array
1 parent d12a2ff commit 2df9a76

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/tools/auth0/handlers/organizations.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,20 @@ export default class OrganizationsHandler extends DefaultHandler {
145145
delete organization.discovery_domains;
146146
}
147147

148-
const { data: created } = await this.client.organizations.create(organization);
148+
const created = await this.client.organizations.create(organization);
149+
150+
if (!created.id) {
151+
throw new Error(
152+
`Organization "${organization.name}" was created but the response did not include an ID. Skipping connection/grant association.`
153+
);
154+
}
155+
156+
const createdId = created.id;
149157

150158
if (typeof org.connections !== 'undefined' && org.connections.length > 0) {
151159
await Promise.all(
152160
org.connections.map((conn) =>
153-
this.client.organizations.enabledConnections.add(created.id, conn)
161+
this.client.organizations.enabledConnections.add(createdId, conn)
154162
)
155163
);
156164
}
@@ -159,7 +167,7 @@ export default class OrganizationsHandler extends DefaultHandler {
159167
await Promise.all(
160168
org.client_grants.map((organizationClientGrants) =>
161169
this.createOrganizationClientGrants(
162-
created.id,
170+
createdId,
163171
this.getClientGrantIDByClientName(organizationClientGrants.client_id)
164172
)
165173
)
@@ -173,13 +181,13 @@ export default class OrganizationsHandler extends DefaultHandler {
173181
generator: (
174182
discoveryDomain: Management.CreateOrganizationDiscoveryDomainRequestContent
175183
) =>
176-
this.createOrganizationDiscoveryDomain(created.id, {
184+
this.createOrganizationDiscoveryDomain(createdId, {
177185
domain: discoveryDomain?.domain,
178186
status: discoveryDomain?.status,
179187
use_for_organization_discovery: discoveryDomain?.use_for_organization_discovery,
180188
}).catch((err) => {
181189
throw new Error(
182-
`Problem creating discovery domain ${discoveryDomain?.domain} for organization ${created.id}\n${err}`
190+
`Problem creating discovery domain ${discoveryDomain?.domain} for organization ${createdId}\n${err}`
183191
);
184192
}),
185193
})

test/tools/auth0/handlers/organizations.tests.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('#organizations handler', () => {
156156
expect(data.display_name).to.equal('Acme');
157157
expect(data.connections).to.equal(undefined);
158158
data.id = 'fake';
159-
return Promise.resolve({ data });
159+
return Promise.resolve(data);
160160
},
161161
update: () => Promise.resolve([]),
162162
delete: () => Promise.resolve([]),
@@ -275,7 +275,7 @@ describe('#organizations handler', () => {
275275
},
276276
});
277277
data.id = 'fake';
278-
return Promise.resolve({ data });
278+
return Promise.resolve(data);
279279
},
280280
update: () => Promise.resolve({ data: [] }),
281281
delete: () => Promise.resolve({ data: [] }),
@@ -893,7 +893,9 @@ describe('#organizations handler', () => {
893893
it('should delete organizations', async () => {
894894
const auth0 = {
895895
organizations: {
896-
create: () => Promise.resolve([]),
896+
create: () => {
897+
throw new Error('create should not be called when deleting organizations');
898+
},
897899
update: () => Promise.resolve([]),
898900
delete: (orgId) => {
899901
expect(orgId).to.equal(sampleOrg.id);
@@ -923,7 +925,7 @@ describe('#organizations handler', () => {
923925
};
924926
const handler = new organizations.default({ client: pageClient(auth0), config });
925927
const stageFn = Object.getPrototypeOf(handler).processChanges;
926-
await stageFn.apply(handler, [{ organizations: [{}] }]);
928+
await stageFn.apply(handler, [{ organizations: [] }]);
927929
});
928930

929931
it('should create organization with discovery domains', async () => {
@@ -935,7 +937,7 @@ describe('#organizations handler', () => {
935937
expect(data.name).to.equal('acme');
936938
expect(data.discovery_domains).to.equal(undefined);
937939
data.id = 'fake';
938-
return Promise.resolve({ data });
940+
return Promise.resolve(data);
939941
},
940942
update: () => Promise.resolve({ data: [] }),
941943
delete: () => Promise.resolve({ data: [] }),

0 commit comments

Comments
 (0)