diff --git a/common/djangoapps/course_modes/tests/test_views.py b/common/djangoapps/course_modes/tests/test_views.py index 1b5bf5f208..ae172c11d0 100644 --- a/common/djangoapps/course_modes/tests/test_views.py +++ b/common/djangoapps/course_modes/tests/test_views.py @@ -4,11 +4,12 @@ Tests for course_modes views. import decimal import unittest -from datetime import datetime +from datetime import datetime, timedelta import ddt import freezegun import httpretty +import pytz import waffle from django.conf import settings from django.core.urlresolvers import reverse @@ -43,44 +44,61 @@ class CourseModeViewTest(CatalogIntegrationMixin, UrlResetMixin, ModuleStoreTest @patch.dict(settings.FEATURES, {'MODE_CREATION_FOR_TESTING': True}) def setUp(self): super(CourseModeViewTest, self).setUp() - self.course = CourseFactory.create() + now = datetime.now(pytz.utc) + day = timedelta(days=1) + tomorrow = now + day + yesterday = now - day + # Create course that has not started yet and course that started + self.course = CourseFactory.create(start=tomorrow) + self.course_that_started = CourseFactory.create(start=yesterday) self.user = UserFactory.create(username="Bob", email="bob@example.com", password="edx") self.client.login(username=self.user.username, password="edx") @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') @httpretty.activate @ddt.data( - # is_active?, enrollment_mode, redirect? - (True, 'verified', True), - (True, 'honor', False), - (True, 'audit', False), - (False, 'verified', False), - (False, 'honor', False), - (False, 'audit', False), - (False, None, False), + # is_active?, enrollment_mode, redirect?, has_started + (True, 'verified', True, False), + (True, 'honor', False, False), + (True, 'audit', False, False), + (True, 'verified', True, True), + (True, 'honor', False, True), + (True, 'audit', False, True), + (False, 'verified', False, False), + (False, 'honor', False, False), + (False, 'audit', False, False), + (False, None, False, False), ) @ddt.unpack - def test_redirect_to_dashboard(self, is_active, enrollment_mode, redirect): + def test_redirect_to_dashboard(self, is_active, enrollment_mode, redirect, has_started): + # Configure whether course has started + # If it has go to course home instead of dashboard + course = self.course_that_started if has_started else self.course # Create the course modes for mode in ('audit', 'honor', 'verified'): - CourseModeFactory.create(mode_slug=mode, course_id=self.course.id) + CourseModeFactory.create(mode_slug=mode, course_id=course.id) # Enroll the user in the test course if enrollment_mode is not None: CourseEnrollmentFactory( is_active=is_active, mode=enrollment_mode, - course_id=self.course.id, + course_id=course.id, user=self.user ) # Configure whether we're upgrading or not - url = reverse('course_modes_choose', args=[unicode(self.course.id)]) + url = reverse('course_modes_choose', args=[unicode(course.id)]) response = self.client.get(url) # Check whether we were correctly redirected if redirect: - self.assertRedirects(response, reverse('dashboard')) + if has_started: + self.assertRedirects( + response, reverse('openedx.course_experience.course_home', kwargs={'course_id': course.id}) + ) + else: + self.assertRedirects(response, reverse('dashboard')) else: self.assertEquals(response.status_code, 200) diff --git a/common/djangoapps/course_modes/views.py b/common/djangoapps/course_modes/views.py index dbaff69419..6ca76524f4 100644 --- a/common/djangoapps/course_modes/views.py +++ b/common/djangoapps/course_modes/views.py @@ -103,6 +103,8 @@ class ChooseModeView(View): redirect_url = ecommerce_service.get_checkout_page_url(professional_mode.bulk_sku) return redirect(redirect_url) + course = modulestore().get_course(course_key) + # If there isn't a verified mode available, then there's nothing # to do on this page. Send the user to the dashboard. if not CourseMode.has_verified_mode(modes): @@ -110,12 +112,14 @@ class ChooseModeView(View): # If a user has already paid, redirect them to the dashboard. if is_active and (enrollment_mode in CourseMode.VERIFIED_MODES + [CourseMode.NO_ID_PROFESSIONAL_MODE]): + # If the course has started redirect to course home instead + if course.has_started(): + return redirect(reverse('openedx.course_experience.course_home', kwargs={'course_id': course_key})) return redirect(reverse('dashboard')) donation_for_course = request.session.get("donation_for_course", {}) chosen_price = donation_for_course.get(unicode(course_key), None) - course = modulestore().get_course(course_key) if CourseEnrollment.is_enrollment_closed(request.user, course): locale = to_locale(get_language()) enrollment_end_date = format_datetime(course.enrollment_end, 'short', locale=locale) @@ -224,10 +228,16 @@ class ChooseModeView(View): # system, such as third-party discovery. These workflows result in learners arriving # directly at this screen, and they will not necessarily be pre-enrolled in the audit mode. CourseEnrollment.enroll(request.user, course_key, CourseMode.AUDIT) + # If the course has started redirect to course home instead + if course.has_started(): + return redirect(reverse('openedx.course_experience.course_home', kwargs={'course_id': course_key})) return redirect(reverse('dashboard')) if requested_mode == 'honor': CourseEnrollment.enroll(user, course_key, mode=requested_mode) + # If the course has started redirect to course home instead + if course.has_started(): + return redirect(reverse('openedx.course_experience.course_home', kwargs={'course_id': course_key})) return redirect(reverse('dashboard')) mode_info = allowed_modes[requested_mode] diff --git a/common/test/acceptance/pages/lms/track_selection.py b/common/test/acceptance/pages/lms/track_selection.py index 42fe1a8140..59e88853c1 100644 --- a/common/test/acceptance/pages/lms/track_selection.py +++ b/common/test/acceptance/pages/lms/track_selection.py @@ -2,7 +2,7 @@ from bok_choy.page_object import PageObject from common.test.acceptance.pages.lms import BASE_URL -from common.test.acceptance.pages.lms.dashboard import DashboardPage +from common.test.acceptance.pages.lms.course_home import CourseHomePage from common.test.acceptance.pages.lms.pay_and_verify import PaymentAndVerificationFlow @@ -52,7 +52,7 @@ class TrackSelectionPage(PageObject): elif mode == "audit": self.q(css="input[name='audit_mode']").click() - return DashboardPage(self.browser).wait_for_page() + return CourseHomePage(self.browser, self._course_id).wait_for_page() else: raise ValueError("Mode must be either 'audit' or 'verified'.")