def setUp(self): super(ReviewTests, self).setUp() self.dummy_request = RequestFactory().get('/') self.exam_creation_params = { 'course_id': 'foo/bar/baz', 'content_id': 'content', 'exam_name': 'Sample Exam', 'time_limit_mins': 10, 'is_proctored': True, 'backend': 'test' } self.exam_id = create_exam(**self.exam_creation_params) self.attempt_id = create_exam_attempt( self.exam_id, self.user.id, taking_as_proctored=True ) self.attempt = get_exam_attempt_by_id(self.attempt_id) set_runtime_service('credit', MockCreditService()) set_runtime_service('instructor', MockInstructorService()) set_runtime_service('grades', MockGradesService()) set_runtime_service('certificates', MockCertificateService()) set_current_request(self.dummy_request)
def make_request(self, user): """ Creates a request """ request = RequestFactory().request() request.user = user self.addCleanup(crum.set_current_request, None) crum.set_current_request(request) return request
def setUp(self): super().setUp() self.user = UserFactory() self.request = RequestFactory().request() self.request.session = {} self.request.site = SiteFactory() self.request.user = self.user self.addCleanup(set_current_request, None) set_current_request(self.request) self.flag = ExperimentWaffleFlag('experiments', 'test', __name__, num_buckets=2, experiment_id=0) self.key = CourseKey.from_string('a/b/c') bucket_patch = patch( 'lms.djangoapps.experiments.flags.stable_bucketing_hash_group', return_value=1) self.addCleanup(bucket_patch.stop) bucket_patch.start() self.addCleanup(RequestCache.clear_all_namespaces)
def test_external_course_updates(self, bucket): """Confirm that we exclude enrollments in the external course updates experiment""" user = UserFactory() overview1 = CourseOverviewFactory( has_highlights=False ) # set has_highlights just to avoid a modulestore lookup overview2 = CourseOverviewFactory(has_highlights=False) # We need to enroll with a request, because our specific experiment code expects it self.addCleanup(crum.set_current_request, None) request = RequestFactory().get(self.site) request.user = user crum.set_current_request(request) enrollment1 = CourseEnrollment.enroll(user, overview1.id) with override_experiment_waffle_flag(_EXTERNAL_COURSE_UPDATES_FLAG, bucket=bucket): enrollment2 = CourseEnrollment.enroll(user, overview2.id) # OK, at this point, we'd expect course1 to be returned, but course2's enrollment to be excluded by the # experiment. Note that the experiment waffle is currently inactive, but they should still be excluded because # they were bucketed at enrollment time. bin_num = BinnedSchedulesBaseResolver.bin_num_for_user_id(user.id) resolver = BinnedSchedulesBaseResolver(None, self.site, datetime.datetime.now(pytz.UTC), 0, bin_num) resolver.schedule_date_field = 'created' schedules = resolver.get_schedules_with_target_date_by_bin_and_orgs() if bucket == 1: assert len(schedules) == 1 assert schedules[0].enrollment == enrollment1 else: assert len(schedules) == 2 assert {s.enrollment for s in schedules} == {enrollment1, enrollment2}
def setUp(self): super(VerifiedUpgradeToolTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments self.course_verified_mode = CourseModeFactory( course_id=self.course.id, mode_slug=CourseMode.VERIFIED, expiration_datetime=self.now + datetime.timedelta(days=30), ) patcher = patch( 'openedx.core.djangoapps.schedules.signals.get_current_site') mock_get_current_site = patcher.start() self.addCleanup(patcher.stop) mock_get_current_site.return_value = SiteFactory.create() DynamicUpgradeDeadlineConfiguration.objects.create(enabled=True) self.request = RequestFactory().request() crum.set_current_request(self.request) self.addCleanup(crum.set_current_request, None) self.enrollment = CourseEnrollmentFactory( course_id=self.course.id, mode=CourseMode.AUDIT, course=self.course_overview, ) self.request.user = self.enrollment.user
def setUp(self): super(VerifiedUpgradeToolTest, self).setUp() self.course_verified_mode = CourseModeFactory( course_id=self.course.id, mode_slug=CourseMode.VERIFIED, expiration_datetime=self.now + datetime.timedelta(days=30), ) patcher = patch('openedx.core.djangoapps.schedules.signals.get_current_site') mock_get_current_site = patcher.start() self.addCleanup(patcher.stop) mock_get_current_site.return_value = SiteFactory.create() DynamicUpgradeDeadlineConfiguration.objects.create(enabled=True) ScheduleConfigFactory.create( site=mock_get_current_site.return_value, create_schedules=True ) self.enrollment = CourseEnrollmentFactory( course_id=self.course.id, mode=CourseMode.AUDIT, course=self.course_overview, ) self.request = RequestFactory().request() self.request.user = self.enrollment.user crum.set_current_request(self.request)
def test_generate_course_expired_message(self, offsets): now = timezone.now() schedule_offset, course_offset = offsets # Set a timezone and request, to test that the message looks at the user's setting request = RequestFactory().get('/') request.user = UserFactory() set_current_request(request) self.addCleanup(set_current_request, None) set_user_preference(request.user, 'time_zone', 'Asia/Tokyo') if schedule_offset is not None: schedule_upgrade_deadline = now + timedelta(days=schedule_offset) else: schedule_upgrade_deadline = None if course_offset is not None: course_upgrade_deadline = now + timedelta(days=course_offset) else: course_upgrade_deadline = None enrollment = CourseEnrollmentFactory.create( course__start=datetime(2018, 1, 1, tzinfo=UTC), course__self_paced=True, ) CourseModeFactory.create( course_id=enrollment.course.id, mode_slug=CourseMode.VERIFIED, expiration_datetime=course_upgrade_deadline, ) CourseModeFactory.create( course_id=enrollment.course.id, mode_slug=CourseMode.AUDIT, ) ScheduleFactory.create( enrollment=enrollment, upgrade_deadline=schedule_upgrade_deadline, ) duration_limit_upgrade_deadline = get_user_course_expiration_date( enrollment.user, enrollment.course) self.assertIsNotNone(duration_limit_upgrade_deadline) message = generate_course_expired_message(enrollment.user, enrollment.course) self.assertDateInMessage(duration_limit_upgrade_deadline, message) self.assertIn('data-timezone="Asia/Tokyo"', message) soft_upgradeable = schedule_upgrade_deadline is not None and now < schedule_upgrade_deadline upgradeable = course_upgrade_deadline is None or now < course_upgrade_deadline has_upgrade_deadline = course_upgrade_deadline is not None if upgradeable and soft_upgradeable: self.assertDateInMessage(schedule_upgrade_deadline, message) elif upgradeable and has_upgrade_deadline: self.assertDateInMessage(course_upgrade_deadline, message) else: self.assertNotIn("Upgrade by", message)
def test_without_request_and_undefined_waffle(self): """ Test the flag behavior when outside a request context and waffle data undefined. """ crum.set_current_request(None) test_course_flag = CourseWaffleFlag(self.NAMESPACED_FLAG_NAME, __name__) assert test_course_flag.is_enabled(self.TEST_COURSE_KEY) is False
def test_calendar_sync_toggle_tool_is_enabled(self, user_type, should_be_enabled): request = RequestFactory().request() request.user = self.create_user_for_course(self.course, user_type) self.addCleanup(crum.set_current_request, None) crum.set_current_request(request) assert CalendarSyncToggleTool.is_enabled( request, self.course.id) == should_be_enabled
def set_request(self, with_user=True, user=None): """Configures a current request, as required by the experiment code""" request = RequestFactory() if with_user: request.user = user or self.user self.addCleanup(crum.set_current_request, None) crum.set_current_request(request)
def test_without_request_and_everyone_active_waffle(self): """ Test the flag behavior when outside a request context and waffle active for everyone. """ crum.set_current_request(None) test_course_flag = CourseWaffleFlag(self.NAMESPACED_FLAG_NAME, __name__) with override_waffle_flag(self.TEST_COURSE_FLAG, active=True): assert test_course_flag.is_enabled(self.TEST_COURSE_KEY) is True
def test_without_request(self, data): """ Test the flag behavior when outside a request context. """ crum.set_current_request(None) test_course_flag = CourseWaffleFlag( self.TEST_NAMESPACE, self.FLAG_NAME, flag_undefined_default=data['flag_undefined_default']) self.assertEqual(test_course_flag.is_enabled(self.TEST_COURSE_KEY), data['result'])
def setUp(self): super().setUp() flag_name = "test_namespace.test_flag" self.waffle_flag = WaffleFlag(flag_name, __name__) request = RequestFactory().request() crum.set_current_request(request) RequestCache.clear_all_namespaces() self.addCleanup(crum.set_current_request, None) self.addCleanup(RequestCache.clear_all_namespaces)
def test_without_request(self, data): """ Test the flag behavior when outside a request context. """ crum.set_current_request(None) test_course_flag = CourseWaffleFlag( self.TEST_NAMESPACE, self.FLAG_NAME, flag_undefined_default=data['flag_undefined_default'] ) self.assertEqual(test_course_flag.is_enabled(self.TEST_COURSE_KEY), data['result'])
def get_request_for_user(user): """Create a request object for user.""" request = RequestFactory() request.user = user request.COOKIES = {} request.META = {} request.is_secure = lambda: True request.get_host = lambda: "edx.org" request.method = 'GET' request.GET = {} request.POST = {} crum.set_current_request(request) return request
def get_mock_request(user=None): """ Create a request object for the user, if specified. """ request = RequestFactory().get('/') if user is not None: request.user = user else: request.user = AnonymousUser() request.is_secure = lambda: True request.get_host = lambda: "edx.org" crum.set_current_request(request) return request
def setUp(self): super().setUp() self.user = UserFactory() self.request = mock.Mock() self.request.user = self.user CourseEnrollmentFactory(course_id=self.course_key) UserCelebration.STREAK_LENGTHS_TO_CELEBRATE = [3] UserCelebration.STREAK_BREAK_LENGTH = 1 self.STREAK_LENGTH_TO_CELEBRATE = UserCelebration.STREAK_LENGTHS_TO_CELEBRATE[ 0] self.STREAK_BREAK_LENGTH = UserCelebration.STREAK_BREAK_LENGTH set_current_request(self.request) self.addCleanup(set_current_request, None)
def setUp(self): super().setUp() flag_name = "test_namespace.test_flag" self.waffle_flag = WaffleFlag( # lint-amnesty, pylint: disable=toggle-missing-annotation flag_name, __name__ ) request = RequestFactory().request() crum.set_current_request(request) RequestCache.clear_all_namespaces() self.addCleanup(crum.set_current_request, None) self.addCleanup(RequestCache.clear_all_namespaces)
def get_mock_request(user=None): """ Create a request object for the user, if specified. """ # Import is placed here to avoid model import at project startup. from django.contrib.auth.models import AnonymousUser request = RequestFactory().get('/') if user is not None: request.user = user else: request.user = AnonymousUser() request.is_secure = lambda: True request.get_host = lambda: "edx.org" crum.set_current_request(request) return request
def setUp(self): super(VerifiedUpgradeToolTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments self.course_verified_mode = CourseModeFactory( course_id=self.course.id, mode_slug=CourseMode.VERIFIED, expiration_datetime=self.now + datetime.timedelta(days=30), ) DynamicUpgradeDeadlineConfiguration.objects.create(enabled=True) self.request = RequestFactory().request() crum.set_current_request(self.request) self.addCleanup(crum.set_current_request, None) self.enrollment = CourseEnrollmentFactory( course_id=self.course.id, mode=CourseMode.AUDIT, course=self.course_overview, ) self.request.user = self.enrollment.user
def setUp(self): super(ReviewTests, self).setUp() self.dummy_request = RequestFactory().get('/') self.exam_id = create_exam(course_id='foo/bar/baz', content_id='content', exam_name='Sample Exam', time_limit_mins=10, is_proctored=True, backend='test') self.attempt_id = create_exam_attempt(self.exam_id, self.user.id, taking_as_proctored=True) self.attempt = get_exam_attempt_by_id(self.attempt_id) set_runtime_service('credit', MockCreditService()) set_runtime_service('instructor', MockInstructorService()) set_runtime_service('grades', MockGradesService()) set_runtime_service('certificates', MockCertificateService()) set_current_request(self.dummy_request)
def setUp(self): super(FinancialAssistanceToolTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments self.course_financial_mode = CourseModeFactory( course_id=self.course.id, mode_slug=CourseMode.VERIFIED, expiration_datetime=self.now + datetime.timedelta(days=1), ) DynamicUpgradeDeadlineConfiguration.objects.create(enabled=True) self.request = RequestFactory().request() crum.set_current_request(self.request) self.addCleanup(crum.set_current_request, None) # baseline course enrollment, future upgrade deadline self.enrollment = CourseEnrollmentFactory( course_id=self.course.id, mode=CourseMode.AUDIT, course=self.course_overview, ) self.request.user = self.enrollment.user # enrollment where learner has upgraded self.enrollment_upgraded = CourseEnrollmentFactory( course_id=self.course.id, mode=CourseMode.VERIFIED, course=self.course_overview, ) # course enrollment for mock: upgrade deadline in the past self.enrollment_deadline_past = self.enrollment self.enrollment_deadline_past.course_upgrade_deadline = self.now - datetime.timedelta( days=1) self.enrollment_deadline_past.save() # course enrollment for mock: no upgrade deadline self.enrollment_deadline_missing = self.enrollment self.enrollment_deadline_missing.course_upgrade_deadline = None self.enrollment_deadline_missing.save()
def setUp(self): super(SiteMixin, self).setUp() # Set the domain used for all test requests domain = 'testserver.fake' self.client = self.client_class(SERVER_NAME=domain) Course.objects.all().delete() Partner.objects.all().delete() Site.objects.all().delete() lms_url_root = "http://lms.testserver.fake" self.site_configuration = SiteConfigurationFactory( lms_url_root=lms_url_root, from_email='*****@*****.**', oauth_settings={ 'SOCIAL_AUTH_EDX_OAUTH2_KEY': 'key', 'SOCIAL_AUTH_EDX_OAUTH2_SECRET': 'secret', 'BACKEND_SERVICE_EDX_OAUTH2_KEY': 'key', 'BACKEND_SERVICE_EDX_OAUTH2_SECRET': 'secret', 'SOCIAL_AUTH_EDX_OAUTH2_LOGOUT_URL': lms_url_root + '/logout', }, partner__name='edX', partner__short_code='edx', segment_key='fake_segment_key', site__domain=domain, site__id=settings.SITE_ID, base_cookie_domain=domain, ) self.partner = self.site_configuration.partner self.partner.default_site = self.site = self.site_configuration.site self.partner.save() self.request = RequestFactory(SERVER_NAME=domain).get('') self.request.session = None self.request.site = self.site set_thread_variable('request', self.request) set_current_request(self.request) self.addCleanup(set_current_request)
def setUp(self): super().setUp() self.request = RequestFactory().request() self.request.session = {} self.request.site = SiteFactory() self.request.user = self.user self.addCleanup(set_current_request, None) set_current_request(self.request) self.addCleanup(RequestCache.clear_all_namespaces) # Enable all experiment waffle flags. experiment_waffle_flag_patcher = patch.object(ExperimentWaffleFlag, 'is_experiment_on', return_value=True) experiment_waffle_flag_patcher.start() self.addCleanup(experiment_waffle_flag_patcher.stop) # Use our custom fake `stable_bucketing_hash_group` implementation. stable_bucket_patcher = patch( 'lms.djangoapps.experiments.flags.stable_bucketing_hash_group', self._mock_stable_bucket) stable_bucket_patcher.start() self.addCleanup(stable_bucket_patcher.stop)
def setUp(self): super(TestCourseWaffleFlag, self).setUp() request = RequestFactory().request() crum.set_current_request(request) RequestCache.clear_request_cache()
def setUp(self): super().setUp() request = RequestFactory().request() self.addCleanup(crum.set_current_request, None) crum.set_current_request(request) RequestCache.clear_all_namespaces()
def tearDownClass(cls): super(TestStudentModuleGrading, cls).tearDownClass() set_current_request(None)
def wrapper(request, *args, **kwargs): if not crum.get_current_request(): crum.set_current_request(request=request) return func(request, *args, **kwargs)
def tearDownClass(cls): super(TestScoreForModule, cls).tearDownClass() set_current_request(None)
def test_no_request(self): set_current_request(None) assert self.get_bucket() == 0
def set_mobile_request(self): request = RequestFactory().request() request.META['HTTP_USER_AGENT'] = 'edX/org.edx.mobile' self.addCleanup(set_current_request, None) set_current_request(request)
def setUp(self): super(OverrideWaffleFlagTests, self).setUp() request = RequestFactory().request() self.addCleanup(crum.set_current_request, None) crum.set_current_request(request) RequestCache.clear_request_cache()
def test_no_request(self): set_current_request(None) self.assertEqual(self.get_bucket(), 0)
def setUp(self): super(TestCourseWaffleFlag, self).setUp() request = RequestFactory().request() self.addCleanup(crum.set_current_request, None) crum.set_current_request(request) RequestCache.clear_all_namespaces()