|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { dbChainMockFns, resetDbChainMock } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { adapter } = vi.hoisted(() => ({ |
| 8 | + adapter: { |
| 9 | + provider: 'gmail' as const, |
| 10 | + requiresRefreshToken: true, |
| 11 | + getPolicy: vi.fn(), |
| 12 | + prepareAuthorization: vi.fn(), |
| 13 | + exchangeAndVerify: vi.fn(), |
| 14 | + hasRequiredScopes: vi.fn(), |
| 15 | + refreshToken: vi.fn(), |
| 16 | + isTerminalRefreshError: vi.fn(), |
| 17 | + }, |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/lib/credential-groups/provider-registry', () => ({ |
| 21 | + getCredentialGroupProviderAdapter: () => adapter, |
| 22 | +})) |
| 23 | + |
| 24 | +import { completeCredentialGroupOAuth } from '@/lib/credential-groups/oauth' |
| 25 | + |
| 26 | +const POLICY = { |
| 27 | + provider: 'gmail' as const, |
| 28 | + providerId: 'google-email', |
| 29 | + authorizationAppId: 'google:client', |
| 30 | + requiredScopes: ['openid', 'https://www.googleapis.com/auth/gmail.modify'], |
| 31 | + scopeVersion: 1, |
| 32 | +} |
| 33 | + |
| 34 | +const CONTEXT = { |
| 35 | + enrollmentId: 'enrollment-1', |
| 36 | + credentialGroupId: 'group-1', |
| 37 | + workspaceId: 'workspace-1', |
| 38 | + workspaceName: 'Workspace', |
| 39 | + workspaceOwnerId: 'owner-1', |
| 40 | + email: 'person@example.com', |
| 41 | + enrollmentStatus: 'in_progress' as const, |
| 42 | + option: { |
| 43 | + id: 'option-1', |
| 44 | + provider: 'gmail' as const, |
| 45 | + label: 'Gmail', |
| 46 | + required: true, |
| 47 | + status: 'active' as const, |
| 48 | + }, |
| 49 | + options: [], |
| 50 | +} |
| 51 | + |
| 52 | +describe('credential group OAuth persistence', () => { |
| 53 | + beforeEach(() => { |
| 54 | + vi.clearAllMocks() |
| 55 | + resetDbChainMock() |
| 56 | + adapter.getPolicy.mockResolvedValue(POLICY) |
| 57 | + adapter.exchangeAndVerify.mockResolvedValue({ |
| 58 | + providerId: POLICY.providerId, |
| 59 | + providerSubjectId: 'google-subject-1', |
| 60 | + providerTenantId: null, |
| 61 | + displayName: 'person@example.com', |
| 62 | + metadata: { email: 'person@example.com' }, |
| 63 | + accessToken: 'access-token', |
| 64 | + refreshToken: 'refresh-token', |
| 65 | + grantedScopes: POLICY.requiredScopes, |
| 66 | + accessTokenExpiresAt: new Date('2026-08-14T00:00:00Z'), |
| 67 | + refreshTokenExpiresAt: null, |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + it('does not reactivate a credential after its enrollment is revoked', async () => { |
| 72 | + dbChainMockFns.limit.mockResolvedValueOnce([{ status: 'revoked' }]) |
| 73 | + |
| 74 | + await expect( |
| 75 | + completeCredentialGroupOAuth( |
| 76 | + CONTEXT, |
| 77 | + { |
| 78 | + state: 'state-1', |
| 79 | + provider: 'gmail', |
| 80 | + nonceHash: 'nonce-hash', |
| 81 | + enrollmentId: CONTEXT.enrollmentId, |
| 82 | + credentialGroupId: CONTEXT.credentialGroupId, |
| 83 | + optionId: CONTEXT.option.id, |
| 84 | + authorizationAppId: POLICY.authorizationAppId, |
| 85 | + scopeVersion: POLICY.scopeVersion, |
| 86 | + requiredScopes: POLICY.requiredScopes, |
| 87 | + redirectUri: 'https://sim.ai/api/credential-groups/oauth/gmail/callback', |
| 88 | + codeVerifier: 'verifier', |
| 89 | + invitationToken: 'invitation-token', |
| 90 | + createdAt: Date.now(), |
| 91 | + }, |
| 92 | + 'authorization-code' |
| 93 | + ) |
| 94 | + ).rejects.toThrow('This account invitation was revoked.') |
| 95 | + |
| 96 | + expect(dbChainMockFns.execute).toHaveBeenCalledTimes(2) |
| 97 | + expect(dbChainMockFns.update).not.toHaveBeenCalled() |
| 98 | + expect(dbChainMockFns.insert).not.toHaveBeenCalled() |
| 99 | + }) |
| 100 | +}) |
0 commit comments