Skip to content

Commit f722014

Browse files
committed
clear node cache before each test
1 parent 9f26381 commit f722014

File tree

9 files changed

+51
-35
lines changed

9 files changed

+51
-35
lines changed

tests/unit/auth.test.ts

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "./secret.testdata.js";
1414
import jwt from "jsonwebtoken";
1515
import { allAppRoles, AppRoles } from "../../src/common/roles.js";
16+
import { beforeEach, describe } from "node:test";
1617

1718
const ddbMock = mockClient(SecretsManagerClient);
1819

@@ -50,40 +51,46 @@ vi.stubEnv("JwtSigningKey", jwt_secret);
5051
const testJwt = createJwt();
5152
const testJwtNoGroups = createJwtNoGroups();
5253

53-
test("Test happy path", async () => {
54-
ddbMock.on(GetSecretValueCommand).resolves({
55-
SecretString: secretJson,
54+
describe("Test authentication", () => {
55+
test("Test happy path", async () => {
56+
ddbMock.on(GetSecretValueCommand).resolves({
57+
SecretString: secretJson,
58+
});
59+
const response = await app.inject({
60+
method: "GET",
61+
url: "/api/v1/protected",
62+
headers: {
63+
authorization: `Bearer ${testJwt}`,
64+
},
65+
});
66+
expect(response.statusCode).toBe(200);
67+
const jsonBody = await response.json();
68+
expect(jsonBody).toEqual({
69+
username: "[email protected]",
70+
roles: allAppRoles,
71+
});
5672
});
57-
const response = await app.inject({
58-
method: "GET",
59-
url: "/api/v1/protected",
60-
headers: {
61-
authorization: `Bearer ${testJwt}`,
62-
},
63-
});
64-
expect(response.statusCode).toBe(200);
65-
const jsonBody = await response.json();
66-
expect(jsonBody).toEqual({
67-
username: "[email protected]",
68-
roles: allAppRoles,
69-
});
70-
});
7173

72-
test("Test user-specific role grants", async () => {
73-
ddbMock.on(GetSecretValueCommand).resolves({
74-
SecretString: secretJson,
74+
test("Test user-specific role grants", async () => {
75+
ddbMock.on(GetSecretValueCommand).resolves({
76+
SecretString: secretJson,
77+
});
78+
const response = await app.inject({
79+
method: "GET",
80+
url: "/api/v1/protected",
81+
headers: {
82+
authorization: `Bearer ${testJwtNoGroups}`,
83+
},
84+
});
85+
expect(response.statusCode).toBe(200);
86+
const jsonBody = await response.json();
87+
expect(jsonBody).toEqual({
88+
username: "[email protected]",
89+
roles: [AppRoles.TICKETS_SCANNER],
90+
});
7591
});
76-
const response = await app.inject({
77-
method: "GET",
78-
url: "/api/v1/protected",
79-
headers: {
80-
authorization: `Bearer ${testJwtNoGroups}`,
81-
},
82-
});
83-
expect(response.statusCode).toBe(200);
84-
const jsonBody = await response.json();
85-
expect(jsonBody).toEqual({
86-
username: "[email protected]",
87-
roles: [AppRoles.TICKETS_SCANNER],
92+
93+
beforeEach(() => {
94+
(app as any).nodeCache.flushAll();
8895
});
8996
});

tests/unit/discordEvent.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe("Test Events <-> Discord integration", () => {
8585
vi.useRealTimers();
8686
});
8787
beforeEach(() => {
88+
(app as any).nodeCache.flushAll();
8889
ddbMock.reset();
8990
smMock.reset();
9091
vi.clearAllMocks();

tests/unit/entraGroupManagement.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const app = await init();
4242

4343
describe("Test Modify Group and List Group Routes", () => {
4444
beforeEach(() => {
45+
(app as any).nodeCache.flushAll();
4546
vi.clearAllMocks();
4647
smMock.on(GetSecretValueCommand).resolves({
4748
SecretString: JSON.stringify({ jwt_key: "test_jwt_key" }),
@@ -130,6 +131,7 @@ describe("Test Modify Group and List Group Routes", () => {
130131
await app.close();
131132
});
132133
beforeEach(() => {
134+
(app as any).nodeCache.flushAll();
133135
vi.clearAllMocks();
134136
vi.useFakeTimers();
135137
(getEntraIdToken as any).mockImplementation(async () => {

tests/unit/entraInviteUser.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe("Test Microsoft Entra ID user invitation", () => {
9595
});
9696

9797
beforeEach(() => {
98+
(app as any).nodeCache.flushAll();
9899
vi.clearAllMocks();
99100
vi.useFakeTimers();
100101
// Re-implement the mock

tests/unit/eventPost.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ afterAll(async () => {
197197
vi.useRealTimers();
198198
});
199199
beforeEach(() => {
200+
(app as any).nodeCache.flushAll();
200201
ddbMock.reset();
201202
smMock.reset();
202203
vi.useFakeTimers();

tests/unit/events.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ test("Test getting events", async () => {
2929
});
3030

3131
test("Test dynamodb error handling", async () => {
32-
ddbMock.on(ScanCommand).rejects("Could not get data.");
3332
const response = await app.inject({
3433
method: "GET",
3534
url: "/api/v1/events",
@@ -64,6 +63,7 @@ afterAll(async () => {
6463
vi.useRealTimers();
6564
});
6665
beforeEach(() => {
66+
(app as any).nodeCache.flushAll();
6767
ddbMock.reset();
6868
vi.useFakeTimers();
6969
});

tests/unit/health.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import init from "../../src/api/index.js";
33
import { EventGetResponse } from "../../src/api/routes/events.js";
44

55
const app = await init();
6-
test("Test getting events", async () => {
6+
test("Test getting health", async () => {
77
const response = await app.inject({
88
method: "GET",
99
url: "/api/v1/healthz",

tests/unit/organizations.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterAll, expect, test } from "vitest";
1+
import { afterAll, expect, test, beforeEach } from "vitest";
22
import init from "../../src/api/index.js";
33

44
const app = await init();
@@ -13,3 +13,6 @@ test("Test getting the list of organizations succeeds", async () => {
1313
afterAll(async () => {
1414
await app.close();
1515
});
16+
beforeEach(() => {
17+
(app as any).nodeCache.flushAll();
18+
});

tests/unit/tickets.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,5 @@ afterAll(async () => {
375375
beforeEach(() => {
376376
ddbMock.reset();
377377
vi.useFakeTimers();
378+
(app as any).nodeCache.flushAll();
378379
});

0 commit comments

Comments
 (0)