summaryrefslogtreecommitdiff
path: root/tests/auth_tests
diff options
context:
space:
mode:
authorJosh Smeaton <josh.smeaton@gmail.com>2015-02-23 11:53:57 +1100
committerJosh Smeaton <josh.smeaton@gmail.com>2015-03-05 10:10:32 +1100
commit39a7eed1bbf12020a077e4bec3d82e08f171a021 (patch)
tree225be14a94d57517d9de646569498eb45d0a4352 /tests/auth_tests
parentd6969abf239d52f6dfed7384c6ceb7df7e618342 (diff)
Converted test fixtures to setUpTestData methods
Diffstat (limited to 'tests/auth_tests')
-rw-r--r--tests/auth_tests/fixtures/authtestdata.json110
-rw-r--r--tests/auth_tests/fixtures/context-processors-users.xml17
-rw-r--r--tests/auth_tests/fixtures/custom_user.json14
-rw-r--r--tests/auth_tests/test_context_processors.py13
-rw-r--r--tests/auth_tests/test_forms.py66
-rw-r--r--tests/auth_tests/test_models.py41
-rw-r--r--tests/auth_tests/test_signals.py22
-rw-r--r--tests/auth_tests/test_views.py53
8 files changed, 168 insertions, 168 deletions
diff --git a/tests/auth_tests/fixtures/authtestdata.json b/tests/auth_tests/fixtures/authtestdata.json
deleted file mode 100644
index 931328899b..0000000000
--- a/tests/auth_tests/fixtures/authtestdata.json
+++ /dev/null
@@ -1,110 +0,0 @@
-[
- {
- "pk": "1",
- "model": "auth.user",
- "fields": {
- "username": "testclient",
- "first_name": "Test",
- "last_name": "Client",
- "is_active": true,
- "is_superuser": false,
- "is_staff": false,
- "last_login": "2006-12-17 07:03:31",
- "groups": [],
- "user_permissions": [],
- "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161",
- "email": "testclient@example.com",
- "date_joined": "2006-12-17 07:03:31"
- }
- },
- {
- "pk": "2",
- "model": "auth.user",
- "fields": {
- "username": "inactive",
- "first_name": "Inactive",
- "last_name": "User",
- "is_active": false,
- "is_superuser": false,
- "is_staff": false,
- "last_login": "2006-12-17 07:03:31",
- "groups": [],
- "user_permissions": [],
- "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161",
- "email": "testclient2@example.com",
- "date_joined": "2006-12-17 07:03:31"
- }
- },
- {
- "pk": "3",
- "model": "auth.user",
- "fields": {
- "username": "staff",
- "first_name": "Staff",
- "last_name": "Member",
- "is_active": true,
- "is_superuser": false,
- "is_staff": true,
- "last_login": "2006-12-17 07:03:31",
- "groups": [],
- "user_permissions": [],
- "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161",
- "email": "staffmember@example.com",
- "date_joined": "2006-12-17 07:03:31"
- }
- },
- {
- "pk": "4",
- "model": "auth.user",
- "fields": {
- "username": "empty_password",
- "first_name": "Empty",
- "last_name": "Password",
- "is_active": true,
- "is_superuser": false,
- "is_staff": false,
- "last_login": "2006-12-17 07:03:31",
- "groups": [],
- "user_permissions": [],
- "password": "",
- "email": "empty_password@example.com",
- "date_joined": "2006-12-17 07:03:31"
- }
- },
- {
- "pk": "5",
- "model": "auth.user",
- "fields": {
- "username": "unmanageable_password",
- "first_name": "Unmanageable",
- "last_name": "Password",
- "is_active": true,
- "is_superuser": false,
- "is_staff": false,
- "last_login": "2006-12-17 07:03:31",
- "groups": [],
- "user_permissions": [],
- "password": "$",
- "email": "unmanageable_password@example.com",
- "date_joined": "2006-12-17 07:03:31"
- }
- },
- {
- "pk": "6",
- "model": "auth.user",
- "fields": {
- "username": "unknown_password",
- "first_name": "Unknown",
- "last_name": "Password",
- "is_active": true,
- "is_superuser": false,
- "is_staff": false,
- "last_login": "2006-12-17 07:03:31",
- "groups": [],
- "user_permissions": [],
- "password": "foo$bar",
- "email": "unknown_password@example.com",
- "date_joined": "2006-12-17 07:03:31"
- }
- }
-]
diff --git a/tests/auth_tests/fixtures/context-processors-users.xml b/tests/auth_tests/fixtures/context-processors-users.xml
deleted file mode 100644
index aba8f4aace..0000000000
--- a/tests/auth_tests/fixtures/context-processors-users.xml
+++ /dev/null
@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<django-objects version="1.0">
- <object pk="100" model="auth.user">
- <field type="CharField" name="username">super</field>
- <field type="CharField" name="first_name">Super</field>
- <field type="CharField" name="last_name">User</field>
- <field type="CharField" name="email">super@example.com</field>
- <field type="CharField" name="password">sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158</field>
- <field type="BooleanField" name="is_staff">True</field>
- <field type="BooleanField" name="is_active">True</field>
- <field type="BooleanField" name="is_superuser">True</field>
- <field type="DateTimeField" name="last_login">2007-05-30 13:20:10</field>
- <field type="DateTimeField" name="date_joined">2007-05-30 13:20:10</field>
- <field to="auth.group" name="groups" rel="ManyToManyRel"></field>
- <field to="auth.permission" name="user_permissions" rel="ManyToManyRel"></field>
- </object>
-</django-objects>
diff --git a/tests/auth_tests/fixtures/custom_user.json b/tests/auth_tests/fixtures/custom_user.json
deleted file mode 100644
index 770bea6541..0000000000
--- a/tests/auth_tests/fixtures/custom_user.json
+++ /dev/null
@@ -1,14 +0,0 @@
-[
- {
- "pk": "1",
- "model": "auth.customuser",
- "fields": {
- "password": "sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161",
- "last_login": "2006-12-17 07:03:31",
- "email": "staffmember@example.com",
- "is_active": true,
- "is_admin": false,
- "date_of_birth": "1976-11-08"
- }
- }
-] \ No newline at end of file
diff --git a/tests/auth_tests/test_context_processors.py b/tests/auth_tests/test_context_processors.py
index 26d73f2841..5f91fd1e0a 100644
--- a/tests/auth_tests/test_context_processors.py
+++ b/tests/auth_tests/test_context_processors.py
@@ -1,3 +1,5 @@
+import datetime
+
from django.contrib.auth import authenticate
from django.contrib.auth.context_processors import PermLookupDict, PermWrapper
from django.contrib.auth.models import Permission, User
@@ -67,7 +69,16 @@ class AuthContextProcessorTests(TestCase):
"""
Tests for the ``django.contrib.auth.context_processors.auth`` processor
"""
- fixtures = ['context-processors-users.xml']
+
+ @classmethod
+ def setUpTestData(cls):
+ # password = "secret"
+ cls.u1 = User.objects.create(
+ id=100, password='sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158',
+ last_login=datetime.datetime(2007, 5, 30, 13, 20, 10), is_superuser=True, username='super',
+ first_name='Super', last_name='User', email='super@example.com',
+ is_staff=True, is_active=True, date_joined=datetime.datetime(2007, 5, 30, 13, 20, 10)
+ )
@override_settings(MIDDLEWARE_CLASSES=AUTH_MIDDLEWARE_CLASSES)
def test_session_not_accessed(self):
diff --git a/tests/auth_tests/test_forms.py b/tests/auth_tests/test_forms.py
index 5d6ca82fe1..28dca7b3a1 100644
--- a/tests/auth_tests/test_forms.py
+++ b/tests/auth_tests/test_forms.py
@@ -1,5 +1,6 @@
from __future__ import unicode_literals
+import datetime
import re
from django import forms
@@ -22,10 +23,49 @@ from django.utils.translation import ugettext as _
from .settings import AUTH_TEMPLATES
-@override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'])
-class UserCreationFormTest(TestCase):
+class TestDataMixin(object):
+
+ @classmethod
+ def setUpTestData(cls):
+ cls.u1 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='testclient',
+ first_name='Test', last_name='Client', email='testclient@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u2 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='inactive',
+ first_name='Inactive', last_name='User', email='testclient2@example.com', is_staff=False, is_active=False,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u3 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='staff',
+ first_name='Staff', last_name='Member', email='staffmember@example.com', is_staff=True, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u4 = User.objects.create(
+ password='', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='empty_password', first_name='Empty', last_name='Password', email='empty_password@example.com',
+ is_staff=False, is_active=True, date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u5 = User.objects.create(
+ password='$', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='unmanageable_password', first_name='Unmanageable', last_name='Password',
+ email='unmanageable_password@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u6 = User.objects.create(
+ password='foo$bar', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='unknown_password', first_name='Unknown', last_name='Password',
+ email='unknown_password@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+
- fixtures = ['authtestdata.json']
+@override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'])
+class UserCreationFormTest(TestDataMixin, TestCase):
def test_user_already_exists(self):
data = {
@@ -90,9 +130,7 @@ class UserCreationFormTest(TestCase):
@override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'])
-class AuthenticationFormTest(TestCase):
-
- fixtures = ['authtestdata.json']
+class AuthenticationFormTest(TestDataMixin, TestCase):
def test_invalid_username(self):
# The user submits an invalid username.
@@ -202,9 +240,7 @@ class AuthenticationFormTest(TestCase):
@override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'])
-class SetPasswordFormTest(TestCase):
-
- fixtures = ['authtestdata.json']
+class SetPasswordFormTest(TestDataMixin, TestCase):
def test_password_verification(self):
# The two new passwords do not match.
@@ -229,9 +265,7 @@ class SetPasswordFormTest(TestCase):
@override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'])
-class PasswordChangeFormTest(TestCase):
-
- fixtures = ['authtestdata.json']
+class PasswordChangeFormTest(TestDataMixin, TestCase):
def test_incorrect_password(self):
user = User.objects.get(username='testclient')
@@ -277,9 +311,7 @@ class PasswordChangeFormTest(TestCase):
@override_settings(USE_TZ=False, PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'])
-class UserChangeFormTest(TestCase):
-
- fixtures = ['authtestdata.json']
+class UserChangeFormTest(TestDataMixin, TestCase):
def test_username_validity(self):
user = User.objects.get(username='testclient')
@@ -359,9 +391,7 @@ class UserChangeFormTest(TestCase):
TEMPLATES=AUTH_TEMPLATES,
USE_TZ=False,
)
-class PasswordResetFormTest(TestCase):
-
- fixtures = ['authtestdata.json']
+class PasswordResetFormTest(TestDataMixin, TestCase):
@classmethod
def setUpClass(cls):
diff --git a/tests/auth_tests/test_models.py b/tests/auth_tests/test_models.py
index 5dff9c16b0..62c38ed578 100644
--- a/tests/auth_tests/test_models.py
+++ b/tests/auth_tests/test_models.py
@@ -1,3 +1,5 @@
+import datetime
+
from django.contrib.auth import get_user_model
from django.contrib.auth.models import (
AbstractUser, Group, Permission, User, UserManager,
@@ -10,7 +12,44 @@ from django.test import TestCase, override_settings
@override_settings(USE_TZ=False)
class NaturalKeysTestCase(TestCase):
- fixtures = ['authtestdata.json']
+
+ @classmethod
+ def setUpTestData(cls):
+ cls.u1 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='testclient',
+ first_name='Test', last_name='Client', email='testclient@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u2 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='inactive',
+ first_name='Inactive', last_name='User', email='testclient2@example.com', is_staff=False, is_active=False,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u3 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='staff',
+ first_name='Staff', last_name='Member', email='staffmember@example.com', is_staff=True, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u4 = User.objects.create(
+ password='', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='empty_password', first_name='Empty', last_name='Password', email='empty_password@example.com',
+ is_staff=False, is_active=True, date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u5 = User.objects.create(
+ password='$', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='unmanageable_password', first_name='Unmanageable', last_name='Password',
+ email='unmanageable_password@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u6 = User.objects.create(
+ password='foo$bar', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='unknown_password', first_name='Unknown', last_name='Password',
+ email='unknown_password@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
def test_user_natural_key(self):
staff_user = User.objects.get(username='staff')
diff --git a/tests/auth_tests/test_signals.py b/tests/auth_tests/test_signals.py
index 893dbad605..e81c202211 100644
--- a/tests/auth_tests/test_signals.py
+++ b/tests/auth_tests/test_signals.py
@@ -1,3 +1,5 @@
+import datetime
+
from django.contrib.auth import signals
from django.contrib.auth.models import User
from django.test import TestCase, override_settings
@@ -8,7 +10,21 @@ from django.test.client import RequestFactory
PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'],
ROOT_URLCONF='auth_tests.urls')
class SignalTestCase(TestCase):
- fixtures = ['authtestdata.json']
+
+ @classmethod
+ def setUpTestData(cls):
+ cls.u1 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='testclient',
+ first_name='Test', last_name='Client', email='testclient@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u3 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='staff',
+ first_name='Staff', last_name='Member', email='staffmember@example.com', is_staff=True, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
def listener_login(self, user, **kwargs):
self.logged_in.append(user)
@@ -66,13 +82,13 @@ class SignalTestCase(TestCase):
def test_update_last_login(self):
"""Ensure that only `last_login` is updated in `update_last_login`"""
- user = User.objects.get(pk=3)
+ user = self.u3
old_last_login = user.last_login
user.username = "This username shouldn't get saved"
request = RequestFactory().get('/login')
signals.user_logged_in.send(sender=user.__class__, request=request,
user=user)
- user = User.objects.get(pk=3)
+ user = User.objects.get(pk=self.u3.pk)
self.assertEqual(user.username, 'staff')
self.assertNotEqual(user.last_login, old_last_login)
diff --git a/tests/auth_tests/test_views.py b/tests/auth_tests/test_views.py
index d6f3b8eca6..a6571a7cca 100644
--- a/tests/auth_tests/test_views.py
+++ b/tests/auth_tests/test_views.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
+import datetime
import itertools
import re
from importlib import import_module
@@ -13,6 +14,7 @@ from django.contrib.auth.forms import (
AuthenticationForm, PasswordChangeForm, SetPasswordForm,
)
from django.contrib.auth.models import User
+from django.contrib.auth.tests.custom_user import CustomUser
from django.contrib.auth.views import login as login_view, redirect_to_login
from django.contrib.sessions.middleware import SessionMiddleware
from django.contrib.sites.requests import RequestSite
@@ -49,7 +51,44 @@ class AuthViewsTestCase(TestCase):
"""
Helper base class for all the follow test cases.
"""
- fixtures = ['authtestdata.json']
+
+ @classmethod
+ def setUpTestData(cls):
+ cls.u1 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='testclient',
+ first_name='Test', last_name='Client', email='testclient@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u2 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='inactive',
+ first_name='Inactive', last_name='User', email='testclient2@example.com', is_staff=False, is_active=False,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u3 = User.objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False, username='staff',
+ first_name='Staff', last_name='Member', email='staffmember@example.com', is_staff=True, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u4 = User.objects.create(
+ password='', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='empty_password', first_name='Empty', last_name='Password', email='empty_password@example.com',
+ is_staff=False, is_active=True, date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u5 = User.objects.create(
+ password='$', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='unmanageable_password', first_name='Unmanageable', last_name='Password',
+ email='unmanageable_password@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
+ cls.u6 = User.objects.create(
+ password='foo$bar', last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), is_superuser=False,
+ username='unknown_password', first_name='Unknown', last_name='Password',
+ email='unknown_password@example.com', is_staff=False, is_active=True,
+ date_joined=datetime.datetime(2006, 12, 17, 7, 3, 31)
+ )
def login(self, username='testclient', password='password'):
response = self.client.post('/login/', {
@@ -333,9 +372,16 @@ class PasswordResetTest(AuthViewsTestCase):
@override_settings(AUTH_USER_MODEL='auth.CustomUser')
class CustomUserPasswordResetTest(AuthViewsTestCase):
- fixtures = ['custom_user.json']
user_email = 'staffmember@example.com'
+ @classmethod
+ def setUpTestData(cls):
+ cls.u1 = CustomUser.custom_objects.create(
+ password='sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161',
+ last_login=datetime.datetime(2006, 12, 17, 7, 3, 31), email='staffmember@example.com', is_active=True,
+ is_admin=False, date_of_birth=datetime.date(1976, 11, 8)
+ )
+
def _test_confirm_start(self):
# Start by creating the email
response = self.client.post('/password_reset/', {'email': self.user_email})
@@ -363,7 +409,6 @@ class CustomUserPasswordResetTest(AuthViewsTestCase):
@override_settings(AUTH_USER_MODEL='auth.UUIDUser')
class UUIDUserPasswordResetTest(CustomUserPasswordResetTest):
- fixtures = None
def _test_confirm_start(self):
# instead of fixture
@@ -846,7 +891,7 @@ class ChangelistTests(AuthViewsTestCase):
# Make me a superuser before logging in.
User.objects.filter(username='testclient').update(is_staff=True, is_superuser=True)
self.login()
- self.admin = User.objects.get(pk=1)
+ self.admin = User.objects.get(pk=self.u1.pk)
def get_user_data(self, user):
return {