|
| 1 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { useOAuthConsent } from '../useOAuthConsent'; |
| 5 | +import { createMockClerk, createMockQueryClient, createMockUser } from './mocks/clerk'; |
| 6 | +import { wrapper } from './wrapper'; |
| 7 | + |
| 8 | +const consentInfo = { |
| 9 | + oauthApplicationName: 'My App', |
| 10 | + oauthApplicationLogoUrl: 'https://img.example/logo.png', |
| 11 | + oauthApplicationUrl: 'https://app.example', |
| 12 | + clientId: 'client_abc', |
| 13 | + state: 's', |
| 14 | + scopes: [] as { scope: string; description: string | null; requiresConsent: boolean }[], |
| 15 | +}; |
| 16 | + |
| 17 | +const getConsentInfoSpy = vi.fn(() => Promise.resolve(consentInfo)); |
| 18 | + |
| 19 | +const defaultQueryClient = createMockQueryClient(); |
| 20 | + |
| 21 | +const mockClerk = createMockClerk({ |
| 22 | + oauthApplication: { |
| 23 | + getConsentInfo: getConsentInfoSpy, |
| 24 | + }, |
| 25 | + queryClient: defaultQueryClient, |
| 26 | +}); |
| 27 | + |
| 28 | +const userState: { current: { id: string } | null } = { |
| 29 | + current: createMockUser(), |
| 30 | +}; |
| 31 | + |
| 32 | +vi.mock('../../contexts', () => { |
| 33 | + return { |
| 34 | + useAssertWrappedByClerkProvider: () => {}, |
| 35 | + useClerkInstanceContext: () => mockClerk, |
| 36 | + useInitialStateContext: () => undefined, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +vi.mock('../base/useUserBase', () => ({ |
| 41 | + useUserBase: () => userState.current, |
| 42 | +})); |
| 43 | + |
| 44 | +describe('useOAuthConsent', () => { |
| 45 | + beforeEach(() => { |
| 46 | + vi.clearAllMocks(); |
| 47 | + defaultQueryClient.client.clear(); |
| 48 | + mockClerk.loaded = true; |
| 49 | + userState.current = createMockUser(); |
| 50 | + mockClerk.oauthApplication = { |
| 51 | + getConsentInfo: getConsentInfoSpy, |
| 52 | + }; |
| 53 | + window.history.replaceState({}, '', '/'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('fetches consent metadata when signed in', async () => { |
| 57 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: 'my_client' }), { wrapper }); |
| 58 | + |
| 59 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 60 | + |
| 61 | + expect(getConsentInfoSpy).toHaveBeenCalledTimes(1); |
| 62 | + expect(getConsentInfoSpy).toHaveBeenCalledWith({ oauthClientId: 'my_client' }); |
| 63 | + expect(result.current.data).toEqual(consentInfo); |
| 64 | + expect(result.current.error).toBeNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('passes scope to getConsentInfo when provided', async () => { |
| 68 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: 'cid', scope: 'openid email' }), { wrapper }); |
| 69 | + |
| 70 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 71 | + |
| 72 | + expect(getConsentInfoSpy).toHaveBeenCalledWith({ oauthClientId: 'cid', scope: 'openid email' }); |
| 73 | + expect(result.current.data).toEqual(consentInfo); |
| 74 | + }); |
| 75 | + |
| 76 | + it('does not call getConsentInfo when user is null', () => { |
| 77 | + userState.current = null; |
| 78 | + |
| 79 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: 'cid' }), { wrapper }); |
| 80 | + |
| 81 | + expect(getConsentInfoSpy).not.toHaveBeenCalled(); |
| 82 | + expect(result.current.isLoading).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not call getConsentInfo when clerk.loaded is false', () => { |
| 86 | + mockClerk.loaded = false; |
| 87 | + |
| 88 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: 'cid' }), { wrapper }); |
| 89 | + |
| 90 | + expect(getConsentInfoSpy).not.toHaveBeenCalled(); |
| 91 | + expect(result.current.isLoading).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it('does not call getConsentInfo when enabled is false', () => { |
| 95 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: 'cid', enabled: false }), { wrapper }); |
| 96 | + |
| 97 | + expect(getConsentInfoSpy).not.toHaveBeenCalled(); |
| 98 | + expect(result.current.isLoading).toBe(false); |
| 99 | + }); |
| 100 | + |
| 101 | + it('does not call getConsentInfo when oauthClientId is empty', () => { |
| 102 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: '' }), { wrapper }); |
| 103 | + |
| 104 | + expect(getConsentInfoSpy).not.toHaveBeenCalled(); |
| 105 | + expect(result.current.isLoading).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it('uses client_id and scope from the URL when hook params omit them', async () => { |
| 109 | + window.history.replaceState({}, '', '/?client_id=from_url&scope=openid%20email'); |
| 110 | + |
| 111 | + const { result } = renderHook(() => useOAuthConsent(), { wrapper }); |
| 112 | + |
| 113 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 114 | + |
| 115 | + expect(getConsentInfoSpy).toHaveBeenCalledTimes(1); |
| 116 | + expect(getConsentInfoSpy).toHaveBeenCalledWith({ oauthClientId: 'from_url', scope: 'openid email' }); |
| 117 | + expect(result.current.data).toEqual(consentInfo); |
| 118 | + }); |
| 119 | + |
| 120 | + it('prefers explicit oauthClientId over URL client_id', async () => { |
| 121 | + window.history.replaceState({}, '', '/?client_id=from_url'); |
| 122 | + |
| 123 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: 'explicit_id' }), { wrapper }); |
| 124 | + |
| 125 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 126 | + |
| 127 | + expect(getConsentInfoSpy).toHaveBeenCalledWith({ oauthClientId: 'explicit_id' }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('does not fall back to URL client_id when oauthClientId is explicitly empty', () => { |
| 131 | + window.history.replaceState({}, '', '/?client_id=from_url'); |
| 132 | + |
| 133 | + const { result } = renderHook(() => useOAuthConsent({ oauthClientId: '' }), { wrapper }); |
| 134 | + |
| 135 | + expect(getConsentInfoSpy).not.toHaveBeenCalled(); |
| 136 | + expect(result.current.isLoading).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it('prefers explicit scope over URL scope', async () => { |
| 140 | + window.history.replaceState({}, '', '/?client_id=cid&scope=from_url'); |
| 141 | + |
| 142 | + const { result } = renderHook(() => useOAuthConsent({ scope: 'explicit_scope' }), { wrapper }); |
| 143 | + |
| 144 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 145 | + |
| 146 | + expect(getConsentInfoSpy).toHaveBeenCalledWith({ oauthClientId: 'cid', scope: 'explicit_scope' }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments