2
0

test_basic.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import warnings
  4. from django.contrib.auth import get_user_model
  5. from django.contrib.auth.models import AnonymousUser, User
  6. from django.core.exceptions import ImproperlyConfigured
  7. from django.db import IntegrityError
  8. from django.test import TestCase, override_settings
  9. from django.utils import translation
  10. from .models import CustomUser
  11. class BasicTestCase(TestCase):
  12. def test_user(self):
  13. "Check that users can be created and can set their password"
  14. u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
  15. self.assertTrue(u.has_usable_password())
  16. self.assertFalse(u.check_password('bad'))
  17. self.assertTrue(u.check_password('testpw'))
  18. # Check we can manually set an unusable password
  19. u.set_unusable_password()
  20. u.save()
  21. self.assertFalse(u.check_password('testpw'))
  22. self.assertFalse(u.has_usable_password())
  23. u.set_password('testpw')
  24. self.assertTrue(u.check_password('testpw'))
  25. u.set_password(None)
  26. self.assertFalse(u.has_usable_password())
  27. # Check username getter
  28. self.assertEqual(u.get_username(), 'testuser')
  29. # Check authentication/permissions
  30. self.assertFalse(u.is_anonymous)
  31. self.assertTrue(u.is_authenticated)
  32. self.assertFalse(u.is_staff)
  33. self.assertTrue(u.is_active)
  34. self.assertFalse(u.is_superuser)
  35. # Check API-based user creation with no password
  36. u2 = User.objects.create_user('testuser2', 'test2@example.com')
  37. self.assertFalse(u2.has_usable_password())
  38. def test_unicode_username(self):
  39. User.objects.create_user('jörg')
  40. User.objects.create_user('Григорий')
  41. # Two equivalent unicode normalized usernames should be duplicates
  42. omega_username = 'iamtheΩ' # U+03A9 GREEK CAPITAL LETTER OMEGA
  43. ohm_username = 'iamtheΩ' # U+2126 OHM SIGN
  44. User.objects.create_user(ohm_username)
  45. with self.assertRaises(IntegrityError):
  46. User.objects.create_user(omega_username)
  47. def test_is_anonymous_authenticated_method_deprecation(self):
  48. deprecation_message = (
  49. 'Using user.is_authenticated() and user.is_anonymous() as a '
  50. 'method is deprecated. Remove the parentheses to use it as an '
  51. 'attribute.'
  52. )
  53. u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
  54. # Backwards-compatibility callables
  55. with warnings.catch_warnings(record=True) as warns:
  56. warnings.simplefilter('always')
  57. self.assertFalse(u.is_anonymous())
  58. self.assertEqual(len(warns), 1)
  59. self.assertEqual(str(warns[0].message), deprecation_message)
  60. with warnings.catch_warnings(record=True) as warns:
  61. warnings.simplefilter('always')
  62. self.assertTrue(u.is_authenticated())
  63. self.assertEqual(len(warns), 1)
  64. self.assertEqual(str(warns[0].message), deprecation_message)
  65. def test_user_no_email(self):
  66. "Check that users can be created without an email"
  67. u = User.objects.create_user('testuser1')
  68. self.assertEqual(u.email, '')
  69. u2 = User.objects.create_user('testuser2', email='')
  70. self.assertEqual(u2.email, '')
  71. u3 = User.objects.create_user('testuser3', email=None)
  72. self.assertEqual(u3.email, '')
  73. def test_anonymous_user(self):
  74. "Check the properties of the anonymous user"
  75. a = AnonymousUser()
  76. self.assertEqual(a.pk, None)
  77. self.assertEqual(a.username, '')
  78. self.assertEqual(a.get_username(), '')
  79. self.assertTrue(a.is_anonymous)
  80. self.assertFalse(a.is_authenticated)
  81. self.assertFalse(a.is_staff)
  82. self.assertFalse(a.is_active)
  83. self.assertFalse(a.is_superuser)
  84. self.assertEqual(a.groups.all().count(), 0)
  85. self.assertEqual(a.user_permissions.all().count(), 0)
  86. def test_anonymous_user_is_anonymous_authenticated_method_deprecation(self):
  87. a = AnonymousUser()
  88. deprecation_message = (
  89. 'Using user.is_authenticated() and user.is_anonymous() as a '
  90. 'method is deprecated. Remove the parentheses to use it as an '
  91. 'attribute.'
  92. )
  93. # Backwards-compatibility callables
  94. with warnings.catch_warnings(record=True) as warns:
  95. warnings.simplefilter('always') # prevent warnings from appearing as errors
  96. self.assertTrue(a.is_anonymous())
  97. self.assertEqual(len(warns), 1)
  98. self.assertEqual(str(warns[0].message), deprecation_message)
  99. with warnings.catch_warnings(record=True) as warns:
  100. warnings.simplefilter('always') # prevent warnings from appearing as errors
  101. self.assertFalse(a.is_authenticated())
  102. self.assertEqual(len(warns), 1)
  103. self.assertEqual(str(warns[0].message), deprecation_message)
  104. def test_superuser(self):
  105. "Check the creation and properties of a superuser"
  106. super = User.objects.create_superuser('super', 'super@example.com', 'super')
  107. self.assertTrue(super.is_superuser)
  108. self.assertTrue(super.is_active)
  109. self.assertTrue(super.is_staff)
  110. def test_get_user_model(self):
  111. "The current user model can be retrieved"
  112. self.assertEqual(get_user_model(), User)
  113. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUser')
  114. def test_swappable_user(self):
  115. "The current user model can be swapped out for another"
  116. self.assertEqual(get_user_model(), CustomUser)
  117. with self.assertRaises(AttributeError):
  118. User.objects.all()
  119. @override_settings(AUTH_USER_MODEL='badsetting')
  120. def test_swappable_user_bad_setting(self):
  121. "The alternate user setting must point to something in the format app.model"
  122. with self.assertRaises(ImproperlyConfigured):
  123. get_user_model()
  124. @override_settings(AUTH_USER_MODEL='thismodel.doesntexist')
  125. def test_swappable_user_nonexistent_model(self):
  126. "The current user model must point to an installed model"
  127. with self.assertRaises(ImproperlyConfigured):
  128. get_user_model()
  129. def test_user_verbose_names_translatable(self):
  130. "Default User model verbose names are translatable (#19945)"
  131. with translation.override('en'):
  132. self.assertEqual(User._meta.verbose_name, 'user')
  133. self.assertEqual(User._meta.verbose_name_plural, 'users')
  134. with translation.override('es'):
  135. self.assertEqual(User._meta.verbose_name, 'usuario')
  136. self.assertEqual(User._meta.verbose_name_plural, 'usuarios')