Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3881 - Validate user account for all routes #3985

Merged
merged 21 commits into from
Nov 25, 2024

Conversation

dheepak-aot
Copy link
Collaborator

@dheepak-aot dheepak-aot commented Nov 22, 2024

Validate user account for all routes

New global guard and decorator

  • New guard RequiresUserAccountGuard has been introduced globally to ensure that routes are authorized with the user token which belongs to valid SIMS user. There are exceptional routes like public routes and routes used that setup the user itself are skipped from this validation.
  • New decorator @RequiresUserAccount() is introduced to get the metadata context for the guard.

Student page container

  • Student page container updated to NOT render restriction and SIN banners for pages which does not require a valid student account.

E2E Tests

  • The existing method to mock the student info from token mockUserLoginInfo() does not have a way to restore the mock, if the mock needs to be restored for other tests in same suite.
    Hence refactored the code to use jest.spyOn() to mock the userService method implementation and also created a reset mock method to reset the mock as required in the test suite.

Here is an example.
Mock applied

image

Mock Reset
image

  • Created new Auth E2E tests

image

Volar extension

  • Updated the workspace file with deprecated vue extension by replacing with recommended extension.
    image

@dheepak-aot dheepak-aot marked this pull request as ready for review November 22, 2024 19:26
}

/**
* Test route which requires a user to be present as default authorization.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review the comment.

return request(app.getHttpServer())
.get("/auth-test/default-requires-user-route")
.auth(studentAccessToken, { type: "bearer" })
.expect(HttpStatus.FORBIDDEN);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even not being consistent in this file we have been validating the full error including the message in the expect.
If possible, I would recommend adding it to these new methods.

Copy link
Collaborator

@andrewsignori-aot andrewsignori-aot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work with the guards, identifying the controllers and making E2E tests work.
Please take a look at the comments.

const axiosError = error as AxiosError;
if (axiosError.response?.status === StatusCodes.FORBIDDEN) {
if (
error instanceof AxiosError &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to check using if (error instanceof ApiProcessError)?

Copy link

E2E Workflow Workers Coverage Report

Totals Coverage
Statements: 65.43% ( 583 / 891 )
Methods: 59.26% ( 64 / 108 )
Lines: 68.54% ( 464 / 677 )
Branches: 51.89% ( 55 / 106 )

Copy link
Collaborator

@andrewsignori-aot andrewsignori-aot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the changes, looks good 👍

Copy link

Backend Unit Tests Coverage Report

Totals Coverage
Statements: 22.07% ( 3733 / 16911 )
Methods: 10.16% ( 214 / 2107 )
Lines: 25.42% ( 3241 / 12749 )
Branches: 13.53% ( 278 / 2055 )

Copy link

E2E Queue Consumers Coverage Report

Totals Coverage
Statements: 84.17% ( 1234 / 1466 )
Methods: 83.8% ( 119 / 142 )
Lines: 85.44% ( 1050 / 1229 )
Branches: 68.42% ( 65 / 95 )

Copy link

E2E SIMS API Coverage Report

Totals Coverage
Statements: 67.04% ( 5810 / 8667 )
Methods: 64.86% ( 720 / 1110 )
Lines: 70.99% ( 4561 / 6425 )
Branches: 46.73% ( 529 / 1132 )

userToken.groups?.some((userGroup) => userGroup.startsWith(group)),
);

// Throw custom error to be handled by the consumer.
if (!hasGroupAccess) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const { user } = context.switchToHttp().getRequest();
const userToken = user as IUserToken;

if (!userToken?.userId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

[context.getHandler(), context.getClass()],
);

if (requiresUserAccount === false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* This could used to reset the mock after each test.
* @param testingModule testing module.
*/
export async function resetMockUserLoginInfo(testingModule: TestingModule) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Collaborator

@guru-aot guru-aot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice work @dheepak-aot

@dheepak-aot dheepak-aot added this pull request to the merge queue Nov 25, 2024
Merged via the queue into main with commit 4f25ed6 Nov 25, 2024
20 checks passed
@dheepak-aot dheepak-aot deleted the fix/#3881-validate-user-account branch November 27, 2024 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants