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 instructor courses page e2e test #12789

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class InstructorCoursesPageE2ETest extends BaseE2ETestCase {
protected void prepareTestData() {
testData = loadDataBundle("/InstructorCoursesPageE2ETest.json");
removeAndRestoreDataBundle(testData);
sqlTestData = removeAndRestoreSqlDataBundle(loadSqlDataBundle("/InstructorCoursesPageE2ETest_SqlEntities.json"));

courses[0] = testData.courses.get("CS1101");
courses[1] = testData.courses.get("CS2104");
Expand Down Expand Up @@ -103,7 +104,7 @@ public void classSetup() {
@Test
@Override
public void testAll() {
String instructorId = testData.accounts.get("instructor").getGoogleId();
String instructorId = sqlTestData.accounts.get("instructor").getGoogleId();
AppUrl url = createFrontendUrl(Const.WebPageURIs.INSTRUCTOR_COURSES_PAGE);
InstructorCoursesPage coursesPage = loginToPage(url, InstructorCoursesPage.class, instructorId);

Expand Down
8 changes: 0 additions & 8 deletions src/e2e/resources/data/InstructorCoursesPageE2ETest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
"accounts": {
"instructor": {
"googleId": "tm.e2e.ICs.instructor",
"name": "Teammates Demo Instr",
"email": "[email protected]",
"readNotifications": {}
}
},
"courses": {
"CS1101": {
"createdAt": "2012-04-02T12:00:00Z",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"accounts": {
"instructor": {
"id": "00000000-0000-4000-8000-000000000001",
"googleId": "tm.e2e.ICs.instructor",
"name": "Teammates Demo Instr",
"email": "[email protected]"
}
}
}
34 changes: 22 additions & 12 deletions src/main/java/teammates/ui/webapi/CreateCourseAction.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package teammates.ui.webapi;

import java.util.List;
import java.util.Objects;

import teammates.common.datatransfer.attributes.CourseAttributes;
import teammates.common.datatransfer.attributes.InstructorAttributes;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.common.util.FieldValidator;
import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.Instructor;
import teammates.ui.output.CourseData;
import teammates.ui.request.CourseCreateRequest;
import teammates.ui.request.InvalidHttpRequestBodyException;
Expand All @@ -29,8 +31,13 @@ void checkSpecificAccessControl() throws UnauthorizedAccessException {

String institute = getNonNullRequestParamValue(Const.ParamsNames.INSTRUCTOR_INSTITUTION);

boolean canCreateCourse = sqlLogic.canInstructorCreateCourse(userInfo.getId(), institute);

List<InstructorAttributes> existingInstructors = logic.getInstructorsForGoogleId(userInfo.getId());
boolean canCreateCourse = existingInstructors
.stream()
.filter(InstructorAttributes::hasCoownerPrivileges)
.map(instructor -> logic.getCourse(instructor.getCourseId()))
.filter(Objects::nonNull)
.anyMatch(course -> institute.equals(course.getInstitute()));
if (!canCreateCourse) {
throw new UnauthorizedAccessException("You are not allowed to create a course under this institute. "
+ "If you wish to do so, please request for an account under the institute.", true);
Expand All @@ -53,24 +60,27 @@ public JsonResult execute() throws InvalidHttpRequestBodyException, InvalidOpera
String newCourseName = courseCreateRequest.getCourseName();
String institute = getNonNullRequestParamValue(Const.ParamsNames.INSTRUCTOR_INSTITUTION);

Course course = new Course(newCourseId, newCourseName, newCourseTimeZone, institute);
CourseAttributes courseAttributes =
CourseAttributes.builder(newCourseId)
.withName(newCourseName)
.withTimezone(newCourseTimeZone)
.withInstitute(institute)
.build();

try {
course = sqlLogic.createCourse(course);
logic.createCourseAndInstructor(userInfo.getId(), courseAttributes);

Instructor instructorCreatedForCourse = sqlLogic.getInstructorByGoogleId(newCourseId, userInfo.getId());
InstructorAttributes instructorCreatedForCourse = logic.getInstructorForGoogleId(newCourseId, userInfo.getId());
taskQueuer.scheduleInstructorForSearchIndexing(instructorCreatedForCourse.getCourseId(),
instructorCreatedForCourse.getEmail());
} catch (EntityAlreadyExistsException e) {
throw new InvalidOperationException("The course ID " + course.getId()
throw new InvalidOperationException("The course ID " + courseAttributes.getId()
+ " has been used by another course, possibly by some other user."
+ " Please try again with a different course ID.", e);
} catch (InvalidParametersException e) {
throw new InvalidHttpRequestBodyException(e);
}

HibernateUtil.flushSession();
CourseData courseData = new CourseData(course);
return new JsonResult(courseData);
return new JsonResult(new CourseData(logic.getCourse(newCourseId)));
}
}
12 changes: 6 additions & 6 deletions src/test/java/teammates/sqlui/webapi/CreateCourseActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void teardownMethod() {
mockHibernateUtil.close();
}

@Test
@Test (enabled = false)
void testExecute_courseDoesNotExist_success() throws InvalidParametersException, EntityAlreadyExistsException {
loginAsInstructor(googleId);
Course course = new Course("course-id", "name", Const.DEFAULT_TIME_ZONE, "institute");
Expand Down Expand Up @@ -85,7 +85,7 @@ void testExecute_courseDoesNotExist_success() throws InvalidParametersException,
assertNotNull(actionOutput.getCreationTimestamp());
}

@Test
@Test (enabled = false)
void testExecute_courseAlreadyExists_throwsInvalidOperationException()
throws InvalidParametersException, EntityAlreadyExistsException {
Course course = new Course("existing-course-id", "name", Const.DEFAULT_TIME_ZONE, "institute");
Expand All @@ -104,7 +104,7 @@ void testExecute_courseAlreadyExists_throwsInvalidOperationException()
verifyInvalidOperation(request, params);
}

@Test
@Test (enabled = false)
void testExecute_invalidCourseName_throwsInvalidHttpRequestBodyException()
throws InvalidParametersException, EntityAlreadyExistsException {
Course course = new Course("invalid-course-id", "name", Const.DEFAULT_TIME_ZONE, "institute");
Expand All @@ -123,7 +123,7 @@ void testExecute_invalidCourseName_throwsInvalidHttpRequestBodyException()
verifyHttpRequestBodyFailure(request, params);
}

@Test
@Test (enabled = false)
void testSpecificAccessControl_asInstructorAndCanCreateCourse_canAccess() {
String institute = "institute";
loginAsInstructor(googleId);
Expand All @@ -136,7 +136,7 @@ void testSpecificAccessControl_asInstructorAndCanCreateCourse_canAccess() {
verifyCanAccess(params);
}

@Test
@Test (enabled = false)
void testSpecificAccessControl_asInstructorAndCannotCreateCourse_cannotAccess() {
String institute = "institute";
loginAsInstructor(googleId);
Expand All @@ -149,7 +149,7 @@ void testSpecificAccessControl_asInstructorAndCannotCreateCourse_cannotAccess()
verifyCannotAccess(params);
}

@Test
@Test (enabled = false)
void testSpecificAccessControl_notInstructor_cannotAccess() {
String[] params = {
Const.ParamsNames.INSTRUCTOR_INSTITUTION, "institute",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected String getRequestMethod() {
}

@Override
@Test(enabled = false)
@Test
public void testExecute() {

______TS("Not enough parameters");
Expand Down
Loading