def test_update_exam_authorization_order(self, order_status): """ Verify that update_exam_authorization_final_grade is called when a fulfilled Order saves """ with mute_signals(post_save): # muted because enrollment also trigger signal for profile creation. right now we are just # looking final grades CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) FinalGradeFactory.create( user=self.profile.user, course_run=self.course_run, passed=True, ) order = OrderFactory.create(user=self.profile.user, fulfilled=False) LineFactory.create(course_key=self.course_run.edx_course_key, order=order) # There is no ExamProfile or ExamAuthorization before creating the FinalGrade. assert ExamProfile.objects.filter(profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course ).exists() is False order.status = order_status order.save() # assert Exam Authorization and profile created. assert ExamProfile.objects.filter(profile=self.profile).exists() is True assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course ).exists() is True
def test_update_exam_authorization_final_grade_when_user_not_paid(self): """ Verify that update_exam_authorization_final_grade is called and log exception when FinalGrade saves user dont match exam authorization criteria """ with mute_signals(post_save): # muting signal for CachedEnrollment. Because CachedEnrollment and FinalGrade both omits # signal, we want to see behaviour of FinalGrade here CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) assert ExamProfile.objects.filter(profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course ).exists() is False FinalGradeFactory.create( user=self.profile.user, course_run=self.course_run, passed=True, course_run_paid_on_edx=False, ) # assert Exam Authorization and profile not created. assert ExamProfile.objects.filter(profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course ).exists() is False
def test_program_enrolled_user_serializer(self): # pylint: disable=no-self-use """ Asserts the output of the serializer for program-enrolled users (ProgramEnrollments) """ with mute_signals(post_save): profile = ProfileFactory.create() EducationFactory.create(profile=profile) EmploymentFactory.create(profile=profile) program = ProgramFactory.create() course = CourseFactory.create(program=program) course_runs = [ CourseRunFactory.create(course=course) for _ in range(2) ] for course_run in course_runs: CachedCertificateFactory.create(user=profile.user, course_run=course_run) CachedEnrollmentFactory.create(user=profile.user, course_run=course_run) program_enrollment = ProgramEnrollment.objects.create( user=profile.user, program=program) assert serialize_program_enrolled_user(program_enrollment) == { '_id': program_enrollment.id, 'id': program_enrollment.id, 'user_id': profile.user.id, 'email': profile.user.email, 'profile': ProfileSerializer().to_representation(profile), 'program': UserProgramSerializer.serialize(program_enrollment) }
def test_get_users_without_frozen_final_grade(self): """ tests for get_users_without_frozen_final_grade function """ assert [user.pk for user in api.get_users_without_frozen_final_grade(self.run_fa)] == [self.user.pk] # create another user and enrollment other_user = UserFactory.create() CachedEnrollmentFactory.create(user=other_user, course_run=self.run_fa) assert sorted( [user.pk for user in api.get_users_without_frozen_final_grade(self.run_fa)] ) == sorted([self.user.pk]) CachedCurrentGradeFactory.create(user=other_user, course_run=self.run_fa) assert sorted( [user.pk for user in api.get_users_without_frozen_final_grade(self.run_fa)] ) == sorted([self.user.pk, other_user.pk]) # add the user to the FinalGrade model as in progress fg_status = FinalGrade.objects.create( user=other_user, course_run=self.run_fa, status=FinalGradeStatus.PENDING, grade=0.0) assert sorted( [user.pk for user in api.get_users_without_frozen_final_grade(self.run_fa)] ) == sorted([self.user.pk, other_user.pk]) # change the final grade status to complete fg_status.status = FinalGradeStatus.COMPLETE fg_status.save() assert [user.pk for user in api.get_users_without_frozen_final_grade(self.run_fa)] == [self.user.pk]
def test_learner_view_needs_paid_learner(self, mock_mailgun_client): """ Test that a learner attempting to email another learner will only succeed if the sender has paid for a course run in a program that the recipient is enrolled in """ mock_mailgun_client.send_individual_email.return_value = Mock( spec=Response, status_code=status.HTTP_200_OK, json=mocked_json()) with mute_signals(post_save): learner_profile = ProfileFactory.create( user__email='*****@*****.**', email_optin=True, ) learner_user = learner_profile.user ProgramEnrollmentFactory.create(user=learner_user, program=self.program) CachedEnrollment.objects.filter(user=learner_user).delete() self.client.force_login(learner_user) url = reverse( self.url_name, kwargs={'student_id': self.recipient_user.profile.student_id}) resp_post = self.client.post(url, data=self.request_data, format='json') assert resp_post.status_code == status.HTTP_403_FORBIDDEN CachedEnrollmentFactory.create( user=learner_user, course_run__course__program=self.program, verified=True) resp_post = self.client.post(url, data=self.request_data, format='json') assert resp_post.status_code == status.HTTP_200_OK
def test_learner_view_needs_paid_learner(self, mock_mailgun_client): """ Test that a learner attempting to email another learner will only succeed if the sender has paid for a course run in a program that the recipient is enrolled in """ mock_mailgun_client.send_individual_email.return_value = Mock( spec=Response, status_code=status.HTTP_200_OK, json=mocked_json() ) with mute_signals(post_save): learner_profile = ProfileFactory.create( user__email='*****@*****.**', email_optin=True, ) learner_user = learner_profile.user ProgramEnrollmentFactory.create(user=learner_user, program=self.program) CachedEnrollment.objects.filter(user=learner_user).delete() self.client.force_login(learner_user) url = reverse(self.url_name, kwargs={'student_id': self.recipient_user.profile.student_id}) resp_post = self.client.post(url, data=self.request_data, format='json') assert resp_post.status_code == status.HTTP_403_FORBIDDEN CachedEnrollmentFactory.create(user=learner_user, course_run__course__program=self.program, verified=True) resp_post = self.client.post(url, data=self.request_data, format='json') assert resp_post.status_code == status.HTTP_200_OK
def test_update_exam_authorization_final_grade(self): """ Verify that update_exam_authorization_final_grade is called when a FinalGrade saves """ create_order(self.profile.user, self.course_run) with mute_signals(post_save): # muted because enrollment also trigger signal for profile creation. right now we are just # looking final grades CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) # There is no ExamProfile or ExamAuthorization before creating the FinalGrade. assert ExamProfile.objects.filter( profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course).exists() is False FinalGradeFactory.create( user=self.profile.user, course_run=self.course_run, passed=True, ) # assert Exam Authorization and profile created. assert ExamProfile.objects.filter( profile=self.profile).exists() is True assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course).exists() is True
def test_update_exam_authorization_final_grade(self): """ Verify that update_exam_authorization_final_grade is called when a FinalGrade saves """ create_order(self.profile.user, self.course_run) with mute_signals(post_save): # muted because enrollment also trigger signal for profile creation. right now we are just # looking final grades CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) # There is no ExamProfile or ExamAuthorization before creating the FinalGrade. assert ExamProfile.objects.filter(profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course ).exists() is False FinalGradeFactory.create( user=self.profile.user, course_run=self.course_run, passed=True, ) # assert Exam Authorization and profile created. assert ExamProfile.objects.filter(profile=self.profile).exists() is True assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course ).exists() is True
def test_update_exam_authorization_final_grade_when_user_not_paid(self): """ Verify that update_exam_authorization_final_grade is called and log exception when FinalGrade saves user dont match exam authorization criteria """ with mute_signals(post_save): # muting signal for CachedEnrollment. Because CachedEnrollment and FinalGrade both omits # signal, we want to see behaviour of FinalGrade here CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) assert ExamProfile.objects.filter( profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course).exists() is False FinalGradeFactory.create( user=self.profile.user, course_run=self.course_run, passed=True, course_run_paid_on_edx=False, ) # assert Exam Authorization and profile not created. assert ExamProfile.objects.filter( profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course).exists() is False
def test_update_exam_authorization_cached_enrollment_user_not_paid(self): """ Test no exam profile created when user enrolled in the course but not paid for it. """ # exam profile before enrollment assert ExamProfile.objects.filter(profile=self.profile).exists() is False CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) assert ExamProfile.objects.filter(profile=self.profile).exists() is False
def test_update_exam_authorization_cached_enrollment(self): """ Test exam profile creation when user enroll in course. """ create_order(self.profile.user, self.course_run) # There is no ExamProfile before enrollment. assert ExamProfile.objects.filter(profile=self.profile).exists() is False CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) assert ExamProfile.objects.filter(profile=self.profile).exists() is True
def test_prev_course(self): """ A coupon for a previously purchased course should be redeemable if it applies to the course which is being purchased """ coupon = CouponFactory.create( coupon_type=Coupon.DISCOUNTED_PREVIOUS_COURSE, content_object=self.run1.course, ) CachedEnrollmentFactory.create(user=self.user, course_run=self.run1) assert is_coupon_redeemable(coupon, self.user) is True
def test_update_exam_authorization_cached_enrollment_user_not_paid(self): """ Test no exam profile created when user enrolled in the course but not paid for it. """ # exam profile before enrollment assert ExamProfile.objects.filter( profile=self.profile).exists() is False CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) assert ExamProfile.objects.filter( profile=self.profile).exists() is False
def test_update_exam_authorization_cached_enrollment(self): """ Test exam profile creation when user enroll in course. """ create_order(self.profile.user, self.course_run) # There is no ExamProfile before enrollment. assert ExamProfile.objects.filter( profile=self.profile).exists() is False CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) assert ExamProfile.objects.filter( profile=self.profile).exists() is True
def test_get_cached_edx_data(self): """ Test for get_cached_edx_data """ with self.assertRaises(ValueError): CachedEdxDataApi.get_cached_edx_data(self.user, 'footype') self.assert_cache_in_db() for run in self.all_runs: CachedEnrollmentFactory.create(user=self.user, course_run=run) CachedCertificateFactory.create(user=self.user, course_run=run) CachedCurrentGradeFactory.create(user=self.user, course_run=run) self.assert_cache_in_db(self.all_course_run_ids, self.all_course_run_ids, self.all_course_run_ids)
def setUpTestData(cls): with mute_signals(post_save): cls.profile = ProfileFactory.create() EducationFactory.create(profile=cls.profile) EmploymentFactory.create(profile=cls.profile) EmploymentFactory.create(profile=cls.profile, end_date=None) program = ProgramFactory.create() course = CourseFactory.create(program=program) course_runs = [CourseRunFactory.create(course=course) for _ in range(2)] for course_run in course_runs: CachedCertificateFactory.create(user=cls.profile.user, course_run=course_run) CachedEnrollmentFactory.create(user=cls.profile.user, course_run=course_run) cls.program_enrollment = ProgramEnrollment.objects.create(user=cls.profile.user, program=program)
def setUpTestData(cls): cls.program, _ = create_program(past=True) cls.course_run = cls.program.course_set.first().courserun_set.first() cls.program_enrollment = ProgramEnrollmentFactory.create(program=cls.program) cls.user = cls.program_enrollment.user create_order(cls.user, cls.course_run) with mute_signals(post_save): CachedEnrollmentFactory.create(user=cls.user, course_run=cls.course_run) cls.final_grade = FinalGradeFactory.create( user=cls.user, course_run=cls.course_run, passed=True, course_run_paid_on_edx=False, )
def test_verified_enroll_factory_fa_create(self): """ Tests that CachedEnrollmentFactory creates additional data for a FA-enabled course run """ assert Line.objects.count() == 0 with mute_signals(post_save): user = UserFactory.create() ProfileFactory.create(user=user) fa_program = FullProgramFactory.create(financial_aid_availability=True) CachedEnrollmentFactory.create(user=user, course_run__course__program=fa_program, verified=True) lines = Line.objects.all() assert len(lines) == 1 assert lines[0].order.status == Order.FULFILLED
def _generate_cached_enrollments(user, program, num_course_runs=1, data=None): """ Helper method to generate CachedEnrollments for test cases """ fake = faker.Factory.create() course = CourseFactory.create(program=program) course_run_params = dict(before_now=True, after_now=False, tzinfo=pytz.utc) course_runs = [ CourseRunFactory.create( course=course, enrollment_start=fake.date_time_this_month( **course_run_params), start_date=fake.date_time_this_month(**course_run_params), enrollment_end=fake.date_time_this_month(**course_run_params), end_date=fake.date_time_this_year(**course_run_params), ) for _ in range(num_course_runs) ] factory_kwargs = dict(user=user) if data is not None: factory_kwargs['data'] = data return [ CachedEnrollmentFactory.create(course_run=course_run, **factory_kwargs) for course_run in course_runs ]
def setUpTestData(cls): cls.user = UserFactory.create() # Create Programs, Courses, CourseRuns... cls.p1_course_run_keys = ['p1_course_run'] cls.p2_course_run_keys = ['p2_course_run_1', 'p2_course_run_2'] cls.p1_course_run = CourseRunFactory.create( edx_course_key=cls.p1_course_run_keys[0]) p2 = FullProgramFactory.create() first_course = p2.course_set.first() extra_course = CourseFactory.create(program=p2) cls.p2_course_run_1 = CourseRunFactory.create( course=first_course, edx_course_key=cls.p2_course_run_keys[0]) cls.p2_course_run_2 = CourseRunFactory.create( course=extra_course, edx_course_key=cls.p2_course_run_keys[1]) all_course_runs = [ cls.p1_course_run, cls.p2_course_run_1, cls.p2_course_run_2 ] # Create cached edX data cls.enrollments = [ CachedEnrollmentFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs ] cls.certificates = [ CachedCertificateFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs ] cls.current_grades = [ CachedCurrentGradeFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs ]
def setUpTestData(cls): cls.user = SocialUserFactory.create() cls.run_fa = CourseRunFactory.create( freeze_grade_date=now_in_utc() - timedelta(days=1), course__program__financial_aid_availability=True, ) cls.run_fa_with_cert = CourseRunFactory.create( freeze_grade_date=None, course__program=cls.run_fa.course.program, ) cls.run_no_fa = CourseRunFactory.create( freeze_grade_date=now_in_utc() + timedelta(days=1), course__program__financial_aid_availability=False, ) cls.run_no_fa_with_cert = CourseRunFactory.create( course__program=cls.run_no_fa.course.program, ) all_course_runs = ( cls.run_fa, cls.run_fa_with_cert, cls.run_no_fa, cls.run_no_fa_with_cert, ) for run in all_course_runs: if run.course.program.financial_aid_availability: FinancialAidFactory.create( user=cls.user, tier_program=TierProgramFactory.create( program=run.course.program, income_threshold=0, current=True), status=FinancialAidStatus.RESET, ) cls.enrollments = { course_run.edx_course_key: CachedEnrollmentFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs } cls.current_grades = { course_run.edx_course_key: CachedCurrentGradeFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs } cls.certificates = { course_run.edx_course_key: CachedCertificateFactory.create(user=cls.user, course_run=course_run) for course_run in (cls.run_fa_with_cert, cls.run_no_fa_with_cert) } cls.user_edx_data = CachedEdxUserData(cls.user)
def setUpTestData(cls): cls.users = [UserFactory.create() for _ in range(35)] freeze_date = now_in_utc()-timedelta(days=1) future_freeze_date = now_in_utc()+timedelta(days=1) cls.course_run1 = CourseRunFactory.create(freeze_grade_date=freeze_date) cls.course_run2 = CourseRunFactory.create(freeze_grade_date=freeze_date) cls.all_freezable_runs = [cls.course_run1, cls.course_run2] cls.course_run_future = CourseRunFactory.create(freeze_grade_date=future_freeze_date) cls.course_run_frozen = CourseRunFactory.create(freeze_grade_date=freeze_date) CourseRunGradingStatus.objects.create(course_run=cls.course_run_frozen, status=FinalGradeStatus.COMPLETE) for user in cls.users: CachedEnrollmentFactory.create(user=user, course_run=cls.course_run1) CachedCurrentGradeFactory.create(user=user, course_run=cls.course_run1)
def setUpTestData(cls): super().setUpTestData() with mute_signals(post_save): staff_profile = ProfileFactory.create() cls.staff_user = staff_profile.user cls.course = CourseFactory.create( contact_email='*****@*****.**', program__financial_aid_availability=False ) course_run = CourseRunFactory.create(course=cls.course) ProgramEnrollmentFactory.create(user=cls.staff_user, program=cls.course.program) CachedEnrollmentFactory.create(user=cls.staff_user, course_run=course_run) cls.url_name = 'course_team_mail_api' cls.request_data = { 'email_subject': 'email subject', 'email_body': 'email body' }
def test_update_exam_authorization_cached_enrollment_when_no_exam_run(self): """ Test no exam profile created when course has no ExamRun """ self.exam_run.delete() course = CourseFactory.create(program=self.program) course_run = CourseRunFactory.create( end_date=now_in_utc() - timedelta(days=100), enrollment_end=now_in_utc() + timedelta(hours=1), course=course ) create_order(self.profile.user, course_run) # exam profile before enrollment. assert ExamProfile.objects.filter(profile=self.profile).exists() is False CachedEnrollmentFactory.create(user=self.profile.user, course_run=course_run) assert ExamProfile.objects.filter(profile=self.profile).exists() is False
def setUpTestData(cls): with mute_signals(post_save): profile = ProfileFactory.create() cls.program, _ = create_program(past=True) cls.user = profile.user cls.course_run = cls.program.course_set.first().courserun_set.first() with mute_signals(post_save): CachedEnrollmentFactory.create(user=cls.user, course_run=cls.course_run) cls.exam_run = ExamRunFactory.create(course=cls.course_run.course) with mute_signals(post_save): cls.final_grade = FinalGradeFactory.create( user=cls.user, course_run=cls.course_run, passed=True, course_run_paid_on_edx=False, )
def setUpTestData(cls): with mute_signals(post_save): cls.profile = ProfileFactory.create() EducationFactory.create(profile=cls.profile) EmploymentFactory.create(profile=cls.profile) EmploymentFactory.create(profile=cls.profile, end_date=None) program = ProgramFactory.create() course = CourseFactory.create(program=program) course_runs = [ CourseRunFactory.create(course=course) for _ in range(2) ] for course_run in course_runs: CachedCertificateFactory.create(user=cls.profile.user, course_run=course_run) CachedEnrollmentFactory.create(user=cls.profile.user, course_run=course_run) cls.program_enrollment = ProgramEnrollment.objects.create( user=cls.profile.user, program=program)
def setUpTestData(cls): super().setUpTestData() with mute_signals(post_save): staff_profile = ProfileFactory.create() cls.staff_user = staff_profile.user cls.course = CourseFactory.create( contact_email='*****@*****.**', program__financial_aid_availability=False) course_run = CourseRunFactory.create(course=cls.course) ProgramEnrollmentFactory.create(user=cls.staff_user, program=cls.course.program) CachedEnrollmentFactory.create(user=cls.staff_user, course_run=course_run) cls.url_name = 'course_team_mail_api' cls.request_data = { 'email_subject': 'email subject', 'email_body': 'email body' }
def _generate_cached_enrollments(user, program, num_course_runs=1, data=None): """ Helper method to generate CachedEnrollments for test cases """ course = CourseFactory.create(program=program) course_runs = [CourseRunFactory.create(course=course) for _ in range(num_course_runs)] factory_kwargs = dict(user=user) if data is not None: factory_kwargs['data'] = data return [CachedEnrollmentFactory.create(course_run=course_run, **factory_kwargs) for course_run in course_runs]
def test_semester_serialization(self): """ Tests that each course run has a string semester value as part of its serialization """ num_courses = 5 courses = CourseFactory.create_batch(num_courses, program=self.program) course_runs = [CourseRunFactory.create(course=course) for course in courses] for course_run in course_runs: CachedEnrollmentFactory.create(user=self.user, course_run=course_run, verified=True) with patch( 'dashboard.serializers.get_year_season_from_course_run', autospec=True, return_value=(2017, 'Spring') ) as get_year_season_patch: serialized_program_user = UserProgramSearchSerializer.serialize(self.program_enrollment) assert len(serialized_program_user['courses']) == num_courses assert all( semester_enrollment['semester'] == '2017 - Spring' for semester_enrollment in serialized_program_user['course_runs'] ) # multiply by two while serialize_enrollments has semester assert get_year_season_patch.call_count == num_courses*2
def test_update_exam_authorization_cached_enrollment_when_no_exam_run( self): """ Test no exam profile created when course has no ExamRun """ self.exam_run.delete() course = CourseFactory.create(program=self.program) course_run = CourseRunFactory.create( end_date=now_in_utc() - timedelta(days=100), enrollment_end=now_in_utc() + timedelta(hours=1), course=course) create_order(self.profile.user, course_run) # exam profile before enrollment. assert ExamProfile.objects.filter( profile=self.profile).exists() is False CachedEnrollmentFactory.create(user=self.profile.user, course_run=course_run) assert ExamProfile.objects.filter( profile=self.profile).exists() is False
def test_exam_authorization_for_inactive_user(self): """ test exam_authorization when inactive user passed and paid for course. """ with mute_signals(post_save): profile = ProfileFactory.create() user = profile.user user.is_active = False user.save() with mute_signals(post_save): CachedEnrollmentFactory.create(user=user, course_run=self.course_run) with mute_signals(post_save): FinalGradeFactory.create( user=user, course_run=self.course_run, passed=True, course_run_paid_on_edx=False, ) create_order(user, self.course_run) mmtrack = get_mmtrack(user, self.program) self.assertTrue(mmtrack.has_paid(self.course_run.edx_course_key)) self.assertTrue( mmtrack.has_passed_course(self.course_run.edx_course_key)) # Neither user has exam profile nor authorization. assert ExamProfile.objects.filter( profile=mmtrack.user.profile).exists() is False assert ExamAuthorization.objects.filter( user=mmtrack.user, course=self.course_run.course).exists() is False with self.assertRaises(ExamAuthorizationException): authorize_for_exam_run(self.user, self.course_run, self.exam_run) # Assert user doesn't have exam profile and authorization assert ExamProfile.objects.filter( profile=mmtrack.user.profile).exists() is False assert ExamAuthorization.objects.filter( user=mmtrack.user, course=self.course_run.course).exists() is False
def test_exam_authorization_for_inactive_user(self): """ test exam_authorization when inactive user passed and paid for course. """ with mute_signals(post_save): profile = ProfileFactory.create() user = profile.user user.is_active = False user.save() with mute_signals(post_save): CachedEnrollmentFactory.create(user=user, course_run=self.course_run) with mute_signals(post_save): FinalGradeFactory.create( user=user, course_run=self.course_run, passed=True, course_run_paid_on_edx=False, ) create_order(user, self.course_run) mmtrack = get_mmtrack(user, self.program) self.assertTrue(mmtrack.has_paid(self.course_run.edx_course_key)) self.assertTrue(mmtrack.has_passed_course(self.course_run.edx_course_key)) # Neither user has exam profile nor authorization. assert ExamProfile.objects.filter(profile=mmtrack.user.profile).exists() is False assert ExamAuthorization.objects.filter( user=mmtrack.user, course=self.course_run.course ).exists() is False with self.assertRaises(ExamAuthorizationException): authorize_for_exam_run(mmtrack, self.course_run, self.exam_run) # Assert user doesn't have exam profile and authorization assert ExamProfile.objects.filter(profile=mmtrack.user.profile).exists() is False assert ExamAuthorization.objects.filter( user=mmtrack.user, course=self.course_run.course ).exists() is False
def setUpTestData(cls): cls.users = [UserFactory.create() for _ in range(35)] freeze_date = now_in_utc() - timedelta(days=1) future_freeze_date = now_in_utc() + timedelta(days=1) cls.course_run1 = CourseRunFactory.create( freeze_grade_date=freeze_date) cls.course_run2 = CourseRunFactory.create( freeze_grade_date=freeze_date) cls.all_freezable_runs = [cls.course_run1, cls.course_run2] cls.course_run_future = CourseRunFactory.create( freeze_grade_date=future_freeze_date) cls.course_run_frozen = CourseRunFactory.create( freeze_grade_date=freeze_date) CourseRunGradingStatus.objects.create(course_run=cls.course_run_frozen, status=FinalGradeStatus.COMPLETE) for user in cls.users: CachedEnrollmentFactory.create(user=user, course_run=cls.course_run1) CachedCurrentGradeFactory.create(user=user, course_run=cls.course_run1)
def test_update_exam_authorization_order(self, order_status): """ Verify that update_exam_authorization_final_grade is called when a fulfilled Order saves """ with mute_signals(post_save): # muted because enrollment also trigger signal for profile creation. right now we are just # looking final grades CachedEnrollmentFactory.create(user=self.profile.user, course_run=self.course_run) FinalGradeFactory.create( user=self.profile.user, course_run=self.course_run, passed=True, ) order = OrderFactory.create(user=self.profile.user, fulfilled=False) LineFactory.create(course_key=self.course_run.edx_course_key, order=order) # There is no ExamProfile or ExamAuthorization before creating the FinalGrade. assert ExamProfile.objects.filter( profile=self.profile).exists() is False assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course).exists() is False order.status = order_status order.save() # assert Exam Authorization and profile created. assert ExamProfile.objects.filter( profile=self.profile).exists() is True assert ExamAuthorization.objects.filter( user=self.profile.user, course=self.course_run.course).exists() is True
def test_semester_serialization(self): """ Tests that each course run has a string semester value as part of its serialization """ num_courses = 5 courses = CourseFactory.create_batch(num_courses, program=self.program) course_runs = [ CourseRunFactory.create(course=course) for course in courses ] for course_run in course_runs: CachedEnrollmentFactory.create(user=self.user, course_run=course_run, verified=True) with patch('dashboard.serializers.get_year_season_from_course_run', autospec=True, return_value=(2017, 'Spring')) as get_year_season_patch: serialized_program_user = UserProgramSearchSerializer.serialize( self.program_enrollment) assert len(serialized_program_user['courses']) == num_courses assert all( semester_enrollment['semester'] == '2017 - Spring' for semester_enrollment in serialized_program_user['course_runs']) # multiply by two while serialize_enrollments has semester assert get_year_season_patch.call_count == num_courses * 2
def setUpTestData(cls): cls.user = SocialUserFactory.create() cls.run_fa = CourseRunFactory.create( freeze_grade_date=now_in_utc()-timedelta(days=1), course__program__financial_aid_availability=True, ) cls.run_fa_with_cert = CourseRunFactory.create( freeze_grade_date=None, course__program=cls.run_fa.course.program, ) cls.run_no_fa = CourseRunFactory.create( freeze_grade_date=now_in_utc()+timedelta(days=1), course__program__financial_aid_availability=False, ) cls.run_no_fa_with_cert = CourseRunFactory.create( course__program=cls.run_no_fa.course.program, ) all_course_runs = (cls.run_fa, cls.run_fa_with_cert, cls.run_no_fa, cls.run_no_fa_with_cert, ) for run in all_course_runs: if run.course.program.financial_aid_availability: FinancialAidFactory.create( user=cls.user, tier_program=TierProgramFactory.create( program=run.course.program, income_threshold=0, current=True ), status=FinancialAidStatus.RESET, ) cls.enrollments = { course_run.edx_course_key: CachedEnrollmentFactory.create( user=cls.user, course_run=course_run) for course_run in all_course_runs } cls.current_grades = { course_run.edx_course_key: CachedCurrentGradeFactory.create( user=cls.user, course_run=course_run) for course_run in all_course_runs } cls.certificates = { course_run.edx_course_key: CachedCertificateFactory.create( user=cls.user, course_run=course_run) for course_run in (cls.run_fa_with_cert, cls.run_no_fa_with_cert) } cls.user_edx_data = CachedEdxUserData(cls.user)
def test_update_cached_enrollments(self, mocked_index): """Test for update_cached_enrollments.""" self.assert_cache_in_db() assert UserCacheRefreshTime.objects.filter(user=self.user).exists() is False CachedEdxDataApi.update_cached_enrollments(self.user, self.edx_client) self.assert_cache_in_db(enrollment_keys=self.enrollment_ids) cache_time = UserCacheRefreshTime.objects.get(user=self.user) now = now_in_utc() assert cache_time.enrollment <= now assert mocked_index.delay.called is True mocked_index.reset_mock() # add another cached element for another course that will be removed by the refresh cached_enr = CachedEnrollmentFactory.create(user=self.user) self.assert_cache_in_db(enrollment_keys=list(self.enrollment_ids) + [cached_enr.course_run.edx_course_key]) CachedEdxDataApi.update_cached_enrollments(self.user, self.edx_client) self.assert_cache_in_db(enrollment_keys=self.enrollment_ids) cache_time.refresh_from_db() assert cache_time.enrollment >= now mocked_index.delay.assert_called_once_with([self.user.id], check_if_changed=True)
def _generate_cached_enrollments(user, program, num_course_runs=1, data=None): """ Helper method to generate CachedEnrollments for test cases """ fake = faker.Factory.create() course = CourseFactory.create(program=program) course_run_params = dict(before_now=True, after_now=False, tzinfo=pytz.utc) course_runs = [ CourseRunFactory.create( course=course, enrollment_start=fake.date_time_this_month(**course_run_params), start_date=fake.date_time_this_month(**course_run_params), enrollment_end=fake.date_time_this_month(**course_run_params), end_date=fake.date_time_this_year(**course_run_params), ) for _ in range(num_course_runs) ] factory_kwargs = dict(user=user) if data is not None: factory_kwargs['data'] = data return [CachedEnrollmentFactory.create(course_run=course_run, **factory_kwargs) for course_run in course_runs]
def setUpTestData(cls): cls.user = UserFactory.create() # Create Programs, Courses, CourseRuns... cls.p1_course_run_keys = ['p1_course_run'] cls.p2_course_run_keys = ['p2_course_run_1', 'p2_course_run_2'] cls.p1_course_run = CourseRunFactory.create(edx_course_key=cls.p1_course_run_keys[0]) p2 = FullProgramFactory.create() first_course = p2.course_set.first() extra_course = CourseFactory.create(program=p2) cls.p2_course_run_1 = CourseRunFactory.create(course=first_course, edx_course_key=cls.p2_course_run_keys[0]) cls.p2_course_run_2 = CourseRunFactory.create(course=extra_course, edx_course_key=cls.p2_course_run_keys[1]) all_course_runs = [cls.p1_course_run, cls.p2_course_run_1, cls.p2_course_run_2] # Create cached edX data cls.enrollments = [ CachedEnrollmentFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs ] cls.certificates = [ CachedCertificateFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs ] cls.current_grades = [ CachedCurrentGradeFactory.create(user=cls.user, course_run=course_run) for course_run in all_course_runs ]
def test_update_cached_enrollments(self, mocked_index): """Test for update_cached_enrollments.""" self.assert_cache_in_db() assert UserCacheRefreshTime.objects.filter( user=self.user).exists() is False CachedEdxDataApi.update_cached_enrollments(self.user, self.edx_client) self.assert_cache_in_db(enrollment_keys=self.enrollment_ids) cache_time = UserCacheRefreshTime.objects.get(user=self.user) now = now_in_utc() assert cache_time.enrollment <= now assert mocked_index.delay.called is True mocked_index.reset_mock() # add another cached element for another course that will be removed by the refresh cached_enr = CachedEnrollmentFactory.create(user=self.user) self.assert_cache_in_db(enrollment_keys=list(self.enrollment_ids) + [cached_enr.course_run.edx_course_key]) CachedEdxDataApi.update_cached_enrollments(self.user, self.edx_client) self.assert_cache_in_db(enrollment_keys=self.enrollment_ids) cache_time.refresh_from_db() assert cache_time.enrollment >= now mocked_index.delay.assert_called_once_with([self.user.id], check_if_changed=True)
def unverified_enroll(cls, user, course_run): """Helper method to create an unverified enrollment for the test user in a course run""" return CachedEnrollmentFactory.create(user=user, course_run=course_run, unverified=True)