def _can_access_descriptor_with_start_date(user, descriptor, course_key): # pylint: disable=invalid-name """ Checks if a user has access to a descriptor based on its start date. If there is no start date specified, grant access. Else, check if we're past the start date. Note: We do NOT check whether the user is staff or if the descriptor is detached... it is assumed both of these are checked by the caller. Arguments: user (User): the user whose descriptor access we are checking. descriptor (AType): the descriptor for which we are checking access, where AType is CourseDescriptor, CourseOverview, or any other class that represents a descriptor and has the attributes .location, .id, .start, and .days_early_for_beta. Returns: AccessResponse: The result of this access check. Possible results are ACCESS_GRANTED or a StartDateError. """ start_dates_disabled = settings.FEATURES['DISABLE_START_DATES'] if start_dates_disabled and not is_masquerading_as_student( user, course_key): return ACCESS_GRANTED else: now = datetime.now(UTC()) effective_start = _adjust_start_date_for_beta_testers( user, descriptor, course_key=course_key) if (descriptor.start is None or now > effective_start or in_preview_mode()): return ACCESS_GRANTED return StartDateError(descriptor.start)
def test_courseware_access(self): 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, [unicode(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() for course_index in range(len(courses)): result = response.data[course_index]['course']['courseware_access'] self.assertEqual(result['error_code'], expected_error_codes[::-1][course_index]) if result['error_code'] is not None: self.assertFalse(result['has_access'])
def check_start_date(user, days_early_for_beta, start, course_key, display_error_to_user=True): """ Verifies whether the given user is allowed access given the start date and the Beta offset for the given course. Arguments: display_error_to_user: If True, display this error to users in the UI. Returns: AccessResponse: Either ACCESS_GRANTED or StartDateError. """ start_dates_disabled = settings.FEATURES['DISABLE_START_DATES'] masquerading_as_student = is_masquerading_as_student(user, course_key) if start_dates_disabled and not masquerading_as_student: return ACCESS_GRANTED else: now = datetime.now(UTC) if start is None or in_preview_mode() or get_course_masquerade( user, course_key): return ACCESS_GRANTED effective_start = adjust_start_date(user, days_early_for_beta, start, course_key) if now > effective_start: return ACCESS_GRANTED return StartDateError(start, display_error_to_user=display_error_to_user)
def test_unreleased_course(self): # ensure the course always starts in the future self.course = CourseFactory.create( mobile_available=True, static_asset_path="needed_for_split") self.course.start = timezone.now() + datetime.timedelta(days=365) self.init_course_access() self._verify_response(self.ALLOW_ACCESS_TO_UNRELEASED_COURSE, StartDateError(self.course.start))
def check_start_date(user, days_early_for_beta, start, course_key): """ Verifies whether the given user is allowed access given the start date and the Beta offset for the given course. Returns: AccessResponse: Either ACCESS_GRANTED or StartDateError. """ start_dates_disabled = settings.FEATURES['DISABLE_START_DATES'] if start_dates_disabled and not is_masquerading_as_student(user, course_key): return ACCESS_GRANTED else: now = datetime.now(UTC()) effective_start = adjust_start_date(user, days_early_for_beta, start, course_key) if start is None or now > effective_start or in_preview_mode(): return ACCESS_GRANTED return StartDateError(start)
def check_start_date(user, days_early_for_beta, start, course_key): """ Verifies whether the given user is allowed access given the start date and the Beta offset for the given course. Returns: AccessResponse: Either ACCESS_GRANTED or StartDateError. """ start_dates_disabled = settings.FEATURES['DISABLE_START_DATES'] masquerading_as_student = is_masquerading_as_student(user, course_key) masquerading_as_specific_student = is_masquerading_as_specific_student( user, course_key) if start_dates_disabled and not masquerading_as_student: return ACCESS_GRANTED else: now = datetime.now(UTC) if start is None or in_preview_mode(): return ACCESS_GRANTED effective_start = adjust_start_date(user, days_early_for_beta, start, course_key) if now > effective_start: return ACCESS_GRANTED if get_course_masquerade(user, course_key): if masquerading_as_student or ( masquerading_as_specific_student and not has_staff_roles(user, course_key)): request = get_current_request() PageLevelMessages.register_warning_message( request, HTML( _('This user does not have access to this content due to the content start date' )), once_only=True) return ACCESS_GRANTED return StartDateError(start)
def test_unreleased_course(self): self.init_course_access() self._verify_response(self.ALLOW_ACCESS_TO_UNRELEASED_COURSE, StartDateError(self.course.start))