Skip to content

Commit

Permalink
add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devksingh4 committed Nov 8, 2024
1 parent a1bd662 commit 8d3b1ee
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/functions/entraId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export async function getEntraIdToken(
* @returns {Promise<boolean>} True if the invitation was successful
*/
export async function addToTenant(token: string, email: string) {
email = email.toLowerCase().replace(/\s/g, "");
if (!email.endsWith("@illinois.edu")) {
throw new EntraInvitationError({
email,
message: "User's domain must be illinois.edu to be invited.",
});
}
try {
const body = {
invitedUserEmailAddress: email,
Expand Down
113 changes: 113 additions & 0 deletions tests/unit/entraInviteUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { afterAll, expect, test, beforeEach, vi, Mock } from "vitest";

Check warning on line 1 in tests/unit/entraInviteUser.test.ts

View workflow job for this annotation

GitHub Actions / Run Unit Tests

'Mock' is defined but never used. Allowed unused vars must match /^_/u
import { mockClient } from "aws-sdk-client-mock";
import init from "../../src/index.js";
import { createJwt } from "./auth.test.js";
import { secretJson, secretObject } from "./secret.testdata.js";
import supertest from "supertest";
import { describe } from "node:test";
import {
GetSecretValueCommand,
SecretsManagerClient,
} from "@aws-sdk/client-secrets-manager";

vi.mock("../../src/functions/entraId.js", () => {
return {
...vi.importActual("../../src/functions/entraId.js"),
getEntraIdToken: vi.fn().mockImplementation(async () => {
return "ey.test.token";
}),
addToTenant: vi.fn().mockImplementation(async (email) => {
console.log("FUCK", email);
return { success: true, email: "[email protected]" };
}),
};
});

import { addToTenant, getEntraIdToken } from "../../src/functions/entraId.js";
import { EntraInvitationError } from "../../src/errors/index.js";

const smMock = mockClient(SecretsManagerClient);
const jwt_secret = secretObject["jwt_key"];

vi.stubEnv("JwtSigningKey", jwt_secret);

const app = await init();

describe("Test Microsoft Entra ID user invitation", () => {
test("Emails must end in @illinois.edu.", async () => {
smMock.on(GetSecretValueCommand).resolves({
SecretString: secretJson,
});
const testJwt = createJwt();
await app.ready();

const response = await supertest(app.server)
.post("/api/v1/sso/inviteUsers")
.set("authorization", `Bearer ${testJwt}`)
.send({
emails: ["[email protected]"],
});
expect(response.statusCode).toBe(500);
expect(getEntraIdToken).toHaveBeenCalled();
expect(addToTenant).toHaveBeenCalled();
});
test("Happy path", async () => {
smMock.on(GetSecretValueCommand).resolves({
SecretString: secretJson,
});
const testJwt = createJwt();
await app.ready();

const response = await supertest(app.server)
.post("/api/v1/sso/inviteUsers")
.set("authorization", `Bearer ${testJwt}`)
.send({
emails: ["[email protected]"],
});
expect(response.statusCode).toBe(201);
expect(getEntraIdToken).toHaveBeenCalled();
expect(addToTenant).toHaveBeenCalled();
});
test("Happy path", async () => {
smMock.on(GetSecretValueCommand).resolves({
SecretString: secretJson,
});
const testJwt = createJwt();
await app.ready();

const response = await supertest(app.server)
.post("/api/v1/sso/inviteUsers")
.set("authorization", `Bearer ${testJwt}`)
.send({
emails: ["[email protected]"],
});
expect(response.statusCode).toBe(201);
expect(getEntraIdToken).toHaveBeenCalled();
expect(addToTenant).toHaveBeenCalled();
});
afterAll(async () => {
await app.close();
vi.useRealTimers();
});

beforeEach(() => {
vi.resetAllMocks();
vi.useFakeTimers();
// Re-implement the mock
(getEntraIdToken as any).mockImplementation(async () => {
return "ey.test.token";
});
(addToTenant as any).mockImplementation(
async (token: string, email: string) => {
email = email.toLowerCase().replace(/\s/g, "");
if (!email.endsWith("@illinois.edu")) {
throw new EntraInvitationError({
email,
message: "User's domain must be illinois.edu to be invited.",
});
}
return { success: true, email: "[email protected]" };
},
);
});
});
2 changes: 2 additions & 0 deletions tests/unit/secret.testdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const secretObject = {
jwt_key: "somethingreallysecret",
discord_guild_id: "12345",
discord_bot_token: "12345",
entra_id_private_key: "",
entra_id_thumbprint: "",
} as SecretConfig & { jwt_key: string };

const secretJson = JSON.stringify(secretObject);
Expand Down

0 comments on commit 8d3b1ee

Please sign in to comment.