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

#1964 - Added role to restrict MSFAA reissue #1985

Merged
merged 11 commits into from
Jun 6, 2023
1 change: 1 addition & 0 deletions sources/packages/backend/apps/api/src/auth/roles.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export enum Role {
InstitutionCreateNote = "institution-create-note",
InstitutionApproveDeclineDesignation = "institution-approve-decline-designation",
InstitutionApproveDeclineOfferingChanges = "institution-approve-decline-offering-changes",
StudentReissueMSFAA = "student-reissue-msfaa",
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,40 @@ describe("ApplicationAESTController(e2e)-reissueMSFAA", () => {
}
}

it("Should not reissue an MSFAA when user is not a business administrator.", async () => {
// Arrange
const student = await saveFakeStudent(db.dataSource);
const currentMSFAA = createFakeMSFAANumber(
{ student },
{
state: MSFAAStates.Signed | MSFAAStates.CancelledOtherProvince,
},
);
await db.msfaaNumber.save(currentMSFAA);
const application = await saveFakeApplicationDisbursements(
db.dataSource,
{ student, msfaaNumber: currentMSFAA },
{
applicationStatus: ApplicationStatus.Completed,
createSecondDisbursement: true,
},
);

const endpoint = `/aest/application/${application.id}/reissue-msfaa`;
const token = await getAESTToken(AESTGroups.OperationsAdministrators);

// Act/Assert
await request(app.getHttpServer())
.post(endpoint)
.auth(token, BEARER_AUTH_TYPE)
.expect(HttpStatus.FORBIDDEN)
.expect({
statusCode: HttpStatus.FORBIDDEN,
message: "Forbidden resource",
error: "Forbidden",
});
});

afterAll(async () => {
await app?.close();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
import { ApplicationService } from "../../services";
import BaseController from "../BaseController";
import { ApplicationBaseAPIOutDTO } from "./models/application.dto";
import { AllowAuthorizedParty, Groups, UserToken } from "../../auth/decorators";
import {
AllowAuthorizedParty,
Groups,
Roles,
UserToken,
} from "../../auth/decorators";
import { AuthorizedParties } from "../../auth/authorized-parties.enum";
import { UserGroups } from "../../auth/user-groups.enum";
import {
Expand All @@ -28,7 +33,7 @@ import {
APPLICATION_NOT_FOUND,
INVALID_OPERATION_IN_THE_CURRENT_STATUS,
} from "@sims/services/constants";
import { IUserToken } from "../../auth";
import { IUserToken, Role } from "../../auth";

@AllowAuthorizedParty(AuthorizedParties.aest)
@Groups(UserGroups.AESTUser)
Expand Down Expand Up @@ -80,6 +85,7 @@ export class ApplicationAESTController extends BaseController {
* @param applicationId reference application id.
* @returns the newly created MSFAA.
*/
@Roles(Role.StudentReissueMSFAA)
@Post(":applicationId/reissue-msfaa")
@ApiNotFoundResponse({ description: "Application id not found." })
@ApiUnprocessableEntityResponse({
Expand Down
18 changes: 18 additions & 0 deletions sources/packages/backend/apps/api/test/auth.aest.e2e-spec.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't the test/ dir be named as _tests_?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added it to the same directory as auth.e2e-spec.ts and institution.e2e-spec.ts. What do you guys think @dheepak-aot @andrewsignori-aot @guru-aot @sh16011993 ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In our application we do not follow the test package at root pattern like typically what OOTB code template creates.
I would suggest this e2e-spec to be in $same_directory_of_code_which_is_tested/tests/auth.aest.e2e-spec.ts

Copy link
Collaborator

Choose a reason for hiding this comment

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

Following the application structure, I believe the directory should be renamed to _tests_ and the test should be moved to the directory of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I moved that test to the same directory of aest-token-helpers.ts.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { JwtService } from "@nestjs/jwt";
import { IUserToken, Role } from "../src/auth";
import { AESTGroups, getAESTToken } from "../src/testHelpers";

const jwtService = new JwtService();

describe("Auth Ministry", () => {
it("Should have all roles when ministry user is a business administrator.", async () => {
//Act
const token = await getAESTToken(AESTGroups.BusinessAdministrators);
const decodedToken = jwtService.decode(token) as IUserToken;
decodedToken.resource_access.aest.roles.sort((a, b) => a.localeCompare(b));
const allAESTRoles = Object.values(Role).sort((a, b) => a.localeCompare(b));

//Assert
expect(decodedToken.resource_access.aest.roles).toEqual(allAESTRoles);
});
});
1 change: 1 addition & 0 deletions sources/packages/web/src/types/contracts/aest/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export enum Role {
InstitutionCreateNote = "institution-create-note",
InstitutionApproveDeclineDesignation = "institution-approve-decline-designation",
InstitutionApproveDeclineOfferingChanges = "institution-approve-decline-offering-changes",
StudentReissueMSFAA = "student-reissue-msfaa",
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about adding <check-permission-role> in the vue side. I know the Vue button is inside form.io. But is there a way to disable the button when the user does not have right permission?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'm working on that. Thanks.

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</template>
<notice-of-assessment-form-view
:assessment-id="assessmentId"
:can-reissue-m-s-f-a-a="true"
:can-reissue-m-s-f-a-a="hasStudentReissueMSFAARole"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe that usually, the vue is disabling the component instead of hiding it. For this case to keep it simple I would no mind having it removed.

@reissue-m-s-f-a-a="reissueMSFAA"
/>
</full-page-container>
Expand All @@ -23,7 +23,8 @@ import { defineComponent } from "vue";
import NoticeOfAssessmentFormView from "@/components/common/NoticeOfAssessmentFormView.vue";
import { AESTRoutesConst } from "@/constants/routes/RouteConstants";
import { ApplicationService } from "@/services/ApplicationService";
import { useSnackBar } from "@/composables";
import { useAuth, useSnackBar } from "@/composables";
import { Role } from "@/types";

export default defineComponent({
components: {
Expand All @@ -45,6 +46,8 @@ export default defineComponent({
},
setup() {
const snackBar = useSnackBar();
const { hasRole } = useAuth();
const hasStudentReissueMSFAARole = hasRole(Role.StudentReissueMSFAA);
const reissueMSFAA = async (
applicationId: number,
reloadNOA: () => Promise<void>,
Expand All @@ -62,7 +65,7 @@ export default defineComponent({
}
};

return { reissueMSFAA, AESTRoutesConst };
return { reissueMSFAA, AESTRoutesConst, hasStudentReissueMSFAARole };
},
});
</script>