def test_courseware_url(settings): """Test that the courseware_url property yields the correct values""" settings.OPENEDX_BASE_REDIRECT_URL = "http://example.com" course_run = CourseRunFactory.build(courseware_url_path="/path") course_run_no_path = CourseRunFactory.build(courseware_url_path=None) assert course_run.courseware_url == "http://example.com/path" assert course_run_no_path.courseware_url is None
def test_serialized_semester_value(self): """ Tests that semester information in a course run is serialized to the right values """ valid_semester_course_run = CourseRunFactory.build(start_date=datetime(2017, 1, 1, tzinfo=pytz.UTC)) no_semester_course_run = CourseRunFactory.build(start_date=None, edx_course_key='bad_key') valid_semester_serialized = UserProgramSearchSerializer.serialize_semester(valid_semester_course_run) no_semester_serialized = UserProgramSearchSerializer.serialize_semester(no_semester_course_run) assert valid_semester_serialized == '2017 - Spring' assert no_semester_serialized is None
def test_serialized_semester_value(self): """ Tests that semester information in a course run is serialized to the right values """ valid_semester_course_run = CourseRunFactory.build( start_date=datetime(2017, 1, 1, tzinfo=pytz.UTC)) no_semester_course_run = CourseRunFactory.build( start_date=None, edx_course_key='bad_key') valid_semester_serialized = UserProgramSearchSerializer.serialize_semester( valid_semester_course_run) no_semester_serialized = UserProgramSearchSerializer.serialize_semester( no_semester_course_run) assert valid_semester_serialized == '2017 - Spring' assert no_semester_serialized is None
def test_enroll_pro_unknown_fail(mocker, user): """ Tests that enroll_in_edx_course_runs raises an UnknownEdxApiEnrollException if an unexpected exception is encountered """ mock_client = mocker.MagicMock() mock_client.enrollments.create_student_enrollment = mocker.Mock( side_effect=ValueError("Unexpected error")) mocker.patch("courseware.api.get_edx_api_client", return_value=mock_client) course_run = CourseRunFactory.build() with pytest.raises(UnknownEdxApiEnrollException): enroll_in_edx_course_runs(user, [course_run])
def test_enroll_pro_api_fail(mocker, user): """ Tests that enroll_in_edx_course_runs raises an EdxApiEnrollErrorException if the request fails for some reason besides an enrollment mode error """ mock_client = mocker.MagicMock() pro_enrollment_response = MockResponse({"message": "no dice"}, status_code=401) mock_client.enrollments.create_student_enrollment = mocker.Mock( side_effect=HTTPError(response=pro_enrollment_response)) mocker.patch("courseware.api.get_edx_api_client", return_value=mock_client) course_run = CourseRunFactory.build() with pytest.raises(EdxApiEnrollErrorException): enroll_in_edx_course_runs(user, [course_run])
def test_enroll_in_edx_course_runs_audit(mocker, user, error_text): """Tests that enroll_in_edx_course_runs fails over to attempting enrollment with 'audit' mode""" mock_client = mocker.MagicMock() pro_enrollment_response = MockResponse({"message": error_text}) audit_result = {"good": "result"} mock_client.enrollments.create_student_enrollment = mocker.Mock( side_effect=[ HTTPError(response=pro_enrollment_response), audit_result ]) patched_log_error = mocker.patch("courseware.api.log.error") mocker.patch("courseware.api.get_edx_api_client", return_value=mock_client) course_run = CourseRunFactory.build() results = enroll_in_edx_course_runs(user, [course_run]) assert mock_client.enrollments.create_student_enrollment.call_count == 2 mock_client.enrollments.create_student_enrollment.assert_any_call( course_run.courseware_id, mode=EDX_ENROLLMENT_PRO_MODE) mock_client.enrollments.create_student_enrollment.assert_any_call( course_run.courseware_id, mode=EDX_ENROLLMENT_AUDIT_MODE) assert results == [audit_result] patched_log_error.assert_called_once()
def test_get_year_season_from_course_run(self): """ Tests that year/season is calculated appropriately from a CourseRun """ fall_2016_dt = datetime(2016, 10, 1, tzinfo=pytz.UTC) test_run1 = CourseRunFactory.build(edx_course_key='course-v1:MITx+CTL.SC0x+1T2016', start_date=fall_2016_dt) test_run2 = CourseRunFactory.build(edx_course_key='MITx/14.73x_1/1T2016', start_date=fall_2016_dt) test_run3 = CourseRunFactory.build(edx_course_key='MITx/14.73x_1', start_date=fall_2016_dt) test_run4 = CourseRunFactory.build(edx_course_key='invalid', start_date=fall_2016_dt) unparseable_test_run1 = CourseRunFactory.build(edx_course_key='invalid', start_date=None) unparseable_test_run2 = CourseRunFactory.build( edx_course_key='course-v1:MITX+MITx_Digital_learning_300+3Tabc', start_date=None ) assert get_year_season_from_course_run(test_run1) == (2016, 'Spring') assert get_year_season_from_course_run(test_run2) == (2016, 'Spring') assert get_year_season_from_course_run(test_run3) == (2016, 'Fall') assert get_year_season_from_course_run(test_run4) == (2016, 'Fall') assert get_year_season_from_course_run(unparseable_test_run1) == () assert get_year_season_from_course_run(unparseable_test_run2) == ()