2010-02-23 13:52:37 +08:00
|
|
|
from django.conf import settings
|
2012-09-08 01:17:09 +08:00
|
|
|
from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable,
|
|
|
|
UserManager)
|
2010-02-23 13:52:37 +08:00
|
|
|
from django.test import TestCase
|
2012-03-21 04:51:16 +08:00
|
|
|
from django.test.utils import override_settings
|
2012-09-08 01:17:09 +08:00
|
|
|
from django.utils import six
|
2012-02-10 02:58:53 +08:00
|
|
|
|
2010-02-23 13:52:37 +08:00
|
|
|
|
2012-06-26 02:24:26 +08:00
|
|
|
@override_settings(USE_TZ=False, AUTH_PROFILE_MODULE='')
|
2010-02-23 13:52:37 +08:00
|
|
|
class ProfileTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_site_profile_not_available(self):
|
2012-06-26 02:24:26 +08:00
|
|
|
user = User.objects.create(username='testclient')
|
|
|
|
|
2010-02-23 13:52:37 +08:00
|
|
|
# calling get_profile without AUTH_PROFILE_MODULE set
|
2012-06-26 02:24:26 +08:00
|
|
|
del settings.AUTH_PROFILE_MODULE
|
2012-09-08 01:17:09 +08:00
|
|
|
with six.assertRaisesRegex(self, SiteProfileNotAvailable,
|
2012-06-26 02:24:26 +08:00
|
|
|
"You need to set AUTH_PROFILE_MODULE in your project"):
|
|
|
|
user.get_profile()
|
2010-02-23 13:52:37 +08:00
|
|
|
|
2012-02-04 20:48:01 +08:00
|
|
|
# Bad syntax in AUTH_PROFILE_MODULE:
|
2010-02-23 13:52:37 +08:00
|
|
|
settings.AUTH_PROFILE_MODULE = 'foobar'
|
2012-09-08 01:17:09 +08:00
|
|
|
with six.assertRaisesRegex(self, SiteProfileNotAvailable,
|
2012-06-26 02:24:26 +08:00
|
|
|
"app_label and model_name should be separated by a dot"):
|
|
|
|
user.get_profile()
|
2010-02-23 13:52:37 +08:00
|
|
|
|
|
|
|
# module that doesn't exist
|
|
|
|
settings.AUTH_PROFILE_MODULE = 'foo.bar'
|
2012-09-08 01:17:09 +08:00
|
|
|
with six.assertRaisesRegex(self, SiteProfileNotAvailable,
|
2012-06-26 02:24:26 +08:00
|
|
|
"Unable to load the profile model"):
|
|
|
|
user.get_profile()
|
2012-02-04 20:48:01 +08:00
|
|
|
|
|
|
|
|
2012-03-30 17:08:29 +08:00
|
|
|
@override_settings(USE_TZ=False)
|
2012-02-04 20:48:01 +08:00
|
|
|
class NaturalKeysTestCase(TestCase):
|
|
|
|
fixtures = ['authtestdata.json']
|
|
|
|
|
|
|
|
def test_user_natural_key(self):
|
|
|
|
staff_user = User.objects.get(username='staff')
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(User.objects.get_by_natural_key('staff'), staff_user)
|
|
|
|
self.assertEqual(staff_user.natural_key(), ('staff',))
|
2012-02-04 20:48:01 +08:00
|
|
|
|
|
|
|
def test_group_natural_key(self):
|
|
|
|
users_group = Group.objects.create(name='users')
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(Group.objects.get_by_natural_key('users'), users_group)
|
2012-02-04 20:48:01 +08:00
|
|
|
|
|
|
|
|
2012-03-30 17:08:29 +08:00
|
|
|
@override_settings(USE_TZ=False)
|
2012-02-04 20:48:01 +08:00
|
|
|
class LoadDataWithoutNaturalKeysTestCase(TestCase):
|
|
|
|
fixtures = ['regular.json']
|
|
|
|
|
|
|
|
def test_user_is_created_and_added_to_group(self):
|
|
|
|
user = User.objects.get(username='my_username')
|
|
|
|
group = Group.objects.get(name='my_group')
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(group, user.groups.get())
|
2012-02-04 20:48:01 +08:00
|
|
|
|
|
|
|
|
2012-03-30 17:08:29 +08:00
|
|
|
@override_settings(USE_TZ=False)
|
2012-02-04 20:48:01 +08:00
|
|
|
class LoadDataWithNaturalKeysTestCase(TestCase):
|
|
|
|
fixtures = ['natural.json']
|
2012-02-10 02:58:53 +08:00
|
|
|
|
2012-02-04 20:48:01 +08:00
|
|
|
def test_user_is_created_and_added_to_group(self):
|
|
|
|
user = User.objects.get(username='my_username')
|
|
|
|
group = Group.objects.get(name='my_group')
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(group, user.groups.get())
|
2012-02-04 20:48:01 +08:00
|
|
|
|
2012-02-10 02:58:53 +08:00
|
|
|
|
|
|
|
class UserManagerTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_create_user(self):
|
|
|
|
email_lowercase = 'normal@normal.com'
|
|
|
|
user = User.objects.create_user('user', email_lowercase)
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(user.email, email_lowercase)
|
|
|
|
self.assertEqual(user.username, 'user')
|
|
|
|
self.assertEqual(user.password, '!')
|
2012-02-10 02:58:53 +08:00
|
|
|
|
|
|
|
def test_create_user_email_domain_normalize_rfc3696(self):
|
|
|
|
# According to http://tools.ietf.org/html/rfc3696#section-3
|
|
|
|
# the "@" symbol can be part of the local part of an email address
|
|
|
|
returned = UserManager.normalize_email(r'Abc\@DEF@EXAMPLE.com')
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(returned, r'Abc\@DEF@example.com')
|
2012-02-10 02:58:53 +08:00
|
|
|
|
|
|
|
def test_create_user_email_domain_normalize(self):
|
|
|
|
returned = UserManager.normalize_email('normal@DOMAIN.COM')
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(returned, 'normal@domain.com')
|
2012-02-10 02:58:53 +08:00
|
|
|
|
|
|
|
def test_create_user_email_domain_normalize_with_whitespace(self):
|
|
|
|
returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(returned, 'email\ with_whitespace@d.com')
|
2012-03-03 00:56:20 +08:00
|
|
|
|
|
|
|
def test_empty_username(self):
|
|
|
|
self.assertRaisesMessage(ValueError,
|
|
|
|
'The given username must be set',
|
|
|
|
User.objects.create_user, username='')
|