|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + getAllProviders, |
| 5 | + getProvider, |
| 6 | + isValidProvider, |
| 7 | + OAuthError, |
| 8 | +} from "./index"; |
| 9 | + |
| 10 | +describe("OAuth Module", () => { |
| 11 | + describe("Provider Registry", () => { |
| 12 | + it("should return provider for valid provider names", () => { |
| 13 | + expect(getProvider("google-mail")).toBeDefined(); |
| 14 | + expect(getProvider("google-calendar")).toBeDefined(); |
| 15 | + expect(getProvider("discord")).toBeDefined(); |
| 16 | + expect(getProvider("linkedin")).toBeDefined(); |
| 17 | + expect(getProvider("reddit")).toBeDefined(); |
| 18 | + expect(getProvider("github")).toBeDefined(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should throw error for unknown provider", () => { |
| 22 | + expect(() => getProvider("unknown")).toThrow("Unknown OAuth provider"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should validate provider names", () => { |
| 26 | + expect(isValidProvider("google-mail")).toBe(true); |
| 27 | + expect(isValidProvider("discord")).toBe(true); |
| 28 | + expect(isValidProvider("unknown")).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should return all providers", () => { |
| 32 | + const providers = getAllProviders(); |
| 33 | + expect(providers).toHaveLength(6); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("Provider Configuration", () => { |
| 38 | + it("should have correct refresh configuration for Google providers", () => { |
| 39 | + const googleMail = getProvider("google-mail"); |
| 40 | + const googleCalendar = getProvider("google-calendar"); |
| 41 | + |
| 42 | + expect(googleMail.refreshEnabled).toBe(true); |
| 43 | + expect(googleCalendar.refreshEnabled).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should have correct refresh configuration for Discord", () => { |
| 47 | + const discord = getProvider("discord"); |
| 48 | + expect(discord.refreshEnabled).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should have correct refresh configuration for LinkedIn", () => { |
| 52 | + const linkedin = getProvider("linkedin"); |
| 53 | + expect(linkedin.refreshEnabled).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should have correct refresh configuration for Reddit", () => { |
| 57 | + const reddit = getProvider("reddit"); |
| 58 | + expect(reddit.refreshEnabled).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should have correct refresh configuration for GitHub", () => { |
| 62 | + const github = getProvider("github"); |
| 63 | + expect(github.refreshEnabled).toBe(false); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("Token Refresh Logic", () => { |
| 68 | + it("should determine when token needs refresh", () => { |
| 69 | + const provider = getProvider("google-mail"); |
| 70 | + |
| 71 | + // No expiration date - no refresh needed |
| 72 | + expect(provider.needsRefresh(undefined)).toBe(false); |
| 73 | + |
| 74 | + // Token expires in 3 minutes (within 5 minute buffer) - needs refresh |
| 75 | + const soonExpiry = new Date(Date.now() + 3 * 60 * 1000); |
| 76 | + expect(provider.needsRefresh(soonExpiry)).toBe(true); |
| 77 | + |
| 78 | + // Token expires in 10 minutes (beyond 5 minute buffer) - no refresh |
| 79 | + const laterExpiry = new Date(Date.now() + 10 * 60 * 1000); |
| 80 | + expect(provider.needsRefresh(laterExpiry)).toBe(false); |
| 81 | + |
| 82 | + // Token already expired - needs refresh |
| 83 | + const pastExpiry = new Date(Date.now() - 1 * 60 * 1000); |
| 84 | + expect(provider.needsRefresh(pastExpiry)).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should respect custom buffer period", () => { |
| 88 | + const provider = getProvider("google-mail"); |
| 89 | + |
| 90 | + // Token expires in 8 minutes |
| 91 | + const expiresAt = new Date(Date.now() + 8 * 60 * 1000); |
| 92 | + |
| 93 | + // With default 5 minute buffer - no refresh |
| 94 | + expect(provider.needsRefresh(expiresAt)).toBe(false); |
| 95 | + |
| 96 | + // With 10 minute buffer - needs refresh |
| 97 | + expect(provider.needsRefresh(expiresAt, 10)).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should throw error when refreshing GitHub tokens", async () => { |
| 101 | + const github = getProvider("github"); |
| 102 | + |
| 103 | + await expect( |
| 104 | + github.refreshToken("test-token", "client-id", "client-secret") |
| 105 | + ).rejects.toThrow("doesn't support token refresh"); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("OAuthError", () => { |
| 110 | + it("should create error with redirect error code", () => { |
| 111 | + const error = new OAuthError("oauth_failed", "Authentication failed"); |
| 112 | + expect(error.message).toBe("Authentication failed"); |
| 113 | + expect(error.redirectError).toBe("oauth_failed"); |
| 114 | + expect(error.name).toBe("OAuthError"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should be instance of Error", () => { |
| 118 | + const error = new OAuthError("oauth_failed", "Authentication failed"); |
| 119 | + expect(error instanceof Error).toBe(true); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments