Files
supabase/apps/studio/tests/components/Database/Roles.test.tsx
39b359a70e chore: keep db partner roles out of picker (#54019)
## Problem

Newly added trusted partner roles appear as user selectable /
manageable. Resolves DEPR-735.

## Solution

Trusted partner roles should not be selectable in temporary access. And
should not appear as modifiable in the database roles tab.

| Before | After |
| --- | --- |
| <img width="553" height="398" alt="CleanShot 2026-11-27 at 12 24 43"
src="https://github.com/user-attachments/assets/14affc16-2f00-496f-a124-d918acd93bf0"
/> | <img width="553" height="363" alt="CleanShot 2026-11-27 at 12 25
00"
src="https://github.com/user-attachments/assets/de06002f-d063-453e-a9da-00eee8e0616d"
/> |
| _This partner role `this_is_a_test` shouldn’t be here._ | _Partner
role `this_is_a_test` is no longer in the list of options._ |
| <img width="940" height="305" alt="CleanShot 2026-11-30 at 10 34 52"
src="https://github.com/user-attachments/assets/bdface9f-b3f4-4620-b567-cba8d829205e"
/> | <img width="935" height="299" alt="CleanShot 2026-11-30 at 10 37
40"
src="https://github.com/user-attachments/assets/da1abaa3-5a98-4837-9e8f-35be84b6be4c"
/> |
| _Previous definition._ | _Broadened definition to better encompass
roles we generally manage
([context](https://github.com/supabase/supabase/pull/54019#discussion_r4716757425))._
|

## How to test

1. Open the [Studio staging
preview](https://studio-staging-git-chore-depr-735-supabase.vercel.app)
and select a staging test project. If **Temporary access** is off in
**Feature previews**, enable it there. Then go to **Database → Settings
→ Temporary access** and make sure it is enabled for the test project.
2. Go to **Database → Roles**. Open Chrome DevTools, select **Network**,
and reload. Filter for `key=database-roles`. Right-click the matching
request and select **Override content**.
3. Append this test role to the roles array. Save the override and
reload.

```json
{
  "id": 99999,
  "name": "this_is_a_test",
  "isSuperuser": false,
  "canCreateDb": false,
  "canCreateRole": false,
  "inheritRole": true,
  "canLogin": true,
  "isReplicationRole": false,
  "canBypassRls": false,
  "activeConnections": 0,
  "connectionLimit": 90,
  "validUntil": null,
  "comment": "{\"_supabase\":{\"oauth_app_id\":\"app\"}}",
  "config": {}
}
```

4. In **Database → Roles**, make sure that `this_is_a_test` appears
under **Supabase managed roles** without edit or delete controls.
5. Go to **Database → Settings → Temporary access**, select **Add
rule**, and open the role picker. The test role should be absent.
6. Set the test role's `comment` to `null` in the override and reload.
Open **Add rule** again and make sure that the role is now selectable.
Close the form without saving and remove the override.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
2026-11-30 15:35:30 +01:00

168 lines
6.4 KiB
TypeScript

import { fireEvent, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { HttpResponse } from 'msw'
import { beforeEach, describe, expect, test, vi } from 'vitest'
import { RoleRow } from '@/components/interfaces/Database/Roles/RoleRow'
import { RolesList } from '@/components/interfaces/Database/Roles/RolesList'
import type { PgRole } from '@/data/database-roles/database-roles-query'
import type { ProjectDetail } from '@/data/projects/project-detail-query'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
mockAnimationsApi()
const createRole = (id: number, name: string, comment: string | null = null): PgRole => ({
id,
name,
isSuperuser: false,
canCreateDb: false,
canCreateRole: false,
inheritRole: true,
canLogin: true,
isReplicationRole: false,
canBypassRls: false,
activeConnections: 0,
connectionLimit: -1,
validUntil: null,
config: {},
comment,
})
const roles = [
createRole(1, 'postgres'),
createRole(2, 'supabase_privileged_role'),
createRole(3, 'supabase_auth_admin'),
createRole(4, 'custom_role'),
createRole(5, 'acme_user', '{"_supabase":{"oauth_app_id":"app"}}'),
]
beforeEach(() => {
addAPIMock({
method: 'get',
path: '/platform/projects/:ref',
response: () =>
HttpResponse.json<ProjectDetail>({
cloud_provider: 'AWS',
connectionString: null,
db_host: 'localhost',
high_availability: false,
id: 1,
inserted_at: '2026-01-01T00:00:00Z',
integration_source: null,
is_branch_enabled: false,
is_physical_backups_enabled: false,
name: 'Test project',
organization_id: 1,
ref: 'default',
region: 'us-east-1',
restUrl: 'http://localhost:54321',
status: 'ACTIVE_HEALTHY',
subscription_id: 'test-subscription',
updated_at: '2026-01-01T00:00:00Z',
}),
})
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: ({ request }) => {
const key = new URL(request.url).searchParams.get('key')
if (key === 'max-connections') {
return HttpResponse.json<{ max_connections: string }[]>([{ max_connections: '100' }])
}
if (key === 'database-roles') {
return HttpResponse.json<PgRole[]>(roles)
}
throw new Error(`Unexpected SQL request: ${key}`)
},
})
})
describe('RoleRow', () => {
test.each(['postgres', 'pg_database_owner', 'pg_read_all_data', 'pg_write_all_data'])(
'disables Delete for the system role %s',
async (name) => {
const onSelectDelete = vi.fn()
customRender(<RoleRow role={createRole(1, name)} onSelectDelete={onSelectDelete} />)
await userEvent.click(screen.getByRole('button', { name: `${name} actions` }))
const deleteItem = await screen.findByRole('menuitem', { name: 'Delete' })
expect(deleteItem).toBeAriaDisabled()
fireEvent.click(deleteItem)
expect(onSelectDelete).not.toHaveBeenCalled()
}
)
test('explains why Delete is disabled for a system role', async () => {
customRender(<RoleRow role={createRole(1, 'postgres')} onSelectDelete={vi.fn()} />)
await userEvent.click(screen.getByRole('button', { name: 'postgres actions' }))
await userEvent.hover(await screen.findByRole('menuitem', { name: 'Delete' }))
expect(
await screen.findByRole('tooltip', {
name: 'The postgres role is a system role and cannot be deleted',
})
).toBeInTheDocument()
})
test('keeps the postgres permission editor available', async () => {
customRender(<RoleRow role={createRole(1, 'postgres')} onSelectDelete={vi.fn()} />)
await userEvent.click(screen.getByRole('button', { name: /postgres \(ID: 1\)/ }))
const loginSwitch = await screen.findByRole('switch', { name: 'User can login' })
expect(loginSwitch).not.toBeDisabled()
await userEvent.click(loginSwitch)
expect(screen.getByRole('button', { name: 'Save' })).toBeEnabled()
})
test('allows selecting a custom role for deletion', async () => {
const onSelectDelete = vi.fn()
customRender(<RoleRow role={createRole(4, 'custom_role')} onSelectDelete={onSelectDelete} />)
await userEvent.click(screen.getByRole('button', { name: 'custom_role actions' }))
const deleteItem = await screen.findByRole('menuitem', { name: 'Delete' })
expect(deleteItem).not.toBeAriaDisabled()
await userEvent.click(deleteItem)
expect(onSelectDelete).toHaveBeenCalledWith('4')
})
test('keeps disabled roles read-only', async () => {
customRender(<RoleRow disabled role={createRole(4, 'custom_role')} onSelectDelete={vi.fn()} />)
expect(screen.queryByRole('button', { name: 'custom_role actions' })).not.toBeInTheDocument()
await userEvent.click(screen.getByRole('button', { name: /custom_role \(ID: 4\)/ }))
expect(await screen.findByRole('switch', { name: 'User can login' })).toBeDisabled()
expect(screen.queryByRole('button', { name: 'Save' })).not.toBeInTheDocument()
})
})
describe('RolesList', () => {
test('separates protected service roles from the project administrator and custom roles', async () => {
customRender(<RolesList />)
const serviceRoles = within(screen.getByRole('region', { name: 'Roles managed by Supabase' }))
const projectRoles = within(screen.getByRole('region', { name: 'Project database roles' }))
expect(await serviceRoles.findByText('supabase_privileged_role')).toBeInTheDocument()
expect(serviceRoles.getByText('supabase_auth_admin')).toBeInTheDocument()
expect(serviceRoles.getByText('acme_user')).toBeInTheDocument()
expect(serviceRoles.queryByText('postgres')).not.toBeInTheDocument()
expect(projectRoles.getByRole('button', { name: /postgres \(ID: 1\)/ })).toBeInTheDocument()
expect(projectRoles.getByRole('button', { name: /custom_role \(ID: 4\)/ })).toBeInTheDocument()
expect(
serviceRoles.queryByRole('button', { name: 'supabase_privileged_role actions' })
).not.toBeInTheDocument()
})
test('opens the deletion confirmation for a custom role', async () => {
customRender(<RolesList />)
await userEvent.click(await screen.findByRole('button', { name: 'custom_role actions' }))
await userEvent.click(await screen.findByRole('menuitem', { name: 'Delete' }))
expect(await screen.findByRole('dialog')).toHaveTextContent(
'Confirm to delete role "custom_role"'
)
})
})