Commit 385e9de1 by Jay Zoldak

Modify auto_auth to accept parameters for overriding created user attributes.

parent 92f5246c
...@@ -37,6 +37,26 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): ...@@ -37,6 +37,26 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase):
user = qset[0] user = qset[0]
assert user.is_active assert user.is_active
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'}
)
qset = User.objects.all()
# assert user was created with the correct username and password
self.assertEqual(qset.count(), 1)
user = qset[0]
self.assertEqual(user.username, 'robot')
self.assertTrue(user.check_password('test'))
self.assertEqual(user.email, 'robot@edx.org')
@patch('student.views.random.randint') @patch('student.views.random.randint')
def test_create_multiple_users(self, randint): def test_create_multiple_users(self, randint):
""" """
...@@ -50,8 +70,13 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): ...@@ -50,8 +70,13 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase):
qset = User.objects.all() qset = User.objects.all()
# make sure that USER_1 and USER_2 were created # make sure that USER_1 and USER_2 were created correctly
self.assertEqual(qset.count(), 2) 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.MITX_FEATURES", {"MAX_AUTO_AUTH_USERS": 1}) @patch.dict("django.conf.settings.MITX_FEATURES", {"MAX_AUTO_AUTH_USERS": 1})
def test_login_already_created_user(self): def test_login_already_created_user(self):
......
...@@ -945,28 +945,33 @@ def auto_auth(request): ...@@ -945,28 +945,33 @@ def auto_auth(request):
settings.MITX_SETTINGS['AUTOMATIC_AUTH_FOR_LOAD_TESTING'] is true. settings.MITX_SETTINGS['AUTOMATIC_AUTH_FOR_LOAD_TESTING'] is true.
""" """
def get_dummy_post_data(username, password): 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 Return a dictionary suitable for passing to post_vars of _do_create_account or post_override
of create_account, with specified username and password. of create_account, with specified values.
""" """
return {'username': username, return {'username': username,
'email': username + "_dummy_test@mitx.mit.edu", 'email': email,
'password': password, 'password': password,
'name': username + " " + username, 'name': name,
'honor_code': u'true', 'honor_code': u'true',
'terms_of_service': u'true', } 'terms_of_service': u'true', }
# generate random user ceredentials from a small name space (determined by settings) # generate random user credentials from a small name space (determined by settings)
name_base = 'USER_' name_base = 'USER_'
pass_base = 'PASS_' pass_base = 'PASS_'
max_users = settings.MITX_FEATURES.get('MAX_AUTO_AUTH_USERS', 200) max_users = settings.MITX_FEATURES.get('MAX_AUTO_AUTH_USERS', 200)
number = random.randint(1, max_users) number = random.randint(1, max_users)
username = name_base + str(number) # Get the params from the request to override default user attributes if specified
password = pass_base + str(number) qdict = request.GET
# 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)
# if they already are a user, log in # if they already are a user, log in
try: try:
...@@ -976,7 +981,7 @@ def auto_auth(request): ...@@ -976,7 +981,7 @@ def auto_auth(request):
# else create and activate account info # else create and activate account info
except ObjectDoesNotExist: except ObjectDoesNotExist:
post_override = get_dummy_post_data(username, password) post_override = get_dummy_post_data(username, password, email, name)
create_account(request, post_override=post_override) create_account(request, post_override=post_override)
request.user.is_active = True request.user.is_active = True
request.user.save() request.user.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment