Skip to content

Commit

Permalink
clear node cache before each test
Browse files Browse the repository at this point in the history
  • Loading branch information
devksingh4 committed Jan 25, 2025
1 parent 9f26381 commit f722014
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 35 deletions.
71 changes: 39 additions & 32 deletions tests/unit/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
});
});
1 change: 1 addition & 0 deletions tests/unit/discordEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe("Test Events <-> Discord integration", () => {
vi.useRealTimers();
});
beforeEach(() => {
(app as any).nodeCache.flushAll();
ddbMock.reset();
smMock.reset();
vi.clearAllMocks();
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/entraGroupManagement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }),
Expand Down Expand Up @@ -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 () => {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/entraInviteUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/unit/eventPost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ afterAll(async () => {
vi.useRealTimers();
});
beforeEach(() => {
(app as any).nodeCache.flushAll();
ddbMock.reset();
smMock.reset();
vi.useFakeTimers();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -64,6 +63,7 @@ afterAll(async () => {
vi.useRealTimers();
});
beforeEach(() => {
(app as any).nodeCache.flushAll();
ddbMock.reset();
vi.useFakeTimers();
});
2 changes: 1 addition & 1 deletion tests/unit/health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/organizations.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -13,3 +13,6 @@ test("Test getting the list of organizations succeeds", async () => {
afterAll(async () => {
await app.close();
});
beforeEach(() => {
(app as any).nodeCache.flushAll();
});
1 change: 1 addition & 0 deletions tests/unit/tickets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,5 @@ afterAll(async () => {
beforeEach(() => {
ddbMock.reset();
vi.useFakeTimers();
(app as any).nodeCache.flushAll();
});

0 comments on commit f722014

Please sign in to comment.