|
| 1 | +import { |
| 2 | + getAuthorizationUrl, |
| 3 | + getLogoutUrl, |
| 4 | + getJwksUrl, |
| 5 | +} from './user-management'; |
| 6 | + |
| 7 | +describe('Public User Management Methods', () => { |
| 8 | + describe('getAuthorizationUrl', () => { |
| 9 | + it('builds correct URL with required params', () => { |
| 10 | + const url = getAuthorizationUrl({ |
| 11 | + clientId: 'client_123', |
| 12 | + redirectUri: 'https://app.com/callback', |
| 13 | + provider: 'authkit', |
| 14 | + }); |
| 15 | + |
| 16 | + expect(url).toContain('https://api.workos.com/user_management/authorize'); |
| 17 | + expect(url).toContain('client_id=client_123'); |
| 18 | + expect(url).toContain('redirect_uri=https%3A%2F%2Fapp.com%2Fcallback'); |
| 19 | + expect(url).toContain('provider=authkit'); |
| 20 | + expect(url).toContain('response_type=code'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('builds URL with organization ID', () => { |
| 24 | + const url = getAuthorizationUrl({ |
| 25 | + clientId: 'client_123', |
| 26 | + redirectUri: 'https://app.com/callback', |
| 27 | + organizationId: 'org_123', |
| 28 | + }); |
| 29 | + |
| 30 | + expect(url).toContain('organization_id=org_123'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('builds URL with connection ID', () => { |
| 34 | + const url = getAuthorizationUrl({ |
| 35 | + clientId: 'client_123', |
| 36 | + redirectUri: 'https://app.com/callback', |
| 37 | + connectionId: 'conn_123', |
| 38 | + }); |
| 39 | + |
| 40 | + expect(url).toContain('connection_id=conn_123'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('includes code challenge for PKCE', () => { |
| 44 | + const url = getAuthorizationUrl({ |
| 45 | + clientId: 'client_123', |
| 46 | + redirectUri: 'https://app.com/callback', |
| 47 | + provider: 'authkit', |
| 48 | + codeChallenge: 'challenge_123', |
| 49 | + codeChallengeMethod: 'S256', |
| 50 | + }); |
| 51 | + |
| 52 | + expect(url).toContain('code_challenge=challenge_123'); |
| 53 | + expect(url).toContain('code_challenge_method=S256'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('includes screenHint for authkit provider', () => { |
| 57 | + const url = getAuthorizationUrl({ |
| 58 | + clientId: 'client_123', |
| 59 | + redirectUri: 'https://app.com/callback', |
| 60 | + provider: 'authkit', |
| 61 | + screenHint: 'sign-up', |
| 62 | + }); |
| 63 | + |
| 64 | + expect(url).toContain('screen_hint=sign-up'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('throws error for screenHint with non-authkit provider', () => { |
| 68 | + expect(() => { |
| 69 | + getAuthorizationUrl({ |
| 70 | + clientId: 'client_123', |
| 71 | + redirectUri: 'https://app.com/callback', |
| 72 | + provider: 'GoogleOAuth', |
| 73 | + screenHint: 'sign-up', |
| 74 | + }); |
| 75 | + }).toThrow("'screenHint' is only supported for 'authkit' provider"); |
| 76 | + }); |
| 77 | + |
| 78 | + it('includes state parameter', () => { |
| 79 | + const url = getAuthorizationUrl({ |
| 80 | + clientId: 'client_123', |
| 81 | + redirectUri: 'https://app.com/callback', |
| 82 | + provider: 'authkit', |
| 83 | + state: 'custom state', |
| 84 | + }); |
| 85 | + |
| 86 | + expect(url).toContain('state=custom+state'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('includes provider scopes', () => { |
| 90 | + const url = getAuthorizationUrl({ |
| 91 | + clientId: 'client_123', |
| 92 | + redirectUri: 'https://app.com/callback', |
| 93 | + provider: 'GoogleOAuth', |
| 94 | + providerScopes: ['read_api', 'read_repository'], |
| 95 | + }); |
| 96 | + |
| 97 | + expect(url).toContain('provider_scopes=read_api'); |
| 98 | + expect(url).toContain('provider_scopes=read_repository'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('includes provider query params', () => { |
| 102 | + const url = getAuthorizationUrl({ |
| 103 | + clientId: 'client_123', |
| 104 | + redirectUri: 'https://app.com/callback', |
| 105 | + provider: 'GoogleOAuth', |
| 106 | + providerQueryParams: { |
| 107 | + foo: 'bar', |
| 108 | + baz: 123, |
| 109 | + bool: true, |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + expect(url).toContain('provider_query_params%5Bfoo%5D=bar'); |
| 114 | + expect(url).toContain('provider_query_params%5Bbaz%5D=123'); |
| 115 | + expect(url).toContain('provider_query_params%5Bbool%5D=true'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('uses custom baseURL when provided', () => { |
| 119 | + const url = getAuthorizationUrl({ |
| 120 | + clientId: 'client_123', |
| 121 | + redirectUri: 'https://app.com/callback', |
| 122 | + provider: 'authkit', |
| 123 | + baseURL: 'https://api.workos.dev', |
| 124 | + }); |
| 125 | + |
| 126 | + expect(url).toContain('https://api.workos.dev/user_management/authorize'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('throws error when no provider, connection, or organization specified', () => { |
| 130 | + expect(() => { |
| 131 | + getAuthorizationUrl({ |
| 132 | + clientId: 'client_123', |
| 133 | + redirectUri: 'https://app.com/callback', |
| 134 | + }); |
| 135 | + }).toThrow( |
| 136 | + "Incomplete arguments. Need to specify either a 'connectionId', 'organizationId', or 'provider'.", |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('warns about deprecated context parameter', () => { |
| 141 | + const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(); |
| 142 | + |
| 143 | + getAuthorizationUrl({ |
| 144 | + clientId: 'client_123', |
| 145 | + redirectUri: 'https://app.com/callback', |
| 146 | + provider: 'authkit', |
| 147 | + context: 'deprecated_context', |
| 148 | + }); |
| 149 | + |
| 150 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 151 | + expect.stringContaining('context` is deprecated'), |
| 152 | + ); |
| 153 | + |
| 154 | + consoleSpy.mockRestore(); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('getLogoutUrl', () => { |
| 159 | + it('builds correct logout URL', () => { |
| 160 | + const url = getLogoutUrl({ |
| 161 | + sessionId: 'session_123', |
| 162 | + }); |
| 163 | + |
| 164 | + expect(url).toBe( |
| 165 | + 'https://api.workos.com/user_management/sessions/logout?session_id=session_123', |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it('includes returnTo parameter when provided', () => { |
| 170 | + const url = getLogoutUrl({ |
| 171 | + sessionId: 'session_123', |
| 172 | + returnTo: 'https://app.com/home', |
| 173 | + }); |
| 174 | + |
| 175 | + expect(url).toBe( |
| 176 | + 'https://api.workos.com/user_management/sessions/logout?session_id=session_123&return_to=https%3A%2F%2Fapp.com%2Fhome', |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + it('uses custom baseURL when provided', () => { |
| 181 | + const url = getLogoutUrl({ |
| 182 | + sessionId: 'session_123', |
| 183 | + baseURL: 'https://api.workos.dev', |
| 184 | + }); |
| 185 | + |
| 186 | + expect(url).toBe( |
| 187 | + 'https://api.workos.dev/user_management/sessions/logout?session_id=session_123', |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + it('throws error when sessionId is not provided', () => { |
| 192 | + expect(() => { |
| 193 | + getLogoutUrl({ |
| 194 | + sessionId: '', |
| 195 | + }); |
| 196 | + }).toThrow("Incomplete arguments. Need to specify 'sessionId'."); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('getJwksUrl', () => { |
| 201 | + it('builds correct JWKS URL', () => { |
| 202 | + const url = getJwksUrl('client_123'); |
| 203 | + |
| 204 | + expect(url).toBe('https://api.workos.com/sso/jwks/client_123'); |
| 205 | + }); |
| 206 | + |
| 207 | + it('uses custom baseURL when provided', () => { |
| 208 | + const url = getJwksUrl('client_123', 'https://api.workos.dev'); |
| 209 | + |
| 210 | + expect(url).toBe('https://api.workos.dev/sso/jwks/client_123'); |
| 211 | + }); |
| 212 | + |
| 213 | + it('throws error when clientId is not provided', () => { |
| 214 | + expect(() => { |
| 215 | + getJwksUrl(''); |
| 216 | + }).toThrow('clientId must be a valid clientId'); |
| 217 | + }); |
| 218 | + }); |
| 219 | +}); |
0 commit comments