Merge pull request #10832 from edx/clytwynec/auto-auth-redirect
Clytwynec/auto auth redirect
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from django.test import TestCase
|
||||
from django.test.client import Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.conf import settings
|
||||
from django_comment_common.models import (
|
||||
Role, FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_STUDENT)
|
||||
from django_comment_common.utils import seed_permissions_roles
|
||||
@@ -175,7 +176,47 @@ 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
|
||||
if settings.ROOT_URLCONF == 'lms.urls':
|
||||
url_pattern = '/info'
|
||||
else:
|
||||
url_pattern = '/course/{}'.format(unicode(course_key))
|
||||
|
||||
self.assertTrue(response.url.endswith(url_pattern)) # 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
|
||||
if settings.ROOT_URLCONF == 'lms.urls':
|
||||
url_pattern = '/dashboard'
|
||||
else:
|
||||
url_pattern = '/home'
|
||||
|
||||
self.assertTrue(response.url.endswith(url_pattern)) # 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 +230,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']:
|
||||
|
||||
@@ -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,
|
||||
@@ -1801,6 +1801,7 @@ def auto_auth(request):
|
||||
* `course_id`: Enroll the student in the course with `course_id`
|
||||
* `roles`: Comma-separated list of roles to grant the student in the course with `course_id`
|
||||
* `no_login`: Define this to create the user but not login
|
||||
* `redirect`: Set to "true" will redirect to course if course_id is defined, otherwise it will redirect to dashboard
|
||||
|
||||
If username, email, or password are not provided, use
|
||||
randomly generated credentials.
|
||||
@@ -1825,6 +1826,7 @@ def auto_auth(request):
|
||||
if course_id:
|
||||
course_key = CourseLocator.from_string(course_id)
|
||||
role_names = [v.strip() for v in request.GET.get('roles', '').split(',') if v.strip()]
|
||||
redirect_when_done = request.GET.get('redirect', '').lower() == 'true'
|
||||
login_when_done = 'no_login' not in request.GET
|
||||
|
||||
form = AccountCreationForm(
|
||||
@@ -1887,8 +1889,32 @@ def auto_auth(request):
|
||||
create_comments_service_user(user)
|
||||
|
||||
# Provide the user with a valid CSRF token
|
||||
# then return a 200 response
|
||||
if request.META.get('HTTP_ACCEPT') == 'application/json':
|
||||
# then return a 200 response unless redirect is true
|
||||
if redirect_when_done:
|
||||
# Redirect to course info page if course_id is known
|
||||
if course_id:
|
||||
try:
|
||||
# redirect to course info page in LMS
|
||||
redirect_url = reverse(
|
||||
'info',
|
||||
kwargs={'course_id': course_id}
|
||||
)
|
||||
except NoReverseMatch:
|
||||
# redirect to course outline page in Studio
|
||||
redirect_url = reverse(
|
||||
'course_handler',
|
||||
kwargs={'course_key_string': course_id}
|
||||
)
|
||||
else:
|
||||
try:
|
||||
# redirect to dashboard for LMS
|
||||
redirect_url = reverse('dashboard')
|
||||
except NoReverseMatch:
|
||||
# redirect to home for Studio
|
||||
redirect_url = reverse('home')
|
||||
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user