Skip to content

Commit

Permalink
[#12048] Migrate Get Instructors Action (#12210)
Browse files Browse the repository at this point in the history
  • Loading branch information
domlimm authored Mar 19, 2023
1 parent b52259d commit 6e4d648
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 31 deletions.
173 changes: 173 additions & 0 deletions src/it/java/teammates/it/ui/webapi/GetInstructorsActionIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package teammates.it.ui.webapi;

import java.util.List;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.util.Const;
import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Student;
import teammates.ui.output.InstructorData;
import teammates.ui.output.InstructorsData;
import teammates.ui.request.Intent;
import teammates.ui.webapi.GetInstructorsAction;
import teammates.ui.webapi.JsonResult;

/**
* SUT: {@link GetInstructorsAction}.
*/
public class GetInstructorsActionIT extends BaseActionIT<GetInstructorsAction> {

@Override
@BeforeMethod
protected void setUp() throws Exception {
super.setUp();
persistDataBundle(typicalBundle);
HibernateUtil.flushSession();
}

@Override
protected String getActionUri() {
return Const.ResourceURIs.INSTRUCTORS;
}

@Override
protected String getRequestMethod() {
return GET;
}

@Test
@Override
protected void testExecute() throws Exception {
Instructor instructor = typicalBundle.instructors.get("instructor1OfCourse1");

loginAsInstructor(instructor.getGoogleId());

______TS("Typical Success Case with FULL_DETAIL");
String[] params = new String[] {
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
Const.ParamsNames.INTENT, Intent.FULL_DETAIL.toString(),
};

GetInstructorsAction action = getAction(params);
JsonResult jsonResult = getJsonResult(action);

InstructorsData output = (InstructorsData) jsonResult.getOutput();
List<InstructorData> instructors = output.getInstructors();

assertEquals(2, instructors.size());

______TS("Typical Success Case with no intent");
params = new String[] {
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
Const.ParamsNames.INTENT, null,
};

action = getAction(params);
jsonResult = getJsonResult(action);

output = (InstructorsData) jsonResult.getOutput();
instructors = output.getInstructors();

assertEquals(2, instructors.size());

for (InstructorData instructorData : instructors) {
assertNull(instructorData.getGoogleId());
assertNull(instructorData.getJoinState());
assertNull(instructorData.getIsDisplayedToStudents());
assertNull(instructorData.getRole());
}

______TS("Unknown intent");
params = new String[] {
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
Const.ParamsNames.INTENT, "Unknown",
};

verifyHttpParameterFailure(params);
}

@Test
@Override
protected void testAccessControl() throws Exception {
Instructor instructor = typicalBundle.instructors.get("instructor1OfCourse1");
Student student = typicalBundle.students.get("student1InCourse1");

______TS("Course not found, logged in as instructor, intent FULL_DETAIL");
loginAsInstructor(instructor.getGoogleId());

String[] params = new String[] {
Const.ParamsNames.COURSE_ID, "does-not-exist-id",
Const.ParamsNames.INTENT, Intent.FULL_DETAIL.toString(),
};

verifyEntityNotFoundAcl(params);

______TS("Course not found, logged in as student, intent undefined");
loginAsStudent(student.getGoogleId());

params = new String[] {
Const.ParamsNames.COURSE_ID, "does-not-exist-id",
};

verifyEntityNotFoundAcl(params);

______TS("Unknown login entity, intent FULL_DETAIL");
loginAsUnregistered("unregistered");

params = new String[] {
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
Const.ParamsNames.INTENT, Intent.FULL_DETAIL.toString(),
};

verifyCannotAccess(params);

______TS("Unknown login entity, intent undefined");
params = new String[] {
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
};

verifyCannotAccess(params);

______TS("Unknown intent, logged in as instructor");
loginAsInstructor(instructor.getGoogleId());

params = new String[] {
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
Const.ParamsNames.INTENT, "Unknown",
};

verifyHttpParameterFailureAcl(params);

______TS("Intent FULL_DETAIL, should authenticate as instructor");
params = new String[] {
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
Const.ParamsNames.INTENT, Intent.FULL_DETAIL.toString(),
};

verifyOnlyInstructorsOfTheSameCourseCanAccess(instructor.getCourse(), params);

______TS("Intent undefined, should authenticate as student, access own course");
loginAsStudent(student.getGoogleId());

params = new String[] {
Const.ParamsNames.COURSE_ID, student.getCourseId(),
};

verifyCanAccess(params);

______TS("Intent undefined, should authenticate as student, access other course");
Student otherStudent = typicalBundle.students.get("student1InCourse2");

assertNotEquals(otherStudent.getCourse(), student.getCourse());

params = new String[] {
Const.ParamsNames.COURSE_ID, otherStudent.getCourseId(),
};

verifyCannotAccess(params);
}

}
26 changes: 26 additions & 0 deletions src/it/resources/data/typicalDataBundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
"id": "course-1"
},
"name": "Section 1"
},
"section1InCourse2": {
"id": "00000000-0000-4000-8000-000000000202",
"course": {
"id": "idOfCourse2"
},
"name": "Section 2"
}
},
"teams": {
Expand All @@ -66,6 +73,13 @@
"id": "00000000-0000-4000-8000-000000000201"
},
"name": "Team 1"
},
"team1InCourse2": {
"id": "00000000-0000-4000-8000-000000000302",
"section": {
"id": "00000000-0000-4000-8000-000000000202"
},
"name": "Team 1"
}
},
"deadlineExtensions": {
Expand Down Expand Up @@ -174,6 +188,18 @@
"email": "[email protected]",
"name": "student2 In Course1",
"comments": ""
},
"student1InCourse2": {
"id": "00000000-0000-4000-8000-000000000603",
"course": {
"id": "idOfCourse2"
},
"team": {
"id": "00000000-0000-4000-8000-000000000302"
},
"email": "[email protected]",
"name": "student1 In Course2",
"comments": ""
}
},
"feedbackSessions": {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ public Instructor getInstructorByGoogleId(String courseId, String googleId) {
return usersLogic.getInstructorByGoogleId(courseId, googleId);
}

/**
* Gets instructors by associated {@code courseId}.
*/
public List<Instructor> getInstructorsByCourse(String courseId) {
return usersLogic.getInstructorsForCourse(courseId);
}

/**
* Creates an instructor.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/teammates/ui/output/InstructorsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.stream.Collectors;

import teammates.common.datatransfer.attributes.InstructorAttributes;
import teammates.storage.sqlentity.Instructor;

/**
* The API output format of a list of instructors.
Expand All @@ -17,8 +17,8 @@ public InstructorsData() {
this.instructors = new ArrayList<>();
}

public InstructorsData(List<InstructorAttributes> instructorAttributesList) {
this.instructors = instructorAttributesList.stream().map(InstructorData::new).collect(Collectors.toList());
public InstructorsData(List<Instructor> instructorsList) {
this.instructors = instructorsList.stream().map(InstructorData::new).collect(Collectors.toList());
}

public List<InstructorData> getInstructors() {
Expand Down
Loading

0 comments on commit 6e4d648

Please sign in to comment.