|
1 | | -import { describe, it, expect, vi } from "vitest"; |
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
2 | 2 | import type { AdapterUser } from "next-auth/adapters"; |
3 | 3 |
|
4 | 4 | // Mock server-only barrier and db import so config can be imported in Vitest |
5 | 5 | vi.mock("server-only", () => ({})); |
6 | | -vi.mock("@/server/db", () => ({ db: { user: { findUnique: vi.fn() } } })); |
| 6 | +vi.mock("@/server/db", () => ({ db: { user: { findUnique: vi.fn(), update: vi.fn() } } })); |
7 | 7 |
|
8 | 8 | const { db } = await import("@/server/db"); |
9 | 9 | const mockDb = vi.mocked(db, true); |
10 | 10 |
|
| 11 | +beforeEach(() => { |
| 12 | + mockDb.user.findUnique.mockReset(); |
| 13 | + mockDb.user.update.mockReset(); |
| 14 | +}); |
| 15 | + |
11 | 16 | describe("NextAuth signIn callback", () => { |
12 | 17 | it("allows calls without account context", async () => { |
13 | 18 | const { authConfig } = await import("@/server/auth/config"); |
@@ -39,4 +44,60 @@ describe("NextAuth signIn callback", () => { |
39 | 44 | }); |
40 | 45 | expect(res).toBe(true); |
41 | 46 | }); |
| 47 | + |
| 48 | + it("allows Google sign-in for existing user and marks email verified", async () => { |
| 49 | + mockDb.user.findUnique.mockResolvedValue({ id: "u1", emailVerified: null }); |
| 50 | + mockDb.user.update.mockResolvedValue({ id: "u1" } as never); |
| 51 | + |
| 52 | + const { authConfig } = await import("@/server/auth/config"); |
| 53 | + const signInCb = authConfig.callbacks?.signIn; |
| 54 | + if (!signInCb) throw new Error("signIn callback missing"); |
| 55 | + |
| 56 | + const res = await signInCb({ |
| 57 | + account: { provider: "google" } as any, |
| 58 | + user: { email: "User@Test.com" } as AdapterUser, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(res).toBe(true); |
| 62 | + expect(mockDb.user.findUnique).toHaveBeenCalledWith({ |
| 63 | + where: { email: "user@test.com" }, |
| 64 | + select: { id: true, emailVerified: true }, |
| 65 | + }); |
| 66 | + expect(mockDb.user.update).toHaveBeenCalledTimes(1); |
| 67 | + const updateArg = mockDb.user.update.mock.calls[0]?.[0]; |
| 68 | + expect(updateArg).toMatchObject({ where: { id: "u1" } }); |
| 69 | + expect(updateArg?.data?.emailVerified).toBeInstanceOf(Date); |
| 70 | + }); |
| 71 | + |
| 72 | + it("skips verification update when Google user already verified", async () => { |
| 73 | + mockDb.user.findUnique.mockResolvedValue({ id: "u2", emailVerified: new Date("2024-01-01T00:00:00.000Z") }); |
| 74 | + |
| 75 | + const { authConfig } = await import("@/server/auth/config"); |
| 76 | + const signInCb = authConfig.callbacks?.signIn; |
| 77 | + if (!signInCb) throw new Error("signIn callback missing"); |
| 78 | + |
| 79 | + const res = await signInCb({ |
| 80 | + account: { provider: "google" } as any, |
| 81 | + user: { email: "verified@test.com" } as AdapterUser, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(res).toBe(true); |
| 85 | + expect(mockDb.user.update).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("denies Google sign-in when user does not exist", async () => { |
| 89 | + mockDb.user.findUnique.mockResolvedValue(null); |
| 90 | + |
| 91 | + const { authConfig } = await import("@/server/auth/config"); |
| 92 | + const signInCb = authConfig.callbacks?.signIn; |
| 93 | + if (!signInCb) throw new Error("signIn callback missing"); |
| 94 | + |
| 95 | + const res = await signInCb({ |
| 96 | + account: { provider: "google" } as any, |
| 97 | + user: { email: "missing@test.com" } as AdapterUser, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(res).toBe(false); |
| 101 | + expect(mockDb.user.update).not.toHaveBeenCalled(); |
| 102 | + }); |
42 | 103 | }); |
0 commit comments