|
1 |
| -import { describe, it, expect } from "vitest"; |
| 1 | +import { describe, it, expect, vi } from "vitest"; |
2 | 2 | import { IssuerRouteTypes, LoginOptions, PromptTypes, Scopes } from "../types";
|
3 | 3 | import { generateAuthUrl } from "./generateAuthUrl";
|
4 | 4 | import { MemoryStorage, StorageKeys } from "../sessionManager";
|
@@ -228,4 +228,90 @@ describe("generateAuthUrl", () => {
|
228 | 228 | result.url.searchParams.delete("state");
|
229 | 229 | expect(result.url.toString()).toBe(expectedUrl);
|
230 | 230 | });
|
| 231 | + |
| 232 | + it("Properties are added when defined", async () => { |
| 233 | + const domain = "https://auth.example.com"; |
| 234 | + const options: LoginOptions = { |
| 235 | + clientId: "client123", |
| 236 | + scope: [Scopes.openid, Scopes.profile, Scopes.offline_access], |
| 237 | + redirectURL: "https://example2.com", |
| 238 | + prompt: PromptTypes.create, |
| 239 | + properties: { |
| 240 | + utm_campaign: "test", |
| 241 | + }, |
| 242 | + }; |
| 243 | + const expectedUrl = |
| 244 | + "https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile+offline&prompt=create&code_challenge_method=S256&utm_campaign=test"; |
| 245 | + |
| 246 | + const result = await generateAuthUrl( |
| 247 | + domain, |
| 248 | + IssuerRouteTypes.login, |
| 249 | + options, |
| 250 | + ); |
| 251 | + const nonce = result.url.searchParams.get("nonce"); |
| 252 | + expect(nonce).not.toBeNull(); |
| 253 | + expect(nonce!.length).toBe(16); |
| 254 | + const state = result.url.searchParams.get("state"); |
| 255 | + expect(state).not.toBeNull(); |
| 256 | + expect(state!.length).toBe(32); |
| 257 | + const codeChallenge = result.url.searchParams.get("code_challenge"); |
| 258 | + expect(codeChallenge!.length).toBeGreaterThanOrEqual(27); |
| 259 | + result.url.searchParams.delete("code_challenge"); |
| 260 | + result.url.searchParams.delete("nonce"); |
| 261 | + result.url.searchParams.delete("state"); |
| 262 | + expect(result.url.toString()).toBe(expectedUrl); |
| 263 | + }); |
| 264 | + |
| 265 | + it("When non whitelisted properties are added when defined, warn for each one do not add to the url", async () => { |
| 266 | + const consoleWarnSpy = vi.spyOn(console, "warn"); |
| 267 | + |
| 268 | + const domain = "https://auth.example.com"; |
| 269 | + const options: LoginOptions<{ |
| 270 | + testProperty1: string; |
| 271 | + testProperty2: string; |
| 272 | + }> = { |
| 273 | + clientId: "client123", |
| 274 | + scope: [Scopes.openid, Scopes.profile, Scopes.offline_access], |
| 275 | + redirectURL: "https://example2.com", |
| 276 | + prompt: PromptTypes.create, |
| 277 | + properties: { |
| 278 | + utm_campaign: "test", |
| 279 | + testProperty1: "testValue1", |
| 280 | + testProperty2: "testValue2", |
| 281 | + }, |
| 282 | + }; |
| 283 | + const expectedUrl = |
| 284 | + "https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&redirect_uri=https%3A%2F%2Fexample2.com&audience=&scope=openid+profile+offline&prompt=create&code_challenge_method=S256&utm_campaign=test"; |
| 285 | + |
| 286 | + const result = await generateAuthUrl( |
| 287 | + domain, |
| 288 | + IssuerRouteTypes.login, |
| 289 | + options, |
| 290 | + ); |
| 291 | + const nonce = result.url.searchParams.get("nonce"); |
| 292 | + expect(nonce).not.toBeNull(); |
| 293 | + expect(nonce!.length).toBe(16); |
| 294 | + const state = result.url.searchParams.get("state"); |
| 295 | + expect(state).not.toBeNull(); |
| 296 | + expect(state!.length).toBe(32); |
| 297 | + const codeChallenge = result.url.searchParams.get("code_challenge"); |
| 298 | + expect(codeChallenge!.length).toBeGreaterThanOrEqual(27); |
| 299 | + result.url.searchParams.delete("code_challenge"); |
| 300 | + result.url.searchParams.delete("nonce"); |
| 301 | + result.url.searchParams.delete("state"); |
| 302 | + expect(result.url.toString()).toBe(expectedUrl); |
| 303 | + |
| 304 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 305 | + "Unsupported Property for url generation: ", |
| 306 | + "testProperty1", |
| 307 | + ); |
| 308 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 309 | + "Unsupported Property for url generation: ", |
| 310 | + "testProperty2", |
| 311 | + ); |
| 312 | + expect(consoleWarnSpy).not.toHaveBeenCalledWith( |
| 313 | + "Unsupported Property for url generation: ", |
| 314 | + "utm_campaign", |
| 315 | + ); |
| 316 | + }); |
231 | 317 | });
|
0 commit comments