Files
Danny White 0b9e4f6ab1 fix(studio): clarify token access settings and SSL prerequisites (#54507)
## Problem

- Token access confirmations use stilted copy. 
- The SSL prerequisite notice sits outside the Token access card,
repeats “enabled”, and includes an unnecessary project reference.
- SSL configuration also shows enforcement as off when the latest update
failed, and its dependency tooltip needs clearer guidance.

## Solution

- Rewrite the token access and SSL confirmations, keeping the connection
warnings.
- Group prerequisite notices inside the Token access card with one
divider, remove the project reference, and simplify their descriptions.
- Keep the SSL switch tied to the fetched configuration independently of
update success.
- Explain the token access dependency in the disabled switch’s tooltip:
“Database token access requires SSL enforcement.” Leave the existing SSL
failure warning unchanged apart from its final full stop.

| Before | After |
| --- | --- |
| <img width="727" height="501" alt="CleanShot 2026-12-03 at 11 21 35"
src="https://github.com/user-attachments/assets/9fd18a91-528a-47dd-82e8-7349e26a8daa"
/> | <img width="736" height="506" alt="CleanShot 2026-12-03 at 12 05
57"
src="https://github.com/user-attachments/assets/8263303a-49d8-4910-9c88-e2fe5ecc1c34"
/> |
| <img width="362" height="236" alt="CleanShot 2026-12-03 at 11 21 54"
src="https://github.com/user-attachments/assets/026852d4-9c1b-4a9d-9833-02e047794199"
/> | <img width="421" height="229" alt="CleanShot 2026-12-03 at 12 06
24"
src="https://github.com/user-attachments/assets/9a53c459-c7ca-4c18-8567-499d67a9c068"
/> |
| <img width="420" height="206" alt="CleanShot 2026-12-03 at 11 22 36"
src="https://github.com/user-attachments/assets/4d2f8711-fd75-4730-832f-ac9e2e3d85ad"
/> | <img width="420" height="207" alt="CleanShot 2026-12-03 at 11 52
51"
src="https://github.com/user-attachments/assets/2fce238e-c8d7-4087-8402-05c2122f92f0"
/> |
| <img width="420" height="208" alt="CleanShot 2026-12-03 at 11 32 11"
src="https://github.com/user-attachments/assets/f081e782-0a6c-499a-947d-0da149311218"
/> | <img width="417" height="219" alt="CleanShot 2026-12-03 at 11 52
05"
src="https://github.com/user-attachments/assets/6b6cfcf5-2c54-4f38-9b16-bcc0cdd500a0"
/> |
| <img width="809" height="255" alt="CleanShot 2026-12-03 at 12 28 47"
src="https://github.com/user-attachments/assets/19cffa41-691f-4f60-96b5-7ba1ebcfb78e"
/> | <img width="804" height="246" alt="CleanShot 2026-12-03 at 12 44
39"
src="https://github.com/user-attachments/assets/2c3f2b74-21a8-41e7-b25e-ea2293350fcc"
/> |


## Review instructions

1. Open Project → Database → Settings in the preview for a project that
requires SSL enforcement before token access.
2. Check that SSL enforcement required sits inside Token access,
separated by one divider, with no project reference in its description.
3. Select Enable SSL enforcement and review the confirmation copy.
Cancel without applying the change.
4. On a project where token access is available, toggle it on and off to
review both confirmations. Cancel each dialog.
5. With token access on, check that SSL enforcement stays on and locked,
with a tooltip explaining that database token access requires SSL
enforcement. Check that the tooltip also opens on keyboard focus.
6. If the API reports an unsuccessful SSL update, check that the switch
still reflects `currentConfig.database` and the existing warning remains
visible.
2026-12-03 11:46:10 +07:00

247 lines
9.2 KiB
TypeScript

import { fireEvent, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { components, paths } from 'api-types'
import { HttpResponse } from 'msw'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { SSLConfiguration } from './SSLConfiguration'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
type ProjectSettingsResponse = components['schemas']['ProjectSettingsResponse_Output']
type SslEnforcementResponse = components['schemas']['SslEnforcementResponse_Output']
type JitAccessConfigResponse =
paths['/v1/projects/{ref}/jit-access']['get']['responses'][200]['content']['application/json']
const { mockUseAsyncCheckPermissions, mockUseHighAvailability, mockUseSelectedProjectQuery } =
vi.hoisted(() => ({
mockUseAsyncCheckPermissions: vi.fn(),
mockUseHighAvailability: vi.fn(),
mockUseSelectedProjectQuery: vi.fn(),
}))
vi.mock('@/hooks/misc/useCheckPermissionsV2', () => ({
useAsyncCheckPermissionsV2: mockUseAsyncCheckPermissions,
FGA_PERMISSIONS: { PROJECT: { DATABASE_SSL_CONFIG_WRITE: 'database_ssl_config_write' } },
}))
vi.mock('@/hooks/misc/useHighAvailability', () => ({
useHighAvailability: mockUseHighAvailability,
}))
vi.mock('@/hooks/misc/useSelectedProject', () => ({
useSelectedProjectQuery: mockUseSelectedProjectQuery,
}))
const HA_TOOLTIP = 'SSL is always enforced on High Availability projects.'
function mockProjectSettings() {
addAPIMock({
method: 'get',
path: '/platform/projects/:ref/settings',
response: () =>
HttpResponse.json<ProjectSettingsResponse>({
app_config: {
db_schema: 'public',
endpoint: 'default.supabase.co',
storage_endpoint: 'storage.default.supabase.co',
},
cloud_provider: 'AWS',
db_dns_name: 'default.supabase.co',
db_host: 'default.supabase.co',
db_ip_addr_config: 'ipv4',
db_name: 'postgres',
db_port: 5432,
db_user: 'postgres',
inserted_at: '2025-02-16T22:24:42.115195',
name: 'default',
ref: 'default',
region: 'us-east-1',
ssl_enforced: true,
status: 'ACTIVE_HEALTHY',
}),
})
}
function mockJitDbAccess() {
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/jit-access',
response: () =>
HttpResponse.json<JitAccessConfigResponse>({ state: 'disabled', appliedSuccessfully: true }),
})
}
/** Registers the SSL enforcement GET and returns a counter of how often it was hit. */
function mockSSLEnforcement(config: SslEnforcementResponse) {
const requests = { count: 0 }
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/ssl-enforcement',
response: () => {
requests.count += 1
return HttpResponse.json<SslEnforcementResponse>(config)
},
})
return requests
}
describe('SSLConfiguration', () => {
beforeEach(() => {
mockUseSelectedProjectQuery.mockReturnValue({ data: { id: 1, ref: 'default' } })
mockUseAsyncCheckPermissions.mockReturnValue({ can: true })
mockProjectSettings()
mockJitDbAccess()
})
it('shows SSL enforcement as always on for High Availability projects', async () => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: true, isPending: false })
const sslEnforcementRequests = mockSSLEnforcement({
appliedSuccessfully: true,
currentConfig: { database: false },
})
customRender(<SSLConfiguration />)
const sslSwitch = await screen.findByRole('switch')
expect(sslSwitch).toBeChecked()
expect(sslSwitch).toBeDisabled()
// Nothing is loading for HA projects, so there is no announcement to make
expect(screen.getByRole('status')).toBeEmptyDOMElement()
// The tooltip trigger is the wrapper around the (disabled) switch. Focus opens a Radix
// tooltip without the hover delay.
fireEvent.focus(sslSwitch.parentElement!)
expect((await screen.findAllByText(HA_TOOLTIP)).length).toBeGreaterThan(0)
expect(sslEnforcementRequests.count).toBe(0)
})
it('does not open the confirm dialog around a disabled switch on High Availability projects', async () => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: true, isPending: false })
mockSSLEnforcement({ appliedSuccessfully: true, currentConfig: { database: false } })
customRender(<SSLConfiguration />)
const sslSwitch = await screen.findByRole('switch')
// Clicking the disabled switch and the row wrapper around it must not open the dialog
await userEvent.click(sslSwitch)
await userEvent.click(sslSwitch.parentElement!.parentElement!)
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
})
it('reflects the fetched SSL enforcement config for other projects', async () => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: false })
const sslEnforcementRequests = mockSSLEnforcement({
appliedSuccessfully: true,
currentConfig: { database: false },
})
customRender(<SSLConfiguration />)
const sslSwitch = await screen.findByRole('switch')
expect(sslSwitch).not.toBeChecked()
expect(sslSwitch).toBeEnabled()
expect(screen.getByRole('status')).toBeEmptyDOMElement()
expect(sslEnforcementRequests.count).toBe(1)
})
it.each([true, false])(
'keeps the fetched SSL state (%s) when the latest update did not apply',
async (database) => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: false })
mockSSLEnforcement({ appliedSuccessfully: false, currentConfig: { database } })
customRender(<SSLConfiguration />)
const sslSwitch = await screen.findByRole('switch')
if (database) expect(sslSwitch).toBeChecked()
else expect(sslSwitch).not.toBeChecked()
expect(await screen.findByText('SSL enforcement was not updated successfully')).toBeVisible()
}
)
it('explains the locked switch in a keyboard-accessible tooltip while preserving the SSL warning', async () => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: false })
mockSSLEnforcement({ appliedSuccessfully: false, currentConfig: { database: true } })
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/jit-access',
response: () =>
HttpResponse.json<JitAccessConfigResponse>({
state: 'enabled',
appliedSuccessfully: true,
}),
})
customRender(<SSLConfiguration />)
const sslSwitch = await screen.findByRole('switch')
await waitFor(() => expect(sslSwitch).toBeDisabled())
expect(sslSwitch).toBeChecked()
expect(
screen.queryByText('SSL enforcement is required while token access is on')
).not.toBeInTheDocument()
expect(screen.getByText('SSL enforcement was not updated successfully')).toBeVisible()
const switchWrapper = sslSwitch.parentElement
if (!switchWrapper) throw new Error('Expected the disabled switch to have a focusable wrapper')
expect(switchWrapper).toHaveAttribute('tabindex', '0')
fireEvent.focus(switchWrapper)
expect(await screen.findByRole('tooltip')).toHaveTextContent(
'Database token access requires SSL enforcement.'
)
await userEvent.click(sslSwitch)
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
})
it('opens the confirm dialog from the switch and leaves it unchanged on cancel', async () => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: false })
mockSSLEnforcement({ appliedSuccessfully: true, currentConfig: { database: false } })
customRender(<SSLConfiguration />)
const sslSwitch = await screen.findByRole('switch')
await userEvent.click(sslSwitch)
const dialog = await screen.findByRole('alertdialog')
expect(
within(dialog).getByRole('heading', { name: 'Enforce SSL on connections' })
).toBeVisible()
expect(screen.getByRole('button', { name: 'Enforce SSL' })).toBeInTheDocument()
// Controlled switch must not flip while the dialog is open
expect(sslSwitch).not.toBeChecked()
await userEvent.click(screen.getByRole('button', { name: 'Cancel' }))
await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument())
expect(sslSwitch).not.toBeChecked()
})
it('disables SSL enforcement without a confirm dialog', async () => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: false })
mockSSLEnforcement({ appliedSuccessfully: true, currentConfig: { database: true } })
const updateRequests: unknown[] = []
addAPIMock({
method: 'put',
path: '/v1/projects/:ref/ssl-enforcement',
response: async ({ request }) => {
updateRequests.push(await request.json())
return HttpResponse.json<SslEnforcementResponse>({
appliedSuccessfully: true,
currentConfig: { database: false },
})
},
})
customRender(<SSLConfiguration />)
const sslSwitch = await screen.findByRole('switch')
expect(sslSwitch).toBeChecked()
await userEvent.click(sslSwitch)
await waitFor(() => expect(updateRequests).toEqual([{ requestedConfig: { database: false } }]))
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
})
})