|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { ConfigurationError, ConfigurationErrorCode } from "../errors/index.js"; |
| 4 | +import { Auth0Client } from "./client.js"; |
| 5 | + |
| 6 | +describe("Auth0Client", () => { |
| 7 | + // Store original env vars |
| 8 | + const originalEnv = { ...process.env }; |
| 9 | + |
| 10 | + // Define correct environment variable names |
| 11 | + const ENV_VARS = { |
| 12 | + DOMAIN: "AUTH0_DOMAIN", |
| 13 | + CLIENT_ID: "AUTH0_CLIENT_ID", |
| 14 | + CLIENT_SECRET: "AUTH0_CLIENT_SECRET", |
| 15 | + CLIENT_ASSERTION_SIGNING_KEY: "AUTH0_CLIENT_ASSERTION_SIGNING_KEY", |
| 16 | + APP_BASE_URL: "APP_BASE_URL", |
| 17 | + SECRET: "AUTH0_SECRET", |
| 18 | + SCOPE: "AUTH0_SCOPE" |
| 19 | + }; |
| 20 | + |
| 21 | + // Clear env vars before each test |
| 22 | + beforeEach(() => { |
| 23 | + vi.resetModules(); |
| 24 | + // Clear all environment variables that might affect the tests |
| 25 | + delete process.env[ENV_VARS.DOMAIN]; |
| 26 | + delete process.env[ENV_VARS.CLIENT_ID]; |
| 27 | + delete process.env[ENV_VARS.CLIENT_SECRET]; |
| 28 | + delete process.env[ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY]; |
| 29 | + delete process.env[ENV_VARS.APP_BASE_URL]; |
| 30 | + delete process.env[ENV_VARS.SECRET]; |
| 31 | + delete process.env[ENV_VARS.SCOPE]; |
| 32 | + }); |
| 33 | + |
| 34 | + // Restore env vars after each test |
| 35 | + afterEach(() => { |
| 36 | + process.env = { ...originalEnv }; |
| 37 | + }); |
| 38 | + |
| 39 | + describe("constructor validation", () => { |
| 40 | + it("should throw ConfigurationError when all required options are missing", () => { |
| 41 | + expect(() => new Auth0Client()).toThrow(ConfigurationError); |
| 42 | + |
| 43 | + try { |
| 44 | + new Auth0Client(); |
| 45 | + } catch (error) { |
| 46 | + const configError = error as ConfigurationError; |
| 47 | + expect(configError).toBeInstanceOf(ConfigurationError); |
| 48 | + expect(configError.code).toBe( |
| 49 | + ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS |
| 50 | + ); |
| 51 | + expect(configError.missingOptions).toContain("domain"); |
| 52 | + expect(configError.missingOptions).toContain("clientId"); |
| 53 | + expect(configError.missingOptions).toContain("clientAuthentication"); |
| 54 | + expect(configError.missingOptions).toContain("appBaseUrl"); |
| 55 | + expect(configError.missingOptions).toContain("secret"); |
| 56 | + |
| 57 | + // Check that error message contains specific text |
| 58 | + expect(configError.message).toContain( |
| 59 | + "Not all required options where provided" |
| 60 | + ); |
| 61 | + expect(configError.message).toContain(ENV_VARS.DOMAIN); |
| 62 | + expect(configError.message).toContain(ENV_VARS.CLIENT_ID); |
| 63 | + expect(configError.message).toContain(ENV_VARS.CLIENT_SECRET); |
| 64 | + expect(configError.message).toContain( |
| 65 | + ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY |
| 66 | + ); |
| 67 | + expect(configError.message).toContain(ENV_VARS.APP_BASE_URL); |
| 68 | + expect(configError.message).toContain(ENV_VARS.SECRET); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it("should throw ConfigurationError when some required options are missing", () => { |
| 73 | + // Provide some but not all required options |
| 74 | + const options = { |
| 75 | + domain: "example.auth0.com", |
| 76 | + clientId: "client_123" |
| 77 | + }; |
| 78 | + |
| 79 | + try { |
| 80 | + new Auth0Client(options); |
| 81 | + } catch (error) { |
| 82 | + const configError = error as ConfigurationError; |
| 83 | + expect(configError).toBeInstanceOf(ConfigurationError); |
| 84 | + expect(configError.code).toBe( |
| 85 | + ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS |
| 86 | + ); |
| 87 | + // These should be missing |
| 88 | + expect(configError.missingOptions).toContain("clientAuthentication"); |
| 89 | + expect(configError.missingOptions).toContain("appBaseUrl"); |
| 90 | + expect(configError.missingOptions).toContain("secret"); |
| 91 | + // These should not be in the missing list |
| 92 | + expect(configError.missingOptions).not.toContain("domain"); |
| 93 | + expect(configError.missingOptions).not.toContain("clientId"); |
| 94 | + |
| 95 | + // Error message should only contain instructions for missing options |
| 96 | + expect(configError.message).toContain(ENV_VARS.CLIENT_SECRET); |
| 97 | + expect(configError.message).toContain( |
| 98 | + ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY |
| 99 | + ); |
| 100 | + expect(configError.message).toContain(ENV_VARS.APP_BASE_URL); |
| 101 | + expect(configError.message).toContain(ENV_VARS.SECRET); |
| 102 | + expect(configError.message).not.toContain(`Set ${ENV_VARS.DOMAIN}`); |
| 103 | + expect(configError.message).not.toContain(`Set ${ENV_VARS.CLIENT_ID}`); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + it("should accept clientSecret as authentication method", () => { |
| 108 | + // Set required environment variables with clientSecret |
| 109 | + process.env[ENV_VARS.DOMAIN] = "env.auth0.com"; |
| 110 | + process.env[ENV_VARS.CLIENT_ID] = "env_client_id"; |
| 111 | + process.env[ENV_VARS.CLIENT_SECRET] = "env_client_secret"; |
| 112 | + process.env[ENV_VARS.APP_BASE_URL] = "https://myapp.com"; |
| 113 | + process.env[ENV_VARS.SECRET] = "env_secret"; |
| 114 | + |
| 115 | + // Should not throw |
| 116 | + const client = new Auth0Client(); |
| 117 | + |
| 118 | + // The client should be instantiated successfully |
| 119 | + expect(client).toBeInstanceOf(Auth0Client); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should accept clientAssertionSigningKey as authentication method", () => { |
| 123 | + // Set required environment variables with clientAssertionSigningKey instead of clientSecret |
| 124 | + process.env[ENV_VARS.DOMAIN] = "env.auth0.com"; |
| 125 | + process.env[ENV_VARS.CLIENT_ID] = "env_client_id"; |
| 126 | + process.env[ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY] = "some-signing-key"; |
| 127 | + process.env[ENV_VARS.APP_BASE_URL] = "https://myapp.com"; |
| 128 | + process.env[ENV_VARS.SECRET] = "env_secret"; |
| 129 | + |
| 130 | + // Should not throw |
| 131 | + const client = new Auth0Client(); |
| 132 | + |
| 133 | + // The client should be instantiated successfully |
| 134 | + expect(client).toBeInstanceOf(Auth0Client); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should prioritize options over environment variables", () => { |
| 138 | + // Set environment variables |
| 139 | + process.env[ENV_VARS.DOMAIN] = "env.auth0.com"; |
| 140 | + process.env[ENV_VARS.CLIENT_ID] = "env_client_id"; |
| 141 | + process.env[ENV_VARS.CLIENT_SECRET] = "env_client_secret"; |
| 142 | + process.env[ENV_VARS.APP_BASE_URL] = "https://myapp.com"; |
| 143 | + process.env[ENV_VARS.SECRET] = "env_secret"; |
| 144 | + |
| 145 | + // Provide conflicting options |
| 146 | + const options = { |
| 147 | + domain: "options.auth0.com", |
| 148 | + clientId: "options_client_id", |
| 149 | + clientSecret: "options_client_secret", |
| 150 | + appBaseUrl: "https://options-app.com", |
| 151 | + secret: "options_secret" |
| 152 | + }; |
| 153 | + |
| 154 | + // Mock the validateAndExtractRequiredOptions to verify which values are used |
| 155 | + const mockValidateAndExtractRequiredOptions = vi |
| 156 | + .fn() |
| 157 | + .mockReturnValue(options); |
| 158 | + const originalValidateAndExtractRequiredOptions = |
| 159 | + Auth0Client.prototype["validateAndExtractRequiredOptions"]; |
| 160 | + Auth0Client.prototype["validateAndExtractRequiredOptions"] = |
| 161 | + mockValidateAndExtractRequiredOptions; |
| 162 | + |
| 163 | + try { |
| 164 | + new Auth0Client(options); |
| 165 | + |
| 166 | + // Check that validateAndExtractRequiredOptions was called with our options |
| 167 | + expect(mockValidateAndExtractRequiredOptions).toHaveBeenCalledWith( |
| 168 | + options |
| 169 | + ); |
| 170 | + // The first argument of the first call should be our options object |
| 171 | + const passedOptions = |
| 172 | + mockValidateAndExtractRequiredOptions.mock.calls[0][0]; |
| 173 | + expect(passedOptions.domain).toBe("options.auth0.com"); |
| 174 | + expect(passedOptions.clientId).toBe("options_client_id"); |
| 175 | + } finally { |
| 176 | + // Restore the original method |
| 177 | + Auth0Client.prototype["validateAndExtractRequiredOptions"] = |
| 178 | + originalValidateAndExtractRequiredOptions; |
| 179 | + } |
| 180 | + }); |
| 181 | + }); |
| 182 | +}); |
0 commit comments