|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + verifyIdToken: vi.fn(), |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock("google-auth-library", () => ({ |
| 8 | + GoogleAuth: class {}, |
| 9 | + OAuth2Client: class { |
| 10 | + verifyIdToken = mocks.verifyIdToken; |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +const { verifyGoogleChatRequest } = await import("./auth.js"); |
| 15 | + |
| 16 | +function mockTicket(payload: Record<string, unknown>) { |
| 17 | + mocks.verifyIdToken.mockResolvedValue({ |
| 18 | + getPayload: () => payload, |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +describe("verifyGoogleChatRequest", () => { |
| 23 | + beforeEach(() => { |
| 24 | + mocks.verifyIdToken.mockReset(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("accepts Google Chat app-url tokens from the Chat issuer", async () => { |
| 28 | + mockTicket({ |
| 29 | + |
| 30 | + email_verified: true, |
| 31 | + }); |
| 32 | + |
| 33 | + await expect( |
| 34 | + verifyGoogleChatRequest({ |
| 35 | + bearer: "token", |
| 36 | + audienceType: "app-url", |
| 37 | + audience: "https://example.com/googlechat", |
| 38 | + }), |
| 39 | + ).resolves.toEqual({ ok: true }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("rejects add-on tokens when no principal binding is configured", async () => { |
| 43 | + mockTicket({ |
| 44 | + |
| 45 | + email_verified: true, |
| 46 | + sub: "principal-1", |
| 47 | + }); |
| 48 | + |
| 49 | + await expect( |
| 50 | + verifyGoogleChatRequest({ |
| 51 | + bearer: "token", |
| 52 | + audienceType: "app-url", |
| 53 | + audience: "https://example.com/googlechat", |
| 54 | + }), |
| 55 | + ).resolves.toEqual({ |
| 56 | + ok: false, |
| 57 | + reason: "missing add-on principal binding", |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("accepts add-on tokens only when the bound principal matches", async () => { |
| 62 | + mockTicket({ |
| 63 | + |
| 64 | + email_verified: true, |
| 65 | + sub: "principal-1", |
| 66 | + }); |
| 67 | + |
| 68 | + await expect( |
| 69 | + verifyGoogleChatRequest({ |
| 70 | + bearer: "token", |
| 71 | + audienceType: "app-url", |
| 72 | + audience: "https://example.com/googlechat", |
| 73 | + expectedAddOnPrincipal: "principal-1", |
| 74 | + }), |
| 75 | + ).resolves.toEqual({ ok: true }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("rejects add-on tokens when the bound principal does not match", async () => { |
| 79 | + mockTicket({ |
| 80 | + |
| 81 | + email_verified: true, |
| 82 | + sub: "principal-2", |
| 83 | + }); |
| 84 | + |
| 85 | + await expect( |
| 86 | + verifyGoogleChatRequest({ |
| 87 | + bearer: "token", |
| 88 | + audienceType: "app-url", |
| 89 | + audience: "https://example.com/googlechat", |
| 90 | + expectedAddOnPrincipal: "principal-1", |
| 91 | + }), |
| 92 | + ).resolves.toEqual({ |
| 93 | + ok: false, |
| 94 | + reason: "unexpected add-on principal: principal-2", |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments