Spaces:
Sleeping
Sleeping
File size: 10,575 Bytes
05c5ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { getAuthConfig } from "./config";
// Mock experimental_taintUniqueValue since it's not available in test environment
vi.mock("react", async () => {
const actual = await vi.importActual("react");
return {
...actual,
experimental_taintUniqueValue: vi.fn(),
};
});
let originalEnv: Record<string, string | undefined>;
describe("Auth Config", () => {
beforeEach(() => {
// get the original environment variables since other tests may have need them
originalEnv = { ...process.env };
// Clear all environment variables before each test
vi.unstubAllEnvs();
// Delete all auth-related environment variables
delete process.env.GITHUB_CLIENT_ID;
delete process.env.GITHUB_CLIENT_SECRET;
delete process.env.GOOGLE_CLIENT_ID;
delete process.env.GOOGLE_CLIENT_SECRET;
delete process.env.GOOGLE_FORCE_ACCOUNT_SELECTION;
delete process.env.MICROSOFT_CLIENT_ID;
delete process.env.MICROSOFT_CLIENT_SECRET;
delete process.env.MICROSOFT_TENANT_ID;
delete process.env.MICROSOFT_FORCE_ACCOUNT_SELECTION;
delete process.env.DISABLE_EMAIL_SIGN_IN;
delete process.env.DISABLE_SIGN_UP;
});
afterEach(() => {
// restore the original environment variables
process.env = { ...originalEnv } as any; //ts-ignore
// Clean up after each test
vi.unstubAllEnvs();
});
describe("getAuthConfig", () => {
it("should return default config when no environment variables are set", () => {
const config = getAuthConfig();
expect(config).toEqual({
emailAndPasswordEnabled: true,
signUpEnabled: true,
socialAuthenticationProviders: {
github: undefined,
google: undefined,
microsoft: undefined,
},
});
});
it("should parse DISABLE_EMAIL_SIGN_IN correctly", () => {
vi.stubEnv("DISABLE_EMAIL_SIGN_IN", "1");
const config = getAuthConfig();
expect(config.emailAndPasswordEnabled).toBe(false);
});
it("should parse DISABLE_EMAIL_SIGN_UP correctly", () => {
vi.stubEnv("DISABLE_EMAIL_SIGN_UP", "1");
const config = getAuthConfig();
expect(config.signUpEnabled).toBe(false);
});
it("should parse boolean environment variables with various formats", () => {
vi.stubEnv("DISABLE_EMAIL_SIGN_IN", "0");
vi.stubEnv("DISABLE_SIGN_UP", "False");
const config = getAuthConfig();
expect(config.emailAndPasswordEnabled).toBe(true);
expect(config.signUpEnabled).toBe(true);
});
it("should include GitHub config when credentials are provided", () => {
vi.stubEnv("GITHUB_CLIENT_ID", "github-client-id");
vi.stubEnv("GITHUB_CLIENT_SECRET", "github-client-secret");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.github).toEqual({
clientId: "github-client-id",
clientSecret: "github-client-secret",
disableSignUp: false,
});
});
it("should include GitHub config with disableSignUp when DISABLE_SIGN_UP is set", () => {
vi.stubEnv("GITHUB_CLIENT_ID", "github-client-id");
vi.stubEnv("GITHUB_CLIENT_SECRET", "github-client-secret");
vi.stubEnv("DISABLE_SIGN_UP", "1");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.github).toEqual({
clientId: "github-client-id",
clientSecret: "github-client-secret",
disableSignUp: true,
});
});
it("should include Google config with force account selection", () => {
vi.stubEnv("GOOGLE_CLIENT_ID", "google-client-id");
vi.stubEnv("GOOGLE_CLIENT_SECRET", "google-client-secret");
vi.stubEnv("GOOGLE_FORCE_ACCOUNT_SELECTION", "true");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.google).toEqual({
clientId: "google-client-id",
clientSecret: "google-client-secret",
prompt: "select_account",
disableSignUp: false,
});
});
it("should include Google config without force account selection", () => {
vi.stubEnv("GOOGLE_CLIENT_ID", "google-client-id");
vi.stubEnv("GOOGLE_CLIENT_SECRET", "google-client-secret");
vi.stubEnv("GOOGLE_FORCE_ACCOUNT_SELECTION", "false");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.google).toEqual({
clientId: "google-client-id",
clientSecret: "google-client-secret",
disableSignUp: false,
});
});
it("should include Microsoft config with custom tenant ID", () => {
vi.stubEnv("MICROSOFT_CLIENT_ID", "microsoft-client-id");
vi.stubEnv("MICROSOFT_CLIENT_SECRET", "microsoft-client-secret");
vi.stubEnv("MICROSOFT_TENANT_ID", "custom-tenant-id");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.microsoft).toEqual({
clientId: "microsoft-client-id",
clientSecret: "microsoft-client-secret",
tenantId: "custom-tenant-id",
disableSignUp: false,
});
});
it("should include Microsoft config with default tenant ID", () => {
vi.stubEnv("MICROSOFT_CLIENT_ID", "microsoft-client-id");
vi.stubEnv("MICROSOFT_CLIENT_SECRET", "microsoft-client-secret");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.microsoft).toEqual({
clientId: "microsoft-client-id",
clientSecret: "microsoft-client-secret",
tenantId: "common",
disableSignUp: false,
});
});
it("should include Microsoft config with force account selection", () => {
vi.stubEnv("MICROSOFT_CLIENT_ID", "microsoft-client-id");
vi.stubEnv("MICROSOFT_CLIENT_SECRET", "microsoft-client-secret");
vi.stubEnv("MICROSOFT_FORCE_ACCOUNT_SELECTION", "true");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.microsoft).toEqual({
clientId: "microsoft-client-id",
clientSecret: "microsoft-client-secret",
tenantId: "common",
prompt: "select_account",
disableSignUp: false,
});
});
it("should handle complete configuration with all providers", () => {
vi.stubEnv("DISABLE_EMAIL_SIGN_IN", "1");
vi.stubEnv("DISABLE_EMAIL_SIGN_UP", "1");
vi.stubEnv("DISABLE_SIGN_UP", "1");
vi.stubEnv("GITHUB_CLIENT_ID", "github-client-id");
vi.stubEnv("GITHUB_CLIENT_SECRET", "github-client-secret");
vi.stubEnv("GOOGLE_CLIENT_ID", "google-client-id");
vi.stubEnv("GOOGLE_CLIENT_SECRET", "google-client-secret");
vi.stubEnv("GOOGLE_FORCE_ACCOUNT_SELECTION", "true");
vi.stubEnv("MICROSOFT_CLIENT_ID", "microsoft-client-id");
vi.stubEnv("MICROSOFT_CLIENT_SECRET", "microsoft-client-secret");
vi.stubEnv("MICROSOFT_TENANT_ID", "custom-tenant");
vi.stubEnv("MICROSOFT_FORCE_ACCOUNT_SELECTION", "true");
const config = getAuthConfig();
expect(config).toEqual({
emailAndPasswordEnabled: false,
signUpEnabled: false,
socialAuthenticationProviders: {
github: {
clientId: "github-client-id",
clientSecret: "github-client-secret",
disableSignUp: true,
},
google: {
clientId: "google-client-id",
clientSecret: "google-client-secret",
prompt: "select_account",
disableSignUp: true,
},
microsoft: {
clientId: "microsoft-client-id",
clientSecret: "microsoft-client-secret",
tenantId: "custom-tenant",
prompt: "select_account",
disableSignUp: true,
},
},
});
});
it("should handle partial provider configurations", () => {
vi.stubEnv("GITHUB_CLIENT_ID", "github-client-id");
vi.stubEnv("GITHUB_CLIENT_SECRET", "github-client-secret");
// Missing Google and Microsoft credentials
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.github).toBeDefined();
expect(config.socialAuthenticationProviders.google).toBeUndefined();
expect(config.socialAuthenticationProviders.microsoft).toBeUndefined();
});
it("should handle empty string environment variables", () => {
vi.stubEnv("GITHUB_CLIENT_ID", "");
vi.stubEnv("GITHUB_CLIENT_SECRET", "github-client-secret");
const config = getAuthConfig();
expect(config.socialAuthenticationProviders.github).toBeUndefined();
});
it("should fallback to defaults when AuthConfig parsing fails", () => {
// Mock console.log to avoid output during test
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
// Set invalid configuration that would cause parsing to fail
// This tests the fallback behavior in the catch block
const config = getAuthConfig();
expect(config).toEqual({
emailAndPasswordEnabled: true,
signUpEnabled: true,
socialAuthenticationProviders: {
github: undefined,
google: undefined,
microsoft: undefined,
},
});
consoleSpy.mockRestore();
});
});
describe("Environment Variable Parsing Edge Cases", () => {
it("should handle 'y' as true for DISABLE variables (disabling features)", () => {
vi.stubEnv("DISABLE_EMAIL_SIGN_IN", "y");
const config = getAuthConfig();
expect(config.emailAndPasswordEnabled).toBe(false);
});
it("should handle case variations for DISABLE variables", () => {
vi.stubEnv("DISABLE_EMAIL_SIGN_IN", "TRUE");
vi.stubEnv("DISABLE_EMAIL_SIGN_UP", "True");
const config = getAuthConfig();
expect(config.emailAndPasswordEnabled).toBe(false);
expect(config.signUpEnabled).toBe(false);
});
it("should treat undefined DISABLE variables as enabled by default", () => {
// Don't set any environment variables
const config = getAuthConfig();
// Should use defaults which are true (features enabled)
expect(config.emailAndPasswordEnabled).toBe(true);
expect(config.signUpEnabled).toBe(true);
});
it("should treat falsy values as not disabling features", () => {
vi.stubEnv("DISABLE_EMAIL_SIGN_IN", "0");
vi.stubEnv("DISABLE_SIGN_UP", "false");
const config = getAuthConfig();
expect(config.emailAndPasswordEnabled).toBe(true);
expect(config.signUpEnabled).toBe(true);
});
});
});
|