|
| 1 | +import { afterAll, expect, test, beforeEach, vi } from "vitest"; |
| 2 | +import { mockClient } from "aws-sdk-client-mock"; |
| 3 | +import init from "../../src/api/index.js"; |
| 4 | +import { createJwt } from "./auth.test.js"; |
| 5 | +import supertest from "supertest"; |
| 6 | +import { describe } from "node:test"; |
| 7 | +import { |
| 8 | + GetSecretValueCommand, |
| 9 | + SecretsManagerClient, |
| 10 | +} from "@aws-sdk/client-secrets-manager"; |
| 11 | +import { EntraGroupError } from "../../src/common/errors/index.js"; |
| 12 | + |
| 13 | +// Mock required dependencies - their real impl's are defined in the beforeEach section. |
| 14 | +vi.mock("../../src/api/functions/entraId.js", () => { |
| 15 | + return { |
| 16 | + ...vi.importActual("../../src/api/functions/entraId.js"), |
| 17 | + getEntraIdToken: vi.fn().mockImplementation(async () => { |
| 18 | + return ""; |
| 19 | + }), |
| 20 | + modifyGroup: vi.fn().mockImplementation(async () => { |
| 21 | + return ""; |
| 22 | + }), |
| 23 | + resolveEmailToOid: vi.fn().mockImplementation(async () => { |
| 24 | + return ""; |
| 25 | + }), |
| 26 | + listGroupMembers: vi.fn().mockImplementation(async () => { |
| 27 | + return ""; |
| 28 | + }), |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +import { |
| 33 | + modifyGroup, |
| 34 | + listGroupMembers, |
| 35 | + getEntraIdToken, |
| 36 | + resolveEmailToOid, |
| 37 | +} from "../../src/api/functions/entraId.js"; |
| 38 | +import { EntraGroupActions } from "../../src/common/types/iam.js"; |
| 39 | + |
| 40 | +const smMock = mockClient(SecretsManagerClient); |
| 41 | +const app = await init(); |
| 42 | + |
| 43 | +describe("Test Modify Group and List Group Routes", () => { |
| 44 | + beforeEach(() => { |
| 45 | + vi.resetAllMocks(); |
| 46 | + smMock.on(GetSecretValueCommand).resolves({ |
| 47 | + SecretString: JSON.stringify({ jwt_key: "test_jwt_key" }), |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + test("Modify group: Add and remove members", async () => { |
| 52 | + const testJwt = createJwt(); |
| 53 | + await app.ready(); |
| 54 | + |
| 55 | + const response = await supertest(app.server) |
| 56 | + .patch("/api/v1/iam/groups/test-group-id") |
| 57 | + .set("authorization", `Bearer ${testJwt}`) |
| 58 | + .send({ |
| 59 | + |
| 60 | + |
| 61 | + }); |
| 62 | + |
| 63 | + expect(response.statusCode).toBe(202); |
| 64 | + expect(modifyGroup).toHaveBeenCalledTimes(2); |
| 65 | + expect(modifyGroup).toHaveBeenNthCalledWith( |
| 66 | + 1, |
| 67 | + "ey.test.token", |
| 68 | + |
| 69 | + "test-group-id", |
| 70 | + EntraGroupActions.ADD, |
| 71 | + ); |
| 72 | + expect(modifyGroup).toHaveBeenNthCalledWith( |
| 73 | + 2, |
| 74 | + "ey.test.token", |
| 75 | + |
| 76 | + "test-group-id", |
| 77 | + EntraGroupActions.REMOVE, |
| 78 | + ); |
| 79 | + expect(response.body.success).toEqual([ |
| 80 | + |
| 81 | + |
| 82 | + ]); |
| 83 | + expect(response.body.failure).toEqual([]); |
| 84 | + }); |
| 85 | + |
| 86 | + test("Modify group: Fail for invalid email domain", async () => { |
| 87 | + const testJwt = createJwt(); |
| 88 | + await app.ready(); |
| 89 | + |
| 90 | + const response = await supertest(app.server) |
| 91 | + .patch("/api/v1/iam/groups/test-group-id") |
| 92 | + .set("authorization", `Bearer ${testJwt}`) |
| 93 | + .send({ |
| 94 | + |
| 95 | + remove: [], |
| 96 | + }); |
| 97 | + |
| 98 | + expect(response.statusCode).toBe(202); |
| 99 | + expect(modifyGroup).toHaveBeenCalledTimes(1); |
| 100 | + expect(response.body.success).toEqual([]); |
| 101 | + expect(response.body.failure).toEqual([ |
| 102 | + { |
| 103 | + |
| 104 | + message: |
| 105 | + "User's domain must be illinois.edu to be added or removed from the group.", |
| 106 | + }, |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + test("List group members: Happy path", async () => { |
| 111 | + const testJwt = createJwt(); |
| 112 | + await app.ready(); |
| 113 | + |
| 114 | + const response = await supertest(app.server) |
| 115 | + .get("/api/v1/iam/groups/test-group-id") |
| 116 | + .set("authorization", `Bearer ${testJwt}`); |
| 117 | + |
| 118 | + expect(response.statusCode).toBe(200); |
| 119 | + expect(listGroupMembers).toHaveBeenCalledWith( |
| 120 | + "ey.test.token", |
| 121 | + "test-group-id", |
| 122 | + ); |
| 123 | + expect(response.body).toEqual([ |
| 124 | + { name: "John Doe", email: "[email protected]" }, |
| 125 | + { name: "Jane Doe", email: "[email protected]" }, |
| 126 | + ]); |
| 127 | + }); |
| 128 | + |
| 129 | + afterAll(async () => { |
| 130 | + await app.close(); |
| 131 | + }); |
| 132 | + beforeEach(() => { |
| 133 | + vi.resetAllMocks(); |
| 134 | + vi.useFakeTimers(); |
| 135 | + (getEntraIdToken as any).mockImplementation(async () => { |
| 136 | + return "ey.test.token"; |
| 137 | + }); |
| 138 | + (modifyGroup as any).mockImplementation( |
| 139 | + async (_token, email, group, _action) => { |
| 140 | + if (!email.endsWith("@illinois.edu")) { |
| 141 | + throw new EntraGroupError({ |
| 142 | + code: 400, |
| 143 | + message: |
| 144 | + "User's domain must be illinois.edu to be added or removed from the group.", |
| 145 | + group, |
| 146 | + }); |
| 147 | + } |
| 148 | + return true; |
| 149 | + }, |
| 150 | + ); |
| 151 | + (resolveEmailToOid as any).mockImplementation(async (_token, email) => { |
| 152 | + if (email === "[email protected]") { |
| 153 | + throw new Error("User not found"); |
| 154 | + } |
| 155 | + return "mocked-oid"; |
| 156 | + }); |
| 157 | + (listGroupMembers as any).mockImplementation(async (_token, group) => { |
| 158 | + if (group === "nonexistent-group-id") { |
| 159 | + throw new EntraGroupError({ |
| 160 | + code: 404, |
| 161 | + message: "Group not found.", |
| 162 | + group, |
| 163 | + }); |
| 164 | + } |
| 165 | + return [ |
| 166 | + { name: "John Doe", email: "[email protected]" }, |
| 167 | + { name: "Jane Doe", email: "[email protected]" }, |
| 168 | + ]; |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments