diff --git a/SUPPORTED.md b/SUPPORTED.md index c9a121f..d14282c 100644 --- a/SUPPORTED.md +++ b/SUPPORTED.md @@ -21,7 +21,7 @@ in [Seed data](docs/seeding.md). -The emulator implements **209 of 269** endpoints in the WorkOS OpenAPI spec (`@workos/openapi-spec@0.160.0`) (**77.7%**). +The emulator implements **210 of 269** endpoints in the WorkOS OpenAPI spec (`@workos/openapi-spec@0.160.0`) (**78.1%**). | Feature | Read | Write | Set up | | --------------------------------------------------------------------- | -------- | -------- | ------------------------------ | @@ -34,7 +34,7 @@ The emulator implements **209 of 269** endpoints in the WorkOS OpenAPI spec (`@w | [SSO](docs/behavior.md#sso) | ⚠️ 5/8 | ⚠️ 4/11 | ✅ seed `connections` | | [Directory Sync](docs/behavior.md#directory-sync) | ✅ 6/6 | ⚠️ 1/2 | ✅ seed `directories` | | [Multi-Factor Auth](docs/behavior.md#multi-factor-auth) | ✅ 2/2 | ✅ 5/5 | ⚠️ API only | -| [FGA / Authorization](docs/behavior.md#fga--authorization) | ✅ 19/19 | ⚠️ 24/27 | ✅ seed `roles`, `permissions` | +| [FGA / Authorization](docs/behavior.md#fga--authorization) | ✅ 19/19 | ⚠️ 25/27 | ✅ seed `roles`, `permissions` | | [Audit Logs](docs/behavior.md#audit-logs) | ⚠️ 3/4 | ⚠️ 3/4 | ⚠️ API only | | [Vault](docs/behavior.md#vault) | ✅ 5/5 | ⚠️ 3/6 | ⚠️ API only | | [Feature Flags](docs/behavior.md#feature-flags) | ✅ 4/4 | ✅ 4/4 | ✅ seed `featureFlags` | diff --git a/docs/behavior.md b/docs/behavior.md index f20df42..eb98d81 100644 --- a/docs/behavior.md +++ b/docs/behavior.md @@ -340,7 +340,10 @@ Deleting a role, group, or resource also removes its group grants. The emulator role-assignment webhook events. Bulk group assignment replacement is not yet supported. Resource updates accept the Node SDK's `PATCH` endpoints by ID and by organization, resource type, -and external ID. The earlier `PUT`-by-ID endpoint remains available. +and external ID. The earlier `PUT`-by-ID endpoint remains available. Deletes are served by ID and by +organization, resource type, and external ID, with the same rules: `409` `resource_has_dependents` +for a resource with descendants or role assignments unless `cascade_delete=true`, which deletes them +with it. ## Audit Logs diff --git a/src/workos/routes/authorization-resources.spec.ts b/src/workos/routes/authorization-resources.spec.ts index 204f6d1..f57498b 100644 --- a/src/workos/routes/authorization-resources.spec.ts +++ b/src/workos/routes/authorization-resources.spec.ts @@ -528,6 +528,37 @@ describe('Authorization resource routes', () => { expect((await req(`/authorization/resources/${child.id}`)).status).toBe(404); }); + it('deletes a resource by organization, type and external id', async () => { + const org = await createOrg('DelExt Org'); + const create = async (body: Record) => + json( + await req('/authorization/resources', { + method: 'POST', + body: JSON.stringify({ organization_id: org.id, ...body }), + }), + ); + const parent = await create({ resource_type_slug: 'workspace', external_id: 'ext-ws', name: 'ext-ws' }); + const child = await create({ + resource_type_slug: 'project', + external_id: 'ext-proj', + name: 'ext-proj', + parent_resource_id: parent.id, + }); + const path = `/authorization/organizations/${org.id}/resources/workspace/ext-ws`; + + const refused = await req(path, { method: 'DELETE' }); + expect(refused.status).toBe(409); + expect((await json(refused)).code).toBe('resource_has_dependents'); + + expect((await req(`${path}?cascade_delete=true`, { method: 'DELETE' })).status).toBe(204); + expect((await req(path)).status).toBe(404); + expect((await req(`/authorization/resources/${child.id}`)).status).toBe(404); + + const missing = await req(path, { method: 'DELETE' }); + expect(missing.status).toBe(404); + expect((await json(missing)).code).toBe('entity_not_found'); + }); + it('filters resources by external id and parent', async () => { const org = await createOrg('ListFilter Org'); const parent = await json( diff --git a/src/workos/routes/authorization-resources.ts b/src/workos/routes/authorization-resources.ts index 139e154..cae8a50 100644 --- a/src/workos/routes/authorization-resources.ts +++ b/src/workos/routes/authorization-resources.ts @@ -240,10 +240,9 @@ export function authorizationResourceRoutes(ctx: RouteContext): void { // Deleting a resource takes its descendants and their role assignments with // it. Without cascade_delete=true a resource that has either is refused, // rather than leaving children pointing at an id that no longer resolves. - app.delete('/authorization/resources/:resource_id', (c) => { - const resourceId = c.req.param('resource_id'); - const resource = ws.authorizationResources.get(resourceId); - if (!resource) throw notFound('AuthorizationResource', resourceId); + const deleteResource = (c: Context, resource: WorkOSAuthorizationResource | undefined, ref: string) => { + if (!resource) throw notFound('AuthorizationResource', ref); + const resourceId = resource.id; if (resource.resource_type_slug === 'organization') { throw new WorkOSApiError(400, 'Cannot mark organization resource as deleting.', 'bad_request'); @@ -268,7 +267,17 @@ export function authorizationResourceRoutes(ctx: RouteContext): void { for (const assignment of groupAssignments) ws.groupRoleAssignments.delete(assignment.id); for (const id of subtree) ws.authorizationResources.delete(id); return c.body(null, 204); - }); + }; + app.delete('/authorization/resources/:resource_id', (c) => + deleteResource(c, ws.authorizationResources.get(c.req.param('resource_id')), c.req.param('resource_id')), + ); + app.delete('/authorization/organizations/:orgId/resources/:type_slug/:external_id', (c) => + deleteResource( + c, + findResourceByExternalId(ws, c.req.param('orgId'), c.req.param('type_slug'), c.req.param('external_id')), + c.req.param('external_id'), + ), + ); // Memberships that hold a permission on a resource. Production validates the query before // it resolves the resource, so a missing permission_slug is a 422 even for an unknown