|
| 1 | +import { ClerkAPIResponseError } from '@clerk/shared/error'; |
| 2 | +import type { InstanceType, OAuthConsentInfoJSON } from '@clerk/shared/types'; |
| 3 | +import { afterEach, describe, expect, it, type Mock, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { mockFetch } from '@/test/core-fixtures'; |
| 6 | + |
| 7 | +import { SUPPORTED_FAPI_VERSION } from '../../constants'; |
| 8 | +import { createFapiClient } from '../../fapiClient'; |
| 9 | +import { BaseResource } from '../internal'; |
| 10 | +import { OAuthApplication } from '../OAuthApplication'; |
| 11 | + |
| 12 | +const consentPayload: OAuthConsentInfoJSON = { |
| 13 | + object: 'oauth_consent_info', |
| 14 | + id: 'client_abc', |
| 15 | + oauth_application_name: 'My App', |
| 16 | + oauth_application_logo_url: 'https://img.example/logo.png', |
| 17 | + oauth_application_url: 'https://app.example', |
| 18 | + client_id: 'client_abc', |
| 19 | + state: 'st', |
| 20 | + scopes: [{ scope: 'openid', description: 'OpenID', requires_consent: true }], |
| 21 | +}; |
| 22 | + |
| 23 | +describe('OAuthApplication.fetchConsentInfo', () => { |
| 24 | + afterEach(() => { |
| 25 | + (global.fetch as Mock)?.mockClear?.(); |
| 26 | + BaseResource.clerk = null as any; |
| 27 | + vi.restoreAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('throws ClerkRuntimeError when there is no active session', async () => { |
| 31 | + const fetchSpy = vi.spyOn(BaseResource, '_fetch'); |
| 32 | + |
| 33 | + BaseResource.clerk = { |
| 34 | + session: undefined, |
| 35 | + } as any; |
| 36 | + |
| 37 | + await expect(OAuthApplication.fetchConsentInfo({ oauthClientId: 'cid' })).rejects.toMatchObject({ |
| 38 | + code: 'cannot_fetch_oauth_consent_no_session', |
| 39 | + }); |
| 40 | + expect(fetchSpy).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('calls BaseResource._fetch with GET, encoded path, sessionId, optional scope, and skipUpdateClient', async () => { |
| 44 | + const fetchSpy = vi.spyOn(BaseResource, '_fetch').mockResolvedValue({ |
| 45 | + response: consentPayload, |
| 46 | + } as any); |
| 47 | + |
| 48 | + BaseResource.clerk = { |
| 49 | + session: { id: 'sess_test' }, |
| 50 | + } as any; |
| 51 | + |
| 52 | + await OAuthApplication.fetchConsentInfo({ oauthClientId: 'my/client id', scope: 'openid email' }); |
| 53 | + |
| 54 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 55 | + { |
| 56 | + method: 'GET', |
| 57 | + path: '/me/oauth/consent/my%2Fclient%20id', |
| 58 | + search: { scope: 'openid email' }, |
| 59 | + sessionId: 'sess_test', |
| 60 | + }, |
| 61 | + { skipUpdateClient: true }, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('omits search when scope is undefined', async () => { |
| 66 | + const fetchSpy = vi.spyOn(BaseResource, '_fetch').mockResolvedValue({ |
| 67 | + response: consentPayload, |
| 68 | + } as any); |
| 69 | + |
| 70 | + BaseResource.clerk = { |
| 71 | + session: { id: 'sess_test' }, |
| 72 | + } as any; |
| 73 | + |
| 74 | + await OAuthApplication.fetchConsentInfo({ oauthClientId: 'cid' }); |
| 75 | + |
| 76 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 77 | + expect.objectContaining({ |
| 78 | + search: undefined, |
| 79 | + }), |
| 80 | + { skipUpdateClient: true }, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns OAuthConsentInfo from the FAPI response envelope', async () => { |
| 85 | + vi.spyOn(BaseResource, '_fetch').mockResolvedValue({ |
| 86 | + response: consentPayload, |
| 87 | + } as any); |
| 88 | + |
| 89 | + BaseResource.clerk = { |
| 90 | + session: { id: 'sess_test' }, |
| 91 | + } as any; |
| 92 | + |
| 93 | + const info = await OAuthApplication.fetchConsentInfo({ oauthClientId: 'client_abc' }); |
| 94 | + |
| 95 | + expect(info).toEqual({ |
| 96 | + oauth_application_name: 'My App', |
| 97 | + oauth_application_logo_url: 'https://img.example/logo.png', |
| 98 | + oauth_application_url: 'https://app.example', |
| 99 | + client_id: 'client_abc', |
| 100 | + state: 'st', |
| 101 | + scopes: [{ scope: 'openid', description: 'OpenID', requires_consent: true }], |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('defaults scopes to an empty array when absent', async () => { |
| 106 | + vi.spyOn(BaseResource, '_fetch').mockResolvedValue({ |
| 107 | + response: { ...consentPayload, scopes: undefined }, |
| 108 | + } as any); |
| 109 | + |
| 110 | + BaseResource.clerk = { |
| 111 | + session: { id: 'sess_test' }, |
| 112 | + } as any; |
| 113 | + |
| 114 | + const info = await OAuthApplication.fetchConsentInfo({ oauthClientId: 'client_abc' }); |
| 115 | + expect(info.scopes).toEqual([]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('maps ClerkAPIResponseError from FAPI on non-2xx', async () => { |
| 119 | + mockFetch(false, 422, { |
| 120 | + errors: [{ code: 'oauth_consent_error', long_message: 'Consent metadata unavailable' }], |
| 121 | + }); |
| 122 | + |
| 123 | + BaseResource.clerk = { |
| 124 | + session: { id: 'sess_1' }, |
| 125 | + getFapiClient: () => |
| 126 | + createFapiClient({ |
| 127 | + frontendApi: 'clerk.example.com', |
| 128 | + getSessionId: () => 'sess_1', |
| 129 | + instanceType: 'development' as InstanceType, |
| 130 | + }), |
| 131 | + __internal_setCountry: vi.fn(), |
| 132 | + handleUnauthenticated: vi.fn(), |
| 133 | + __internal_handleUnauthenticatedDevBrowser: vi.fn(), |
| 134 | + } as any; |
| 135 | + |
| 136 | + await expect(OAuthApplication.fetchConsentInfo({ oauthClientId: 'cid' })).rejects.toSatisfy( |
| 137 | + (err: unknown) => err instanceof ClerkAPIResponseError && err.message === 'Consent metadata unavailable', |
| 138 | + ); |
| 139 | + |
| 140 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 141 | + const [url] = (global.fetch as Mock).mock.calls[0]; |
| 142 | + expect(url.toString()).toContain(`/v1/me/oauth/consent/cid`); |
| 143 | + expect(url.toString()).toContain(`__clerk_api_version=${SUPPORTED_FAPI_VERSION}`); |
| 144 | + }); |
| 145 | + |
| 146 | + it('throws ClerkRuntimeError when _fetch returns null (offline)', async () => { |
| 147 | + vi.spyOn(BaseResource, '_fetch').mockResolvedValue(null); |
| 148 | + |
| 149 | + BaseResource.clerk = { |
| 150 | + session: { id: 'sess_test' }, |
| 151 | + } as any; |
| 152 | + |
| 153 | + await expect(OAuthApplication.fetchConsentInfo({ oauthClientId: 'cid' })).rejects.toMatchObject({ |
| 154 | + code: 'network_error', |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments