From ea31b17d576337f12a2678a3947c045caf7d6eaa Mon Sep 17 00:00:00 2001 From: Jay Zoldak Date: Tue, 23 Jul 2013 16:22:51 -0400 Subject: [PATCH] Fix up auto auth tests. --- .../student/tests/test_auto_auth.py | 76 +++++++------------ 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/common/djangoapps/student/tests/test_auto_auth.py b/common/djangoapps/student/tests/test_auto_auth.py index 3f642fd595..1ac2460c17 100644 --- a/common/djangoapps/student/tests/test_auto_auth.py +++ b/common/djangoapps/student/tests/test_auto_auth.py @@ -1,18 +1,11 @@ from django.test import TestCase -from util.testing import UrlResetMixin +from django.test.client import Client from django.contrib.auth.models import User -from django.conf import settings +from util.testing import UrlResetMixin from mock import patch -# from copy import deepcopy, copy - -# NEW_SETTINGS = deepcopy(settings) - -# update just the auth flag -# NEW_SETTINGS.MITX_FEATURES['AUTOMATIC_AUTH_FOR_LOAD_TESTING'] = True -# NEW_SETTINGS.MITX_FEATURES['MAX_AUTO_AUTH_USERS'] = 1 -class TestAutoAuthEnabled(UrlResetMixin, TestCase): +class AutoAuthEnabledTestCase(UrlResetMixin, TestCase): """ Tests for the Auto auth view that we have for load testing. """ @@ -23,15 +16,15 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase): # value affects the contents of urls.py, # so we need to call super.setUp() which reloads urls.py (because # of the UrlResetMixin) - super(TestAutoAuthEnabled, self).setUp() + super(AutoAuthEnabledTestCase, self).setUp() + self.url = '/auto_auth' + self.client = Client() def test_create_user(self): """ - tests that user gets created when visiting the page + Test that user gets created when visiting the page. """ - print settings.MITX_FEATURES['AUTOMATIC_AUTH_FOR_LOAD_TESTING'] - url = '/auto_auth' - self.client.get(url) + self.client.get(self.url) qset = User.objects.all() @@ -40,43 +33,29 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase): user = qset[0] assert user.is_active - @patch.dict("django.conf.settings.MITX_FEATURES", {"MAX_AUTO_AUTH_USERS": 10000000}) - def test_create_multiple_users(self): + @patch('student.views.random.randint') + def test_create_multiple_users(self, randint): """ - speculative test to make sure multiple users are created. - Technically, this test is probabalistic. - - However, my judgement is that if the chance of failing due - only to bad luck is less than 1:10^1000, we are OK (it is more - likely that the test failed because the jenkins server was hit - by an asteroid, or the person running the tests was a freind - of Hamlet's). + Test to make sure multiple users are created. """ + randint.return_value = 1 + self.client.get(self.url) - url = '/auto_auth' - - # hit the url a few times - # mathematically, is much more efficient - # to hit the site many many times, and - # have a smaller MAX user count, but it is - # the GET request that actually takes a lot - # of time. - for i in range(200): - self.client.get(url) + randint.return_value = 2 + self.client.get(self.url) qset = User.objects.all() - # make sure it is the smae user - self.assertGreater(qset.count(), 1) + # make sure that USER_1 and USER_2 were created + self.assertEqual(qset.count(), 2) @patch.dict("django.conf.settings.MITX_FEATURES", {"MAX_AUTO_AUTH_USERS": 1}) - def test_login(self): + def test_login_already_created_user(self): """ - test that when we have reached the limit for automatic users + 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) @@ -85,11 +64,11 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase): self.client.get(url) qset = User.objects.all() - # make sure it is the smae user + # make sure it is the same user self.assertEqual(qset.count(), 1) -class TestAutoAuthDisabled(UrlResetMixin, TestCase): +class AutoAuthDisabledTestCase(UrlResetMixin): """ Test that the page is inaccessible with default settings """ @@ -100,14 +79,13 @@ class TestAutoAuthDisabled(UrlResetMixin, TestCase): # value affects the contents of urls.py, # so we need to call super.setUp() which reloads urls.py (because # of the UrlResetMixin) - super(TestAutoAuthDisabled, self).setUp() + super(AutoAuthDisabledTestCase, self).setUp() + self.url = '/auto_auth' + self.client = Client() - def test_404(self): + def test_auto_auth_disabled(self): """ - make sure automatic authentication is invisible + Make sure automatic authentication is disabled. """ - - url = '/auto_auth' - response = self.client.get(url) - + response = self.client.get(self.url) self.assertEqual(response.status_code, 404)