import { fireEvent, screen, waitFor } 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 z from 'zod' import { RoleRow } from './RoleRow' import { ProjectContextProvider } from '@/components/layouts/ProjectLayout/ProjectContext' import type { PgRole } from '@/data/database-roles/database-roles-query' import { customRender } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' mockAnimationsApi() const QueryBodySchema = z.object({ query: z.string() }) const createRole = (overrides: Partial = {}): PgRole => ({ id: 16500, name: 'app_user', isSuperuser: false, canCreateDb: false, canCreateRole: false, inheritRole: true, canLogin: false, isReplicationRole: false, canBypassRls: false, activeConnections: 0, connectionLimit: -1, validUntil: null, config: {}, comment: null, ...overrides, }) describe('RoleRow', () => { let queries: string[] beforeEach(() => { queries = [] // useSelectedProjectQuery addAPIMock({ method: 'get', path: '/platform/projects/:ref', // @ts-expect-error partial project is enough for the connection string lookup response: { cloud_provider: 'localhost', id: 1, inserted_at: '2021-08-02T06:40:40.646Z', name: 'Default Project', organization_id: 1, ref: 'default', region: 'local', status: 'ACTIVE_HEALTHY', }, }) // useDatabaseRoleUpdateMutation executes SQL, and the invalidated roles query lists roles addAPIMock({ method: 'post', path: '/platform/pg-meta/:ref/query', response: async ({ request }) => { const body = QueryBodySchema.parse(await request.json()) queries.push(body.query) return HttpResponse.json[]>([]) }, }) }) const renderExpandedRow = async (role: PgRole) => { customRender( ) const user = userEvent.setup() await user.click(await screen.findByText(role.name)) return user } const getLoginSwitch = () => screen.getByLabelText('User can login') const submit = (role: PgRole) => fireEvent.submit(document.getElementById(`role-update-form-${role.id}`)!) const getUpdateQuery = () => queries.find((query) => query.includes('alter role')) test('does not offer a password for a role that can already log in', async () => { await renderExpandedRow(createRole({ canLogin: true })) expect(getLoginSwitch()).toBeChecked() expect(screen.queryByLabelText('Password')).not.toBeInTheDocument() }) test('shows the password field only while login is turned on for a role that cannot log in', async () => { const user = await renderExpandedRow(createRole()) expect(screen.queryByLabelText('Password')).not.toBeInTheDocument() await user.click(getLoginSwitch()) expect(await screen.findByLabelText('Password')).toBeInTheDocument() // The role may already have a password, so the text must not claim it has none expect(screen.getByText(/setting a password replaces any existing one/i)).toBeInTheDocument() await user.click(getLoginSwitch()) expect(screen.queryByLabelText('Password')).not.toBeInTheDocument() }) test('sets the password when login is turned on', async () => { const role = createRole() const user = await renderExpandedRow(role) await user.click(getLoginSwitch()) await user.click(await screen.findByLabelText('Password')) await user.paste('s3cret') submit(role) await waitFor(() => expect(getUpdateQuery()).toBeDefined()) expect(getUpdateQuery()).toMatch(/\blogin\b/) expect(getUpdateQuery()).toContain(`'SCRAM-SHA-256$`) expect(getUpdateQuery()).not.toContain('s3cret') }) test('turns on login without a password when the field is empty', async () => { const role = createRole() const user = await renderExpandedRow(role) await user.click(getLoginSwitch()) submit(role) await waitFor(() => expect(getUpdateQuery()).toBeDefined()) expect(getUpdateQuery()).toMatch(/\blogin\b/) expect(getUpdateQuery()).not.toContain('password') }) test('does not send a password with other changes to a role that can already log in', async () => { const role = createRole({ canLogin: true }) const user = await renderExpandedRow(role) await user.click(screen.getByLabelText('User can create databases')) submit(role) await waitFor(() => expect(getUpdateQuery()).toBeDefined()) expect(getUpdateQuery()).toContain('createdb') expect(getUpdateQuery()).not.toContain('password') }) describe('reset password', () => { const openMenu = async (role: PgRole) => { customRender( ) const user = userEvent.setup() await user.click(await screen.findByRole('button', { name: `${role.name} actions` })) return user } test('is available for a custom role that can log in', async () => { await openMenu(createRole({ canLogin: true })) expect(await screen.findByRole('menuitem', { name: 'Reset password' })).not.toHaveAttribute( 'data-disabled' ) }) test.each([ ['a system role', createRole({ name: 'postgres', canLogin: true })], ['a role that cannot log in', createRole({ canLogin: false })], ])('is disabled for %s', async (_, role) => { await openMenu(role) expect(await screen.findByRole('menuitem', { name: 'Reset password' })).toHaveAttribute( 'data-disabled' ) }) test('requires a password', async () => { const user = await openMenu(createRole({ canLogin: true })) await user.click(await screen.findByRole('menuitem', { name: 'Reset password' })) fireEvent.submit(document.getElementById('reset-role-password-form')!) expect(await screen.findByText('You must provide a password')).toBeInTheDocument() expect(getUpdateQuery()).toBeUndefined() }) test('sets the new password and closes the dialog', async () => { const user = await openMenu(createRole({ canLogin: true })) await user.click(await screen.findByRole('menuitem', { name: 'Reset password' })) await user.click(await screen.findByLabelText('Password')) await user.paste('n3w-s3cret') fireEvent.submit(document.getElementById('reset-role-password-form')!) await waitFor(() => expect(getUpdateQuery()).toBeDefined()) expect(getUpdateQuery()).toContain(`'SCRAM-SHA-256$`) expect(getUpdateQuery()).not.toContain('n3w-s3cret') await waitFor(() => expect(screen.queryByLabelText('Password')).not.toBeInTheDocument()) }) test('closes on cancel without asking when nothing was entered', async () => { const user = await openMenu(createRole({ canLogin: true })) await user.click(await screen.findByRole('menuitem', { name: 'Reset password' })) await user.click(await screen.findByRole('button', { name: 'Cancel' })) await waitFor(() => expect(screen.queryByLabelText('Password')).not.toBeInTheDocument()) expect(screen.queryByText('Unsaved changes')).not.toBeInTheDocument() }) test('asks before discarding an entered password, and starts empty when reopened', async () => { const role = createRole({ canLogin: true }) const user = await openMenu(role) await user.click(await screen.findByRole('menuitem', { name: 'Reset password' })) await user.click(await screen.findByLabelText('Password')) await user.paste('n3w-s3cret') await user.click(await screen.findByRole('button', { name: 'Cancel' })) expect(await screen.findByText('Unsaved changes')).toBeInTheDocument() expect(screen.getByLabelText('Password')).toHaveValue('n3w-s3cret') await user.click(await screen.findByRole('button', { name: 'Discard changes' })) await waitFor(() => expect(screen.queryByLabelText('Password')).not.toBeInTheDocument()) await user.click(await screen.findByRole('button', { name: `${role.name} actions` })) await user.click(await screen.findByRole('menuitem', { name: 'Reset password' })) expect(await screen.findByLabelText('Password')).toHaveValue('') expect(getUpdateQuery()).toBeUndefined() }) test('ignores dismissal while the password is being saved', async () => { let finishUpdate = () => {} const updateFinished = new Promise((resolve) => (finishUpdate = resolve)) addAPIMock({ method: 'post', path: '/platform/pg-meta/:ref/query', response: async ({ request }) => { queries.push(QueryBodySchema.parse(await request.json()).query) await updateFinished return HttpResponse.json[]>([]) }, }) const user = await openMenu(createRole({ canLogin: true })) await user.click(await screen.findByRole('menuitem', { name: 'Reset password' })) await user.click(await screen.findByLabelText('Password')) await user.paste('n3w-s3cret') fireEvent.submit(document.getElementById('reset-role-password-form')!) await waitFor(() => expect(getUpdateQuery()).toBeDefined()) await user.keyboard('{Escape}') expect(screen.queryByText('Unsaved changes')).not.toBeInTheDocument() expect(screen.getByLabelText('Password')).toBeInTheDocument() finishUpdate() await waitFor(() => expect(screen.queryByLabelText('Password')).not.toBeInTheDocument()) }) }) })