|
| 1 | +import { generateAuthUrl } from "./generateAuthUrl"; |
| 2 | +import { IssuerRouteTypes, LoginOptions, Scopes } from "../types"; |
| 3 | +import { generateRandomString } from "./generateRandomString"; |
| 4 | +import { describe, it, expect, vi } from "vitest"; |
| 5 | +vi.stubGlobal("crypto", undefined); |
| 6 | +Object.defineProperty(global, "crypto", { |
| 7 | + value: undefined, // Set to undefined to 'clear' crypto |
| 8 | + writable: true, // Allow the property to be rewritten later if needed |
| 9 | + configurable: true, // Allow the property definition itself to be changed, enabling resetting in teardown |
| 10 | +}); |
| 11 | + |
| 12 | +describe("generateRandomString - no crypto", () => { |
| 13 | + it("should generate a string of the specified length", () => { |
| 14 | + const length = 10; |
| 15 | + const result = generateRandomString(length); |
| 16 | + expect(result).toHaveLength(length); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should generate the correct auth URL with required parameters", async () => { |
| 20 | + const domain = "https://auth.example.com"; |
| 21 | + const options: LoginOptions = { |
| 22 | + clientId: "client123", |
| 23 | + responseType: "code", |
| 24 | + scope: [Scopes.openid, Scopes.profile], |
| 25 | + |
| 26 | + isCreateOrg: true, |
| 27 | + connectionId: "conn123", |
| 28 | + redirectURL: "https://example.com", |
| 29 | + audience: "audience123", |
| 30 | + state: "state123", |
| 31 | + }; |
| 32 | + const expectedUrl = |
| 33 | + "https://auth.example.com/oauth2/auth?client_id=client123&response_type=code&login_hint=user%40example.com&is_create_org=true&connection_id=conn123&redirect_uri=https%3A%2F%2Fexample.com&audience=audience123&scope=openid+profile&state=state123&code_challenge_method=S256"; |
| 34 | + |
| 35 | + const result = await generateAuthUrl( |
| 36 | + domain, |
| 37 | + IssuerRouteTypes.login, |
| 38 | + options, |
| 39 | + ); |
| 40 | + const nonce = result.url.searchParams.get("nonce"); |
| 41 | + expect(nonce).not.toBeNull(); |
| 42 | + expect(nonce!.length).toBe(16); |
| 43 | + result.url.searchParams.delete("nonce"); |
| 44 | + const codeChallenge = result.url.searchParams.get("code_challenge"); |
| 45 | + expect(codeChallenge!.length).toBeGreaterThanOrEqual(27); |
| 46 | + result.url.searchParams.delete("code_challenge"); |
| 47 | + expect(result.url.toString()).toBe(expectedUrl); |
| 48 | + }); |
| 49 | +}); |
0 commit comments