|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { oauthPlugin } from "@/lib/plugins/auth/oauth"; |
| 3 | + |
| 4 | +describe("OAuth 2.0 Auth Plugin", () => { |
| 5 | + const originalEnv = process.env; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + process.env = { ...originalEnv }; |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + process.env = originalEnv; |
| 13 | + }); |
| 14 | + |
| 15 | + it("should have correct plugin id and name", () => { |
| 16 | + expect(oauthPlugin.id).toBe("oauth"); |
| 17 | + expect(oauthPlugin.name).toBe("Generic OAuth 2.0"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should have PKCE enabled by default", () => { |
| 21 | + const provider: any = oauthPlugin.getProvider(); |
| 22 | + expect(provider.checks).toBeUndefined(); // Enabled by default |
| 23 | + }); |
| 24 | + |
| 25 | + it("should allow disabling PKCE explicitly", () => { |
| 26 | + process.env.AUTH_OAUTH_ENABLE_PKCE = "false"; |
| 27 | + const provider: any = oauthPlugin.getProvider(); |
| 28 | + expect(provider.checks).toEqual(["state"]); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should configure standard OAuth provider dynamically from env", () => { |
| 32 | + process.env.AUTH_OAUTH_ID = "test-client-id"; |
| 33 | + process.env.AUTH_OAUTH_SECRET = "test-client-secret"; |
| 34 | + process.env.AUTH_OAUTH_ISSUER = "https://sso.test.com"; |
| 35 | + |
| 36 | + const provider: any = oauthPlugin.getProvider(); |
| 37 | + |
| 38 | + expect(provider.id).toBe("oauth"); |
| 39 | + expect(provider.type).toBe("oauth"); |
| 40 | + expect(provider.clientId).toBe("test-client-id"); |
| 41 | + expect(provider.clientSecret).toBe("test-client-secret"); |
| 42 | + expect(provider.issuer).toBe("https://sso.test.com"); |
| 43 | + |
| 44 | + // Check fallback URLs derived from issuer |
| 45 | + expect(provider.authorization.url).toBe("https://sso.test.com/authorize"); |
| 46 | + expect(provider.token).toBe("https://sso.test.com/token"); |
| 47 | + expect(provider.userinfo).toBe("https://sso.test.com/userinfo"); |
| 48 | + |
| 49 | + // Check default scope (should NOT include openid for relaxed OAuth) |
| 50 | + expect(provider.authorization.params.scope).toBe("email profile"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should use explicit URL overrides when provided", () => { |
| 54 | + process.env.AUTH_OAUTH_ID = "test-client-id"; |
| 55 | + process.env.AUTH_OAUTH_SECRET = "test-client-secret"; |
| 56 | + process.env.AUTH_OAUTH_ISSUER = "https://sso.test.com"; |
| 57 | + |
| 58 | + process.env.AUTH_OAUTH_AUTHORIZATION_URL = "https://custom.test.com/auth"; |
| 59 | + process.env.AUTH_OAUTH_TOKEN_URL = "https://custom.test.com/token"; |
| 60 | + process.env.AUTH_OAUTH_USERINFO_URL = "https://custom.test.com/me"; |
| 61 | + |
| 62 | + const provider: any = oauthPlugin.getProvider(); |
| 63 | + |
| 64 | + expect(provider.authorization.url).toBe("https://custom.test.com/auth"); |
| 65 | + expect(provider.token).toBe("https://custom.test.com/token"); |
| 66 | + expect(provider.userinfo).toBe("https://custom.test.com/me"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should handle wellknown discovery correctly", () => { |
| 70 | + process.env.AUTH_OAUTH_ID = "test-client-id"; |
| 71 | + process.env.AUTH_OAUTH_SECRET = "test-client-secret"; |
| 72 | + process.env.AUTH_OAUTH_ISSUER = "https://sso.test.com"; |
| 73 | + process.env.AUTH_OAUTH_WELLKNOWN = "https://sso.test.com/.well-known"; |
| 74 | + |
| 75 | + const provider: any = oauthPlugin.getProvider(); |
| 76 | + |
| 77 | + expect(provider.wellKnown).toBe("https://sso.test.com/.well-known"); |
| 78 | + expect(provider.authorization.url).toBeUndefined(); // Let wellKnown handle it |
| 79 | + expect(provider.token).toBeUndefined(); |
| 80 | + expect(provider.userinfo).toBeUndefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should handle custom style logo", () => { |
| 84 | + process.env.AUTH_OAUTH_LOGO = "https://logo.com/image.png"; |
| 85 | + const provider: any = oauthPlugin.getProvider(); |
| 86 | + |
| 87 | + expect(provider.style?.logo).toBe("https://logo.com/image.png"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should process user profile mappings correctly", () => { |
| 91 | + const provider: any = oauthPlugin.getProvider(); |
| 92 | + |
| 93 | + const mockProfile = { |
| 94 | + sub: "12345", |
| 95 | + name: "Test User", |
| 96 | + email: "test@example.com", |
| 97 | + picture: "https://avatar.com/me.png", |
| 98 | + preferred_username: "testuser" |
| 99 | + }; |
| 100 | + |
| 101 | + const parsedProfile = provider.profile(mockProfile); |
| 102 | + |
| 103 | + expect(parsedProfile.id).toBe("12345"); |
| 104 | + expect(parsedProfile.name).toBe("Test User"); |
| 105 | + expect(parsedProfile.email).toBe("test@example.com"); |
| 106 | + expect(parsedProfile.image).toBe("https://avatar.com/me.png"); |
| 107 | + expect(parsedProfile.username).toBe("testuser"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should fallback correctly if standard profile fields are missing", () => { |
| 111 | + const provider: any = oauthPlugin.getProvider(); |
| 112 | + |
| 113 | + const weirdProfile = { |
| 114 | + id: 999, // Testing numeric ID coercion |
| 115 | + email: "weird@example.com", |
| 116 | + avatar_url: "https://avatar.com/me2.png" |
| 117 | + }; |
| 118 | + |
| 119 | + const parsedProfile = provider.profile(weirdProfile); |
| 120 | + |
| 121 | + expect(parsedProfile.id).toBe("999"); |
| 122 | + expect(parsedProfile.name).toBe("weird@example.com"); // Fallback to email |
| 123 | + expect(parsedProfile.email).toBe("weird@example.com"); |
| 124 | + expect(parsedProfile.image).toBe("https://avatar.com/me2.png"); |
| 125 | + expect(parsedProfile.username).toBe("weird"); // Fallback to email prefix |
| 126 | + }); |
| 127 | +}); |
0 commit comments