The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
test(studio): cover Supabase API key formats in project client tests (#47960)
Katerina Skroumpelou069051b03270
diff --git a/apps/studio/lib/project-supabase-client.contract.test.ts b/apps/studio/lib/project-supabase-client.contract.test.tsnew file mode 100644index 00000000000..9f0ed4fcb0d--- /dev/null+++ b/apps/studio/lib/project-supabase-client.contract.test.ts@@ -0,0 +1,36 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'++import { createProjectSupabaseClient } from './project-supabase-client'+import * as apiKeysUtils from '@/data/api-keys/temp-api-keys-utils'++// Unlike project-supabase-client.test.ts, this file does not mock+// @supabase/supabase-js: the real client constructor must run.+vi.mock('@/data/api-keys/temp-api-keys-utils', () => ({+ getOrRefreshTemporaryApiKey: vi.fn(),+}))++// Supabase API key formats this client must accept.+const KEY_SHAPES = [+ ['temporary key', 'sb_temp_nonce1234567890ab_payload'],+ ['publishable key', 'sb_publishable_abc123'],+ ['secret key', 'sb_secret_abc123'],+ ['legacy JWT key', 'eyJhbGciOiJIUzI1NiJ9.payload.signature'],+]++describe('createProjectSupabaseClient key format contract', () => {+ beforeEach(() => {+ vi.clearAllMocks()+ })++ it.each(KEY_SHAPES)('constructs a client with a %s', async (_label, apiKey) => {+ vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({+ apiKey,+ expiryTimeMs: Date.now() + 3600000,+ })++ const client = await createProjectSupabaseClient('test-ref', 'https://test.supabase.co')++ expect(client).toBeDefined()+ expect(client.auth).toBeDefined()+ })+})diff --git a/apps/studio/lib/project-supabase-client.test.ts b/apps/studio/lib/project-supabase-client.test.tsindex 62f4a1a47f6..8a50f9b5f4e 100644--- a/apps/studio/lib/project-supabase-client.test.ts+++ b/apps/studio/lib/project-supabase-client.test.ts@@ -19,7 +19,7 @@ describe('project-supabase-client', () => {describe('createProjectSupabaseClient', () => {it('should create a Supabase client with temporary API key', async () => {- const mockApiKey = 'test-api-key-123'+ const mockApiKey = 'sb_temp_nonce1234567890ab_payload1'const mockClient = { from: vi.fn() }const projectRef = 'test-project-ref'const clientEndpoint = 'https://test.supabase.co'@@ -49,7 +49,7 @@ describe('project-supabase-client', () => {})it('should configure storage to not persist session', async () => {- const mockApiKey = 'test-api-key-456'+ const mockApiKey = 'sb_temp_nonce1234567890ab_payload2'const mockClient = { from: vi.fn() }vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({@@ -82,7 +82,7 @@ describe('project-supabase-client', () => {})it('should pass through different project refs and endpoints', async () => {- const mockApiKey = 'api-key'+ const mockApiKey = 'sb_temp_nonce1234567890ab_payload3'const mockClient = { from: vi.fn() }vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({@@ -102,7 +102,7 @@ describe('project-supabase-client', () => {})it('should disable session persistence options', async () => {- const mockApiKey = 'api-key'+ const mockApiKey = 'sb_temp_nonce1234567890ab_payload4'const mockClient = { from: vi.fn() }vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({