From fc34be896d89374991ff7ef5a5ff205536f5143b Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 5 Apr 2016 18:39:59 -0400 Subject: [PATCH] Extracted auth checks tests into their own module. --- tests/auth_tests/test_checks.py | 82 ++++++++++++++++++++++++++ tests/auth_tests/test_management.py | 90 +---------------------------- 2 files changed, 85 insertions(+), 87 deletions(-) create mode 100644 tests/auth_tests/test_checks.py diff --git a/tests/auth_tests/test_checks.py b/tests/auth_tests/test_checks.py new file mode 100644 index 0000000000..392e72e62c --- /dev/null +++ b/tests/auth_tests/test_checks.py @@ -0,0 +1,82 @@ +from __future__ import unicode_literals + +from django.contrib.auth.checks import check_user_model +from django.contrib.auth.models import AbstractBaseUser +from django.core import checks +from django.db import models +from django.test import ( + SimpleTestCase, override_settings, override_system_checks, +) +from django.test.utils import isolate_apps + +from .models import CustomUserNonUniqueUsername + + +@isolate_apps('auth_tests', attr_name='apps') +@override_system_checks([check_user_model]) +class UserModelChecksTests(SimpleTestCase): + @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonListRequiredFields') + def test_required_fields_is_list(self): + """REQUIRED_FIELDS should be a list.""" + class CustomUserNonListRequiredFields(AbstractBaseUser): + username = models.CharField(max_length=30, unique=True) + date_of_birth = models.DateField() + + USERNAME_FIELD = 'username' + REQUIRED_FIELDS = 'date_of_birth' + + errors = checks.run_checks(app_configs=self.apps.get_app_configs()) + self.assertEqual(errors, [ + checks.Error( + "'REQUIRED_FIELDS' must be a list or tuple.", + obj=CustomUserNonListRequiredFields, + id='auth.E001', + ), + ]) + + @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserBadRequiredFields') + def test_username_not_in_required_fields(self): + """USERNAME_FIELD should not appear in REQUIRED_FIELDS.""" + class CustomUserBadRequiredFields(AbstractBaseUser): + username = models.CharField(max_length=30, unique=True) + date_of_birth = models.DateField() + + USERNAME_FIELD = 'username' + REQUIRED_FIELDS = ['username', 'date_of_birth'] + + errors = checks.run_checks(self.apps.get_app_configs()) + self.assertEqual(errors, [ + checks.Error( + "The field named as the 'USERNAME_FIELD' for a custom user model " + "must not be included in 'REQUIRED_FIELDS'.", + obj=CustomUserBadRequiredFields, + id='auth.E002', + ), + ]) + + @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonUniqueUsername') + def test_username_non_unique(self): + """ + A non-unique USERNAME_FIELD should raise an error only if we use the + default authentication backend. Otherwise, an warning should be raised. + """ + errors = checks.run_checks() + self.assertEqual(errors, [ + checks.Error( + "'CustomUserNonUniqueUsername.username' must be " + "unique because it is named as the 'USERNAME_FIELD'.", + obj=CustomUserNonUniqueUsername, + id='auth.E003', + ), + ]) + with self.settings(AUTHENTICATION_BACKENDS=['my.custom.backend']): + errors = checks.run_checks() + self.assertEqual(errors, [ + checks.Warning( + "'CustomUserNonUniqueUsername.username' is named as " + "the 'USERNAME_FIELD', but it is not unique.", + hint='Ensure that your authentication backend(s) can handle non-unique usernames.', + obj=CustomUserNonUniqueUsername, + id='auth.W004', + ), + ]) diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index 1584ca4d46..3f31e7d66a 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -5,23 +5,16 @@ from datetime import date from django.apps import apps from django.contrib.auth import management -from django.contrib.auth.checks import check_user_model from django.contrib.auth.management import create_permissions from django.contrib.auth.management.commands import ( changepassword, createsuperuser, ) -from django.contrib.auth.models import ( - AbstractBaseUser, Group, Permission, User, -) +from django.contrib.auth.models import Group, Permission, User from django.contrib.contenttypes.models import ContentType -from django.core import checks, exceptions +from django.core import exceptions from django.core.management import call_command from django.core.management.base import CommandError -from django.db import models -from django.test import ( - SimpleTestCase, TestCase, mock, override_settings, override_system_checks, -) -from django.test.utils import isolate_apps +from django.test import TestCase, mock, override_settings from django.utils import six from django.utils.encoding import force_str from django.utils.translation import ugettext_lazy as _ @@ -569,83 +562,6 @@ class MultiDBCreatesuperuserTestCase(TestCase): self.assertEqual(user.email, 'joe@somewhere.org') -class CustomUserModelValidationTestCase(SimpleTestCase): - @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonListRequiredFields') - @override_system_checks([check_user_model]) - @isolate_apps('auth_tests', kwarg_name='apps') - def test_required_fields_is_list(self, apps): - """REQUIRED_FIELDS should be a list.""" - class CustomUserNonListRequiredFields(AbstractBaseUser): - username = models.CharField(max_length=30, unique=True) - date_of_birth = models.DateField() - - USERNAME_FIELD = 'username' - REQUIRED_FIELDS = 'date_of_birth' - - errors = checks.run_checks(app_configs=apps.get_app_configs()) - expected = [ - checks.Error( - "'REQUIRED_FIELDS' must be a list or tuple.", - obj=CustomUserNonListRequiredFields, - id='auth.E001', - ), - ] - self.assertEqual(errors, expected) - - @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserBadRequiredFields') - @override_system_checks([check_user_model]) - @isolate_apps('auth_tests', kwarg_name='apps') - def test_username_not_in_required_fields(self, apps): - """USERNAME_FIELD should not appear in REQUIRED_FIELDS.""" - class CustomUserBadRequiredFields(AbstractBaseUser): - username = models.CharField(max_length=30, unique=True) - date_of_birth = models.DateField() - - USERNAME_FIELD = 'username' - REQUIRED_FIELDS = ['username', 'date_of_birth'] - - errors = checks.run_checks(apps.get_app_configs()) - expected = [ - checks.Error( - "The field named as the 'USERNAME_FIELD' for a custom user model " - "must not be included in 'REQUIRED_FIELDS'.", - obj=CustomUserBadRequiredFields, - id='auth.E002', - ), - ] - self.assertEqual(errors, expected) - - @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonUniqueUsername') - @override_system_checks([check_user_model]) - def test_username_non_unique(self): - """ - A non-unique USERNAME_FIELD should raise an error only if we use the - default authentication backend. Otherwise, an warning should be raised. - """ - errors = checks.run_checks() - expected = [ - checks.Error( - "'CustomUserNonUniqueUsername.username' must be " - "unique because it is named as the 'USERNAME_FIELD'.", - obj=CustomUserNonUniqueUsername, - id='auth.E003', - ), - ] - self.assertEqual(errors, expected) - with self.settings(AUTHENTICATION_BACKENDS=['my.custom.backend']): - errors = checks.run_checks() - expected = [ - checks.Warning( - "'CustomUserNonUniqueUsername.username' is named as " - "the 'USERNAME_FIELD', but it is not unique.", - hint='Ensure that your authentication backend(s) can handle non-unique usernames.', - obj=CustomUserNonUniqueUsername, - id='auth.W004', - ) - ] - self.assertEqual(errors, expected) - - class PermissionTestCase(TestCase): def setUp(self):