Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

3.2 KB
Raw Blame
import { waitFor } from '@testing-library/react'
import { http, HttpResponse } from 'msw'
import { describe, expect, it } from 'vitest'

import { useAuditLogDrainsQuery } from './audit-log-drains-query'
import { useCreateAuditLogDrainMutation } from './create-audit-log-drain-mutation'
import { useDeleteAuditLogDrainMutation } from './delete-audit-log-drain-mutation'
import { useTestLogDrainMutation } from './test-log-drain-mutation'
import { API_URL } from '@/lib/constants'
import { customRenderHook } from '@/tests/lib/custom-render'
import { mswServer } from '@/tests/lib/msw'

const SLUG = 'my-org'
const AUDIT_BASE = `${API_URL}/platform/organizations/:slug/analytics/audit-log-drains`

const REF = 'my-project'
const PROJECT_BASE = `${API_URL}/platform/projects/:ref/analytics/log-drains`

const DRAIN = {
  id: 1,
  token: 'tok-1',
  name: 'Audit drain',
  description: '',
  type: 'webhook',
  config: { url: 'https://example.com' },
}

describe('org audit log drain hooks', () => {
  it('lists audit log drains for an organization', async () => {
    mswServer.use(http.get(AUDIT_BASE, () => HttpResponse.json([DRAIN])))

    const { result } = customRenderHook(() => useAuditLogDrainsQuery({ slug: SLUG }))

    await waitFor(() => expect(result.current.isSuccess).toBe(true))
    expect(result.current.data).toHaveLength(1)
    expect(result.current.data?.[0].name).toBe('Audit drain')
  })

  it('creates an audit log drain at the org-scoped path', async () => {
    let receivedSlug: string | undefined
    mswServer.use(
      http.post(AUDIT_BASE, ({ params }) => {
        receivedSlug = params.slug as string
        return HttpResponse.json(DRAIN)
      })
    )

    const { result } = customRenderHook(() => useCreateAuditLogDrainMutation())

    result.current.mutate({
      slug: SLUG,
      name: 'Audit drain',
      description: '',
      type: 'webhook',
      config: {} as any,
    })

    await waitFor(() => expect(result.current.isSuccess).toBe(true))
    expect(receivedSlug).toBe(SLUG)
  })

  it('deletes an audit log drain by token', async () => {
    let receivedToken: string | undefined
    mswServer.use(
      http.delete(`${AUDIT_BASE}/:token`, ({ params }) => {
        receivedToken = params.token as string
        return new HttpResponse(null, { status: 204 })
      })
    )

    const { result } = customRenderHook(() => useDeleteAuditLogDrainMutation())

    result.current.mutate({ slug: SLUG, token: 'tok-1' })

    await waitFor(() => expect(result.current.isSuccess).toBe(true))
    expect(receivedToken).toBe('tok-1')
  })
})

describe('project log drain hooks', () => {
  it('tests a log drain connection by token at the project-scoped path', async () => {
    let receivedRef: string | undefined
    let receivedToken: string | undefined
    mswServer.use(
      http.post(`${PROJECT_BASE}/:token/test`, ({ params }) => {
        receivedRef = params.ref as string
        receivedToken = params.token as string
        return HttpResponse.json({})
      })
    )

    const { result } = customRenderHook(() => useTestLogDrainMutation())

    result.current.mutate({ projectRef: REF, token: 'tok-1' })

    await waitFor(() => expect(result.current.isSuccess).toBe(true))
    expect(receivedRef).toBe(REF)
    expect(receivedToken).toBe('tok-1')
  })
})