|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | + |
| 3 | +const { findFirstMock, updateManyMock } = vi.hoisted(() => ({ |
| 4 | + findFirstMock: vi.fn(), |
| 5 | + updateManyMock: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock("~/db.server", () => ({ |
| 9 | + prisma: { |
| 10 | + personalAccessToken: { |
| 11 | + findFirst: findFirstMock, |
| 12 | + updateMany: updateManyMock, |
| 13 | + }, |
| 14 | + }, |
| 15 | + $replica: {}, |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("~/env.server", () => ({ |
| 19 | + env: { ENCRYPTION_KEY: "0".repeat(64) }, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("~/utils/tokens.server", () => ({ |
| 23 | + hashToken: (t: string) => `hashed:${t}`, |
| 24 | + encryptToken: () => ({ nonce: "n", ciphertext: "c", tag: "t" }), |
| 25 | + decryptToken: () => "tr_pat_validtoken", |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock("./logger.server", () => ({ |
| 29 | + logger: { warn: vi.fn(), error: vi.fn() }, |
| 30 | +})); |
| 31 | + |
| 32 | +import { |
| 33 | + authenticatePersonalAccessToken, |
| 34 | + PAT_LAST_ACCESSED_THROTTLE_MS, |
| 35 | +} from "~/services/personalAccessToken.server"; |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + vi.useFakeTimers(); |
| 39 | + vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z")); |
| 40 | + findFirstMock.mockReset(); |
| 41 | + updateManyMock.mockReset(); |
| 42 | + updateManyMock.mockResolvedValue({ count: 1 }); |
| 43 | +}); |
| 44 | + |
| 45 | +afterEach(() => { |
| 46 | + vi.useRealTimers(); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("authenticatePersonalAccessToken — lastAccessedAt throttle", () => { |
| 50 | + test("issues a conditional updateMany that skips writes when lastAccessedAt is recent", async () => { |
| 51 | + findFirstMock.mockResolvedValueOnce({ |
| 52 | + id: "pat_123", |
| 53 | + userId: "user_1", |
| 54 | + hashedToken: "hashed:tr_pat_validtoken", |
| 55 | + encryptedToken: { nonce: "n", ciphertext: "c", tag: "t" }, |
| 56 | + }); |
| 57 | + |
| 58 | + const result = await authenticatePersonalAccessToken("tr_pat_validtoken"); |
| 59 | + |
| 60 | + expect(result).toEqual({ userId: "user_1" }); |
| 61 | + expect(updateManyMock).toHaveBeenCalledTimes(1); |
| 62 | + |
| 63 | + const call = updateManyMock.mock.calls[0][0]; |
| 64 | + expect(call.where.id).toBe("pat_123"); |
| 65 | + expect(call.where.revokedAt).toBeNull(); |
| 66 | + expect(call.data.lastAccessedAt).toBeInstanceOf(Date); |
| 67 | + |
| 68 | + // The WHERE clause should require the existing lastAccessedAt to be null |
| 69 | + // or strictly older than the throttle window — that's the entire point. |
| 70 | + expect(call.where.OR).toEqual([ |
| 71 | + { lastAccessedAt: null }, |
| 72 | + { lastAccessedAt: { lt: expect.any(Date) } }, |
| 73 | + ]); |
| 74 | + |
| 75 | + // With fake timers, the cutoff lands exactly throttle-ms before "now". |
| 76 | + const cutoff = call.where.OR[1].lastAccessedAt.lt as Date; |
| 77 | + expect(cutoff.getTime()).toBe(Date.now() - PAT_LAST_ACCESSED_THROTTLE_MS); |
| 78 | + }); |
| 79 | + |
| 80 | + test("skips updateMany when token is not found", async () => { |
| 81 | + findFirstMock.mockResolvedValueOnce(null); |
| 82 | + |
| 83 | + const result = await authenticatePersonalAccessToken("tr_pat_validtoken"); |
| 84 | + |
| 85 | + expect(result).toBeUndefined(); |
| 86 | + expect(updateManyMock).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + test("skips updateMany when token doesn't start with prefix", async () => { |
| 90 | + const result = await authenticatePersonalAccessToken("not_a_pat"); |
| 91 | + |
| 92 | + expect(result).toBeUndefined(); |
| 93 | + expect(findFirstMock).not.toHaveBeenCalled(); |
| 94 | + expect(updateManyMock).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments