|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + authGetUser: vi.fn(), |
| 5 | + from: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock('@supabase/supabase-js', () => ({ |
| 9 | + createClient: vi.fn(() => ({ |
| 10 | + auth: { getUser: mocks.authGetUser }, |
| 11 | + from: mocks.from, |
| 12 | + })), |
| 13 | +})); |
| 14 | + |
| 15 | +describe('all public keys cookie authentication', () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.resetModules(); |
| 18 | + vi.clearAllMocks(); |
| 19 | + process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://example.supabase.co'; |
| 20 | + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'anon-key'; |
| 21 | + process.env.SUPABASE_SERVICE_ROLE_KEY = 'service-key'; |
| 22 | + mocks.authGetUser.mockResolvedValue({ data: { user: { id: 'auth-user-id' } }, error: null }); |
| 23 | + mocks.from.mockReturnValue({ |
| 24 | + select: vi.fn(() => ({ |
| 25 | + not: vi.fn(() => Promise.resolve({ data: [{ user_id: 'user-1', public_key: 'key-1' }], error: null })), |
| 26 | + })), |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('preserves equals padding in base64 auth cookies', async () => { |
| 31 | + const { GET } = await import('./route.js'); |
| 32 | + const response = await GET( |
| 33 | + new Request('https://qrypt.chat/api/crypto/public-keys/all', { |
| 34 | + headers: { |
| 35 | + cookie: 'sb-xydzwxwsbgmznthiiscl-auth-token=base64-eyJhY2Nlc3NfdG9rZW4iOiJhYmMiLCJwYWRkaW5nIjoieCJ9==', |
| 36 | + }, |
| 37 | + }), |
| 38 | + ); |
| 39 | + |
| 40 | + expect(response.status).toBe(200); |
| 41 | + expect(await response.json()).toEqual([{ user_id: 'user-1', public_key: 'key-1' }]); |
| 42 | + expect(mocks.authGetUser).toHaveBeenCalledWith('abc'); |
| 43 | + }); |
| 44 | +}); |
0 commit comments