-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(test) post auth test * update test for mostly full coverage * update word to not look like a secret * some test clean up and refactoring * clean up * allow bad secret value for test * refined a test, and made pr changes
- Loading branch information
Showing
9 changed files
with
228 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { describe, it, expect, vi, afterAll } from "vitest"; | ||
import { Context } from "aws-lambda"; | ||
import { handler } from "./postAuth"; | ||
import { | ||
makoStateSubmitter, | ||
setMockUsername, | ||
superUser, | ||
TEST_IDM_USERS, | ||
USER_POOL_ID, | ||
} from "mocks"; | ||
|
||
const callback = vi.fn(); | ||
describe("process emails Handler", () => { | ||
afterAll(() => { | ||
setMockUsername(makoStateSubmitter); | ||
}); | ||
it("should return an error due to missing arn", async () => { | ||
delete process.env.idmAuthzApiKeyArn; | ||
|
||
await expect(handler({ test: "test" }, {} as Context, callback)).rejects.toThrowError( | ||
"ERROR: process.env.idmAuthzApiKeyArn is required", | ||
); | ||
}); | ||
it("should return an error due to a missing endpoint", async () => { | ||
delete process.env.idmAuthzApiEndpoint; | ||
await expect(handler({ test: "test" }, {} as Context, callback)).rejects.toThrowError( | ||
"ERROR: process.env.idmAuthzApiEndpoint is required", | ||
); | ||
}); | ||
it("should return an error due to the arn being incorrect", async () => { | ||
process.env.idmAuthzApiKeyArn = "bad-ARN"; // pragma: allowlist secret | ||
await expect(handler({ test: "test" }, {} as Context, callback)).rejects.toThrowError( | ||
"Failed to fetch secret bad-ARN: Secret bad-ARN has no SecretString field present in response", | ||
); | ||
}); | ||
|
||
it("should return the request if it is missing an identity", async () => { | ||
const consoleSpy = vi.spyOn(console, "log"); | ||
const missingIdentity = await handler( | ||
{ | ||
request: { | ||
userAttributes: TEST_IDM_USERS.testStateIDMUserMissingIdentity, | ||
}, | ||
}, | ||
{} as Context, | ||
callback, | ||
); | ||
expect(consoleSpy).toBeCalledWith("User is not managed externally. Nothing to do."); | ||
expect(missingIdentity).toStrictEqual({ | ||
request: { | ||
userAttributes: TEST_IDM_USERS.testStateIDMUserMissingIdentity, | ||
}, | ||
}); | ||
}); | ||
it("should log an error since it cannot authorize the user", async () => { | ||
const errorSpy = vi.spyOn(console, "error"); | ||
const missingIdentity = await handler( | ||
{ | ||
request: { | ||
userAttributes: TEST_IDM_USERS.testStateIDMUser, | ||
}, | ||
}, | ||
{} as Context, | ||
callback, | ||
); | ||
const error = new Error("Network response was not ok. Response was 401: Unauthorized"); | ||
expect(errorSpy).toHaveBeenCalledWith("Error performing post auth:", error); | ||
expect(errorSpy).toBeCalledTimes(1); | ||
expect(missingIdentity).toStrictEqual({ | ||
request: { | ||
userAttributes: TEST_IDM_USERS.testStateIDMUser, | ||
}, | ||
}); | ||
}); | ||
it("should return the user and update the user in the service", async () => { | ||
const consoleSpy = vi.spyOn(console, "log"); | ||
const validUser = await handler( | ||
{ | ||
request: { | ||
userAttributes: TEST_IDM_USERS.testStateIDMUserGood, | ||
}, | ||
userName: superUser.Username, | ||
userPoolId: USER_POOL_ID, | ||
}, | ||
{} as Context, | ||
callback, | ||
); | ||
expect(consoleSpy).toBeCalledWith( | ||
`Attributes for user ${superUser.Username} updated successfully.`, | ||
); | ||
expect(validUser).toStrictEqual({ | ||
request: { | ||
userAttributes: TEST_IDM_USERS.testStateIDMUserGood, | ||
}, | ||
userName: superUser.Username, | ||
userPoolId: USER_POOL_ID, | ||
}); | ||
}); | ||
}); |
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
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,40 @@ | ||
export const testStateIDMUserMissingIdentity = { | ||
sub: "0000aaaa-0000-00aa-0a0a-aaaaaa000000", | ||
"custom:cms-roles": "onemac-micro-statesubmitter", | ||
"custom:state": "VA,OH,SC,CO,GA,MD", | ||
email_verified: true, | ||
given_name: "State", | ||
family_name: "Person", | ||
username: "abcd", | ||
email: "[email protected]", | ||
}; | ||
|
||
export const testStateIDMUser = { | ||
sub: "0000aaaa-0000-00aa-0a0a-aaaaaa000000", | ||
"custom:cms-roles": "onemac-micro-statesubmitter", | ||
"custom:state": "VA,OH,SC,CO,GA,MD", | ||
email_verified: true, | ||
given_name: "State", | ||
family_name: "Person", | ||
"custom:username": "fail", | ||
email: "[email protected]", | ||
identities: | ||
'[{"dateCreated":"1709308952587","userId":"abc123","providerName":"IDM","providerType":"OIDC","issuer":null,"primary":"true"}]', | ||
}; | ||
export const testStateIDMUserGood = { | ||
sub: "0000aaaa-0000-00aa-0a0a-aaaaaa000000", | ||
"custom:cms-roles": "onemac-micro-super", | ||
"custom:state": "VA,OH,SC,CO,GA,MD", | ||
email_verified: true, | ||
given_name: "State", | ||
family_name: "Person", | ||
"custom:username": "abcd", | ||
email: "[email protected]", | ||
identities: | ||
'[{"dateCreated":"1709308952587","userId":"abc123","providerName":"IDM","providerType":"OIDC","issuer":null,"primary":"true"}]', | ||
}; | ||
export const TEST_IDM_USERS = { | ||
testStateIDMUser, | ||
testStateIDMUserGood, | ||
testStateIDMUserMissingIdentity, | ||
}; |
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 |
---|---|---|
|
@@ -34,6 +34,39 @@ export const makoStateSubmitter: TestUserData = { | |
], | ||
Username: "cd400c39-9e7c-4341-b62f-234e2ecb339d", | ||
}; | ||
export const superUser: TestUserData = { | ||
UserAttributes: [ | ||
{ | ||
Name: "email", | ||
Value: "[email protected]", | ||
}, | ||
{ | ||
Name: "email_verified", | ||
Value: "true", | ||
}, | ||
{ | ||
Name: "given_name", | ||
Value: "Stateuser", | ||
}, | ||
{ | ||
Name: "family_name", | ||
Value: "Tester", | ||
}, | ||
{ | ||
Name: "custom:state", | ||
Value: "ZZ", | ||
}, | ||
{ | ||
Name: "custom:cms-roles", | ||
Value: "onemac-micro-super", | ||
}, | ||
{ | ||
Name: "sub", | ||
Value: "cd400c39-9e7c-4341-b62f-234e2ecb339e", | ||
}, | ||
], | ||
Username: "cd400c39-9e7c-4341-b62f-234e2ecb339e", | ||
}; | ||
|
||
export const stateSubmitter: TestUserData = { | ||
UserAttributes: [ | ||
|
@@ -275,6 +308,7 @@ export const testNewStateSubmitter: TestUserData = { | |
|
||
export const stateSubmitters: TestUserData[] = [ | ||
makoStateSubmitter, | ||
superUser, | ||
stateSubmitter, | ||
noDataStateSubmitter, | ||
coStateSubmitter, | ||
|
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,30 @@ | ||
import { http, HttpResponse } from "msw"; | ||
|
||
const defaultIDMHandler = http.get( | ||
"https://dimauthzendpoint.com/api/v1/authz/id/all", | ||
async ({ request }) => { | ||
const url = new URL(request.url); | ||
const id = url.searchParams.get("userId"); | ||
|
||
if (id === "fail") { | ||
return HttpResponse.json({ text: "Failed to retrieve user" }, { status: 401 }); | ||
} else if (id === "abcd") { | ||
return HttpResponse.json( | ||
{ | ||
userProfileAppRoles: { | ||
userRolesInfoList: [ | ||
{ | ||
roleName: "onemac-micro-statesubmitter", | ||
roleAttributes: [{ name: "State/Territory", value: "VA" }], | ||
}, | ||
], | ||
}, | ||
}, | ||
{ status: 200 }, | ||
); | ||
} | ||
return HttpResponse.json({ text: "Failed to retrieve user" }, { status: 200 }); | ||
}, | ||
); | ||
|
||
export const idmHandlers = [defaultIDMHandler]; |
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