/** * Tests for OidcSessionRepo */ import OidcSessionRepo from "../../src/repos/OidcSessionRepo"; import { createMockDb } from "../helpers/test-helpers"; import OidcSession from "../../src/schemas/OidcSession"; describe("OidcSessionRepo", () => { let repo: OidcSessionRepo; let db: any; let cleanup: () => Promise; beforeEach(async () => { const mockDb = await createMockDb(); db = mockDb.db; cleanup = mockDb.cleanup; repo = new OidcSessionRepo("oidc_sessions_test", db); }); afterEach(async () => { await cleanup(); }); describe("create", () => { it("should create a new session", async () => { const session: Omit = { sessionId: "session-123", state: "state-abc", codeVerifier: "verifier-xyz", nonce: "nonce-789", provider: "google", redirectUri: "http://localhost:3000/callback", createdAt: new Date(), }; const created = await repo.create(session); expect(created._id).toBeDefined(); expect(created.sessionId).toBe("session-123"); expect(created.state).toBe("state-abc"); expect(created.codeVerifier).toBe("verifier-xyz"); expect(created.nonce).toBe("nonce-789"); expect(created.provider).toBe("google"); }); }); describe("getByState", () => { it("should find session by state", async () => { const session: Omit = { sessionId: "session-456", state: "state-unique-123", codeVerifier: "verifier-abc", nonce: "nonce-xyz", provider: "microsoft", redirectUri: "http://localhost:3000/callback", createdAt: new Date(), }; await repo.create(session); const found = await repo.getByState("state-unique-123"); expect(found).toBeDefined(); expect(found?.sessionId).toBe("session-456"); expect(found?.state).toBe("state-unique-123"); }); it("should return null if session not found", async () => { const found = await repo.getByState("nonexistent-state"); expect(found).toBeNull(); }); }); describe("getOne({ sessionId", () => { it("should find session by session ID", async () => { const session: Omit = { sessionId: "session-unique-789", state: "state-def", codeVerifier: "verifier-ghi", nonce: "nonce-jkl", provider: "okta", redirectUri: "http://localhost:3000/callback", createdAt: new Date(), }; await repo.create(session); const found = await repo.getOne({ sessionId: "session-unique-789" }); expect(found).toBeDefined(); expect(found?.sessionId).toBe("session-unique-789"); expect(found?.provider).toBe("okta"); }); it("should return null if session not found", async () => { const found = await repo.getOne({ sessionId: "nonexistent-session" }); expect(found).toBeNull(); }); }); describe("deleteByState", () => { it("should delete session by state", async () => { const session: Omit = { sessionId: "session-delete-1", state: "state-delete-1", codeVerifier: "verifier-delete", nonce: "nonce-delete", provider: "auth0", redirectUri: "http://localhost:3000/callback", createdAt: new Date(), }; await repo.create(session); await repo.deleteByState("state-delete-1"); const found = await repo.getByState("state-delete-1"); expect(found).toBeNull(); }); it("should not throw if session not found", async () => { await repo.deleteByState("nonexistent-state"); // Should complete without error expect(true).toBe(true); }); }); describe("deleteBySessionId", () => { it("should delete session by session ID", async () => { const session: Omit = { sessionId: "session-delete-2", state: "state-delete-2", codeVerifier: "verifier-delete-2", nonce: "nonce-delete-2", provider: "custom", redirectUri: "http://localhost:3000/callback", createdAt: new Date(), }; await repo.create(session); await repo.deleteBySessionId("session-delete-2"); const found = await repo.getOne({ sessionId: "session-delete-2" }); expect(found).toBeNull(); }); it("should not throw if session not found", async () => { await repo.deleteBySessionId("nonexistent-session"); // Should complete without error expect(true).toBe(true); }); }); describe("multiple sessions", () => { it("should handle multiple sessions independently", async () => { const session1: Omit = { sessionId: "session-multi-1", state: "state-multi-1", codeVerifier: "verifier-1", nonce: "nonce-1", provider: "google", redirectUri: "http://localhost:3000/callback", createdAt: new Date(), }; const session2: Omit = { sessionId: "session-multi-2", state: "state-multi-2", codeVerifier: "verifier-2", nonce: "nonce-2", provider: "microsoft", redirectUri: "http://localhost:3000/callback", createdAt: new Date(), }; await repo.create(session1); await repo.create(session2); const found1 = await repo.getByState("state-multi-1"); const found2 = await repo.getByState("state-multi-2"); expect(found1?.sessionId).toBe("session-multi-1"); expect(found2?.sessionId).toBe("session-multi-2"); await repo.deleteByState("state-multi-1"); const stillExists = await repo.getByState("state-multi-2"); expect(stillExists).toBeDefined(); const deleted = await repo.getByState("state-multi-1"); expect(deleted).toBeNull(); }); }); });