-
Notifications
You must be signed in to change notification settings - Fork 14
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
Conversation
sources/packages/backend/apps/api/src/auth/guards/requires-user-account.guard.ts
Show resolved
Hide resolved
sources/packages/backend/apps/api/src/testHelpers/auth/student-user-helpers.ts
Show resolved
Hide resolved
} | ||
|
||
/** | ||
* Test route which requires a user to be present as default authorization. |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
sources/packages/backend/apps/api/src/route-controllers/user/user.aest.controller.ts
Show resolved
Hide resolved
There was a problem hiding this 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 && |
There was a problem hiding this comment.
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)
?
Quality Gate passedIssues Measures |
There was a problem hiding this 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 👍
userToken.groups?.some((userGroup) => userGroup.startsWith(group)), | ||
); | ||
|
||
// Throw custom error to be handled by the consumer. | ||
if (!hasGroupAccess) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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
Validate user account for all routes
New global guard and decorator
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.@RequiresUserAccount()
is introduced to get the metadata context for the guard.Student page container
E2E Tests
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
Mock Reset
Volar extension