|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { createKeysRepository } from '@/core/modules/keys/repository.server' |
| 3 | + |
| 4 | +const loggerMocks = vi.hoisted(() => ({ |
| 5 | + warn: vi.fn(), |
| 6 | +})) |
| 7 | + |
| 8 | +vi.mock('@/core/shared/clients/supabase/admin', () => ({ |
| 9 | + supabaseAdmin: { |
| 10 | + from: vi.fn(), |
| 11 | + }, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/core/shared/clients/logger/logger', () => ({ |
| 15 | + l: loggerMocks, |
| 16 | + serializeErrorForLog: vi.fn((error: unknown) => error), |
| 17 | +})) |
| 18 | + |
| 19 | +function createApiResponse<T>(input: { |
| 20 | + ok: boolean |
| 21 | + status: number |
| 22 | + data?: T |
| 23 | + error?: { message?: string } | null |
| 24 | +}) { |
| 25 | + return { |
| 26 | + data: input.data, |
| 27 | + error: input.error ?? null, |
| 28 | + response: { |
| 29 | + ok: input.ok, |
| 30 | + status: input.status, |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const baseApiKey = { |
| 36 | + createdAt: '2026-01-01T00:00:00Z', |
| 37 | + id: 'api-key-id', |
| 38 | + mask: { |
| 39 | + prefix: 'e2b', |
| 40 | + valueLength: 16, |
| 41 | + maskedValuePrefix: 'abc', |
| 42 | + maskedValueSuffix: 'xyz', |
| 43 | + }, |
| 44 | + name: 'Key', |
| 45 | +} |
| 46 | + |
| 47 | +describe('createKeysRepository', () => { |
| 48 | + it('hydrates creator emails from Supabase Auth when listing API keys', async () => { |
| 49 | + const firstUserId = '11111111-1111-1111-1111-111111111111' |
| 50 | + const secondUserId = '22222222-2222-2222-2222-222222222222' |
| 51 | + const resolveAuthUserEmailsById = vi.fn().mockResolvedValue( |
| 52 | + new Map([ |
| 53 | + [firstUserId, 'first@e2b.dev'], |
| 54 | + [secondUserId, 'second@e2b.dev'], |
| 55 | + ]) |
| 56 | + ) |
| 57 | + |
| 58 | + const infraClient = { |
| 59 | + GET: vi.fn().mockResolvedValue( |
| 60 | + createApiResponse({ |
| 61 | + ok: true, |
| 62 | + status: 200, |
| 63 | + data: [ |
| 64 | + { |
| 65 | + ...baseApiKey, |
| 66 | + id: 'first-key', |
| 67 | + createdBy: { |
| 68 | + id: firstUserId, |
| 69 | + email: null, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + ...baseApiKey, |
| 74 | + id: 'second-key', |
| 75 | + createdBy: { |
| 76 | + id: secondUserId, |
| 77 | + email: 'deprecated-response-value@e2b.dev', |
| 78 | + }, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }) |
| 82 | + ), |
| 83 | + POST: vi.fn(), |
| 84 | + DELETE: vi.fn(), |
| 85 | + } |
| 86 | + |
| 87 | + const repository = createKeysRepository( |
| 88 | + { |
| 89 | + accessToken: 'token', |
| 90 | + teamId: 'team-id', |
| 91 | + }, |
| 92 | + { |
| 93 | + infraClient: |
| 94 | + infraClient as unknown as typeof import('@/core/shared/clients/api').infra, |
| 95 | + authHeaders: vi.fn(() => ({ 'X-Supabase-Token': 'token' })), |
| 96 | + resolveAuthUserEmailsById, |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + const result = await repository.listTeamApiKeys() |
| 101 | + |
| 102 | + expect(resolveAuthUserEmailsById).toHaveBeenCalledWith([ |
| 103 | + firstUserId, |
| 104 | + secondUserId, |
| 105 | + ]) |
| 106 | + expect(result).toEqual({ |
| 107 | + ok: true, |
| 108 | + data: [ |
| 109 | + expect.objectContaining({ |
| 110 | + id: 'first-key', |
| 111 | + createdBy: { |
| 112 | + id: firstUserId, |
| 113 | + email: 'first@e2b.dev', |
| 114 | + }, |
| 115 | + }), |
| 116 | + expect.objectContaining({ |
| 117 | + id: 'second-key', |
| 118 | + createdBy: { |
| 119 | + id: secondUserId, |
| 120 | + email: 'second@e2b.dev', |
| 121 | + }, |
| 122 | + }), |
| 123 | + ], |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + it('keeps API key listing usable when creator email lookup fails', async () => { |
| 128 | + loggerMocks.warn.mockClear() |
| 129 | + const userId = '11111111-1111-1111-1111-111111111111' |
| 130 | + const lookupError = new Error('lookup failed') |
| 131 | + const resolveAuthUserEmailsById = vi.fn().mockRejectedValue(lookupError) |
| 132 | + |
| 133 | + const apiKey = { |
| 134 | + ...baseApiKey, |
| 135 | + createdBy: { |
| 136 | + id: userId, |
| 137 | + email: null, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + const infraClient = { |
| 142 | + GET: vi.fn().mockResolvedValue( |
| 143 | + createApiResponse({ |
| 144 | + ok: true, |
| 145 | + status: 200, |
| 146 | + data: [apiKey], |
| 147 | + }) |
| 148 | + ), |
| 149 | + POST: vi.fn(), |
| 150 | + DELETE: vi.fn(), |
| 151 | + } |
| 152 | + |
| 153 | + const repository = createKeysRepository( |
| 154 | + { |
| 155 | + accessToken: 'token', |
| 156 | + teamId: 'team-id', |
| 157 | + }, |
| 158 | + { |
| 159 | + infraClient: |
| 160 | + infraClient as unknown as typeof import('@/core/shared/clients/api').infra, |
| 161 | + authHeaders: vi.fn(() => ({ 'X-Supabase-Token': 'token' })), |
| 162 | + resolveAuthUserEmailsById, |
| 163 | + } |
| 164 | + ) |
| 165 | + |
| 166 | + await expect(repository.listTeamApiKeys()).resolves.toEqual({ |
| 167 | + ok: true, |
| 168 | + data: [apiKey], |
| 169 | + }) |
| 170 | + |
| 171 | + expect(loggerMocks.warn).toHaveBeenCalledWith( |
| 172 | + { |
| 173 | + key: 'auth_user_emails:resolve_failed', |
| 174 | + error: lookupError, |
| 175 | + context: { |
| 176 | + userCount: 1, |
| 177 | + }, |
| 178 | + }, |
| 179 | + 'Failed to resolve creator emails from Supabase Auth' |
| 180 | + ) |
| 181 | + }) |
| 182 | +}) |
0 commit comments