diff --git a/tests/unit/auth.test.ts b/tests/unit/auth.test.ts index 5008a6a..f4f50c3 100644 --- a/tests/unit/auth.test.ts +++ b/tests/unit/auth.test.ts @@ -13,6 +13,7 @@ import { } from "./secret.testdata.js"; import jwt from "jsonwebtoken"; import { allAppRoles, AppRoles } from "../../src/common/roles.js"; +import { beforeEach, describe } from "node:test"; const ddbMock = mockClient(SecretsManagerClient); @@ -50,40 +51,46 @@ vi.stubEnv("JwtSigningKey", jwt_secret); const testJwt = createJwt(); const testJwtNoGroups = createJwtNoGroups(); -test("Test happy path", async () => { - ddbMock.on(GetSecretValueCommand).resolves({ - SecretString: secretJson, +describe("Test authentication", () => { + test("Test happy path", async () => { + ddbMock.on(GetSecretValueCommand).resolves({ + SecretString: secretJson, + }); + const response = await app.inject({ + method: "GET", + url: "/api/v1/protected", + headers: { + authorization: `Bearer ${testJwt}`, + }, + }); + expect(response.statusCode).toBe(200); + const jsonBody = await response.json(); + expect(jsonBody).toEqual({ + username: "infra-unit-test@acm.illinois.edu", + roles: allAppRoles, + }); }); - const response = await app.inject({ - method: "GET", - url: "/api/v1/protected", - headers: { - authorization: `Bearer ${testJwt}`, - }, - }); - expect(response.statusCode).toBe(200); - const jsonBody = await response.json(); - expect(jsonBody).toEqual({ - username: "infra-unit-test@acm.illinois.edu", - roles: allAppRoles, - }); -}); -test("Test user-specific role grants", async () => { - ddbMock.on(GetSecretValueCommand).resolves({ - SecretString: secretJson, + test("Test user-specific role grants", async () => { + ddbMock.on(GetSecretValueCommand).resolves({ + SecretString: secretJson, + }); + const response = await app.inject({ + method: "GET", + url: "/api/v1/protected", + headers: { + authorization: `Bearer ${testJwtNoGroups}`, + }, + }); + expect(response.statusCode).toBe(200); + const jsonBody = await response.json(); + expect(jsonBody).toEqual({ + username: "infra-unit-test-nogrp@acm.illinois.edu", + roles: [AppRoles.TICKETS_SCANNER], + }); }); - const response = await app.inject({ - method: "GET", - url: "/api/v1/protected", - headers: { - authorization: `Bearer ${testJwtNoGroups}`, - }, - }); - expect(response.statusCode).toBe(200); - const jsonBody = await response.json(); - expect(jsonBody).toEqual({ - username: "infra-unit-test-nogrp@acm.illinois.edu", - roles: [AppRoles.TICKETS_SCANNER], + + beforeEach(() => { + (app as any).nodeCache.flushAll(); }); }); diff --git a/tests/unit/discordEvent.test.ts b/tests/unit/discordEvent.test.ts index 3d13829..11461e4 100644 --- a/tests/unit/discordEvent.test.ts +++ b/tests/unit/discordEvent.test.ts @@ -85,6 +85,7 @@ describe("Test Events <-> Discord integration", () => { vi.useRealTimers(); }); beforeEach(() => { + (app as any).nodeCache.flushAll(); ddbMock.reset(); smMock.reset(); vi.clearAllMocks(); diff --git a/tests/unit/entraGroupManagement.test.ts b/tests/unit/entraGroupManagement.test.ts index f1d4961..0f3d37e 100644 --- a/tests/unit/entraGroupManagement.test.ts +++ b/tests/unit/entraGroupManagement.test.ts @@ -42,6 +42,7 @@ const app = await init(); describe("Test Modify Group and List Group Routes", () => { beforeEach(() => { + (app as any).nodeCache.flushAll(); vi.clearAllMocks(); smMock.on(GetSecretValueCommand).resolves({ SecretString: JSON.stringify({ jwt_key: "test_jwt_key" }), @@ -130,6 +131,7 @@ describe("Test Modify Group and List Group Routes", () => { await app.close(); }); beforeEach(() => { + (app as any).nodeCache.flushAll(); vi.clearAllMocks(); vi.useFakeTimers(); (getEntraIdToken as any).mockImplementation(async () => { diff --git a/tests/unit/entraInviteUser.test.ts b/tests/unit/entraInviteUser.test.ts index 40e7e2c..59d7e24 100644 --- a/tests/unit/entraInviteUser.test.ts +++ b/tests/unit/entraInviteUser.test.ts @@ -95,6 +95,7 @@ describe("Test Microsoft Entra ID user invitation", () => { }); beforeEach(() => { + (app as any).nodeCache.flushAll(); vi.clearAllMocks(); vi.useFakeTimers(); // Re-implement the mock diff --git a/tests/unit/eventPost.test.ts b/tests/unit/eventPost.test.ts index f846b4a..437f127 100644 --- a/tests/unit/eventPost.test.ts +++ b/tests/unit/eventPost.test.ts @@ -197,6 +197,7 @@ afterAll(async () => { vi.useRealTimers(); }); beforeEach(() => { + (app as any).nodeCache.flushAll(); ddbMock.reset(); smMock.reset(); vi.useFakeTimers(); diff --git a/tests/unit/events.test.ts b/tests/unit/events.test.ts index 4b16227..6c000d9 100644 --- a/tests/unit/events.test.ts +++ b/tests/unit/events.test.ts @@ -29,7 +29,6 @@ test("Test getting events", async () => { }); test("Test dynamodb error handling", async () => { - ddbMock.on(ScanCommand).rejects("Could not get data."); const response = await app.inject({ method: "GET", url: "/api/v1/events", @@ -64,6 +63,7 @@ afterAll(async () => { vi.useRealTimers(); }); beforeEach(() => { + (app as any).nodeCache.flushAll(); ddbMock.reset(); vi.useFakeTimers(); }); diff --git a/tests/unit/health.test.ts b/tests/unit/health.test.ts index e72e85a..9765850 100644 --- a/tests/unit/health.test.ts +++ b/tests/unit/health.test.ts @@ -3,7 +3,7 @@ import init from "../../src/api/index.js"; import { EventGetResponse } from "../../src/api/routes/events.js"; const app = await init(); -test("Test getting events", async () => { +test("Test getting health", async () => { const response = await app.inject({ method: "GET", url: "/api/v1/healthz", diff --git a/tests/unit/organizations.test.ts b/tests/unit/organizations.test.ts index 50e9805..7a605f7 100644 --- a/tests/unit/organizations.test.ts +++ b/tests/unit/organizations.test.ts @@ -1,4 +1,4 @@ -import { afterAll, expect, test } from "vitest"; +import { afterAll, expect, test, beforeEach } from "vitest"; import init from "../../src/api/index.js"; const app = await init(); @@ -13,3 +13,6 @@ test("Test getting the list of organizations succeeds", async () => { afterAll(async () => { await app.close(); }); +beforeEach(() => { + (app as any).nodeCache.flushAll(); +}); diff --git a/tests/unit/tickets.test.ts b/tests/unit/tickets.test.ts index a056519..662c8e0 100644 --- a/tests/unit/tickets.test.ts +++ b/tests/unit/tickets.test.ts @@ -375,4 +375,5 @@ afterAll(async () => { beforeEach(() => { ddbMock.reset(); vi.useFakeTimers(); + (app as any).nodeCache.flushAll(); });