/** * Tests for OidcPlugin factory * * Note: These are unit tests for plugin configuration and initialization logic. * For full integration tests, use the test-utils package in a demo app. */ import { oidcPlugin } from "../../src/OidcPlugin"; import { createTestProviderConfig } from "../helpers/test-helpers"; describe("OidcPlugin", () => { describe("configuration validation", () => { it("should throw error if no providers configured", () => { expect(() => { oidcPlugin({ providers: {}, onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); }).toThrowError(/At least one provider must be configured/); }); it("should throw error if onAuthSuccess is missing", () => { expect(() => { oidcPlugin({ providers: { test: createTestProviderConfig(), }, onAuthSuccess: undefined as any, }); }).toThrowError(/onAuthSuccess callback is required/); }); it("should throw error if provider missing required fields", () => { expect(() => { oidcPlugin({ providers: { test: { issuer: "https://test.example.com", clientId: "", clientSecret: "", callbackUrl: "", } as any, }, onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); }).toThrowError(/clientId is required/); }); it("should throw error if encryption key too short", () => { expect(() => { oidcPlugin({ providers: { test: createTestProviderConfig(), }, encryptionKey: "short", onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); }).toThrowError(/at least 32 characters/); }); it("should accept valid configuration", () => { expect(() => { oidcPlugin({ providers: { test: createTestProviderConfig(), }, encryptionKey: "valid-encryption-key-at-least-32-chars-long", onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); }).not.toThrow(); }); }); describe("plugin structure", () => { it("should return plugin with correct ID", () => { const plugin = oidcPlugin({ providers: { test: createTestProviderConfig(), }, onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); expect(plugin.id).toBe("oidc"); }); it("should configure database usage", () => { const plugin = oidcPlugin({ providers: { test: createTestProviderConfig(), }, onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); expect(plugin.db).toBeDefined(); expect(plugin.db?.useHostDb).toBe(true); }); it("should provide plugin context with API methods", () => { const plugin = oidcPlugin({ providers: { test: createTestProviderConfig(), }, onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); expect(plugin.ctx).toBeDefined(); expect(plugin.ctx.getConnection).toBeDefined(); expect(plugin.ctx.getConnections).toBeDefined(); expect(plugin.ctx.deleteConnection).toBeDefined(); expect(plugin.ctx.options).toBeDefined(); }); it("should provide init function", () => { const plugin = oidcPlugin({ providers: { test: createTestProviderConfig(), }, onAuthSuccess: async () => ({ user: {}, token: "", redirectUrl: "" }), }); expect(plugin.init).toBeDefined(); expect(typeof plugin.init).toBe("function"); }); }); });