Files
kemal.earthandClaude Sonnet 5 33482bdc88 feat(studio): lifecycle and role-aware scoped token view sheet (#48848)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Extracts the token view sheet slice of #48742
(w3b6x9/scoped-pat-access-feedback, commit 56ef87a). The role-evaluation
logic (estimateRoleLevel, computeTokenRoleContext,
applySelectionToRoleContext, groupFailingResources) already landed on
master via #48805 and #48809 — this PR only wires the view sheet up to
it:

- Bindings whose project/org was deleted (FGA bindings erased) render a
"resources no longer exist" state; bindings the user can no longer reach
render an anonymous count with a "No longer accessible" badge and a
"removed from" admonition.
- Accessible resources list their name plus ref/slug; capabilities show
"Exceeds your role" pills and the risk badge reflects what the owner's
current role actually allows.
- Header split into separate "Access control" and "API docs" buttons.
- Everything recomputes from live org/project/permission queries (no
stored state) and degrades to no warnings while loading or on
self-hosted.

Also gives DocsButton an optional `label` prop (defaults preserve
existing behavior for every other consumer) so the two header buttons
can carry distinct text.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Enhanced access-token details with permission categories, risk
summaries, endpoint, and MCP information.
  * Added warnings for permissions exceeding the token’s role.
* Clearly identifies inaccessible, deleted, or unavailable organizations
and projects.
  * Added resource details and remediation guidance for unusable tokens.
  * Documentation links can now display custom labels.

* **Bug Fixes**
* Improved access evaluation when organization or project data is
incomplete or access has changed.
  * Deferred resource loading until token details are opened.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-07 17:19:30 +01:00

123 lines
4.4 KiB
TypeScript

import { platformComponents as components } from 'api-types'
import { HttpResponse } from 'msw'
import { createMockOrganizationResponse, createMockProject } from '@/tests/helpers'
import { addAPIMock } from '@/tests/lib/msw'
import type { Permission } from '@/types'
/**
* Test-only fixtures for the scoped-access-token surfaces.
*
* The permission-row builders mirror the shape of /platform/profile/permissions rows for each
* base role, per the ABAC default_permissions seeds (platform: middleware-db). Roles inherit
* lower roles' rows. Keep the rows in lockstep with ROLE_PROBES in AccessToken.roles.ts.
*/
type AccessControlPermission = components['schemas']['AccessControlPermission']
type OrganizationResponse = components['schemas']['OrganizationResponse']
type ProjectsResponse = components['schemas']['ListProjectsPaginatedResponse']
/** Satisfies both Studio's `Permission` type and the API's `AccessControlPermission` row shape. */
export type PermissionRowFixture = Permission & {
organization_id: number | null
project_ids: number[] | null
}
export const permissionRow = (
organization_slug: string,
actions: string[],
resources: string[],
// Org-wide rows serialize as [] or null on the wire (nullable in the API contract; the
// view-synthesized admin rows for auth.subject_roles/user_invites are null).
project_refs: string[] | null = []
): PermissionRowFixture => ({
actions: actions as Permission['actions'],
condition: null as unknown as Permission['condition'],
organization_id: null,
organization_slug,
project_ids: null,
resources,
restrictive: false,
project_refs,
})
export const memberRows = (slug: string, refs: string[] = []) => [
permissionRow(slug, ['read:Read'], ['members', 'organizations', 'auth.subject_roles'], refs),
]
export const readonlyRows = (slug: string, refs: string[] = []) => [
...memberRows(slug, refs),
permissionRow(slug, ['analytics:Read', 'tenant:Sql:Read:Select'], ['%'], refs),
]
export const developerRows = (slug: string, refs: string[] = []) => [
...readonlyRows(slug, refs),
permissionRow(
slug,
['functions:Write', 'tenant:Sql:Admin:Write', 'tenant:Sql:Query'],
['%'],
refs
),
]
export const administratorRows = (slug: string, refs: string[] = []) => [
...developerRows(slug, refs),
permissionRow(slug, ['write:Create', 'write:Update'], ['projects'], refs),
permissionRow(slug, ['billing:Write', 'infra:Execute'], ['%'], refs),
]
export const ownerRows = (slug: string, refs: string[] = []) => [
...administratorRows(slug, refs),
permissionRow(slug, ['write:Update'], ['organizations'], refs),
permissionRow(slug, ['write:Create', 'write:Delete'], ['auth.subject_roles'], refs),
]
export const MOCK_ORG = { slug: 'acme-prod', name: 'Acme Production' }
export const MOCK_PROJECT = { ref: 'project-1', name: 'Project 1' }
/**
* Registers the GET mocks every scoped-token surface fires on mount: one organization
* ({@link MOCK_ORG}), one project ({@link MOCK_PROJECT}), and the permission scope map.
*/
export const mockScopedTokenEnvironment = () => {
addAPIMock({
method: 'get',
path: '/platform/organizations',
response: () =>
HttpResponse.json<OrganizationResponse[]>([
createMockOrganizationResponse({ slug: MOCK_ORG.slug, name: MOCK_ORG.name }),
]),
})
addAPIMock({
method: 'get',
path: '/platform/projects',
response: () =>
HttpResponse.json<ProjectsResponse>({
pagination: { count: 1, limit: 100, offset: 0 },
projects: [
{
...createMockProject({ id: 1, ref: MOCK_PROJECT.ref, name: MOCK_PROJECT.name }),
organization_slug: MOCK_ORG.slug,
preview_branch_refs: [],
},
],
}),
})
addAPIMock({
method: 'get',
// @ts-expect-error Studio API is missing from types
path: '/scoped-access-token-permissions',
response: () => HttpResponse.json({ scopes: {}, endpoints: {}, mcp_tools: {} }),
})
}
export const mockPermissionsApi = (rows: PermissionRowFixture[]) =>
addAPIMock({
method: 'get',
path: '/platform/profile/permissions',
// Permission['condition'] (jsonLogic operator interfaces) has no index signature, so TS won't
// match it against the API row's `{ [key: string]: unknown }` — the runtime shape is fine.
response: () =>
HttpResponse.json<AccessControlPermission[]>(rows as unknown as AccessControlPermission[]),
})