mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 16:39:05 +00:00
## Context
_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_
We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.
## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
221 lines
7.3 KiB
TypeScript
221 lines
7.3 KiB
TypeScript
import { fireEvent, screen, waitFor, within } from '@testing-library/react'
|
|
import { mockAnimationsApi } from 'jsdom-testing-mocks'
|
|
import { HttpResponse } from 'msw'
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
import { SessionsAuthSettingsForm } from './SessionsAuthSettingsForm'
|
|
import {
|
|
MAX_REFRESH_TOKEN_REUSE_INTERVAL_MESSAGE,
|
|
MAX_SESSIONS_INACTIVITY_TIMEOUT_MESSAGE,
|
|
MAX_SESSIONS_TIMEBOX_MESSAGE,
|
|
} from './SessionsAuthSettingsForm.utils'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
|
|
// FormMessage animates via Motion, which relies on the Web Animations API.
|
|
mockAnimationsApi()
|
|
|
|
vi.mock('@/lib/constants', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@/lib/constants')>()
|
|
return { ...actual, IS_PLATFORM: true }
|
|
})
|
|
|
|
vi.mock('@/hooks/misc/useCheckEntitlements', () => ({
|
|
useCheckEntitlements: () => ({ hasAccess: true, isLoading: false }),
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckPermissionsV2', () => ({
|
|
useAsyncCheckPermissionsV2: () => ({ can: true, isLoading: false, isSuccess: true }),
|
|
FGA_PERMISSIONS: {
|
|
PROJECT: {
|
|
AUTH_CONFIG_READ: 'auth_config_read',
|
|
AUTH_CONFIG_WRITE: 'auth_config_write',
|
|
},
|
|
},
|
|
}))
|
|
|
|
function mockAuthConfig(overrides: Record<string, unknown> = {}) {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/auth/:ref/config',
|
|
response: () =>
|
|
HttpResponse.json<any>({
|
|
JWT_EXP: 3600,
|
|
REFRESH_TOKEN_ROTATION_ENABLED: true,
|
|
SECURITY_REFRESH_TOKEN_REUSE_INTERVAL: 10,
|
|
SESSIONS_TIMEBOX: 0,
|
|
SESSIONS_INACTIVITY_TIMEOUT: 0,
|
|
SESSIONS_SINGLE_PER_USER: false,
|
|
...overrides,
|
|
}),
|
|
})
|
|
}
|
|
|
|
function mockUpdateAuthConfig(onPatch?: (body: unknown) => void) {
|
|
addAPIMock({
|
|
method: 'patch',
|
|
path: '/platform/auth/:ref/config',
|
|
response: async ({ request }) => {
|
|
const body = await request.json()
|
|
onPatch?.(body)
|
|
return HttpResponse.json<any>({ ...(body as object) })
|
|
},
|
|
})
|
|
}
|
|
|
|
describe('SessionsAuthSettingsForm — Access Tokens', () => {
|
|
test('renders the access token expiry from the auth config', async () => {
|
|
mockAuthConfig({ JWT_EXP: 3600 })
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
expect(await screen.findByText('Access Tokens')).toBeInTheDocument()
|
|
expect(await screen.findByDisplayValue('3600')).toBeInTheDocument()
|
|
expect(screen.getByText('Access token expiry time')).toBeInTheDocument()
|
|
})
|
|
|
|
test('saving a new expiry issues a PATCH with the JWT_EXP payload', async () => {
|
|
mockAuthConfig({ JWT_EXP: 3600 })
|
|
let patchBody: unknown
|
|
mockUpdateAuthConfig((body) => {
|
|
patchBody = body
|
|
})
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
const input = await screen.findByDisplayValue('3600')
|
|
fireEvent.change(input, { target: { value: '7200' } })
|
|
|
|
const form = input.closest('form') as HTMLFormElement
|
|
const saveButton = within(form).getByRole('button', { name: 'Save changes' })
|
|
await waitFor(() => expect(saveButton).toBeEnabled())
|
|
fireEvent.click(saveButton)
|
|
|
|
await waitFor(() => expect(patchBody).toEqual({ JWT_EXP: 7200 }))
|
|
})
|
|
|
|
test('blocks submit when the expiry exceeds the maximum', async () => {
|
|
mockAuthConfig({ JWT_EXP: 3600 })
|
|
let patchCalled = false
|
|
mockUpdateAuthConfig(() => {
|
|
patchCalled = true
|
|
})
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
const input = await screen.findByDisplayValue('3600')
|
|
fireEvent.change(input, { target: { value: '999999' } })
|
|
|
|
const form = input.closest('form') as HTMLFormElement
|
|
const saveButton = within(form).getByRole('button', { name: 'Save changes' })
|
|
await waitFor(() => expect(saveButton).toBeEnabled())
|
|
fireEvent.click(saveButton)
|
|
|
|
expect(await screen.findByText('Must be less than 604800')).toBeInTheDocument()
|
|
expect(patchCalled).toBe(false)
|
|
})
|
|
})
|
|
|
|
async function saveForm(control: HTMLElement) {
|
|
const form = control.closest('form') as HTMLFormElement
|
|
const saveButton = within(form).getByRole('button', { name: 'Save changes' })
|
|
await waitFor(() => expect(saveButton).toBeEnabled())
|
|
fireEvent.click(saveButton)
|
|
}
|
|
|
|
// Boundary and reduction rules are covered by SessionsAuthSettingsForm.utils.test.ts; these
|
|
// tests check the schemas are wired into the form (errors shown, stored values honoured).
|
|
describe('SessionsAuthSettingsForm — User Sessions', () => {
|
|
test('blocks submit when the timebox exceeds the maximum', async () => {
|
|
mockAuthConfig({ SESSIONS_TIMEBOX: 0 })
|
|
let patchCalled = false
|
|
mockUpdateAuthConfig(() => {
|
|
patchCalled = true
|
|
})
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
const input = await screen.findByLabelText('Time-box user sessions')
|
|
fireEvent.change(input, { target: { value: '9000' } })
|
|
await saveForm(input)
|
|
|
|
expect(await screen.findByText(MAX_SESSIONS_TIMEBOX_MESSAGE)).toBeInTheDocument()
|
|
expect(patchCalled).toBe(false)
|
|
})
|
|
|
|
test('saves an over-limit timebox that is already stored', async () => {
|
|
mockAuthConfig({ SESSIONS_TIMEBOX: 20000 })
|
|
let patchBody: unknown
|
|
mockUpdateAuthConfig((body) => {
|
|
patchBody = body
|
|
})
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
// Save is gated on isDirty, so change an unrelated field in the same card
|
|
const singleSessionSwitch = await screen.findByLabelText('Enforce single session per user')
|
|
fireEvent.click(singleSessionSwitch)
|
|
await saveForm(singleSessionSwitch)
|
|
|
|
await waitFor(() =>
|
|
expect(patchBody).toMatchObject({ SESSIONS_TIMEBOX: 20000, SESSIONS_SINGLE_PER_USER: true })
|
|
)
|
|
})
|
|
|
|
test('blocks submit when the inactivity timeout exceeds the maximum', async () => {
|
|
mockAuthConfig({ SESSIONS_INACTIVITY_TIMEOUT: 0 })
|
|
let patchCalled = false
|
|
mockUpdateAuthConfig(() => {
|
|
patchCalled = true
|
|
})
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
const input = await screen.findByLabelText('Inactivity timeout')
|
|
fireEvent.change(input, { target: { value: '10000' } })
|
|
await saveForm(input)
|
|
|
|
expect(await screen.findByText(MAX_SESSIONS_INACTIVITY_TIMEOUT_MESSAGE)).toBeInTheDocument()
|
|
expect(patchCalled).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('SessionsAuthSettingsForm — Refresh Tokens', () => {
|
|
test('blocks submit when the reuse interval exceeds the maximum', async () => {
|
|
mockAuthConfig({ SECURITY_REFRESH_TOKEN_REUSE_INTERVAL: 10 })
|
|
let patchCalled = false
|
|
mockUpdateAuthConfig(() => {
|
|
patchCalled = true
|
|
})
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
const input = await screen.findByLabelText('Refresh token reuse interval')
|
|
fireEvent.change(input, { target: { value: '600' } })
|
|
await saveForm(input)
|
|
|
|
expect(await screen.findByText(MAX_REFRESH_TOKEN_REUSE_INTERVAL_MESSAGE)).toBeInTheDocument()
|
|
expect(patchCalled).toBe(false)
|
|
})
|
|
|
|
test('saves an over-limit reuse interval that is already stored', async () => {
|
|
mockAuthConfig({ SECURITY_REFRESH_TOKEN_REUSE_INTERVAL: 600 })
|
|
let patchBody: unknown
|
|
mockUpdateAuthConfig((body) => {
|
|
patchBody = body
|
|
})
|
|
|
|
customRender(<SessionsAuthSettingsForm />)
|
|
|
|
const rotationSwitch = await screen.findByLabelText(
|
|
'Detect and revoke potentially compromised refresh tokens'
|
|
)
|
|
fireEvent.click(rotationSwitch)
|
|
await saveForm(rotationSwitch)
|
|
|
|
await waitFor(() =>
|
|
expect(patchBody).toMatchObject({ SECURITY_REFRESH_TOKEN_REUSE_INTERVAL: 600 })
|
|
)
|
|
})
|
|
})
|