1
from django.conf import settings
2
from django.contrib.auth import get_user_model
3
from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable,
5
from django.contrib.auth.tests.utils import skipIfCustomUser
6
from django.test import TestCase
7
from django.test.utils import override_settings
8
from django.utils import six
12
@override_settings(USE_TZ=False, AUTH_PROFILE_MODULE='')
13
class ProfileTestCase(TestCase):
15
def test_site_profile_not_available(self):
16
user = User.objects.create(username='testclient')
18
# calling get_profile without AUTH_PROFILE_MODULE set
19
del settings.AUTH_PROFILE_MODULE
20
with six.assertRaisesRegex(self, SiteProfileNotAvailable,
21
"You need to set AUTH_PROFILE_MODULE in your project"):
24
# Bad syntax in AUTH_PROFILE_MODULE:
25
settings.AUTH_PROFILE_MODULE = 'foobar'
26
with six.assertRaisesRegex(self, SiteProfileNotAvailable,
27
"app_label and model_name should be separated by a dot"):
30
# module that doesn't exist
31
settings.AUTH_PROFILE_MODULE = 'foo.bar'
32
with six.assertRaisesRegex(self, SiteProfileNotAvailable,
33
"Unable to load the profile model"):
38
@override_settings(USE_TZ=False)
39
class NaturalKeysTestCase(TestCase):
40
fixtures = ['authtestdata.json']
42
def test_user_natural_key(self):
43
staff_user = User.objects.get(username='staff')
44
self.assertEqual(User.objects.get_by_natural_key('staff'), staff_user)
45
self.assertEqual(staff_user.natural_key(), ('staff',))
47
def test_group_natural_key(self):
48
users_group = Group.objects.create(name='users')
49
self.assertEqual(Group.objects.get_by_natural_key('users'), users_group)
53
@override_settings(USE_TZ=False)
54
class LoadDataWithoutNaturalKeysTestCase(TestCase):
55
fixtures = ['regular.json']
57
def test_user_is_created_and_added_to_group(self):
58
user = User.objects.get(username='my_username')
59
group = Group.objects.get(name='my_group')
60
self.assertEqual(group, user.groups.get())
64
@override_settings(USE_TZ=False)
65
class LoadDataWithNaturalKeysTestCase(TestCase):
66
fixtures = ['natural.json']
68
def test_user_is_created_and_added_to_group(self):
69
user = User.objects.get(username='my_username')
70
group = Group.objects.get(name='my_group')
71
self.assertEqual(group, user.groups.get())
75
class UserManagerTestCase(TestCase):
77
def test_create_user(self):
78
email_lowercase = 'normal@normal.com'
79
user = User.objects.create_user('user', email_lowercase)
80
self.assertEqual(user.email, email_lowercase)
81
self.assertEqual(user.username, 'user')
82
self.assertEqual(user.password, '!')
84
def test_create_user_email_domain_normalize_rfc3696(self):
85
# According to http://tools.ietf.org/html/rfc3696#section-3
86
# the "@" symbol can be part of the local part of an email address
87
returned = UserManager.normalize_email(r'Abc\@DEF@EXAMPLE.com')
88
self.assertEqual(returned, r'Abc\@DEF@example.com')
90
def test_create_user_email_domain_normalize(self):
91
returned = UserManager.normalize_email('normal@DOMAIN.COM')
92
self.assertEqual(returned, 'normal@domain.com')
94
def test_create_user_email_domain_normalize_with_whitespace(self):
95
returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
96
self.assertEqual(returned, 'email\ with_whitespace@d.com')
98
def test_empty_username(self):
99
self.assertRaisesMessage(ValueError,
100
'The given username must be set',
101
User.objects.create_user, username='')
104
class IsActiveTestCase(TestCase):
106
Tests the behavior of the guaranteed is_active attribute
110
def test_builtin_user_isactive(self):
111
user = User.objects.create(username='foo', email='foo@bar.com')
112
# is_active is true by default
113
self.assertEqual(user.is_active, True)
114
user.is_active = False
116
user_fetched = User.objects.get(pk=user.pk)
117
# the is_active flag is saved
118
self.assertFalse(user_fetched.is_active)
120
@override_settings(AUTH_USER_MODEL='auth.IsActiveTestUser1')
121
def test_is_active_field_default(self):
123
tests that the default value for is_active is provided
125
UserModel = get_user_model()
126
user = UserModel(username='foo')
127
self.assertEqual(user.is_active, True)
128
# you can set the attribute - but it will not save
129
user.is_active = False
130
# there should be no problem saving - but the attribute is not saved
132
user_fetched = UserModel._default_manager.get(pk=user.pk)
133
# the attribute is always true for newly retrieved instance
134
self.assertEqual(user_fetched.is_active, True)