test_models.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. from unittest import mock
  2. from django.conf.global_settings import PASSWORD_HASHERS
  3. from django.contrib.auth import get_user_model
  4. from django.contrib.auth.backends import ModelBackend
  5. from django.contrib.auth.base_user import AbstractBaseUser
  6. from django.contrib.auth.hashers import get_hasher
  7. from django.contrib.auth.models import (
  8. AnonymousUser,
  9. Group,
  10. Permission,
  11. User,
  12. UserManager,
  13. )
  14. from django.contrib.contenttypes.models import ContentType
  15. from django.core import mail
  16. from django.db import connection, migrations
  17. from django.db.migrations.state import ModelState, ProjectState
  18. from django.db.models.signals import post_save
  19. from django.test import SimpleTestCase, TestCase, TransactionTestCase, override_settings
  20. from .models import CustomEmailField, IntegerUsernameUser
  21. class NaturalKeysTestCase(TestCase):
  22. def test_user_natural_key(self):
  23. staff_user = User.objects.create_user(username="staff")
  24. self.assertEqual(User.objects.get_by_natural_key("staff"), staff_user)
  25. self.assertEqual(staff_user.natural_key(), ("staff",))
  26. async def test_auser_natural_key(self):
  27. staff_user = await User.objects.acreate_user(username="staff")
  28. self.assertEqual(await User.objects.aget_by_natural_key("staff"), staff_user)
  29. self.assertEqual(staff_user.natural_key(), ("staff",))
  30. def test_group_natural_key(self):
  31. users_group = Group.objects.create(name="users")
  32. self.assertEqual(Group.objects.get_by_natural_key("users"), users_group)
  33. async def test_agroup_natural_key(self):
  34. users_group = await Group.objects.acreate(name="users")
  35. self.assertEqual(await Group.objects.aget_by_natural_key("users"), users_group)
  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. class LoadDataWithNaturalKeysTestCase(TestCase):
  43. fixtures = ["natural.json"]
  44. def test_user_is_created_and_added_to_group(self):
  45. user = User.objects.get(username="my_username")
  46. group = Group.objects.get(name="my_group")
  47. self.assertEqual(group, user.groups.get())
  48. class LoadDataWithNaturalKeysAndMultipleDatabasesTestCase(TestCase):
  49. databases = {"default", "other"}
  50. def test_load_data_with_user_permissions(self):
  51. # Create test contenttypes for both databases
  52. default_objects = [
  53. ContentType.objects.db_manager("default").create(
  54. model="examplemodela",
  55. app_label="app_a",
  56. ),
  57. ContentType.objects.db_manager("default").create(
  58. model="examplemodelb",
  59. app_label="app_b",
  60. ),
  61. ]
  62. other_objects = [
  63. ContentType.objects.db_manager("other").create(
  64. model="examplemodelb",
  65. app_label="app_b",
  66. ),
  67. ContentType.objects.db_manager("other").create(
  68. model="examplemodela",
  69. app_label="app_a",
  70. ),
  71. ]
  72. # Now we create the test UserPermission
  73. Permission.objects.db_manager("default").create(
  74. name="Can delete example model b",
  75. codename="delete_examplemodelb",
  76. content_type=default_objects[1],
  77. )
  78. Permission.objects.db_manager("other").create(
  79. name="Can delete example model b",
  80. codename="delete_examplemodelb",
  81. content_type=other_objects[0],
  82. )
  83. perm_default = Permission.objects.get_by_natural_key(
  84. "delete_examplemodelb",
  85. "app_b",
  86. "examplemodelb",
  87. )
  88. perm_other = Permission.objects.db_manager("other").get_by_natural_key(
  89. "delete_examplemodelb",
  90. "app_b",
  91. "examplemodelb",
  92. )
  93. self.assertEqual(perm_default.content_type_id, default_objects[1].id)
  94. self.assertEqual(perm_other.content_type_id, other_objects[0].id)
  95. class UserManagerTestCase(TransactionTestCase):
  96. available_apps = [
  97. "auth_tests",
  98. "django.contrib.auth",
  99. "django.contrib.contenttypes",
  100. ]
  101. def test_create_user(self):
  102. email_lowercase = "normal@normal.com"
  103. user = User.objects.create_user("user", email_lowercase)
  104. self.assertEqual(user.email, email_lowercase)
  105. self.assertEqual(user.username, "user")
  106. self.assertFalse(user.has_usable_password())
  107. def test_create_user_email_domain_normalize_rfc3696(self):
  108. # According to RFC 3696 Section 3 the "@" symbol can be part of the
  109. # local part of an email address.
  110. returned = UserManager.normalize_email(r"Abc\@DEF@EXAMPLE.com")
  111. self.assertEqual(returned, r"Abc\@DEF@example.com")
  112. def test_create_user_email_domain_normalize(self):
  113. returned = UserManager.normalize_email("normal@DOMAIN.COM")
  114. self.assertEqual(returned, "normal@domain.com")
  115. def test_create_user_email_domain_normalize_with_whitespace(self):
  116. returned = UserManager.normalize_email(r"email\ with_whitespace@D.COM")
  117. self.assertEqual(returned, r"email\ with_whitespace@d.com")
  118. def test_empty_username(self):
  119. with self.assertRaisesMessage(ValueError, "The given username must be set"):
  120. User.objects.create_user(username="")
  121. def test_create_user_is_staff(self):
  122. email = "normal@normal.com"
  123. user = User.objects.create_user("user", email, is_staff=True)
  124. self.assertEqual(user.email, email)
  125. self.assertEqual(user.username, "user")
  126. self.assertTrue(user.is_staff)
  127. def test_create_super_user_raises_error_on_false_is_superuser(self):
  128. with self.assertRaisesMessage(
  129. ValueError, "Superuser must have is_superuser=True."
  130. ):
  131. User.objects.create_superuser(
  132. username="test",
  133. email="test@test.com",
  134. password="test",
  135. is_superuser=False,
  136. )
  137. async def test_acreate_super_user_raises_error_on_false_is_superuser(self):
  138. with self.assertRaisesMessage(
  139. ValueError, "Superuser must have is_superuser=True."
  140. ):
  141. await User.objects.acreate_superuser(
  142. username="test",
  143. email="test@test.com",
  144. password="test",
  145. is_superuser=False,
  146. )
  147. def test_create_superuser_raises_error_on_false_is_staff(self):
  148. with self.assertRaisesMessage(ValueError, "Superuser must have is_staff=True."):
  149. User.objects.create_superuser(
  150. username="test",
  151. email="test@test.com",
  152. password="test",
  153. is_staff=False,
  154. )
  155. async def test_acreate_superuser_raises_error_on_false_is_staff(self):
  156. with self.assertRaisesMessage(ValueError, "Superuser must have is_staff=True."):
  157. await User.objects.acreate_superuser(
  158. username="test",
  159. email="test@test.com",
  160. password="test",
  161. is_staff=False,
  162. )
  163. def test_runpython_manager_methods(self):
  164. def forwards(apps, schema_editor):
  165. UserModel = apps.get_model("auth", "User")
  166. user = UserModel.objects.create_user("user1", password="secure")
  167. self.assertIsInstance(user, UserModel)
  168. operation = migrations.RunPython(forwards, migrations.RunPython.noop)
  169. project_state = ProjectState()
  170. project_state.add_model(ModelState.from_model(User))
  171. project_state.add_model(ModelState.from_model(Group))
  172. project_state.add_model(ModelState.from_model(Permission))
  173. project_state.add_model(ModelState.from_model(ContentType))
  174. new_state = project_state.clone()
  175. with connection.schema_editor() as editor:
  176. operation.state_forwards("test_manager_methods", new_state)
  177. operation.database_forwards(
  178. "test_manager_methods",
  179. editor,
  180. project_state,
  181. new_state,
  182. )
  183. user = User.objects.get(username="user1")
  184. self.assertTrue(user.check_password("secure"))
  185. class AbstractBaseUserTests(SimpleTestCase):
  186. def test_has_usable_password(self):
  187. """
  188. Passwords are usable even if they don't correspond to a hasher in
  189. settings.PASSWORD_HASHERS.
  190. """
  191. self.assertIs(User(password="some-gibbberish").has_usable_password(), True)
  192. def test_normalize_username(self):
  193. self.assertEqual(IntegerUsernameUser().normalize_username(123), 123)
  194. def test_clean_normalize_username(self):
  195. # The normalization happens in AbstractBaseUser.clean()
  196. ohm_username = "iamtheΩ" # U+2126 OHM SIGN
  197. for model in ("auth.User", "auth_tests.CustomUser"):
  198. with self.subTest(model=model), self.settings(AUTH_USER_MODEL=model):
  199. User = get_user_model()
  200. user = User(**{User.USERNAME_FIELD: ohm_username, "password": "foo"})
  201. user.clean()
  202. username = user.get_username()
  203. self.assertNotEqual(username, ohm_username)
  204. self.assertEqual(
  205. username, "iamtheΩ"
  206. ) # U+03A9 GREEK CAPITAL LETTER OMEGA
  207. def test_default_email(self):
  208. self.assertEqual(AbstractBaseUser.get_email_field_name(), "email")
  209. def test_custom_email(self):
  210. user = CustomEmailField()
  211. self.assertEqual(user.get_email_field_name(), "email_address")
  212. class AbstractUserTestCase(TestCase):
  213. def test_email_user(self):
  214. # valid send_mail parameters
  215. kwargs = {
  216. "fail_silently": False,
  217. "auth_user": None,
  218. "auth_password": None,
  219. "connection": None,
  220. "html_message": None,
  221. }
  222. user = User(email="foo@bar.com")
  223. user.email_user(
  224. subject="Subject here",
  225. message="This is a message",
  226. from_email="from@domain.com",
  227. **kwargs,
  228. )
  229. self.assertEqual(len(mail.outbox), 1)
  230. message = mail.outbox[0]
  231. self.assertEqual(message.subject, "Subject here")
  232. self.assertEqual(message.body, "This is a message")
  233. self.assertEqual(message.from_email, "from@domain.com")
  234. self.assertEqual(message.to, [user.email])
  235. def test_last_login_default(self):
  236. user1 = User.objects.create(username="user1")
  237. self.assertIsNone(user1.last_login)
  238. user2 = User.objects.create_user(username="user2")
  239. self.assertIsNone(user2.last_login)
  240. def test_user_clean_normalize_email(self):
  241. user = User(username="user", password="foo", email="foo@BAR.com")
  242. user.clean()
  243. self.assertEqual(user.email, "foo@bar.com")
  244. def test_user_double_save(self):
  245. """
  246. Calling user.save() twice should trigger password_changed() once.
  247. """
  248. user = User.objects.create_user(username="user", password="foo")
  249. user.set_password("bar")
  250. with mock.patch(
  251. "django.contrib.auth.password_validation.password_changed"
  252. ) as pw_changed:
  253. user.save()
  254. self.assertEqual(pw_changed.call_count, 1)
  255. user.save()
  256. self.assertEqual(pw_changed.call_count, 1)
  257. @override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS)
  258. def test_check_password_upgrade(self):
  259. """
  260. password_changed() shouldn't be called if User.check_password()
  261. triggers a hash iteration upgrade.
  262. """
  263. user = User.objects.create_user(username="user", password="foo")
  264. initial_password = user.password
  265. self.assertTrue(user.check_password("foo"))
  266. hasher = get_hasher("default")
  267. self.assertEqual("pbkdf2_sha256", hasher.algorithm)
  268. old_iterations = hasher.iterations
  269. try:
  270. # Upgrade the password iterations
  271. hasher.iterations = old_iterations + 1
  272. with mock.patch(
  273. "django.contrib.auth.password_validation.password_changed"
  274. ) as pw_changed:
  275. user.check_password("foo")
  276. self.assertEqual(pw_changed.call_count, 0)
  277. self.assertNotEqual(initial_password, user.password)
  278. finally:
  279. hasher.iterations = old_iterations
  280. @override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS)
  281. async def test_acheck_password_upgrade(self):
  282. user = await User.objects.acreate_user(username="user", password="foo")
  283. initial_password = user.password
  284. self.assertIs(await user.acheck_password("foo"), True)
  285. hasher = get_hasher("default")
  286. self.assertEqual("pbkdf2_sha256", hasher.algorithm)
  287. old_iterations = hasher.iterations
  288. try:
  289. # Upgrade the password iterations.
  290. hasher.iterations = old_iterations + 1
  291. with mock.patch(
  292. "django.contrib.auth.password_validation.password_changed"
  293. ) as pw_changed:
  294. self.assertIs(await user.acheck_password("foo"), True)
  295. self.assertEqual(pw_changed.call_count, 0)
  296. self.assertNotEqual(initial_password, user.password)
  297. finally:
  298. hasher.iterations = old_iterations
  299. class CustomModelBackend(ModelBackend):
  300. def with_perm(
  301. self, perm, is_active=True, include_superusers=True, backend=None, obj=None
  302. ):
  303. if obj is not None and obj.username == "charliebrown":
  304. return User.objects.filter(pk=obj.pk)
  305. return User.objects.filter(username__startswith="charlie")
  306. class UserWithPermTestCase(TestCase):
  307. @classmethod
  308. def setUpTestData(cls):
  309. content_type = ContentType.objects.get_for_model(Group)
  310. cls.permission = Permission.objects.create(
  311. name="test",
  312. content_type=content_type,
  313. codename="test",
  314. )
  315. # User with permission.
  316. cls.user1 = User.objects.create_user("user 1", "foo@example.com")
  317. cls.user1.user_permissions.add(cls.permission)
  318. # User with group permission.
  319. group1 = Group.objects.create(name="group 1")
  320. group1.permissions.add(cls.permission)
  321. group2 = Group.objects.create(name="group 2")
  322. group2.permissions.add(cls.permission)
  323. cls.user2 = User.objects.create_user("user 2", "bar@example.com")
  324. cls.user2.groups.add(group1, group2)
  325. # Users without permissions.
  326. cls.user_charlie = User.objects.create_user("charlie", "charlie@example.com")
  327. cls.user_charlie_b = User.objects.create_user(
  328. "charliebrown", "charlie@brown.com"
  329. )
  330. # Superuser.
  331. cls.superuser = User.objects.create_superuser(
  332. "superuser",
  333. "superuser@example.com",
  334. "superpassword",
  335. )
  336. # Inactive user with permission.
  337. cls.inactive_user = User.objects.create_user(
  338. "inactive_user",
  339. "baz@example.com",
  340. is_active=False,
  341. )
  342. cls.inactive_user.user_permissions.add(cls.permission)
  343. def test_invalid_permission_name(self):
  344. msg = "Permission name should be in the form app_label.permission_codename."
  345. for perm in ("nodots", "too.many.dots", "...", ""):
  346. with self.subTest(perm), self.assertRaisesMessage(ValueError, msg):
  347. User.objects.with_perm(perm)
  348. def test_invalid_permission_type(self):
  349. msg = "The `perm` argument must be a string or a permission instance."
  350. for perm in (b"auth.test", object(), None):
  351. with self.subTest(perm), self.assertRaisesMessage(TypeError, msg):
  352. User.objects.with_perm(perm)
  353. def test_invalid_backend_type(self):
  354. msg = "backend must be a dotted import path string (got %r)."
  355. for backend in (b"auth_tests.CustomModelBackend", object()):
  356. with self.subTest(backend):
  357. with self.assertRaisesMessage(TypeError, msg % backend):
  358. User.objects.with_perm("auth.test", backend=backend)
  359. def test_basic(self):
  360. active_users = [self.user1, self.user2]
  361. tests = [
  362. ({}, [*active_users, self.superuser]),
  363. ({"obj": self.user1}, []),
  364. # Only inactive users.
  365. ({"is_active": False}, [self.inactive_user]),
  366. # All users.
  367. ({"is_active": None}, [*active_users, self.superuser, self.inactive_user]),
  368. # Exclude superusers.
  369. ({"include_superusers": False}, active_users),
  370. (
  371. {"include_superusers": False, "is_active": False},
  372. [self.inactive_user],
  373. ),
  374. (
  375. {"include_superusers": False, "is_active": None},
  376. [*active_users, self.inactive_user],
  377. ),
  378. ]
  379. for kwargs, expected_users in tests:
  380. for perm in ("auth.test", self.permission):
  381. with self.subTest(perm=perm, **kwargs):
  382. self.assertCountEqual(
  383. User.objects.with_perm(perm, **kwargs),
  384. expected_users,
  385. )
  386. @override_settings(
  387. AUTHENTICATION_BACKENDS=["django.contrib.auth.backends.BaseBackend"]
  388. )
  389. def test_backend_without_with_perm(self):
  390. self.assertSequenceEqual(User.objects.with_perm("auth.test"), [])
  391. def test_nonexistent_permission(self):
  392. self.assertSequenceEqual(User.objects.with_perm("auth.perm"), [self.superuser])
  393. def test_nonexistent_backend(self):
  394. with self.assertRaises(ImportError):
  395. User.objects.with_perm(
  396. "auth.test",
  397. backend="invalid.backend.CustomModelBackend",
  398. )
  399. def test_invalid_backend_submodule(self):
  400. with self.assertRaises(ImportError):
  401. User.objects.with_perm(
  402. "auth.test",
  403. backend="json.tool",
  404. )
  405. @override_settings(
  406. AUTHENTICATION_BACKENDS=["auth_tests.test_models.CustomModelBackend"]
  407. )
  408. def test_custom_backend(self):
  409. for perm in ("auth.test", self.permission):
  410. with self.subTest(perm):
  411. self.assertCountEqual(
  412. User.objects.with_perm(perm),
  413. [self.user_charlie, self.user_charlie_b],
  414. )
  415. @override_settings(
  416. AUTHENTICATION_BACKENDS=["auth_tests.test_models.CustomModelBackend"]
  417. )
  418. def test_custom_backend_pass_obj(self):
  419. for perm in ("auth.test", self.permission):
  420. with self.subTest(perm):
  421. self.assertSequenceEqual(
  422. User.objects.with_perm(perm, obj=self.user_charlie_b),
  423. [self.user_charlie_b],
  424. )
  425. @override_settings(
  426. AUTHENTICATION_BACKENDS=[
  427. "auth_tests.test_models.CustomModelBackend",
  428. "django.contrib.auth.backends.ModelBackend",
  429. ]
  430. )
  431. def test_multiple_backends(self):
  432. msg = (
  433. "You have multiple authentication backends configured and "
  434. "therefore must provide the `backend` argument."
  435. )
  436. with self.assertRaisesMessage(ValueError, msg):
  437. User.objects.with_perm("auth.test")
  438. backend = "auth_tests.test_models.CustomModelBackend"
  439. self.assertCountEqual(
  440. User.objects.with_perm("auth.test", backend=backend),
  441. [self.user_charlie, self.user_charlie_b],
  442. )
  443. class IsActiveTestCase(TestCase):
  444. """
  445. Tests the behavior of the guaranteed is_active attribute
  446. """
  447. def test_builtin_user_isactive(self):
  448. user = User.objects.create(username="foo", email="foo@bar.com")
  449. # is_active is true by default
  450. self.assertIs(user.is_active, True)
  451. user.is_active = False
  452. user.save()
  453. user_fetched = User.objects.get(pk=user.pk)
  454. # the is_active flag is saved
  455. self.assertFalse(user_fetched.is_active)
  456. @override_settings(AUTH_USER_MODEL="auth_tests.IsActiveTestUser1")
  457. def test_is_active_field_default(self):
  458. """
  459. tests that the default value for is_active is provided
  460. """
  461. UserModel = get_user_model()
  462. user = UserModel(username="foo")
  463. self.assertIs(user.is_active, True)
  464. # you can set the attribute - but it will not save
  465. user.is_active = False
  466. # there should be no problem saving - but the attribute is not saved
  467. user.save()
  468. user_fetched = UserModel._default_manager.get(pk=user.pk)
  469. # the attribute is always true for newly retrieved instance
  470. self.assertIs(user_fetched.is_active, True)
  471. class TestCreateSuperUserSignals(TestCase):
  472. """
  473. Simple test case for ticket #20541
  474. """
  475. def post_save_listener(self, *args, **kwargs):
  476. self.signals_count += 1
  477. def setUp(self):
  478. self.signals_count = 0
  479. post_save.connect(self.post_save_listener, sender=User)
  480. self.addCleanup(post_save.disconnect, self.post_save_listener, sender=User)
  481. def test_create_user(self):
  482. User.objects.create_user("JohnDoe")
  483. self.assertEqual(self.signals_count, 1)
  484. def test_create_superuser(self):
  485. User.objects.create_superuser("JohnDoe", "mail@example.com", "1")
  486. self.assertEqual(self.signals_count, 1)
  487. class AnonymousUserTests(SimpleTestCase):
  488. no_repr_msg = "Django doesn't provide a DB representation for AnonymousUser."
  489. def setUp(self):
  490. self.user = AnonymousUser()
  491. def test_properties(self):
  492. self.assertIsNone(self.user.pk)
  493. self.assertEqual(self.user.username, "")
  494. self.assertEqual(self.user.get_username(), "")
  495. self.assertIs(self.user.is_anonymous, True)
  496. self.assertIs(self.user.is_authenticated, False)
  497. self.assertIs(self.user.is_staff, False)
  498. self.assertIs(self.user.is_active, False)
  499. self.assertIs(self.user.is_superuser, False)
  500. self.assertEqual(self.user.groups.count(), 0)
  501. self.assertEqual(self.user.user_permissions.count(), 0)
  502. self.assertEqual(self.user.get_user_permissions(), set())
  503. self.assertEqual(self.user.get_group_permissions(), set())
  504. async def test_properties_async_versions(self):
  505. self.assertEqual(await self.user.groups.acount(), 0)
  506. self.assertEqual(await self.user.user_permissions.acount(), 0)
  507. self.assertEqual(await self.user.aget_user_permissions(), set())
  508. self.assertEqual(await self.user.aget_group_permissions(), set())
  509. def test_str(self):
  510. self.assertEqual(str(self.user), "AnonymousUser")
  511. def test_eq(self):
  512. self.assertEqual(self.user, AnonymousUser())
  513. self.assertNotEqual(self.user, User("super", "super@example.com", "super"))
  514. def test_hash(self):
  515. self.assertEqual(hash(self.user), 1)
  516. def test_int(self):
  517. msg = (
  518. "Cannot cast AnonymousUser to int. Are you trying to use it in "
  519. "place of User?"
  520. )
  521. with self.assertRaisesMessage(TypeError, msg):
  522. int(self.user)
  523. def test_delete(self):
  524. with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg):
  525. self.user.delete()
  526. def test_save(self):
  527. with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg):
  528. self.user.save()
  529. def test_set_password(self):
  530. with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg):
  531. self.user.set_password("password")
  532. def test_check_password(self):
  533. with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg):
  534. self.user.check_password("password")
  535. class GroupTests(SimpleTestCase):
  536. def test_str(self):
  537. g = Group(name="Users")
  538. self.assertEqual(str(g), "Users")
  539. class PermissionTests(TestCase):
  540. def test_str(self):
  541. p = Permission.objects.get(codename="view_customemailfield")
  542. self.assertEqual(
  543. str(p), "Auth_Tests | custom email field | Can view custom email field"
  544. )