diff --git a/src/e2e/java/teammates/e2e/cases/InstructorCoursesPageE2ETest.java b/src/e2e/java/teammates/e2e/cases/InstructorCoursesPageE2ETest.java index cde0bc98f21..2f95cb043ea 100644 --- a/src/e2e/java/teammates/e2e/cases/InstructorCoursesPageE2ETest.java +++ b/src/e2e/java/teammates/e2e/cases/InstructorCoursesPageE2ETest.java @@ -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"); @@ -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); diff --git a/src/e2e/resources/data/InstructorCoursesPageE2ETest.json b/src/e2e/resources/data/InstructorCoursesPageE2ETest.json index 518af46126c..9d050c8d99e 100644 --- a/src/e2e/resources/data/InstructorCoursesPageE2ETest.json +++ b/src/e2e/resources/data/InstructorCoursesPageE2ETest.json @@ -1,12 +1,4 @@ { - "accounts": { - "instructor": { - "googleId": "tm.e2e.ICs.instructor", - "name": "Teammates Demo Instr", - "email": "ICs.instructor@gmail.tmt", - "readNotifications": {} - } - }, "courses": { "CS1101": { "createdAt": "2012-04-02T12:00:00Z", diff --git a/src/e2e/resources/data/InstructorCoursesPageE2ETest_SqlEntities.json b/src/e2e/resources/data/InstructorCoursesPageE2ETest_SqlEntities.json new file mode 100644 index 00000000000..a4fc25d8e82 --- /dev/null +++ b/src/e2e/resources/data/InstructorCoursesPageE2ETest_SqlEntities.json @@ -0,0 +1,10 @@ +{ + "accounts": { + "instructor": { + "id": "00000000-0000-4000-8000-000000000001", + "googleId": "tm.e2e.ICs.instructor", + "name": "Teammates Demo Instr", + "email": "ICs.instructor@gmail.tmt" + } + } +} diff --git a/src/main/java/teammates/ui/webapi/CreateCourseAction.java b/src/main/java/teammates/ui/webapi/CreateCourseAction.java index 8214dfd3f95..8e6af9fab83 100644 --- a/src/main/java/teammates/ui/webapi/CreateCourseAction.java +++ b/src/main/java/teammates/ui/webapi/CreateCourseAction.java @@ -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; @@ -29,8 +31,13 @@ void checkSpecificAccessControl() throws UnauthorizedAccessException { String institute = getNonNullRequestParamValue(Const.ParamsNames.INSTRUCTOR_INSTITUTION); - boolean canCreateCourse = sqlLogic.canInstructorCreateCourse(userInfo.getId(), institute); - + List 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); @@ -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))); } } diff --git a/src/test/java/teammates/sqlui/webapi/CreateCourseActionTest.java b/src/test/java/teammates/sqlui/webapi/CreateCourseActionTest.java index d0abcaa9202..619522cf6d0 100644 --- a/src/test/java/teammates/sqlui/webapi/CreateCourseActionTest.java +++ b/src/test/java/teammates/sqlui/webapi/CreateCourseActionTest.java @@ -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"); @@ -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"); @@ -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"); @@ -123,7 +123,7 @@ void testExecute_invalidCourseName_throwsInvalidHttpRequestBodyException() verifyHttpRequestBodyFailure(request, params); } - @Test + @Test (enabled = false) void testSpecificAccessControl_asInstructorAndCanCreateCourse_canAccess() { String institute = "institute"; loginAsInstructor(googleId); @@ -136,7 +136,7 @@ void testSpecificAccessControl_asInstructorAndCanCreateCourse_canAccess() { verifyCanAccess(params); } - @Test + @Test (enabled = false) void testSpecificAccessControl_asInstructorAndCannotCreateCourse_cannotAccess() { String institute = "institute"; loginAsInstructor(googleId); @@ -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", diff --git a/src/test/java/teammates/ui/webapi/CreateCourseActionTest.java b/src/test/java/teammates/ui/webapi/CreateCourseActionTest.java index 2b39599c940..143fb2eb186 100644 --- a/src/test/java/teammates/ui/webapi/CreateCourseActionTest.java +++ b/src/test/java/teammates/ui/webapi/CreateCourseActionTest.java @@ -27,7 +27,7 @@ protected String getRequestMethod() { } @Override - @Test(enabled = false) + @Test public void testExecute() { ______TS("Not enough parameters");