Commit ea31b17d by Jay Zoldak

Fix up auto auth tests.

parent 967ef6ff
from django.test import TestCase 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.contrib.auth.models import User
from django.conf import settings from util.testing import UrlResetMixin
from mock import patch 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 AutoAuthEnabledTestCase(UrlResetMixin, TestCase):
class TestAutoAuthEnabled(UrlResetMixin, TestCase):
""" """
Tests for the Auto auth view that we have for load testing. Tests for the Auto auth view that we have for load testing.
""" """
...@@ -23,15 +16,15 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase): ...@@ -23,15 +16,15 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase):
# value affects the contents of urls.py, # value affects the contents of urls.py,
# so we need to call super.setUp() which reloads urls.py (because # so we need to call super.setUp() which reloads urls.py (because
# of the UrlResetMixin) # of the UrlResetMixin)
super(TestAutoAuthEnabled, self).setUp() super(AutoAuthEnabledTestCase, self).setUp()
self.url = '/auto_auth'
self.client = Client()
def test_create_user(self): 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'] self.client.get(self.url)
url = '/auto_auth'
self.client.get(url)
qset = User.objects.all() qset = User.objects.all()
...@@ -40,43 +33,29 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase): ...@@ -40,43 +33,29 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase):
user = qset[0] user = qset[0]
assert user.is_active assert user.is_active
@patch.dict("django.conf.settings.MITX_FEATURES", {"MAX_AUTO_AUTH_USERS": 10000000}) @patch('student.views.random.randint')
def test_create_multiple_users(self): def test_create_multiple_users(self, randint):
""" """
speculative test to make sure multiple users are created. 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).
""" """
randint.return_value = 1
self.client.get(self.url)
url = '/auto_auth' randint.return_value = 2
self.client.get(self.url)
# 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)
qset = User.objects.all() qset = User.objects.all()
# make sure it is the smae user # make sure that USER_1 and USER_2 were created
self.assertGreater(qset.count(), 1) self.assertEqual(qset.count(), 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(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 a subsequent request results in an already existant one being
logged in. logged in.
""" """
# auto-generate 1 user (the max) # auto-generate 1 user (the max)
url = '/auto_auth' url = '/auto_auth'
self.client.get(url) self.client.get(url)
...@@ -85,11 +64,11 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase): ...@@ -85,11 +64,11 @@ class TestAutoAuthEnabled(UrlResetMixin, TestCase):
self.client.get(url) self.client.get(url)
qset = User.objects.all() qset = User.objects.all()
# make sure it is the smae user # make sure it is the same user
self.assertEqual(qset.count(), 1) self.assertEqual(qset.count(), 1)
class TestAutoAuthDisabled(UrlResetMixin, TestCase): class AutoAuthDisabledTestCase(UrlResetMixin):
""" """
Test that the page is inaccessible with default settings Test that the page is inaccessible with default settings
""" """
...@@ -100,14 +79,13 @@ class TestAutoAuthDisabled(UrlResetMixin, TestCase): ...@@ -100,14 +79,13 @@ class TestAutoAuthDisabled(UrlResetMixin, TestCase):
# value affects the contents of urls.py, # value affects the contents of urls.py,
# so we need to call super.setUp() which reloads urls.py (because # so we need to call super.setUp() which reloads urls.py (because
# of the UrlResetMixin) # 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.
""" """
response = self.client.get(self.url)
url = '/auto_auth'
response = self.client.get(url)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
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