diff --git a/common/djangoapps/student/tests/test_auto_auth.py b/common/djangoapps/student/tests/test_auto_auth.py index 2605cc65f5..22a45982b3 100644 --- a/common/djangoapps/student/tests/test_auto_auth.py +++ b/common/djangoapps/student/tests/test_auto_auth.py @@ -175,7 +175,40 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): response_data ) - def _auto_auth(self, params=None, **kwargs): + @ddt.data(*COURSE_IDS_DDT) + @ddt.unpack + def test_redirect_to_course(self, course_id, course_key): + + # Create a user and enroll in a course + response = self._auto_auth({ + 'username': 'test', + 'course_id': course_id, + 'redirect': True, + 'staff': 'true', + }, status_code=302) + + # Check that a course enrollment was created for the user + self.assertEqual(CourseEnrollment.objects.count(), 1) + enrollment = CourseEnrollment.objects.get(course_id=course_key) + self.assertEqual(enrollment.user.username, "test") + + # Check that the redirect was to the course info/outline page + urls = ('/info', 'course/{}'.format(course_key.to_deprecated_string())) + response.url.endswith(urls) # pylint: disable=no-member + + def test_redirect_to_main(self): + # Create user and redirect to 'home' (cms) or 'dashboard' (lms) + response = self._auto_auth({ + 'username': 'test', + 'redirect': True, + 'staff': 'true', + }, status_code=302) + + # Check that the redirect was to either /dashboard or /home + urls = ('/dashboard', '/home') + response.url.endswith(urls) # pylint: disable=no-member + + def _auto_auth(self, params=None, status_code=None, **kwargs): """ Make a request to the auto-auth end-point and check that the response is successful. @@ -189,7 +222,9 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): """ params = params or {} response = self.client.get(self.url, params, **kwargs) - self.assertEqual(response.status_code, 200) + + expected_status_code = status_code if status_code else 200 + self.assertEqual(response.status_code, expected_status_code) # Check that session and CSRF are set in the response for cookie in ['csrftoken', 'sessionid']: diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 63c0d09511..4ccb99f418 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -21,7 +21,7 @@ from django.contrib.auth.views import password_reset_confirm from django.contrib import messages from django.core.context_processors import csrf from django.core import mail -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse, NoReverseMatch from django.core.validators import validate_email, ValidationError from django.db import IntegrityError, transaction from django.http import (HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, @@ -1891,12 +1891,28 @@ def auto_auth(request): if redirect_when_done: # Redirect to course info page if course_id is known if course_id: - return redirect(reverse('info', kwargs={'course_id': unicode(course_id)})) - # Otherwise redirect to dashboard + try: + # redirect to course info page in LMS + redirect_url = reverse( + 'info', + kwargs={'course_id': unicode(course_id)} + ) + except NoReverseMatch: + # redirect to course outline page in Studio + redirect_url = reverse( + 'course_handler', + kwargs={'course_key_string': unicode(course_key)} + ) else: - return redirect(reverse('dashboard')) + try: + # redirect to dashboard for LMS + redirect_url = reverse('dashboard') + except NoReverseMatch: + # redirect to home for Studio + redirect_url = reverse('home') - if request.META.get('HTTP_ACCEPT') == 'application/json': + return redirect(redirect_url) + elif request.META.get('HTTP_ACCEPT') == 'application/json': response = JsonResponse({ 'created_status': u"Logged in" if login_when_done else "Created", 'username': username,