Refactored bok-choy directory structure

Added fixtures for course and xblock creation
Added bok-choy Studio tests
Added bok-choy tests for ora self- and ai- assessment
Refactored auto-auth; added staff and course-enrollment options
Removed extra javascript properties from page objects
This commit is contained in:
Will Daly
2014-01-03 13:38:15 -08:00
parent f3f0e8a514
commit 4afd5ea49f
57 changed files with 1524 additions and 450 deletions

View File

@@ -1,6 +1,7 @@
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from student.models import CourseEnrollment, UserProfile
from util.testing import UrlResetMixin
from mock import patch
from django.core.urlresolvers import reverse, NoReverseMatch
@@ -19,82 +20,101 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase):
# of the UrlResetMixin)
super(AutoAuthEnabledTestCase, self).setUp()
self.url = '/auto_auth'
self.cms_csrf_url = "signup"
self.lms_csrf_url = "signin_user"
self.client = Client()
def test_create_user(self):
"""
Test that user gets created when visiting the page.
"""
self._auto_auth()
self.assertEqual(User.objects.count(), 1)
self.assertTrue(User.objects.all()[0].is_active)
self.client.get(self.url)
def test_create_same_user(self):
self._auto_auth(username='test')
self._auto_auth(username='test')
self.assertEqual(User.objects.count(), 1)
qset = User.objects.all()
# assert user was created and is active
self.assertEqual(qset.count(), 1)
user = qset[0]
assert user.is_active
def test_create_multiple_users(self):
"""
Test to make sure multiple users are created.
"""
self._auto_auth()
self._auto_auth()
self.assertEqual(User.objects.all().count(), 2)
def test_create_defined_user(self):
"""
Test that the user gets created with the correct attributes
when they are passed as parameters on the auto-auth page.
"""
self.client.get(
self.url,
{'username': 'robot', 'password': 'test', 'email': 'robot@edx.org'}
self._auto_auth(
username='robot', password='test',
email='robot@edx.org', full_name="Robot Name"
)
qset = User.objects.all()
# assert user was created with the correct username and password
self.assertEqual(qset.count(), 1)
user = qset[0]
# Check that the user has the correct info
user = User.objects.get(username='robot')
self.assertEqual(user.username, 'robot')
self.assertTrue(user.check_password('test'))
self.assertEqual(user.email, 'robot@edx.org')
@patch('student.views.random.randint')
def test_create_multiple_users(self, randint):
# Check that the user has a profile
user_profile = UserProfile.objects.get(user=user)
self.assertEqual(user_profile.name, "Robot Name")
# By default, the user should not be global staff
self.assertFalse(user.is_staff)
def test_create_staff_user(self):
# Create a staff user
self._auto_auth(username='test', staff='true')
user = User.objects.get(username='test')
self.assertTrue(user.is_staff)
# Revoke staff privileges
self._auto_auth(username='test', staff='false')
user = User.objects.get(username='test')
self.assertFalse(user.is_staff)
def test_course_enrollment(self):
# Create a user and enroll in a course
course_id = "edX/Test101/2014_Spring"
self._auto_auth(username='test', course_id=course_id)
# Check that a course enrollment was created for the user
self.assertEqual(CourseEnrollment.objects.count(), 1)
enrollment = CourseEnrollment.objects.get(course_id=course_id)
self.assertEqual(enrollment.user.username, "test")
def test_double_enrollment(self):
# Create a user and enroll in a course
course_id = "edX/Test101/2014_Spring"
self._auto_auth(username='test', course_id=course_id)
# Make the same call again, re-enrolling the student in the same course
self._auto_auth(username='test', course_id=course_id)
# Check that only one course enrollment was created for the user
self.assertEqual(CourseEnrollment.objects.count(), 1)
enrollment = CourseEnrollment.objects.get(course_id=course_id)
self.assertEqual(enrollment.user.username, "test")
def _auto_auth(self, **params):
"""
Test to make sure multiple users are created.
Make a request to the auto-auth end-point and check
that the response is successful.
"""
randint.return_value = 1
self.client.get(self.url)
response = self.client.get(self.url, params)
self.assertEqual(response.status_code, 200)
randint.return_value = 2
self.client.get(self.url)
qset = User.objects.all()
# make sure that USER_1 and USER_2 were created correctly
self.assertEqual(qset.count(), 2)
user1 = qset[0]
self.assertEqual(user1.username, 'USER_1')
self.assertTrue(user1.check_password('PASS_1'))
self.assertEqual(user1.email, 'USER_1_dummy_test@mitx.mit.edu')
self.assertEqual(qset[1].username, 'USER_2')
@patch.dict("django.conf.settings.FEATURES", {"MAX_AUTO_AUTH_USERS": 1})
def test_login_already_created_user(self):
"""
Test that when we have reached the limit for automatic users
a subsequent request results in an already existant one being
logged in.
"""
# auto-generate 1 user (the max)
url = '/auto_auth'
self.client.get(url)
# go to the site again
self.client.get(url)
qset = User.objects.all()
# make sure it is the same user
self.assertEqual(qset.count(), 1)
# Check that session and CSRF are set in the response
for cookie in ['csrftoken', 'sessionid']:
self.assertIn(cookie, response.cookies) #pylint: disable=E1103
self.assertTrue(response.cookies[cookie].value) #pylint: disable=E1103
class AutoAuthDisabledTestCase(UrlResetMixin, TestCase):
@@ -118,19 +138,3 @@ class AutoAuthDisabledTestCase(UrlResetMixin, TestCase):
"""
response = self.client.get(self.url)
self.assertEqual(response.status_code, 404)
def test_csrf_enabled(self):
"""
test that when not load testing, csrf protection is on
"""
cms_csrf_url = "signup"
lms_csrf_url = "signin_user"
self.client = Client(enforce_csrf_checks=True)
try:
csrf_protected_url = reverse(cms_csrf_url)
response = self.client.post(csrf_protected_url)
except NoReverseMatch:
csrf_protected_url = reverse(lms_csrf_url)
response = self.client.post(csrf_protected_url)
self.assertEqual(response.status_code, 403)

View File

@@ -981,54 +981,85 @@ def create_account(request, post_override=None):
def auto_auth(request):
"""
Automatically logs the user in with a generated random credentials
This view is only accessible when
Create or configure a user account, then log in as that user.
Enabled only when
settings.FEATURES['AUTOMATIC_AUTH_FOR_TESTING'] is true.
Accepts the following querystring parameters:
* `username`, `email`, and `password` for the user account
* `full_name` for the user profile (the user's full name; defaults to the username)
* `staff`: Set to "true" to make the user global staff.
* `course_id`: Enroll the student in the course with `course_id`
If username, email, or password are not provided, use
randomly generated credentials.
"""
def get_dummy_post_data(username, password, email, name):
"""
Return a dictionary suitable for passing to post_vars of _do_create_account or post_override
of create_account, with specified values.
"""
return {'username': username,
'email': email,
'password': password,
'name': name,
'honor_code': u'true',
'terms_of_service': u'true', }
# generate random user credentials from a small name space (determined by settings)
name_base = 'USER_'
pass_base = 'PASS_'
max_users = settings.FEATURES.get('MAX_AUTO_AUTH_USERS', 200)
number = random.randint(1, max_users)
# Get the params from the request to override default user attributes if specified
qdict = request.GET
# Generate a unique name to use if none provided
unique_name = uuid.uuid4().hex[0:30]
# Use the params from the request, otherwise use these defaults
username = qdict.get('username', name_base + str(number))
password = qdict.get('password', pass_base + str(number))
email = qdict.get('email', '%s_dummy_test@mitx.mit.edu' % username)
name = qdict.get('name', '%s Test' % username)
username = request.GET.get('username', unique_name)
password = request.GET.get('password', unique_name)
email = request.GET.get('email', unique_name + "@example.com")
full_name = request.GET.get('full_name', username)
is_staff = request.GET.get('staff', None)
course_id = request.GET.get('course_id', None)
# if they already are a user, log in
try:
# Get or create the user object
post_data = {
'username': username,
'email': email,
'password': password,
'name': full_name,
'honor_code': u'true',
'terms_of_service': u'true',
}
# Attempt to create the account.
# If successful, this will return a tuple containing
# the new user object; otherwise it will return an error
# message.
result = _do_create_account(post_data)
if isinstance(result, tuple):
user = result[0]
# If we did not create a new account, the user might already
# exist. Attempt to retrieve it.
else:
user = User.objects.get(username=username)
user = authenticate(username=username, password=password, request=request)
login(request, user)
user.email = email
user.set_password(password)
user.save()
# else create and activate account info
except ObjectDoesNotExist:
post_override = get_dummy_post_data(username, password, email, name)
create_account(request, post_override=post_override)
request.user.is_active = True
request.user.save()
# Set the user's global staff bit
if is_staff is not None:
user.is_staff = (is_staff == "true")
user.save()
# return empty success
return HttpResponse('')
# Activate the user
reg = Registration.objects.get(user=user)
reg.activate()
reg.save()
# Enroll the user in a course
if course_id is not None:
CourseEnrollment.enroll(user, course_id)
# Log in as the user
user = authenticate(username=username, password=password)
login(request, user)
# Provide the user with a valid CSRF token
# then return a 200 response
success_msg = u"Logged in user {0} ({1}) with password {2}".format(
username, email, password
)
response = HttpResponse(success_msg)
response.set_cookie('csrftoken', csrf(request)['csrf_token'])
return response
@ensure_csrf_cookie