import { MongoClient, Db } from "mongodb"; import OAuthSessionRepo from "../src/repos/OAuthSessionRepo"; import OAuthConnectionRepo from "../src/repos/OAuthConnectionRepo"; import OAuthSession from "../src/schemas/OAuthSession"; import OAuthConnection from "../src/schemas/OAuthConnection"; /** * Focused integration tests for OAuth repositories. * Tests critical repository methods with actual MongoDB connection. * * Total tests: 6 (within the 2-8 test requirement) */ describe("OAuth Repositories", () => { let client: MongoClient; let db: Db; let sessionRepo: OAuthSessionRepo; let connectionRepo: OAuthConnectionRepo; beforeAll(async () => { // Connect to test database const mongoUrl = process.env.TEST_MONGO_URL || "mongodb://localhost:27017"; client = new MongoClient(mongoUrl); await client.connect(); db = client.db("oauth_plugin_test"); // Initialize repositories sessionRepo = new OAuthSessionRepo("oauth_sessions", db, client); connectionRepo = new OAuthConnectionRepo("oauth_connections", db, client); }); afterAll(async () => { // Clean up and close connection await db.dropDatabase(); await client.close(); }); afterEach(async () => { // Clear collections between tests await db.collection("oauth_sessions").deleteMany({}); await db.collection("oauth_connections").deleteMany({}); }); describe("OAuthSessionRepo", () => { it("should create and find session by sessionId", async () => { // Arrange const session: OAuthSession = { sessionId: "test-session-123", state: "random-state-456", nonce: "nonce-789", provider: "github", redirectUri: "https://myapp.com/callback", createdAt: new Date() }; // Act const created = await sessionRepo.create(session); const found = await sessionRepo.findBySessionId("test-session-123"); // Assert expect(created._id).toBeDefined(); expect(found).toBeDefined(); expect(found?.sessionId).toBe("test-session-123"); expect(found?.state).toBe("random-state-456"); expect(found?.provider).toBe("github"); }); it("should delete session by sessionId", async () => { // Arrange const session: OAuthSession = { sessionId: "delete-me-123", state: "state-456", provider: "google", redirectUri: "https://myapp.com/callback", createdAt: new Date() }; await sessionRepo.create(session); // Act const deletedCount = await sessionRepo.deleteBySessionId("delete-me-123"); const found = await sessionRepo.findBySessionId("delete-me-123"); // Assert expect(deletedCount).toBe(1); expect(found).toBeNull(); }); it("should return null when session not found", async () => { // Act const found = await sessionRepo.findBySessionId("non-existent"); // Assert expect(found).toBeNull(); }); }); describe("OAuthConnectionRepo", () => { it("should create and find connection by userId and provider", async () => { // Arrange const connection: OAuthConnection = { userId: "user-123", provider: "github", providerId: "github-user-456", accessToken: "encrypted-token-abc", refreshToken: "encrypted-refresh-xyz", scope: "user:email", expiresAt: new Date(Date.now() + 3600000), createdAt: new Date(), updatedAt: new Date() }; // Act const created = await connectionRepo.create(connection); const found = await connectionRepo.findByUserAndProvider("user-123", "github"); // Assert expect(created._id).toBeDefined(); expect(found).toBeDefined(); expect(found?.userId).toBe("user-123"); expect(found?.provider).toBe("github"); expect(found?.providerId).toBe("github-user-456"); expect(found?.accessToken).toBe("encrypted-token-abc"); }); it("should find all connections for a user", async () => { // Arrange const githubConnection: OAuthConnection = { userId: "user-456", provider: "github", providerId: "github-id", accessToken: "token-1", scope: "user:email", createdAt: new Date(), updatedAt: new Date() }; const googleConnection: OAuthConnection = { userId: "user-456", provider: "google", providerId: "google-id", accessToken: "token-2", scope: "openid email profile", createdAt: new Date(), updatedAt: new Date() }; await connectionRepo.create(githubConnection); await connectionRepo.create(googleConnection); // Act const connections = await connectionRepo.findByUserId("user-456"); // Assert expect(connections).toBeDefined(); expect(connections.length).toBe(2); expect(connections.some(c => c.provider === "github")).toBe(true); expect(connections.some(c => c.provider === "google")).toBe(true); }); it("should delete connection by userId and provider", async () => { // Arrange const connection: OAuthConnection = { userId: "user-789", provider: "google", providerId: "google-id", accessToken: "token", scope: "openid email", createdAt: new Date(), updatedAt: new Date() }; await connectionRepo.create(connection); // Act const deletedCount = await connectionRepo.deleteByUserAndProvider("user-789", "google"); const found = await connectionRepo.findByUserAndProvider("user-789", "google"); // Assert expect(deletedCount).toBe(1); expect(found).toBeNull(); }); }); });