|
1 | 1 | import { afterAll, expect, test, vi } from "vitest";
|
2 | 2 | import init from "../../src/api/index.js";
|
3 | 3 | import { EventGetResponse } from "../../src/api/routes/events.js";
|
4 |
| -import { describe } from "node:test"; |
| 4 | +import { afterEach, describe } from "node:test"; |
5 | 5 | import { setPaidMembershipInTable } from "../../src/api/functions/membership.js";
|
| 6 | +import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb"; |
| 7 | +import { genericConfig } from "../../src/common/config.js"; |
| 8 | +import { marshall } from "@aws-sdk/util-dynamodb"; |
| 9 | +import { mockClient } from "aws-sdk-client-mock"; |
6 | 10 |
|
7 | 11 | const app = await init();
|
| 12 | +const ddbMock = mockClient(DynamoDBClient); |
| 13 | + |
8 | 14 | vi.mock("../../src/api/functions/entraId.js", () => {
|
9 | 15 | return {
|
10 | 16 | ...vi.importActual("../../src/api/functions/entraId.js"),
|
@@ -46,22 +52,117 @@ describe("Test membership routes", async () => {
|
46 | 52 | });
|
47 | 53 |
|
48 | 54 | test("Entra-only members are added to Dynamo", async () => {
|
49 |
| - const response = await app.inject({ |
| 55 | + let response = await app.inject({ |
50 | 56 | method: "GET",
|
51 | 57 | url: "/api/v1/membership/eadon2",
|
52 | 58 | });
|
53 | 59 |
|
54 | 60 | expect(response.statusCode).toBe(200);
|
55 |
| - const responseDataJson = (await response.json()) as EventGetResponse; |
| 61 | + let responseDataJson = (await response.json()) as EventGetResponse; |
56 | 62 | expect(response.headers).toHaveProperty("x-acm-data-source");
|
57 | 63 | expect(response.headers["x-acm-data-source"]).toEqual("aad");
|
58 | 64 | expect(responseDataJson).toEqual({ netId: "eadon2", isPaidMember: true });
|
59 | 65 | expect(spySetPaidMembership).toHaveBeenCalledWith(
|
60 | 66 | "eadon2",
|
61 | 67 | expect.any(Object),
|
62 | 68 | );
|
| 69 | + response = await app.inject({ |
| 70 | + method: "GET", |
| 71 | + url: "/api/v1/membership/eadon2", |
| 72 | + }); |
| 73 | + expect(response.statusCode).toBe(200); |
| 74 | + responseDataJson = (await response.json()) as EventGetResponse; |
| 75 | + expect(response.headers).toHaveProperty("x-acm-data-source"); |
| 76 | + expect(response.headers["x-acm-data-source"]).toEqual("cache"); |
| 77 | + expect(responseDataJson).toEqual({ netId: "eadon2", isPaidMember: true }); |
63 | 78 | });
|
| 79 | + test("External list members are correctly found", async () => { |
| 80 | + ddbMock.on(QueryCommand).callsFake((command) => { |
| 81 | + if (command.TableName === genericConfig.ExternalMembershipTableName) { |
| 82 | + const requestedEmail = command.ExpressionAttributeValues[":pk"].S; |
| 83 | + const mockMembershipData = { |
| 84 | + eadon2_built: { netid_list: "eadon2_built" }, |
| 85 | + yourm4_wcs: { netid_list: "yourm4_wcs" }, |
| 86 | + }; |
64 | 87 |
|
| 88 | + return Promise.resolve({ |
| 89 | + Items: mockMembershipData[requestedEmail] |
| 90 | + ? [marshall(mockMembershipData[requestedEmail])] |
| 91 | + : [], |
| 92 | + }); |
| 93 | + } |
| 94 | + return Promise.reject(new Error("Table not mocked")); |
| 95 | + }); |
| 96 | + let response = await app.inject({ |
| 97 | + method: "GET", |
| 98 | + url: "/api/v1/membership/eadon2?list=built", |
| 99 | + }); |
| 100 | + |
| 101 | + expect(response.statusCode).toBe(200); |
| 102 | + let responseDataJson = (await response.json()) as EventGetResponse; |
| 103 | + expect(response.headers).toHaveProperty("x-acm-data-source"); |
| 104 | + expect(response.headers["x-acm-data-source"]).toEqual("dynamo"); |
| 105 | + expect(responseDataJson).toEqual({ |
| 106 | + netId: "eadon2", |
| 107 | + list: "built", |
| 108 | + isPaidMember: true, |
| 109 | + }); |
| 110 | + response = await app.inject({ |
| 111 | + method: "GET", |
| 112 | + url: "/api/v1/membership/eadon2?list=wcs", |
| 113 | + }); |
| 114 | + expect(response.statusCode).toBe(200); |
| 115 | + responseDataJson = (await response.json()) as EventGetResponse; |
| 116 | + expect(response.headers).toHaveProperty("x-acm-data-source"); |
| 117 | + expect(response.headers["x-acm-data-source"]).toEqual("dynamo"); |
| 118 | + expect(responseDataJson).toEqual({ |
| 119 | + netId: "eadon2", |
| 120 | + list: "wcs", |
| 121 | + isPaidMember: false, |
| 122 | + }); |
| 123 | + response = await app.inject({ |
| 124 | + method: "GET", |
| 125 | + url: "/api/v1/membership/yourm4?list=wcs", |
| 126 | + }); |
| 127 | + expect(response.statusCode).toBe(200); |
| 128 | + responseDataJson = (await response.json()) as EventGetResponse; |
| 129 | + expect(response.headers).toHaveProperty("x-acm-data-source"); |
| 130 | + expect(response.headers["x-acm-data-source"]).toEqual("dynamo"); |
| 131 | + expect(responseDataJson).toEqual({ |
| 132 | + netId: "yourm4", |
| 133 | + list: "wcs", |
| 134 | + isPaidMember: true, |
| 135 | + }); |
| 136 | + response = await app.inject({ |
| 137 | + method: "GET", |
| 138 | + url: "/api/v1/membership/eadon2?list=wcs", |
| 139 | + }); |
| 140 | + expect(response.statusCode).toBe(200); |
| 141 | + responseDataJson = (await response.json()) as EventGetResponse; |
| 142 | + expect(response.headers).toHaveProperty("x-acm-data-source"); |
| 143 | + expect(response.headers["x-acm-data-source"]).toEqual("cache"); |
| 144 | + expect(responseDataJson).toEqual({ |
| 145 | + netId: "eadon2", |
| 146 | + list: "wcs", |
| 147 | + isPaidMember: false, |
| 148 | + }); |
| 149 | + response = await app.inject({ |
| 150 | + method: "GET", |
| 151 | + url: "/api/v1/membership/eadon2?list=built", |
| 152 | + }); |
| 153 | + expect(response.statusCode).toBe(200); |
| 154 | + responseDataJson = (await response.json()) as EventGetResponse; |
| 155 | + expect(response.headers).toHaveProperty("x-acm-data-source"); |
| 156 | + expect(response.headers["x-acm-data-source"]).toEqual("cache"); |
| 157 | + expect(responseDataJson).toEqual({ |
| 158 | + netId: "eadon2", |
| 159 | + list: "built", |
| 160 | + isPaidMember: true, |
| 161 | + }); |
| 162 | + }); |
| 163 | + afterEach(async () => { |
| 164 | + ddbMock.reset(); |
| 165 | + }); |
65 | 166 | afterAll(async () => {
|
66 | 167 | await app.close();
|
67 | 168 | });
|
|
0 commit comments