project-supabase-client.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as supabaseJs from '@supabase/supabase-js'
  2. import { beforeEach, describe, expect, it, vi } from 'vitest'
  3. import { createProjectSupabaseClient } from './project-supabase-client'
  4. import * as apiKeysUtils from '@/data/api-keys/temp-api-keys-utils'
  5. vi.mock('@supabase/supabase-js', () => ({
  6. createClient: vi.fn(),
  7. }))
  8. vi.mock('@/data/api-keys/temp-api-keys-utils', () => ({
  9. getOrRefreshTemporaryApiKey: vi.fn(),
  10. }))
  11. describe('project-supabase-client', () => {
  12. beforeEach(() => {
  13. vi.clearAllMocks()
  14. })
  15. describe('createProjectSupabaseClient', () => {
  16. it('should create a Supabase client with temporary API key', async () => {
  17. const mockApiKey = 'test-api-key-123'
  18. const mockClient = { from: vi.fn() }
  19. const projectRef = 'test-project-ref'
  20. const clientEndpoint = 'https://test.supabase.co'
  21. vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
  22. apiKey: mockApiKey,
  23. expiryTimeMs: Date.now() + 3600000,
  24. })
  25. vi.mocked(supabaseJs.createClient).mockReturnValue(mockClient as any)
  26. const result = await createProjectSupabaseClient(projectRef, clientEndpoint)
  27. expect(apiKeysUtils.getOrRefreshTemporaryApiKey).toHaveBeenCalledWith(projectRef)
  28. expect(supabaseJs.createClient).toHaveBeenCalledWith(clientEndpoint, mockApiKey, {
  29. auth: {
  30. persistSession: false,
  31. autoRefreshToken: false,
  32. detectSessionInUrl: false,
  33. storage: {
  34. getItem: expect.any(Function),
  35. setItem: expect.any(Function),
  36. removeItem: expect.any(Function),
  37. },
  38. },
  39. })
  40. expect(result).toBe(mockClient)
  41. })
  42. it('should configure storage to not persist session', async () => {
  43. const mockApiKey = 'test-api-key-456'
  44. const mockClient = { from: vi.fn() }
  45. vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
  46. apiKey: mockApiKey,
  47. expiryTimeMs: Date.now() + 3600000,
  48. })
  49. vi.mocked(supabaseJs.createClient).mockReturnValue(mockClient as any)
  50. await createProjectSupabaseClient('ref', 'https://example.com')
  51. const config = vi.mocked(supabaseJs.createClient).mock.calls[0][2]
  52. if (!config?.auth?.storage) throw new Error('storage config is missing')
  53. const storage = config.auth.storage
  54. // Test storage methods return expected values
  55. expect(storage.getItem('any-key')).toBeNull()
  56. expect(storage.setItem('key', 'value')).toBeUndefined()
  57. expect(storage.removeItem('key')).toBeUndefined()
  58. })
  59. it('should throw error if API key retrieval fails', async () => {
  60. const error = new Error('Failed to get API key')
  61. vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockRejectedValue(error)
  62. await expect(createProjectSupabaseClient('ref', 'https://example.com')).rejects.toThrow(
  63. 'Failed to get API key'
  64. )
  65. expect(supabaseJs.createClient).not.toHaveBeenCalled()
  66. })
  67. it('should pass through different project refs and endpoints', async () => {
  68. const mockApiKey = 'api-key'
  69. const mockClient = { from: vi.fn() }
  70. vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
  71. apiKey: mockApiKey,
  72. expiryTimeMs: Date.now() + 3600000,
  73. })
  74. vi.mocked(supabaseJs.createClient).mockReturnValue(mockClient as any)
  75. await createProjectSupabaseClient('project-123', 'https://project123.supabase.co')
  76. expect(apiKeysUtils.getOrRefreshTemporaryApiKey).toHaveBeenCalledWith('project-123')
  77. expect(supabaseJs.createClient).toHaveBeenCalledWith(
  78. 'https://project123.supabase.co',
  79. mockApiKey,
  80. expect.any(Object)
  81. )
  82. })
  83. it('should disable session persistence options', async () => {
  84. const mockApiKey = 'api-key'
  85. const mockClient = { from: vi.fn() }
  86. vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
  87. apiKey: mockApiKey,
  88. expiryTimeMs: Date.now() + 3600000,
  89. })
  90. vi.mocked(supabaseJs.createClient).mockReturnValue(mockClient as any)
  91. await createProjectSupabaseClient('ref', 'https://example.com')
  92. const config = vi.mocked(supabaseJs.createClient).mock.calls[0][2]
  93. if (!config?.auth) throw new Error('auth config is missing')
  94. expect(config.auth.persistSession).toBe(false)
  95. expect(config.auth.autoRefreshToken).toBe(false)
  96. expect(config.auth.detectSessionInUrl).toBe(false)
  97. })
  98. })
  99. })