def test_courseware_access(self, api_version): self.login() course_with_prereq = CourseFactory.create(start=self.LAST_WEEK, mobile_available=True) prerequisite_course = CourseFactory.create() set_prerequisite_courses(course_with_prereq.id, [str(prerequisite_course.id)]) # Create list of courses with various expected courseware_access responses and corresponding expected codes courses = [ course_with_prereq, CourseFactory.create(start=self.NEXT_WEEK, mobile_available=True), CourseFactory.create(visible_to_staff_only=True, mobile_available=True), CourseFactory.create(start=self.LAST_WEEK, mobile_available=True, visible_to_staff_only=False), ] expected_error_codes = [ MilestoneAccessError().error_code, # 'unfulfilled_milestones' StartDateError(self.NEXT_WEEK).error_code, # 'course_not_started' VisibilityError().error_code, # 'not_visible_to_user' None, ] # Enroll in all the courses for course in courses: self.enroll(course.id) # Verify courses have the correct response through error code. Last enrolled course is first course in response response = self.api_response(api_version=api_version) for course_index in range(len(courses)): result = response.data[course_index]['course']['courseware_access'] assert result['error_code'] == expected_error_codes[::(- 1)][course_index] if result['error_code'] is not None: assert not result['has_access']
def test_visible_to_staff_only_course(self, role, should_succeed): self.init_course_access() self.course.visible_to_staff_only = True self.store.update_item(self.course, self.user.id) if self.ALLOW_ACCESS_TO_NON_VISIBLE_COURSE: should_succeed = True self._verify_response(should_succeed, VisibilityError(), role)
def _visible_to_nonstaff_users(descriptor, display_error_to_user=True): """ Returns if the object is visible to nonstaff users. Arguments: descriptor: object to check display_error_to_user: If True, show an error message to the user say the content was hidden. Otherwise, hide the content silently. """ if descriptor.visible_to_staff_only: return VisibilityError(display_error_to_user=display_error_to_user) else: return ACCESS_GRANTED