From 385e9de1fdff44445dc3d4153a8e26dc83d2da0d Mon Sep 17 00:00:00 2001 From: Jay Zoldak Date: Wed, 7 Aug 2013 16:33:37 -0400 Subject: [PATCH] Modify auto_auth to accept parameters for overriding created user attributes. --- .../student/tests/test_auto_auth.py | 27 ++++++++++++++++++- common/djangoapps/student/views.py | 23 +++++++++------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/common/djangoapps/student/tests/test_auto_auth.py b/common/djangoapps/student/tests/test_auto_auth.py index dca2937b01..a61591dc89 100644 --- a/common/djangoapps/student/tests/test_auto_auth.py +++ b/common/djangoapps/student/tests/test_auto_auth.py @@ -37,6 +37,26 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): user = qset[0] 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') def test_create_multiple_users(self, randint): """ @@ -50,8 +70,13 @@ class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): 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) + 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}) def test_login_already_created_user(self): diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index bc6afdce0b..ddae3a294e 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -945,28 +945,33 @@ def auto_auth(request): 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 - of create_account, with specified username and password. + of create_account, with specified values. """ - return {'username': username, - 'email': username + "_dummy_test@mitx.mit.edu", + 'email': email, 'password': password, - 'name': username + " " + username, + 'name': name, 'honor_code': 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_' pass_base = 'PASS_' max_users = settings.MITX_FEATURES.get('MAX_AUTO_AUTH_USERS', 200) number = random.randint(1, max_users) - username = name_base + str(number) - password = pass_base + str(number) + # Get the params from the request to override default user attributes if specified + 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 try: @@ -976,7 +981,7 @@ def auto_auth(request): # else create and activate account info 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) request.user.is_active = True request.user.save()