Files
supabase/apps/studio/tests/components/OAuthApps/consent-fixtures.ts
Danny WhiteandJoshen Lim 4dbb966b2e feat(studio): show Trusted Partner access on OAuth consent (#53583)
> [!WARNING]
> Live readiness checks require
[CTRL-2277](https://linear.app/supabase/issue/CTRL-2277) in the target
environment; production deployment is pending.

## Problem

Trusted Partner consent must explain direct database access and project
blockers before authorisation.
[DEPR-732](https://linear.app/supabase/issue/DEPR-732)

## Solution

- Show Direct database access alongside the requested permissions.
- Check selected projects, or every project page for organisation-wide
grants.
- Separate blocking issues from SSL notices; block authorisation during
loading or errors and offer retry.

| After |
| --- |
| <img width="1280" height="1323" alt="Authorize API Access Supabase"
src="https://github.com/user-attachments/assets/adb6367a-bace-4b62-81b5-68ba4c11396c"
/> |
| Consent screen with Direct database access (previously “Table data”)
and Database management |
| <img width="1280" height="1323" alt="Authorize API Access Supabase"
src="https://github.com/user-attachments/assets/0c35900c-1059-49cd-b1c5-4e0cf5441b9b"
/> |
| Selected-project blockers and the separate SSL notice |
| <img width="1280" height="1323" alt="95741"
src="https://github.com/user-attachments/assets/1fbef4f0-0433-4c93-b390-84d210dfb665"
/> |
| Organisation-wide blockers _without_ project-removal actions |

## Review instructions

1. Open the [staging consent
screen](https://studio-staging-git-dnywh-depr-732-trusted-partn-f24cfb-supabase.vercel.app/dashboard/authorize?auth_id=11666aa8-7f7e-4705-b33a-981903fd682e).
Enable `enableScopedOAuthConsent` in the dev toolbar’s **Flags** tab if
needed.
2. Open Chrome DevTools → **Network** and reload. Select the **GET**
request ending in `/platform/oauth/authorizations/{id}`.
3. Right-click that request → **Override content**. Choose a local
folder and click **Allow**. Add `database_token_access:read` and
`database_token_access:write` to `scopes`. Use only the read scope to
check read-only access. Keep every other field, save with
**Cmd/Ctrl+S**, then reload with DevTools open.
4. Select projects. Override each **GET** request ending in
`/platform/organizations/{slug}/oauth/authorizations/{id}/projects/{ref}/database-access`.
Use `{"issues":[],"enables_ssl":false}` for ready projects, then
`{"issues":[],"enables_ssl":true}` for an SSL notice. Save and reload
after each change. Check Direct database access and Database management
are separate, and SSL alone allows authorisation.
5. Change a readiness response to
`{"issues":["postgres_upgrade_required","platform_unsupported"],"enables_ssl":true}`.
Save and reload. Check blockers prevent authorisation, SSL stays
separate, and removing a blocked project clears its blockers.
6. Set `project_scoping_mode: false` in the authorisation override. Save
and reload. Check any blocked project prevents organisation-wide
authorisation, with no project-removal actions.
7. Block a database-access request in Network and reload. Check
authorisation stays disabled. Unblock it and choose **Retry checks**.
8. For a returning app, override `existing_grant.approved_scopes` in the
request ending in `/preflight`. Save and reload. Check added, removed
and unchanged permissions.
9. Disable **Sources → Overrides → Enable Local Overrides**. Repeat
ready and blocked project checks with a real staging authorisation
before approving live readiness behaviour.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-12-02 16:19:30 +07:00

160 lines
5.3 KiB
TypeScript

import { http, HttpResponse } from 'msw'
import type { OAuthAppsAuthorizeRequestData } from '@/data/oauth-apps/oauth-apps-authorize-request-query'
import type { OAuthDatabaseAccessReadiness } from '@/data/oauth-apps/oauth-apps-database-access-readiness-query'
import type { OAuthAppsPreflightValidationData } from '@/data/oauth-apps/oauth-apps-preflight-validation-query'
import type { OrgProjectsResponse } from '@/data/projects/org-projects-infinite-query'
import type { ProjectDetail } from '@/data/projects/project-detail-query'
import { API_URL } from '@/lib/constants'
import { createMockOrganization } from '@/tests/helpers'
import { addAPIMock, mswServer } from '@/tests/lib/msw'
import type { Organization } from '@/types'
type ReadinessCheck = (
ref: string
) => OAuthDatabaseAccessReadiness | Response | Promise<OAuthDatabaseAccessReadiness | Response>
type ConsentFixtureOptions = {
access?: 'read' | 'write'
request?: Partial<AuthorizationResponse>
partner?: boolean
allProjects?: boolean
returningPartner?: boolean
existingProjectRefs?: string[]
readiness?: Record<string, OAuthDatabaseAccessReadiness>
check?: ReadinessCheck
}
// The database token scopes are deployed before the generated API scope enum.
type AuthorizationResponse = Omit<OAuthAppsAuthorizeRequestData, 'scopes'> & { scopes: string[] }
export function mockReadiness(check: ReadinessCheck) {
addAPIMock({
method: 'get',
path: '/platform/organizations/:slug/oauth/authorizations/:id/projects/:ref/database-access',
response: async ({ params }) => {
const result = await check(String(params.ref))
return result instanceof Response
? result
: HttpResponse.json<OAuthDatabaseAccessReadiness>(result)
},
})
}
export function mockConsent({
access = 'write',
request,
partner = false,
allProjects = false,
returningPartner = false,
existingProjectRefs,
readiness = {},
check,
}: ConsentFixtureOptions = {}) {
mswServer.use(
http.get(`${API_URL}/platform/oauth/authorizations/:id`, () =>
HttpResponse.json<AuthorizationResponse>({
allow_partial_grants: false,
app_id: partner ? 'partner-example' : 'vercel',
name: partner ? 'Example Partner' : 'Vercel',
website: 'https://example.com',
domain: 'example.com',
redirect_uri: 'https://example.com/callback',
registration_type: 'manual',
expires_at: '',
grant_kind: partner ? 'organization_bound' : 'member_bound',
project_scoping_mode: !allProjects,
scopes: partner ? ['projects:read', `database_token_access:${access}`] : ['projects:read'],
...request,
})
)
)
addAPIMock({
method: 'get',
path: '/platform/organizations',
response: () =>
HttpResponse.json<Organization[]>([
createMockOrganization({ id: 1, slug: 'northwind-traders', name: 'Northwind Traders' }),
]),
})
addAPIMock({
method: 'get',
path: '/platform/projects/:ref',
response: () =>
HttpResponse.json<ProjectDetail>({
id: 1,
ref: 'northwindcms1',
name: 'northwind-cms',
organization_id: 1,
cloud_provider: '',
connectionString: '',
db_host: '',
high_availability: false,
inserted_at: '',
integration_source: '',
is_branch_enabled: false,
is_physical_backups_enabled: false,
region: '',
restUrl: '',
status: 'ACTIVE_HEALTHY',
subscription_id: '',
updated_at: '',
dbVersion: '',
}),
})
addAPIMock({
method: 'get',
path: '/platform/organizations/:slug/projects',
response: () =>
HttpResponse.json<OrgProjectsResponse>({
projects: [
['northwindstorefront1', 'northwind-storefront'],
['northwindcms1', 'northwind-cms'],
['fabrikamapi1', 'fabrikam-api'],
['fabrikamjobs1', 'fabrikam-jobs'],
['northwindreporting1', 'northwind-reporting'],
['northwindwarehouse1', 'northwind-warehouse'],
].map(([ref, name]) => ({
cloud_provider: '',
databases: [],
inserted_at: '',
integration_source: null,
is_branch: false,
ref,
name,
region: '',
status: 'ACTIVE_HEALTHY',
})),
pagination: { count: 6, limit: 96, offset: 0 },
}),
})
// Keep the wire scope strings in the test boundary until API types are regenerated.
const preflight: Omit<OAuthAppsPreflightValidationData, 'existing_grant'> & {
existing_grant: null | {
grant_id: string
approved_scopes: string[]
project_refs: string[]
approved_at: string
}
} = {
validation_failure: null,
existing_grant:
returningPartner || existingProjectRefs
? {
grant_id: 'existing-grant',
approved_scopes: returningPartner
? ['projects:read', 'database_token_access:read']
: ['projects:read'],
project_refs: existingProjectRefs ?? ['northwindstorefront1'],
approved_at: '2026-08-14T09:12:00.000Z',
}
: null,
}
mswServer.use(
http.get(`${API_URL}/platform/organizations/:slug/oauth/authorizations/:id/preflight`, () =>
HttpResponse.json<typeof preflight>(preflight)
)
)
mockReadiness(check ?? ((ref) => readiness[ref] ?? { issues: [], enables_ssl: false }))
}