Files
supabase/apps/studio/lib/api/apiAuthenticate.test.ts
T
Alaister YoungandAlaister Young 0252fd8cad [FE-5114] (part 7) – refactor(studio): remove Next API adapter (#54034)
## Problem

With all Studio API routes migrated to native TanStack Start handlers
(or removed when unused) in parts 1–6, the Next Pages Router API adapter
and legacy `NextApiRequest`/`NextApiResponse` wrappers are no longer
used. Part 7 of 7 for
[FE-5114](https://linear.app/supabase/issue/FE-5114/rewrite-api-routes-as-native-tanstack-start-handlers-and-remove-the)
tears them down.

## Solution

**Removed:**

- `compat/next/api.ts` and `compat/next/api.test.ts` (`toWebHandler`)
- Legacy `apiWrapper`, `apiAuthenticate`, and `fetchUserClaims`
(`NextApiRequest`/`NextApiResponse`) functions and their legacy tests in
`lib/api/apiWrapper.ts`, `lib/api/apiAuthenticate.ts`,
`lib/api/apiWrappers.test.ts`, and `lib/api/apiAuthenticate.test.ts`
- The now-unused `fromNodeHeaders` helper (and its `node:http` import)
and its tests in `lib/api/apiHelpers.ts` / `lib/api/apiHelpers.test.ts`

**Changed:**

- Updated comment in `lib/server/configcat.ts` from `apiAuthenticate` to
`authenticateRequest`

## Review instructions

1. Confirm `git grep -E
"NextApiRequest|NextApiResponse|compat/next/api|toWebHandler|fromNodeHeaders|from
'next'" apps/studio/` returns no matches.
2. Run `pnpm --filter studio run typecheck`, `pnpm knip --workspace
apps/studio`, and `pnpm --filter studio exec vitest run lib/api/
tests/routes/api/ scripts/__tests__/server-source-maps.test.ts`.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 23:01:47 +08:00

80 lines
2.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { authenticateRequest } from './apiAuthenticate'
const mocks = vi.hoisted(() => {
return {
getUserClaims: vi.fn().mockResolvedValue({
claims: {
sub: 'test-gotrue-id',
email: 'test@example.com',
},
error: null,
}),
}
})
vi.mock('@/lib/gotrue', () => ({
getUserClaims: mocks.getUserClaims,
}))
describe('authenticateRequest', () => {
beforeEach(() => {
mocks.getUserClaims.mockResolvedValue({
claims: {
sub: 'test-gotrue-id',
email: 'test@example.com',
},
error: null,
})
})
it('returns claims when authorization header contains a valid bearer token', async () => {
const request = new Request('http://localhost/api/test', {
headers: { Authorization: 'Bearer test-token' },
})
const result = await authenticateRequest(request)
expect(result).toStrictEqual({
sub: 'test-gotrue-id',
email: 'test@example.com',
})
expect(mocks.getUserClaims).toHaveBeenCalledWith('test-token')
})
it('returns error when authorization header is missing', async () => {
const request = new Request('http://localhost/api/test')
const result = await authenticateRequest(request)
expect(result).toStrictEqual({ error: new Error('missing access token') })
})
it('returns error when auth user fetch fails', async () => {
mocks.getUserClaims.mockResolvedValue({
claims: null,
error: new Error('Auth failed'),
})
const request = new Request('http://localhost/api/test', {
headers: { Authorization: 'Bearer test-token' },
})
const result = await authenticateRequest(request)
expect(result).toStrictEqual({ error: new Error('Auth failed') })
})
it('returns error when user does not exist', async () => {
mocks.getUserClaims.mockResolvedValue({
claims: null,
error: null,
})
const request = new Request('http://localhost/api/test', {
headers: { Authorization: 'Bearer test-token' },
})
const result = await authenticateRequest(request)
expect(result).toStrictEqual({ error: new Error('The user does not exist') })
})
})