|
| 1 | +import { afterAll, expect, test, beforeEach, vi } from "vitest"; |
| 2 | +import { SendRawEmailCommand, SESClient } from "@aws-sdk/client-ses"; |
| 3 | +import { mockClient } from "aws-sdk-client-mock"; |
| 4 | +import { secretObject } from "./secret.testdata.js"; |
| 5 | +import init from "../../src/api/index.js"; |
| 6 | +import { describe } from "node:test"; |
| 7 | +import { EntraFetchError } from "../../src/common/errors/index.js"; |
| 8 | + |
| 9 | +const sesMock = mockClient(SESClient); |
| 10 | +const jwt_secret = secretObject["jwt_key"]; |
| 11 | +vi.stubEnv("JwtSigningKey", jwt_secret); |
| 12 | + |
| 13 | +vi.mock("fs", () => { |
| 14 | + return { |
| 15 | + ...vi.importActual("fs"), |
| 16 | + promises: { |
| 17 | + readFile: vi.fn(() => { |
| 18 | + return ""; |
| 19 | + }), |
| 20 | + }, |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock("../../src/api/functions/membership.js", () => { |
| 25 | + return { |
| 26 | + ...vi.importActual("../../src/api/functions/membership.js"), |
| 27 | + checkPaidMembership: vi.fn( |
| 28 | + (_endpoint: string, _log: any, netId: string) => { |
| 29 | + if (netId === "valid") { |
| 30 | + return true; |
| 31 | + } |
| 32 | + return false; |
| 33 | + }, |
| 34 | + ), |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +vi.mock("../../src/api/functions/entraId.js", () => { |
| 39 | + return { |
| 40 | + ...vi.importActual("../../src/api/functions/entraId.js"), |
| 41 | + getEntraIdToken: vi.fn().mockImplementation(async () => { |
| 42 | + return "atokenofalltime"; |
| 43 | + }), |
| 44 | + getUserProfile: vi |
| 45 | + .fn() |
| 46 | + .mockImplementation(async (_token: string, email: string) => { |
| 47 | + if (email === "[email protected]") { |
| 48 | + return { displayName: "John Doe" }; |
| 49 | + } |
| 50 | + throw new EntraFetchError({ |
| 51 | + message: "User not found", |
| 52 | + email, |
| 53 | + }); |
| 54 | + }), |
| 55 | + resolveEmailToOid: vi.fn().mockImplementation(async () => { |
| 56 | + return "12345"; |
| 57 | + }), |
| 58 | + }; |
| 59 | +}); |
| 60 | + |
| 61 | +const app = await init(); |
| 62 | +describe("Mobile wallet pass issuance", async () => { |
| 63 | + test("Test that passes will not be issued for non-emails", async () => { |
| 64 | + const response = await app.inject({ |
| 65 | + method: "GET", |
| 66 | + url: "/api/v1/mobileWallet/membership?email=notanemail", |
| 67 | + }); |
| 68 | + expect(response.statusCode).toBe(400); |
| 69 | + await response.json(); |
| 70 | + }); |
| 71 | + test("Test that passes will not be issued for non-members", async () => { |
| 72 | + const response = await app.inject({ |
| 73 | + method: "GET", |
| 74 | + url: "/api/v1/mobileWallet/[email protected]", |
| 75 | + }); |
| 76 | + expect(response.statusCode).toBe(403); |
| 77 | + await response.json(); |
| 78 | + }); |
| 79 | + test("Test that passes will be issued for members", async () => { |
| 80 | + sesMock.on(SendRawEmailCommand).resolves({}); |
| 81 | + const response = await app.inject({ |
| 82 | + method: "GET", |
| 83 | + url: "/api/v1/mobileWallet/[email protected]", |
| 84 | + }); |
| 85 | + expect(response.statusCode).toBe(202); |
| 86 | + }); |
| 87 | + test("Test that SES errors result in a server error", async () => { |
| 88 | + sesMock.on(SendRawEmailCommand).rejects({}); |
| 89 | + const response = await app.inject({ |
| 90 | + method: "GET", |
| 91 | + url: "/api/v1/mobileWallet/[email protected]", |
| 92 | + }); |
| 93 | + expect(response.statusCode).toBe(500); |
| 94 | + }); |
| 95 | + afterAll(async () => { |
| 96 | + await app.close(); |
| 97 | + }); |
| 98 | + beforeEach(() => { |
| 99 | + (app as any).nodeCache.flushAll(); |
| 100 | + vi.clearAllMocks(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments