Skip to content

Commit d91fd6a

Browse files
committed
add tests
1 parent 6839dc1 commit d91fd6a

File tree

2 files changed

+333
-0
lines changed

2 files changed

+333
-0
lines changed

src/public/sso.spec.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { getAuthorizationUrl } from './sso';
2+
3+
describe('Public SSO Methods', () => {
4+
describe('getAuthorizationUrl', () => {
5+
it('builds correct URL with provider', () => {
6+
const url = getAuthorizationUrl({
7+
clientId: 'client_123',
8+
redirectUri: 'https://app.com/callback',
9+
provider: 'GoogleOAuth',
10+
});
11+
12+
expect(url).toContain('https://api.workos.com/sso/authorize');
13+
expect(url).toContain('client_id=client_123');
14+
expect(url).toContain('redirect_uri=https%3A%2F%2Fapp.com%2Fcallback');
15+
expect(url).toContain('provider=GoogleOAuth');
16+
expect(url).toContain('response_type=code');
17+
});
18+
19+
it('builds URL with organization', () => {
20+
const url = getAuthorizationUrl({
21+
clientId: 'client_123',
22+
redirectUri: 'https://app.com/callback',
23+
organization: 'org_123',
24+
});
25+
26+
expect(url).toContain('organization=org_123');
27+
});
28+
29+
it('builds URL with connection', () => {
30+
const url = getAuthorizationUrl({
31+
clientId: 'client_123',
32+
redirectUri: 'https://app.com/callback',
33+
connection: 'conn_123',
34+
});
35+
36+
expect(url).toContain('connection=conn_123');
37+
});
38+
39+
it('includes state parameter', () => {
40+
const url = getAuthorizationUrl({
41+
clientId: 'client_123',
42+
redirectUri: 'https://app.com/callback',
43+
provider: 'GoogleOAuth',
44+
state: 'custom state',
45+
});
46+
47+
expect(url).toContain('state=custom+state');
48+
});
49+
50+
it('includes domainHint and loginHint', () => {
51+
const url = getAuthorizationUrl({
52+
clientId: 'client_123',
53+
redirectUri: 'https://app.com/callback',
54+
provider: 'GoogleOAuth',
55+
domainHint: 'example.com',
56+
loginHint: '[email protected]',
57+
});
58+
59+
expect(url).toContain('domain_hint=example.com');
60+
expect(url).toContain('login_hint=user%40example.com');
61+
});
62+
63+
it('includes provider scopes', () => {
64+
const url = getAuthorizationUrl({
65+
clientId: 'client_123',
66+
redirectUri: 'https://app.com/callback',
67+
provider: 'GoogleOAuth',
68+
providerScopes: ['read_api', 'read_repository'],
69+
});
70+
71+
expect(url).toContain('provider_scopes=read_api');
72+
expect(url).toContain('provider_scopes=read_repository');
73+
});
74+
75+
it('includes provider query params', () => {
76+
const url = getAuthorizationUrl({
77+
clientId: 'client_123',
78+
redirectUri: 'https://app.com/callback',
79+
provider: 'GoogleOAuth',
80+
providerQueryParams: {
81+
foo: 'bar',
82+
baz: 123,
83+
bool: true,
84+
},
85+
});
86+
87+
expect(url).toContain('provider_query_params%5Bfoo%5D=bar');
88+
expect(url).toContain('provider_query_params%5Bbaz%5D=123');
89+
expect(url).toContain('provider_query_params%5Bbool%5D=true');
90+
});
91+
92+
it('uses custom baseURL when provided', () => {
93+
const url = getAuthorizationUrl({
94+
clientId: 'client_123',
95+
redirectUri: 'https://app.com/callback',
96+
provider: 'GoogleOAuth',
97+
baseURL: 'https://api.workos.dev',
98+
});
99+
100+
expect(url).toContain('https://api.workos.dev/sso/authorize');
101+
});
102+
103+
it('throws error when no provider, connection, or organization specified', () => {
104+
expect(() => {
105+
getAuthorizationUrl({
106+
clientId: 'client_123',
107+
redirectUri: 'https://app.com/callback',
108+
});
109+
}).toThrow(
110+
"Incomplete arguments. Need to specify either a 'connection', 'organization', or 'provider'.",
111+
);
112+
});
113+
});
114+
});

src/public/user-management.spec.ts

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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

Comments
 (0)