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

[#12048] Migrate tests for GetStudentsActionTest #13243

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mingyang143
Copy link
Contributor

Part of #12048

Outline of Solution
Change GetStudentsActionTest.java to ensure compatibility with the PostgreSQL database following the database migration.

@mingyang143 mingyang143 self-assigned this Mar 10, 2025
@domoberzin domoberzin added the s.ToReview The PR is waiting for review(s) label Mar 22, 2025
Copy link
Contributor

@DhiraPT DhiraPT left a comment

Choose a reason for hiding this comment

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

Appreciate your effort! This is a complex class to test.

Just keep in mind that all the Google IDs are null by default since Student and Instructor does not have an Account unless we set using setAccount. So better to store the IDs in variables so they can be reused for stubbing or logging in.

Also, consider standardizing the test/variable names (for instructor-related) to something like withCoursePermission, withSectionPermission, withoutCoursePermission, and withoutSectionPermission, rather than using "same" or "different" so it's clearer which conditional branch is being tested. For teams, we can still use "same" or "different."

@mingyang143 mingyang143 added s.Ongoing The PR is being worked on by the author(s) and removed s.ToReview The PR is waiting for review(s) labels Mar 24, 2025
Copy link
Contributor

@dishenggg dishenggg 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 the detailed test cases!

Just some comments and I noticed that you have marked the @DhiraPT 's comments as resolved but the changes are not reflected in the PR maybe you can look into that as well

}

@Test
void testExecute_instructorWithoutPermissionWithInvalidTeamParams_emptyList() {
Copy link
Contributor

Choose a reason for hiding this comment

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

small nit here lets also rename to WithNullTeamParams since null is a valid value for this action

Comment on lines +152 to +169
void testExecute_instructorWithoutPermissionWithTeamParams_success() {
loginAsInstructor(stubInstructorWithoutPrivileges.getGoogleId());
when(mockLogic.getInstructorByGoogleId(stubCourse.getId(), stubInstructorWithoutPrivileges.getGoogleId()))
.thenReturn(stubInstructorWithoutPrivileges);
when(mockLogic.getStudentsByTeamName(stubTeamOne.getName(), stubCourse.getId())).thenReturn(stubStudentListOne);

String[] params = {
Const.ParamsNames.COURSE_ID, stubCourse.getId(),
Const.ParamsNames.TEAM_NAME, stubTeamOne.getName(),
};
GetStudentsAction action = getAction(params);
JsonResult jsonResult = getJsonResult(action);
StudentsData actualStudentsData = (StudentsData) jsonResult.getOutput();

verifyStudentsData(stubStudentListOne, actualStudentsData, Type.STUDENT);
verify(mockLogic, times(1)).getStudentsByTeamName(stubTeamOne.getName(), stubCourse.getId());
verify(mockLogic, never()).getStudentsForCourse(stubCourse.getId());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think since the test skips checkSpecificAccessControl, this test runs with no problems but it should fail the access control check.

Lets modify this to be a AccessControlTest instead

Same for #13243 (comment)

Comment on lines +190 to +215
@Test
void testExecute_instructorWithDifferentSectionPrivilegesAsStudents_emptyList() {
InstructorPrivileges wrongPrivileges =
new InstructorPrivileges(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_CUSTOM);
wrongPrivileges.updatePrivilege("random-1",
Const.InstructorPermissions.CAN_VIEW_STUDENT_IN_SECTIONS, true);
Instructor stubInstructorWithOnlyViewPrivilegesForDifferentSection =
new Instructor(stubCourse, "instructor-3-name", "valid3@teammates.tmt",
false, Const.DEFAULT_DISPLAY_NAME_FOR_INSTRUCTOR, customRole, wrongPrivileges);
loginAsInstructor(stubInstructorWithOnlyViewPrivilegesForDifferentSection.getGoogleId());
when(mockLogic.getInstructorByGoogleId(stubCourse.getId(),
stubInstructorWithOnlyViewPrivilegesForDifferentSection.getGoogleId()))
.thenReturn(stubInstructorWithOnlyViewPrivilegesForDifferentSection);
when(mockLogic.getStudentsForCourse(stubCourse.getId())).thenReturn(stubStudentListTwo);

String[] params = {
Const.ParamsNames.COURSE_ID, stubCourse.getId(),
};
GetStudentsAction action = getAction(params);
JsonResult jsonResult = getJsonResult(action);
StudentsData actualStudentsData = (StudentsData) jsonResult.getOutput();

assertEquals(0, actualStudentsData.getStudents().size());
verify(mockLogic, never()).getStudentsByTeamName(null, stubCourse.getId());
verify(mockLogic, times(1)).getStudentsForCourse(stubCourse.getId());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly I think this should fail at access control check as well

Comment on lines +114 to +119
void testExecute_instructorWithPermission_success() {
loginAsInstructor(stubInstructorWithAllPrivileges.getGoogleId());
when(mockLogic.getInstructorByGoogleId(stubCourse.getId(), stubInstructorWithAllPrivileges.getGoogleId()))
.thenReturn(stubInstructorWithAllPrivileges);
when(mockLogic.getStudentsForCourse(stubCourse.getId())).thenReturn(stubStudentListTwo);

Copy link
Contributor

Choose a reason for hiding this comment

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

Lets create 1 more stubSection for this test case

Since the difference between this test case and testExecute_instructorWithSameSectionPrivilegesAsStudents_success is the filtering of students by section

We can check that for this test cases students in all sections are returned and for the other test only students in the section with permission

Comment on lines +237 to +275
@Test
void testExecute_adminWithTeamNameParams_success() {
loginAsAdmin();
when(mockLogic.getStudentsByTeamName(stubStudentOne.getTeam().getName(),
stubStudentOne.getCourse().getId())).thenReturn(stubStudentListOne);

String[] params = {
Const.ParamsNames.COURSE_ID, stubStudentOne.getCourse().getId(),
Const.ParamsNames.TEAM_NAME, stubStudentOne.getTeam().getName(),
};
GetStudentsAction action = getAction(params);
JsonResult jsonResult = getJsonResult(action);
StudentsData actualStudentsData = (StudentsData) jsonResult.getOutput();

verifyStudentsData(stubStudentListOne, actualStudentsData, Type.STUDENT);
verify(mockLogic, times(1))
.getStudentsByTeamName(stubStudentOne.getTeamName(), stubCourse.getId());
verify(mockLogic, never()).getStudentsForCourse(stubCourse.getId());
}

@Test
void testExecute_unregisteredLoggedInUser_success() {
loginAsUnregistered("id");
when(mockLogic.getStudentsByTeamName(stubStudentOne.getTeam().getName(),
stubStudentOne.getCourse().getId())).thenReturn(stubStudentListOne);

String[] params = {
Const.ParamsNames.COURSE_ID, stubStudentOne.getCourse().getId(),
Const.ParamsNames.TEAM_NAME, stubStudentOne.getTeam().getName(),
};
GetStudentsAction action = getAction(params);
JsonResult jsonResult = getJsonResult(action);
StudentsData actualStudentsData = (StudentsData) jsonResult.getOutput();

verifyStudentsData(stubStudentListOne, actualStudentsData, Type.STUDENT);
verify(mockLogic, times(1))
.getStudentsByTeamName(stubStudentOne.getTeamName(), stubCourse.getId());
verify(mockLogic, never()).getStudentsForCourse(stubCourse.getId());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Something I just noticed for testExecute_.... we dont have to do loginAs... as the permissions are not checked

This means we can convert these to be tested under testSpecificAccessControl instead

@mingyang143
Copy link
Contributor Author

mingyang143 commented Mar 28, 2025

Thanks for the detailed test cases!

Just some comments and I noticed that you have marked the @DhiraPT 's comments as resolved but the changes are not reflected in the PR maybe you can look into that as well

Oh, @dishenggg, so sorry I forgot to push the changes. Will puhs the updated changes in abit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s.Ongoing The PR is being worked on by the author(s)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants