-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1bd662
commit 8d3b1ee
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { afterAll, expect, test, beforeEach, vi, Mock } from "vitest"; | ||
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]" }; | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters