Skip to content
Snippets Groups Projects
Commit 7be356b3 authored by jsa's avatar jsa
Browse files

auto_auth: add ability to configure forum roles and scrape user id.

parent 7ee08eae
No related branches found
No related tags found
No related merge requests found
from django.test import TestCase from django.test import TestCase
from django.test.client import Client from django.test.client import Client
from django.contrib.auth.models import User from django.contrib.auth.models import User
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
from student.models import CourseEnrollment, UserProfile from student.models import CourseEnrollment, UserProfile
from util.testing import UrlResetMixin from util.testing import UrlResetMixin
from mock import patch from mock import patch
from django.core.urlresolvers import reverse, NoReverseMatch
class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): class AutoAuthEnabledTestCase(UrlResetMixin, TestCase):
...@@ -103,6 +105,39 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): ...@@ -103,6 +105,39 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase):
enrollment = CourseEnrollment.objects.get(course_id=course_id) enrollment = CourseEnrollment.objects.get(course_id=course_id)
self.assertEqual(enrollment.user.username, "test") self.assertEqual(enrollment.user.username, "test")
def test_set_roles(self):
course_id = "edX/Test101/2014_Spring"
seed_permissions_roles(course_id)
course_roles = dict((r.name, r) for r in Role.objects.filter(course_id=course_id))
self.assertEqual(len(course_roles), 4) # sanity check
# Student role is assigned by default on course enrollment.
self._auto_auth(username='a_student', course_id=course_id)
user = User.objects.get(username='a_student')
user_roles = user.roles.all()
self.assertEqual(len(user_roles), 1)
self.assertEqual(user_roles[0], course_roles[FORUM_ROLE_STUDENT])
self._auto_auth(username='a_moderator', course_id=course_id, roles='Moderator')
user = User.objects.get(username='a_moderator')
user_roles = user.roles.all()
self.assertEqual(
set(user_roles),
set([course_roles[FORUM_ROLE_STUDENT],
course_roles[FORUM_ROLE_MODERATOR]]))
# check multiple roles work.
self._auto_auth(username='an_admin', course_id=course_id,
roles='{},{}'.format(FORUM_ROLE_MODERATOR, FORUM_ROLE_ADMINISTRATOR))
user = User.objects.get(username='an_admin')
user_roles = user.roles.all()
self.assertEqual(
set(user_roles),
set([course_roles[FORUM_ROLE_STUDENT],
course_roles[FORUM_ROLE_MODERATOR],
course_roles[FORUM_ROLE_ADMINISTRATOR]]))
def _auto_auth(self, **params): def _auto_auth(self, **params):
""" """
Make a request to the auto-auth end-point and check Make a request to the auto-auth end-point and check
...@@ -113,8 +148,8 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): ...@@ -113,8 +148,8 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase):
# Check that session and CSRF are set in the response # Check that session and CSRF are set in the response
for cookie in ['csrftoken', 'sessionid']: for cookie in ['csrftoken', 'sessionid']:
self.assertIn(cookie, response.cookies) #pylint: disable=E1103 self.assertIn(cookie, response.cookies) # pylint: disable=E1103
self.assertTrue(response.cookies[cookie].value) #pylint: disable=E1103 self.assertTrue(response.cookies[cookie].value) # pylint: disable=E1103
class AutoAuthDisabledTestCase(UrlResetMixin, TestCase): class AutoAuthDisabledTestCase(UrlResetMixin, TestCase):
......
...@@ -1274,8 +1274,8 @@ def auto_auth(request): ...@@ -1274,8 +1274,8 @@ def auto_auth(request):
# Provide the user with a valid CSRF token # Provide the user with a valid CSRF token
# then return a 200 response # then return a 200 response
success_msg = u"Logged in user {0} ({1}) with password {2}".format( success_msg = u"Logged in user {0} ({1}) with password {2} and user_id {3}".format(
username, email, password username, email, password, user.id
) )
response = HttpResponse(success_msg) response = HttpResponse(success_msg)
response.set_cookie('csrftoken', csrf(request)['csrf_token']) response.set_cookie('csrftoken', csrf(request)['csrf_token'])
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Auto-auth page (used to automatically log in during testing). Auto-auth page (used to automatically log in during testing).
""" """
import re
import urllib import urllib
from bok_choy.page_object import PageObject from bok_choy.page_object import PageObject
from . import BASE_URL from . import BASE_URL
...@@ -14,7 +15,7 @@ class AutoAuthPage(PageObject): ...@@ -14,7 +15,7 @@ class AutoAuthPage(PageObject):
this url will create a user and log them in. this url will create a user and log them in.
""" """
def __init__(self, browser, username=None, email=None, password=None, staff=None, course_id=None): def __init__(self, browser, username=None, email=None, password=None, staff=None, course_id=None, roles=None):
""" """
Auto-auth is an end-point for HTTP GET requests. Auto-auth is an end-point for HTTP GET requests.
By default, it will create accounts with random user credentials, By default, it will create accounts with random user credentials,
...@@ -47,6 +48,9 @@ class AutoAuthPage(PageObject): ...@@ -47,6 +48,9 @@ class AutoAuthPage(PageObject):
if course_id is not None: if course_id is not None:
self._params['course_id'] = course_id self._params['course_id'] = course_id
if roles is not None:
self._params['roles'] = roles
@property @property
def url(self): def url(self):
""" """
...@@ -62,3 +66,8 @@ class AutoAuthPage(PageObject): ...@@ -62,3 +66,8 @@ class AutoAuthPage(PageObject):
def is_browser_on_page(self): def is_browser_on_page(self):
return True return True
def get_user_id(self):
message = self.css_text('BODY')[0].strip()
match = re.search(r' user_id ([^$]+)$', message)
return match.groups()[0] if match else None
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment