PageRenderTime 130ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/auth/tests/models.py

https://github.com/gregmuellegger/django
Python | 93 lines | 72 code | 19 blank | 2 comment | 1 complexity | dc1bd45844bd3002e4c959e086fe3368 MD5 | raw file
  1. from django.conf import settings
  2. from django.test import TestCase
  3. from django.test.utils import override_settings
  4. from django.contrib.auth.models import (Group, User,
  5. SiteProfileNotAvailable, UserManager)
  6. @override_settings(USE_TZ=False, AUTH_PROFILE_MODULE='')
  7. class ProfileTestCase(TestCase):
  8. def test_site_profile_not_available(self):
  9. user = User.objects.create(username='testclient')
  10. # calling get_profile without AUTH_PROFILE_MODULE set
  11. del settings.AUTH_PROFILE_MODULE
  12. with self.assertRaisesRegexp(SiteProfileNotAvailable,
  13. "You need to set AUTH_PROFILE_MODULE in your project"):
  14. user.get_profile()
  15. # Bad syntax in AUTH_PROFILE_MODULE:
  16. settings.AUTH_PROFILE_MODULE = 'foobar'
  17. with self.assertRaisesRegexp(SiteProfileNotAvailable,
  18. "app_label and model_name should be separated by a dot"):
  19. user.get_profile()
  20. # module that doesn't exist
  21. settings.AUTH_PROFILE_MODULE = 'foo.bar'
  22. with self.assertRaisesRegexp(SiteProfileNotAvailable,
  23. "Unable to load the profile model"):
  24. user.get_profile()
  25. @override_settings(USE_TZ=False)
  26. class NaturalKeysTestCase(TestCase):
  27. fixtures = ['authtestdata.json']
  28. def test_user_natural_key(self):
  29. staff_user = User.objects.get(username='staff')
  30. self.assertEqual(User.objects.get_by_natural_key('staff'), staff_user)
  31. self.assertEqual(staff_user.natural_key(), ('staff',))
  32. def test_group_natural_key(self):
  33. users_group = Group.objects.create(name='users')
  34. self.assertEqual(Group.objects.get_by_natural_key('users'), users_group)
  35. @override_settings(USE_TZ=False)
  36. class LoadDataWithoutNaturalKeysTestCase(TestCase):
  37. fixtures = ['regular.json']
  38. def test_user_is_created_and_added_to_group(self):
  39. user = User.objects.get(username='my_username')
  40. group = Group.objects.get(name='my_group')
  41. self.assertEqual(group, user.groups.get())
  42. @override_settings(USE_TZ=False)
  43. class LoadDataWithNaturalKeysTestCase(TestCase):
  44. fixtures = ['natural.json']
  45. def test_user_is_created_and_added_to_group(self):
  46. user = User.objects.get(username='my_username')
  47. group = Group.objects.get(name='my_group')
  48. self.assertEqual(group, user.groups.get())
  49. class UserManagerTestCase(TestCase):
  50. def test_create_user(self):
  51. email_lowercase = 'normal@normal.com'
  52. user = User.objects.create_user('user', email_lowercase)
  53. self.assertEqual(user.email, email_lowercase)
  54. self.assertEqual(user.username, 'user')
  55. self.assertEqual(user.password, '!')
  56. def test_create_user_email_domain_normalize_rfc3696(self):
  57. # According to http://tools.ietf.org/html/rfc3696#section-3
  58. # the "@" symbol can be part of the local part of an email address
  59. returned = UserManager.normalize_email(r'Abc\@DEF@EXAMPLE.com')
  60. self.assertEqual(returned, r'Abc\@DEF@example.com')
  61. def test_create_user_email_domain_normalize(self):
  62. returned = UserManager.normalize_email('normal@DOMAIN.COM')
  63. self.assertEqual(returned, 'normal@domain.com')
  64. def test_create_user_email_domain_normalize_with_whitespace(self):
  65. returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
  66. self.assertEqual(returned, 'email\ with_whitespace@d.com')
  67. def test_empty_username(self):
  68. self.assertRaisesMessage(ValueError,
  69. 'The given username must be set',
  70. User.objects.create_user, username='')