diff --git a/common/djangoapps/student/tests/test_recent_enrollments.py b/common/djangoapps/student/tests/test_recent_enrollments.py index ccba26e903..618c378477 100644 --- a/common/djangoapps/student/tests/test_recent_enrollments.py +++ b/common/djangoapps/student/tests/test_recent_enrollments.py @@ -43,7 +43,7 @@ class TestRecentEnrollments(ModuleStoreTestCase): # New Course course_location = locator.CourseLocator('Org1', 'Course1', 'Run1') - self.course, _ = self._create_course_and_enrollment(course_location) + self.course, self.enrollment = self._create_course_and_enrollment(course_location) def _create_course_and_enrollment(self, course_location): """ Creates a course and associated enrollment. """ @@ -112,10 +112,10 @@ class TestRecentEnrollments(ModuleStoreTestCase): recent_course_list = _get_recently_enrolled_courses(courses_list) self.assertEqual(len(recent_course_list), 5) - self.assertEqual(recent_course_list[1], courses[0]) - self.assertEqual(recent_course_list[2], courses[1]) - self.assertEqual(recent_course_list[3], courses[2]) - self.assertEqual(recent_course_list[4], courses[3]) + self.assertEqual(recent_course_list[1][0], courses[0]) + self.assertEqual(recent_course_list[2][0], courses[1]) + self.assertEqual(recent_course_list[3][0], courses[2]) + self.assertEqual(recent_course_list[4][0], courses[3]) def test_dashboard_rendering(self): """ @@ -127,15 +127,29 @@ class TestRecentEnrollments(ModuleStoreTestCase): self.assertContains(response, "Thank you for enrolling in") @ddt.data( - (['audit', 'honor', 'verified'], False), - (['professional'], False), - (['verified'], False), - (['audit'], True), - (['honor'], True), - ([], True) + #Register as an honor in any course modes with no payment option + ([('audit', 0), ('honor', 0)], 'honor', True), + ([('honor', 0)], 'honor', True), + ([], 'honor', True), + #Register as an honor in any course modes which has payment option + ([('honor', 10)], 'honor', False), # This is a paid course + ([('audit', 0), ('honor', 0), ('professional', 20)], 'honor', True), + ([('audit', 0), ('honor', 0), ('verified', 20)], 'honor', True), + ([('audit', 0), ('honor', 0), ('verified', 20), ('professional', 20)], 'honor', True), + ([], 'honor', True), + #Register as an audit in any course modes with no payment option + ([('audit', 0), ('honor', 0)], 'audit', True), + ([('audit', 0)], 'audit', True), + #Register as an audit in any course modes which has no payment option + ([('audit', 0), ('honor', 0), ('verified', 10)], 'audit', True), + #Register as a verified in any course modes which has payment option + ([('professional', 20)], 'professional', False), + ([('verified', 20)], 'verified', False), + ([('professional', 20), ('verified', 20)], 'verified', False), + ([('audit', 0), ('honor', 0), ('verified', 20)], 'verified', False) ) @ddt.unpack - def test_donate_button(self, course_modes, show_donate): + def test_donate_button(self, course_modes, enrollment_mode, show_donate): # Enable the enrollment success message self._configure_message_timeout(10000) @@ -143,8 +157,11 @@ class TestRecentEnrollments(ModuleStoreTestCase): DonationConfiguration(enabled=True).save() # Create the course mode(s) - for mode in course_modes: - CourseModeFactory(mode_slug=mode, course_id=self.course.id) + for mode, min_price in course_modes: + CourseModeFactory(mode_slug=mode, course_id=self.course.id, min_price=min_price) + + self.enrollment.mode = enrollment_mode + self.enrollment.save() # Check that the donate button is or is not displayed self.client.login(username=self.student.username, password=self.PASSWORD) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index f5f75485c8..af017129f5 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -712,9 +712,9 @@ def _create_recent_enrollment_message(course_enrollment_pairs, course_modes): { "course_id": course.id, "course_name": course.display_name, - "allow_donation": _allow_donation(course_modes, course.id) + "allow_donation": _allow_donation(course_modes, course.id, enrollment) } - for course in recently_enrolled_courses + for course, enrollment in recently_enrolled_courses ] return render_to_string( @@ -738,14 +738,14 @@ def _get_recently_enrolled_courses(course_enrollment_pairs): seconds = DashboardConfiguration.current().recent_enrollment_time_delta time_delta = (datetime.datetime.now(UTC) - datetime.timedelta(seconds=seconds)) return [ - course for course, enrollment in course_enrollment_pairs + (course, enrollment) for course, enrollment in course_enrollment_pairs # If the enrollment has no created date, we are explicitly excluding the course # from the list of recent enrollments. if enrollment.is_active and enrollment.created > time_delta ] -def _allow_donation(course_modes, course_id): +def _allow_donation(course_modes, course_id, enrollment): """Determines if the dashboard will request donations for the given course. Check if donations are configured for the platform, and if the current course is accepting donations. @@ -753,15 +753,14 @@ def _allow_donation(course_modes, course_id): Args: course_modes (dict): Mapping of course ID's to course mode dictionaries. course_id (str): The unique identifier for the course. + enrollment(CourseEnrollment): The enrollment object in which the user is enrolled Returns: True if the course is allowing donations. """ donations_enabled = DonationConfiguration.current().enabled - is_verified_mode = CourseMode.has_verified_mode(course_modes[course_id]) - has_payment_option = CourseMode.has_payment_options(course_id) - return donations_enabled and not is_verified_mode and not has_payment_option + return donations_enabled and enrollment.mode in course_modes[course_id] and course_modes[course_id][enrollment.mode].min_price == 0 def try_change_enrollment(request):