test_management.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. import builtins
  2. import getpass
  3. import os
  4. import sys
  5. from datetime import date
  6. from io import StringIO
  7. from unittest import mock
  8. from django.apps import apps
  9. from django.contrib.auth import get_permission_codename, management
  10. from django.contrib.auth.management import (
  11. create_permissions, get_default_username,
  12. )
  13. from django.contrib.auth.management.commands import (
  14. changepassword, createsuperuser,
  15. )
  16. from django.contrib.auth.models import Group, Permission, User
  17. from django.contrib.contenttypes.models import ContentType
  18. from django.core.management import call_command
  19. from django.core.management.base import CommandError
  20. from django.db import migrations
  21. from django.test import TestCase, override_settings
  22. from django.utils.translation import gettext_lazy as _
  23. from .models import (
  24. CustomUser, CustomUserNonUniqueUsername, CustomUserWithFK,
  25. CustomUserWithM2M, Email, Organization, UserProxy,
  26. )
  27. MOCK_INPUT_KEY_TO_PROMPTS = {
  28. # @mock_inputs dict key: [expected prompt messages],
  29. 'bypass': ['Bypass password validation and create user anyway? [y/N]: '],
  30. 'email': ['Email address: '],
  31. 'date_of_birth': ['Date of birth: '],
  32. 'first_name': ['First name: '],
  33. 'username': ['Username: ', lambda: "Username (leave blank to use '%s'): " % get_default_username()],
  34. }
  35. def mock_inputs(inputs):
  36. """
  37. Decorator to temporarily replace input/getpass to allow interactive
  38. createsuperuser.
  39. """
  40. def inner(test_func):
  41. def wrapped(*args):
  42. class mock_getpass:
  43. @staticmethod
  44. def getpass(prompt=b'Password: ', stream=None):
  45. if callable(inputs['password']):
  46. return inputs['password']()
  47. return inputs['password']
  48. def mock_input(prompt):
  49. assert '__proxy__' not in prompt
  50. response = None
  51. for key, val in inputs.items():
  52. if val == 'KeyboardInterrupt':
  53. raise KeyboardInterrupt
  54. # get() fallback because sometimes 'key' is the actual
  55. # prompt rather than a shortcut name.
  56. prompt_msgs = MOCK_INPUT_KEY_TO_PROMPTS.get(key, key)
  57. if isinstance(prompt_msgs, list):
  58. prompt_msgs = [msg() if callable(msg) else msg for msg in prompt_msgs]
  59. if prompt in prompt_msgs:
  60. if callable(val):
  61. response = val()
  62. else:
  63. response = val
  64. break
  65. if response is None:
  66. raise ValueError('Mock input for %r not found.' % prompt)
  67. return response
  68. old_getpass = createsuperuser.getpass
  69. old_input = builtins.input
  70. createsuperuser.getpass = mock_getpass
  71. builtins.input = mock_input
  72. try:
  73. test_func(*args)
  74. finally:
  75. createsuperuser.getpass = old_getpass
  76. builtins.input = old_input
  77. return wrapped
  78. return inner
  79. class MockTTY:
  80. """
  81. A fake stdin object that pretends to be a TTY to be used in conjunction
  82. with mock_inputs.
  83. """
  84. def isatty(self):
  85. return True
  86. class MockInputTests(TestCase):
  87. @mock_inputs({'username': 'alice'})
  88. def test_input_not_found(self):
  89. with self.assertRaisesMessage(ValueError, "Mock input for 'Email address: ' not found."):
  90. call_command('createsuperuser', stdin=MockTTY())
  91. class GetDefaultUsernameTestCase(TestCase):
  92. def setUp(self):
  93. self.old_get_system_username = management.get_system_username
  94. def tearDown(self):
  95. management.get_system_username = self.old_get_system_username
  96. def test_actual_implementation(self):
  97. self.assertIsInstance(management.get_system_username(), str)
  98. def test_simple(self):
  99. management.get_system_username = lambda: 'joe'
  100. self.assertEqual(management.get_default_username(), 'joe')
  101. def test_existing(self):
  102. User.objects.create(username='joe')
  103. management.get_system_username = lambda: 'joe'
  104. self.assertEqual(management.get_default_username(), '')
  105. self.assertEqual(
  106. management.get_default_username(check_db=False), 'joe')
  107. def test_i18n(self):
  108. # 'Julia' with accented 'u':
  109. management.get_system_username = lambda: 'J\xfalia'
  110. self.assertEqual(management.get_default_username(), 'julia')
  111. @override_settings(AUTH_PASSWORD_VALIDATORS=[
  112. {'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
  113. ])
  114. class ChangepasswordManagementCommandTestCase(TestCase):
  115. @classmethod
  116. def setUpTestData(cls):
  117. cls.user = User.objects.create_user(username='joe', password='qwerty')
  118. def setUp(self):
  119. self.stdout = StringIO()
  120. self.stderr = StringIO()
  121. def tearDown(self):
  122. self.stdout.close()
  123. self.stderr.close()
  124. @mock.patch.object(getpass, 'getpass', return_value='password')
  125. def test_get_pass(self, mock_get_pass):
  126. call_command('changepassword', username='joe', stdout=self.stdout)
  127. self.assertIs(User.objects.get(username='joe').check_password('password'), True)
  128. @mock.patch.object(getpass, 'getpass', return_value='')
  129. def test_get_pass_no_input(self, mock_get_pass):
  130. with self.assertRaisesMessage(CommandError, 'aborted'):
  131. call_command('changepassword', username='joe', stdout=self.stdout)
  132. @mock.patch.object(changepassword.Command, '_get_pass', return_value='new_password')
  133. def test_system_username(self, mock_get_pass):
  134. """The system username is used if --username isn't provided."""
  135. username = getpass.getuser()
  136. User.objects.create_user(username=username, password='qwerty')
  137. call_command('changepassword', stdout=self.stdout)
  138. self.assertIs(User.objects.get(username=username).check_password('new_password'), True)
  139. def test_nonexistent_username(self):
  140. with self.assertRaisesMessage(CommandError, "user 'test' does not exist"):
  141. call_command('changepassword', username='test', stdout=self.stdout)
  142. @mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
  143. def test_that_changepassword_command_changes_joes_password(self, mock_get_pass):
  144. "Executing the changepassword management command should change joe's password"
  145. self.assertTrue(self.user.check_password('qwerty'))
  146. call_command('changepassword', username='joe', stdout=self.stdout)
  147. command_output = self.stdout.getvalue().strip()
  148. self.assertEqual(
  149. command_output,
  150. "Changing password for user 'joe'\nPassword changed successfully for user 'joe'"
  151. )
  152. self.assertTrue(User.objects.get(username="joe").check_password("not qwerty"))
  153. @mock.patch.object(changepassword.Command, '_get_pass', side_effect=lambda *args: str(args))
  154. def test_that_max_tries_exits_1(self, mock_get_pass):
  155. """
  156. A CommandError should be thrown by handle() if the user enters in
  157. mismatched passwords three times.
  158. """
  159. msg = "Aborting password change for user 'joe' after 3 attempts"
  160. with self.assertRaisesMessage(CommandError, msg):
  161. call_command('changepassword', username='joe', stdout=self.stdout, stderr=self.stderr)
  162. @mock.patch.object(changepassword.Command, '_get_pass', return_value='1234567890')
  163. def test_password_validation(self, mock_get_pass):
  164. """
  165. A CommandError should be raised if the user enters in passwords which
  166. fail validation three times.
  167. """
  168. abort_msg = "Aborting password change for user 'joe' after 3 attempts"
  169. with self.assertRaisesMessage(CommandError, abort_msg):
  170. call_command('changepassword', username='joe', stdout=self.stdout, stderr=self.stderr)
  171. self.assertIn('This password is entirely numeric.', self.stderr.getvalue())
  172. @mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
  173. def test_that_changepassword_command_works_with_nonascii_output(self, mock_get_pass):
  174. """
  175. #21627 -- Executing the changepassword management command should allow
  176. non-ASCII characters from the User object representation.
  177. """
  178. # 'Julia' with accented 'u':
  179. User.objects.create_user(username='J\xfalia', password='qwerty')
  180. call_command('changepassword', username='J\xfalia', stdout=self.stdout)
  181. class MultiDBChangepasswordManagementCommandTestCase(TestCase):
  182. databases = {'default', 'other'}
  183. @mock.patch.object(changepassword.Command, '_get_pass', return_value='not qwerty')
  184. def test_that_changepassword_command_with_database_option_uses_given_db(self, mock_get_pass):
  185. """
  186. changepassword --database should operate on the specified DB.
  187. """
  188. user = User.objects.db_manager('other').create_user(username='joe', password='qwerty')
  189. self.assertTrue(user.check_password('qwerty'))
  190. out = StringIO()
  191. call_command('changepassword', username='joe', database='other', stdout=out)
  192. command_output = out.getvalue().strip()
  193. self.assertEqual(
  194. command_output,
  195. "Changing password for user 'joe'\nPassword changed successfully for user 'joe'"
  196. )
  197. self.assertTrue(User.objects.using('other').get(username="joe").check_password('not qwerty'))
  198. @override_settings(
  199. SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
  200. AUTH_PASSWORD_VALIDATORS=[{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'}],
  201. )
  202. class CreatesuperuserManagementCommandTestCase(TestCase):
  203. def test_no_email_argument(self):
  204. new_io = StringIO()
  205. with self.assertRaisesMessage(CommandError, 'You must use --email with --noinput.'):
  206. call_command('createsuperuser', interactive=False, username='joe', stdout=new_io)
  207. def test_basic_usage(self):
  208. "Check the operation of the createsuperuser management command"
  209. # We can use the management command to create a superuser
  210. new_io = StringIO()
  211. call_command(
  212. "createsuperuser",
  213. interactive=False,
  214. username="joe",
  215. email="joe@somewhere.org",
  216. stdout=new_io
  217. )
  218. command_output = new_io.getvalue().strip()
  219. self.assertEqual(command_output, 'Superuser created successfully.')
  220. u = User.objects.get(username="joe")
  221. self.assertEqual(u.email, 'joe@somewhere.org')
  222. # created password should be unusable
  223. self.assertFalse(u.has_usable_password())
  224. def test_non_ascii_verbose_name(self):
  225. @mock_inputs({
  226. 'password': "nopasswd",
  227. "Uživatel (leave blank to use '%s'): " % get_default_username(): 'foo', # username (cz)
  228. 'email': 'nolocale@somewhere.org',
  229. })
  230. def test(self):
  231. username_field = User._meta.get_field('username')
  232. old_verbose_name = username_field.verbose_name
  233. username_field.verbose_name = _('u\u017eivatel')
  234. new_io = StringIO()
  235. try:
  236. call_command(
  237. "createsuperuser",
  238. interactive=True,
  239. stdout=new_io,
  240. stdin=MockTTY(),
  241. )
  242. finally:
  243. username_field.verbose_name = old_verbose_name
  244. command_output = new_io.getvalue().strip()
  245. self.assertEqual(command_output, 'Superuser created successfully.')
  246. test(self)
  247. def test_verbosity_zero(self):
  248. # We can suppress output on the management command
  249. new_io = StringIO()
  250. call_command(
  251. "createsuperuser",
  252. interactive=False,
  253. username="joe2",
  254. email="joe2@somewhere.org",
  255. verbosity=0,
  256. stdout=new_io
  257. )
  258. command_output = new_io.getvalue().strip()
  259. self.assertEqual(command_output, '')
  260. u = User.objects.get(username="joe2")
  261. self.assertEqual(u.email, 'joe2@somewhere.org')
  262. self.assertFalse(u.has_usable_password())
  263. def test_email_in_username(self):
  264. new_io = StringIO()
  265. call_command(
  266. "createsuperuser",
  267. interactive=False,
  268. username="joe+admin@somewhere.org",
  269. email="joe@somewhere.org",
  270. stdout=new_io
  271. )
  272. u = User._default_manager.get(username="joe+admin@somewhere.org")
  273. self.assertEqual(u.email, 'joe@somewhere.org')
  274. self.assertFalse(u.has_usable_password())
  275. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUser')
  276. def test_swappable_user(self):
  277. "A superuser can be created when a custom user model is in use"
  278. # We can use the management command to create a superuser
  279. # We skip validation because the temporary substitution of the
  280. # swappable User model messes with validation.
  281. new_io = StringIO()
  282. call_command(
  283. "createsuperuser",
  284. interactive=False,
  285. email="joe@somewhere.org",
  286. date_of_birth="1976-04-01",
  287. first_name='Joe',
  288. stdout=new_io,
  289. )
  290. command_output = new_io.getvalue().strip()
  291. self.assertEqual(command_output, 'Superuser created successfully.')
  292. u = CustomUser._default_manager.get(email="joe@somewhere.org")
  293. self.assertEqual(u.date_of_birth, date(1976, 4, 1))
  294. # created password should be unusable
  295. self.assertFalse(u.has_usable_password())
  296. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUser')
  297. def test_swappable_user_missing_required_field(self):
  298. "A Custom superuser won't be created when a required field isn't provided"
  299. # We can use the management command to create a superuser
  300. # We skip validation because the temporary substitution of the
  301. # swappable User model messes with validation.
  302. new_io = StringIO()
  303. with self.assertRaisesMessage(CommandError, 'You must use --email with --noinput.'):
  304. call_command(
  305. "createsuperuser",
  306. interactive=False,
  307. stdout=new_io,
  308. stderr=new_io,
  309. )
  310. self.assertEqual(CustomUser._default_manager.count(), 0)
  311. @override_settings(
  312. AUTH_USER_MODEL='auth_tests.CustomUserNonUniqueUsername',
  313. AUTHENTICATION_BACKENDS=['my.custom.backend'],
  314. )
  315. def test_swappable_user_username_non_unique(self):
  316. @mock_inputs({
  317. 'username': 'joe',
  318. 'password': 'nopasswd',
  319. })
  320. def createsuperuser():
  321. new_io = StringIO()
  322. call_command(
  323. "createsuperuser",
  324. interactive=True,
  325. email="joe@somewhere.org",
  326. stdout=new_io,
  327. stdin=MockTTY(),
  328. )
  329. command_output = new_io.getvalue().strip()
  330. self.assertEqual(command_output, 'Superuser created successfully.')
  331. for i in range(2):
  332. createsuperuser()
  333. users = CustomUserNonUniqueUsername.objects.filter(username="joe")
  334. self.assertEqual(users.count(), 2)
  335. def test_skip_if_not_in_TTY(self):
  336. """
  337. If the command is not called from a TTY, it should be skipped and a
  338. message should be displayed (#7423).
  339. """
  340. class FakeStdin:
  341. """A fake stdin object that has isatty() return False."""
  342. def isatty(self):
  343. return False
  344. out = StringIO()
  345. call_command(
  346. "createsuperuser",
  347. stdin=FakeStdin(),
  348. stdout=out,
  349. interactive=True,
  350. )
  351. self.assertEqual(User._default_manager.count(), 0)
  352. self.assertIn("Superuser creation skipped", out.getvalue())
  353. def test_passing_stdin(self):
  354. """
  355. You can pass a stdin object as an option and it should be
  356. available on self.stdin.
  357. If no such option is passed, it defaults to sys.stdin.
  358. """
  359. sentinel = object()
  360. command = createsuperuser.Command()
  361. call_command(
  362. command,
  363. stdin=sentinel,
  364. stdout=StringIO(),
  365. stderr=StringIO(),
  366. interactive=False,
  367. verbosity=0,
  368. username='janet',
  369. email='janet@example.com',
  370. )
  371. self.assertIs(command.stdin, sentinel)
  372. command = createsuperuser.Command()
  373. call_command(
  374. command,
  375. stdout=StringIO(),
  376. stderr=StringIO(),
  377. interactive=False,
  378. verbosity=0,
  379. username='joe',
  380. email='joe@example.com',
  381. )
  382. self.assertIs(command.stdin, sys.stdin)
  383. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithFK')
  384. def test_fields_with_fk(self):
  385. new_io = StringIO()
  386. group = Group.objects.create(name='mygroup')
  387. email = Email.objects.create(email='mymail@gmail.com')
  388. call_command(
  389. 'createsuperuser',
  390. interactive=False,
  391. username=email.pk,
  392. email=email.email,
  393. group=group.pk,
  394. stdout=new_io,
  395. )
  396. command_output = new_io.getvalue().strip()
  397. self.assertEqual(command_output, 'Superuser created successfully.')
  398. u = CustomUserWithFK._default_manager.get(email=email)
  399. self.assertEqual(u.username, email)
  400. self.assertEqual(u.group, group)
  401. non_existent_email = 'mymail2@gmail.com'
  402. msg = 'email instance with email %r does not exist.' % non_existent_email
  403. with self.assertRaisesMessage(CommandError, msg):
  404. call_command(
  405. 'createsuperuser',
  406. interactive=False,
  407. username=email.pk,
  408. email=non_existent_email,
  409. stdout=new_io,
  410. )
  411. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithFK')
  412. def test_fields_with_fk_interactive(self):
  413. new_io = StringIO()
  414. group = Group.objects.create(name='mygroup')
  415. email = Email.objects.create(email='mymail@gmail.com')
  416. @mock_inputs({
  417. 'password': 'nopasswd',
  418. 'Username (Email.id): ': email.pk,
  419. 'Email (Email.email): ': email.email,
  420. 'Group (Group.id): ': group.pk,
  421. })
  422. def test(self):
  423. call_command(
  424. 'createsuperuser',
  425. interactive=True,
  426. stdout=new_io,
  427. stdin=MockTTY(),
  428. )
  429. command_output = new_io.getvalue().strip()
  430. self.assertEqual(command_output, 'Superuser created successfully.')
  431. u = CustomUserWithFK._default_manager.get(email=email)
  432. self.assertEqual(u.username, email)
  433. self.assertEqual(u.group, group)
  434. test(self)
  435. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithM2m')
  436. def test_fields_with_m2m(self):
  437. new_io = StringIO()
  438. org_id_1 = Organization.objects.create(name='Organization 1').pk
  439. org_id_2 = Organization.objects.create(name='Organization 2').pk
  440. call_command(
  441. 'createsuperuser',
  442. interactive=False,
  443. username='joe',
  444. orgs=[org_id_1, org_id_2],
  445. stdout=new_io,
  446. )
  447. command_output = new_io.getvalue().strip()
  448. self.assertEqual(command_output, 'Superuser created successfully.')
  449. user = CustomUserWithM2M._default_manager.get(username='joe')
  450. self.assertEqual(user.orgs.count(), 2)
  451. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithM2M')
  452. def test_fields_with_m2m_interactive(self):
  453. new_io = StringIO()
  454. org_id_1 = Organization.objects.create(name='Organization 1').pk
  455. org_id_2 = Organization.objects.create(name='Organization 2').pk
  456. @mock_inputs({
  457. 'password': 'nopasswd',
  458. 'Username: ': 'joe',
  459. 'Orgs (Organization.id): ': '%s, %s' % (org_id_1, org_id_2),
  460. })
  461. def test(self):
  462. call_command(
  463. 'createsuperuser',
  464. interactive=True,
  465. stdout=new_io,
  466. stdin=MockTTY(),
  467. )
  468. command_output = new_io.getvalue().strip()
  469. self.assertEqual(command_output, 'Superuser created successfully.')
  470. user = CustomUserWithM2M._default_manager.get(username='joe')
  471. self.assertEqual(user.orgs.count(), 2)
  472. test(self)
  473. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithM2M')
  474. def test_fields_with_m2m_interactive_blank(self):
  475. new_io = StringIO()
  476. org_id = Organization.objects.create(name='Organization').pk
  477. entered_orgs = [str(org_id), ' ']
  478. def return_orgs():
  479. return entered_orgs.pop()
  480. @mock_inputs({
  481. 'password': 'nopasswd',
  482. 'Username: ': 'joe',
  483. 'Orgs (Organization.id): ': return_orgs,
  484. })
  485. def test(self):
  486. call_command(
  487. 'createsuperuser',
  488. interactive=True,
  489. stdout=new_io,
  490. stderr=new_io,
  491. stdin=MockTTY(),
  492. )
  493. self.assertEqual(
  494. new_io.getvalue().strip(),
  495. 'Error: This field cannot be blank.\n'
  496. 'Superuser created successfully.',
  497. )
  498. test(self)
  499. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithM2MThrough')
  500. def test_fields_with_m2m_and_through(self):
  501. msg = (
  502. "Required field 'orgs' specifies a many-to-many relation through "
  503. "model, which is not supported."
  504. )
  505. with self.assertRaisesMessage(CommandError, msg):
  506. call_command('createsuperuser')
  507. def test_default_username(self):
  508. """createsuperuser uses a default username when one isn't provided."""
  509. # Get the default username before creating a user.
  510. default_username = get_default_username()
  511. new_io = StringIO()
  512. entered_passwords = ['password', 'password']
  513. def return_passwords():
  514. return entered_passwords.pop(0)
  515. @mock_inputs({'password': return_passwords, 'username': '', 'email': ''})
  516. def test(self):
  517. call_command(
  518. 'createsuperuser',
  519. interactive=True,
  520. stdin=MockTTY(),
  521. stdout=new_io,
  522. stderr=new_io,
  523. )
  524. self.assertEqual(new_io.getvalue().strip(), 'Superuser created successfully.')
  525. self.assertTrue(User.objects.filter(username=default_username).exists())
  526. test(self)
  527. def test_password_validation(self):
  528. """
  529. Creation should fail if the password fails validation.
  530. """
  531. new_io = StringIO()
  532. entered_passwords = ['1234567890', '1234567890', 'password', 'password']
  533. def bad_then_good_password():
  534. return entered_passwords.pop(0)
  535. @mock_inputs({
  536. 'password': bad_then_good_password,
  537. 'username': 'joe1234567890',
  538. 'email': '',
  539. 'bypass': 'n',
  540. })
  541. def test(self):
  542. call_command(
  543. "createsuperuser",
  544. interactive=True,
  545. stdin=MockTTY(),
  546. stdout=new_io,
  547. stderr=new_io,
  548. )
  549. self.assertEqual(
  550. new_io.getvalue().strip(),
  551. "This password is entirely numeric.\n"
  552. "Superuser created successfully."
  553. )
  554. test(self)
  555. @override_settings(AUTH_PASSWORD_VALIDATORS=[
  556. {'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'},
  557. ])
  558. def test_validate_password_against_username(self):
  559. new_io = StringIO()
  560. username = 'supremelycomplex'
  561. entered_passwords = [username, username, 'superduperunguessablepassword', 'superduperunguessablepassword']
  562. def bad_then_good_password():
  563. return entered_passwords.pop(0)
  564. @mock_inputs({
  565. 'password': bad_then_good_password,
  566. 'username': username,
  567. 'email': '',
  568. 'bypass': 'n',
  569. })
  570. def test(self):
  571. call_command(
  572. 'createsuperuser',
  573. interactive=True,
  574. stdin=MockTTY(),
  575. stdout=new_io,
  576. stderr=new_io,
  577. )
  578. self.assertEqual(
  579. new_io.getvalue().strip(),
  580. 'The password is too similar to the username.\n'
  581. 'Superuser created successfully.'
  582. )
  583. test(self)
  584. @override_settings(
  585. AUTH_USER_MODEL='auth_tests.CustomUser',
  586. AUTH_PASSWORD_VALIDATORS=[
  587. {'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'},
  588. ]
  589. )
  590. def test_validate_password_against_required_fields(self):
  591. new_io = StringIO()
  592. first_name = 'josephine'
  593. entered_passwords = [first_name, first_name, 'superduperunguessablepassword', 'superduperunguessablepassword']
  594. def bad_then_good_password():
  595. return entered_passwords.pop(0)
  596. @mock_inputs({
  597. 'password': bad_then_good_password,
  598. 'username': 'whatever',
  599. 'first_name': first_name,
  600. 'date_of_birth': '1970-01-01',
  601. 'email': 'joey@example.com',
  602. 'bypass': 'n',
  603. })
  604. def test(self):
  605. call_command(
  606. 'createsuperuser',
  607. interactive=True,
  608. stdin=MockTTY(),
  609. stdout=new_io,
  610. stderr=new_io,
  611. )
  612. self.assertEqual(
  613. new_io.getvalue().strip(),
  614. "The password is too similar to the first name.\n"
  615. "Superuser created successfully."
  616. )
  617. test(self)
  618. def test_blank_username(self):
  619. """Creation fails if --username is blank."""
  620. new_io = StringIO()
  621. def test(self):
  622. with self.assertRaisesMessage(CommandError, 'Username cannot be blank.'):
  623. call_command(
  624. 'createsuperuser',
  625. username='',
  626. stdin=MockTTY(),
  627. stdout=new_io,
  628. stderr=new_io,
  629. )
  630. test(self)
  631. def test_blank_username_non_interactive(self):
  632. new_io = StringIO()
  633. def test(self):
  634. with self.assertRaisesMessage(CommandError, 'Username cannot be blank.'):
  635. call_command(
  636. 'createsuperuser',
  637. username='',
  638. interactive=False,
  639. stdin=MockTTY(),
  640. stdout=new_io,
  641. stderr=new_io,
  642. )
  643. test(self)
  644. def test_password_validation_bypass(self):
  645. """
  646. Password validation can be bypassed by entering 'y' at the prompt.
  647. """
  648. new_io = StringIO()
  649. @mock_inputs({
  650. 'password': '1234567890',
  651. 'username': 'joe1234567890',
  652. 'email': '',
  653. 'bypass': 'y',
  654. })
  655. def test(self):
  656. call_command(
  657. 'createsuperuser',
  658. interactive=True,
  659. stdin=MockTTY(),
  660. stdout=new_io,
  661. stderr=new_io,
  662. )
  663. self.assertEqual(
  664. new_io.getvalue().strip(),
  665. 'This password is entirely numeric.\n'
  666. 'Superuser created successfully.'
  667. )
  668. test(self)
  669. def test_invalid_username(self):
  670. """Creation fails if the username fails validation."""
  671. user_field = User._meta.get_field(User.USERNAME_FIELD)
  672. new_io = StringIO()
  673. entered_passwords = ['password', 'password']
  674. # Enter an invalid (too long) username first and then a valid one.
  675. invalid_username = ('x' * user_field.max_length) + 'y'
  676. entered_usernames = [invalid_username, 'janet']
  677. def return_passwords():
  678. return entered_passwords.pop(0)
  679. def return_usernames():
  680. return entered_usernames.pop(0)
  681. @mock_inputs({'password': return_passwords, 'username': return_usernames, 'email': ''})
  682. def test(self):
  683. call_command(
  684. 'createsuperuser',
  685. interactive=True,
  686. stdin=MockTTY(),
  687. stdout=new_io,
  688. stderr=new_io,
  689. )
  690. self.assertEqual(
  691. new_io.getvalue().strip(),
  692. 'Error: Ensure this value has at most %s characters (it has %s).\n'
  693. 'Superuser created successfully.' % (user_field.max_length, len(invalid_username))
  694. )
  695. test(self)
  696. @mock_inputs({'username': 'KeyboardInterrupt'})
  697. def test_keyboard_interrupt(self):
  698. new_io = StringIO()
  699. with self.assertRaises(SystemExit):
  700. call_command(
  701. 'createsuperuser',
  702. interactive=True,
  703. stdin=MockTTY(),
  704. stdout=new_io,
  705. stderr=new_io,
  706. )
  707. self.assertEqual(new_io.getvalue(), '\nOperation cancelled.\n')
  708. def test_existing_username(self):
  709. """Creation fails if the username already exists."""
  710. user = User.objects.create(username='janet')
  711. new_io = StringIO()
  712. entered_passwords = ['password', 'password']
  713. # Enter the existing username first and then a new one.
  714. entered_usernames = [user.username, 'joe']
  715. def return_passwords():
  716. return entered_passwords.pop(0)
  717. def return_usernames():
  718. return entered_usernames.pop(0)
  719. @mock_inputs({'password': return_passwords, 'username': return_usernames, 'email': ''})
  720. def test(self):
  721. call_command(
  722. 'createsuperuser',
  723. interactive=True,
  724. stdin=MockTTY(),
  725. stdout=new_io,
  726. stderr=new_io,
  727. )
  728. self.assertEqual(
  729. new_io.getvalue().strip(),
  730. 'Error: That username is already taken.\n'
  731. 'Superuser created successfully.'
  732. )
  733. test(self)
  734. def test_existing_username_non_interactive(self):
  735. """Creation fails if the username already exists."""
  736. User.objects.create(username='janet')
  737. new_io = StringIO()
  738. with self.assertRaisesMessage(CommandError, "Error: That username is already taken."):
  739. call_command(
  740. 'createsuperuser',
  741. username='janet',
  742. email='',
  743. interactive=False,
  744. stdout=new_io,
  745. )
  746. def test_existing_username_provided_via_option_and_interactive(self):
  747. """call_command() gets username='janet' and interactive=True."""
  748. new_io = StringIO()
  749. entered_passwords = ['password', 'password']
  750. User.objects.create(username='janet')
  751. def return_passwords():
  752. return entered_passwords.pop(0)
  753. @mock_inputs({
  754. 'password': return_passwords,
  755. 'username': 'janet1',
  756. 'email': 'test@test.com'
  757. })
  758. def test(self):
  759. call_command(
  760. 'createsuperuser',
  761. username='janet',
  762. interactive=True,
  763. stdin=MockTTY(),
  764. stdout=new_io,
  765. stderr=new_io,
  766. )
  767. msg = 'Error: That username is already taken.\nSuperuser created successfully.'
  768. self.assertEqual(new_io.getvalue().strip(), msg)
  769. test(self)
  770. def test_validation_mismatched_passwords(self):
  771. """
  772. Creation should fail if the user enters mismatched passwords.
  773. """
  774. new_io = StringIO()
  775. # The first two passwords do not match, but the second two do match and
  776. # are valid.
  777. entered_passwords = ["password", "not password", "password2", "password2"]
  778. def mismatched_passwords_then_matched():
  779. return entered_passwords.pop(0)
  780. @mock_inputs({
  781. 'password': mismatched_passwords_then_matched,
  782. 'username': 'joe1234567890',
  783. 'email': '',
  784. })
  785. def test(self):
  786. call_command(
  787. "createsuperuser",
  788. interactive=True,
  789. stdin=MockTTY(),
  790. stdout=new_io,
  791. stderr=new_io,
  792. )
  793. self.assertEqual(
  794. new_io.getvalue().strip(),
  795. "Error: Your passwords didn't match.\n"
  796. "Superuser created successfully."
  797. )
  798. test(self)
  799. def test_validation_blank_password_entered(self):
  800. """
  801. Creation should fail if the user enters blank passwords.
  802. """
  803. new_io = StringIO()
  804. # The first two passwords are empty strings, but the second two are
  805. # valid.
  806. entered_passwords = ["", "", "password2", "password2"]
  807. def blank_passwords_then_valid():
  808. return entered_passwords.pop(0)
  809. @mock_inputs({
  810. 'password': blank_passwords_then_valid,
  811. 'username': 'joe1234567890',
  812. 'email': '',
  813. })
  814. def test(self):
  815. call_command(
  816. "createsuperuser",
  817. interactive=True,
  818. stdin=MockTTY(),
  819. stdout=new_io,
  820. stderr=new_io,
  821. )
  822. self.assertEqual(
  823. new_io.getvalue().strip(),
  824. "Error: Blank passwords aren't allowed.\n"
  825. "Superuser created successfully."
  826. )
  827. test(self)
  828. @override_settings(AUTH_USER_MODEL='auth_tests.NoPasswordUser')
  829. def test_usermodel_without_password(self):
  830. new_io = StringIO()
  831. def test(self):
  832. call_command(
  833. 'createsuperuser',
  834. interactive=False,
  835. stdin=MockTTY(),
  836. stdout=new_io,
  837. stderr=new_io,
  838. username='username',
  839. )
  840. self.assertEqual(new_io.getvalue().strip(), 'Superuser created successfully.')
  841. test(self)
  842. @override_settings(AUTH_USER_MODEL='auth_tests.NoPasswordUser')
  843. def test_usermodel_without_password_interactive(self):
  844. new_io = StringIO()
  845. @mock_inputs({'username': 'username'})
  846. def test(self):
  847. call_command(
  848. 'createsuperuser',
  849. interactive=True,
  850. stdin=MockTTY(),
  851. stdout=new_io,
  852. stderr=new_io,
  853. )
  854. self.assertEqual(new_io.getvalue().strip(), 'Superuser created successfully.')
  855. test(self)
  856. @mock.patch.dict(os.environ, {
  857. 'DJANGO_SUPERUSER_PASSWORD': 'test_password',
  858. 'DJANGO_SUPERUSER_USERNAME': 'test_superuser',
  859. 'DJANGO_SUPERUSER_EMAIL': 'joe@somewhere.org',
  860. 'DJANGO_SUPERUSER_FIRST_NAME': 'ignored_first_name',
  861. })
  862. def test_environment_variable_non_interactive(self):
  863. call_command('createsuperuser', interactive=False, stdout=StringIO())
  864. user = User.objects.get(username='test_superuser')
  865. self.assertEqual(user.email, 'joe@somewhere.org')
  866. self.assertTrue(user.check_password('test_password'))
  867. # Environment variables are ignored for non-required fields.
  868. self.assertEqual(user.first_name, '')
  869. @mock.patch.dict(os.environ, {
  870. 'DJANGO_SUPERUSER_USERNAME': 'test_superuser',
  871. 'DJANGO_SUPERUSER_EMAIL': 'joe@somewhere.org',
  872. })
  873. def test_ignore_environment_variable_non_interactive(self):
  874. # Environment variables are ignored in non-interactive mode, if
  875. # provided by a command line arguments.
  876. call_command(
  877. 'createsuperuser',
  878. interactive=False,
  879. username='cmd_superuser',
  880. email='cmd@somewhere.org',
  881. stdout=StringIO(),
  882. )
  883. user = User.objects.get(username='cmd_superuser')
  884. self.assertEqual(user.email, 'cmd@somewhere.org')
  885. self.assertFalse(user.has_usable_password())
  886. @mock.patch.dict(os.environ, {
  887. 'DJANGO_SUPERUSER_PASSWORD': 'test_password',
  888. 'DJANGO_SUPERUSER_USERNAME': 'test_superuser',
  889. 'DJANGO_SUPERUSER_EMAIL': 'joe@somewhere.org',
  890. })
  891. def test_ignore_environment_variable_interactive(self):
  892. # Environment variables are ignored in interactive mode.
  893. @mock_inputs({'password': 'cmd_password'})
  894. def test(self):
  895. call_command(
  896. 'createsuperuser',
  897. interactive=True,
  898. username='cmd_superuser',
  899. email='cmd@somewhere.org',
  900. stdin=MockTTY(),
  901. stdout=StringIO(),
  902. )
  903. user = User.objects.get(username='cmd_superuser')
  904. self.assertEqual(user.email, 'cmd@somewhere.org')
  905. self.assertTrue(user.check_password('cmd_password'))
  906. test(self)
  907. class MultiDBCreatesuperuserTestCase(TestCase):
  908. databases = {'default', 'other'}
  909. def test_createsuperuser_command_with_database_option(self):
  910. """
  911. changepassword --database should operate on the specified DB.
  912. """
  913. new_io = StringIO()
  914. call_command(
  915. 'createsuperuser',
  916. interactive=False,
  917. username='joe',
  918. email='joe@somewhere.org',
  919. database='other',
  920. stdout=new_io,
  921. )
  922. command_output = new_io.getvalue().strip()
  923. self.assertEqual(command_output, 'Superuser created successfully.')
  924. user = User.objects.using('other').get(username='joe')
  925. self.assertEqual(user.email, 'joe@somewhere.org')
  926. class CreatePermissionsTests(TestCase):
  927. def setUp(self):
  928. self._original_permissions = Permission._meta.permissions[:]
  929. self._original_default_permissions = Permission._meta.default_permissions
  930. self.app_config = apps.get_app_config('auth')
  931. def tearDown(self):
  932. Permission._meta.permissions = self._original_permissions
  933. Permission._meta.default_permissions = self._original_default_permissions
  934. ContentType.objects.clear_cache()
  935. def test_default_permissions(self):
  936. permission_content_type = ContentType.objects.get_by_natural_key('auth', 'permission')
  937. Permission._meta.permissions = [
  938. ('my_custom_permission', 'Some permission'),
  939. ]
  940. create_permissions(self.app_config, verbosity=0)
  941. # view/add/change/delete permission by default + custom permission
  942. self.assertEqual(Permission.objects.filter(
  943. content_type=permission_content_type,
  944. ).count(), 5)
  945. Permission.objects.filter(content_type=permission_content_type).delete()
  946. Permission._meta.default_permissions = []
  947. create_permissions(self.app_config, verbosity=0)
  948. # custom permission only since default permissions is empty
  949. self.assertEqual(Permission.objects.filter(
  950. content_type=permission_content_type,
  951. ).count(), 1)
  952. def test_unavailable_models(self):
  953. """
  954. #24075 - Permissions shouldn't be created or deleted if the ContentType
  955. or Permission models aren't available.
  956. """
  957. state = migrations.state.ProjectState()
  958. # Unavailable contenttypes.ContentType
  959. with self.assertNumQueries(0):
  960. create_permissions(self.app_config, verbosity=0, apps=state.apps)
  961. # Unavailable auth.Permission
  962. state = migrations.state.ProjectState(real_apps=['contenttypes'])
  963. with self.assertNumQueries(0):
  964. create_permissions(self.app_config, verbosity=0, apps=state.apps)
  965. def test_create_permissions_checks_contenttypes_created(self):
  966. """
  967. `post_migrate` handler ordering isn't guaranteed. Simulate a case
  968. where create_permissions() is called before create_contenttypes().
  969. """
  970. # Warm the manager cache.
  971. ContentType.objects.get_for_model(Group)
  972. # Apply a deletion as if e.g. a database 'flush' had been executed.
  973. ContentType.objects.filter(app_label='auth', model='group').delete()
  974. # This fails with a foreign key constraint without the fix.
  975. create_permissions(apps.get_app_config('auth'), interactive=False, verbosity=0)
  976. def test_permission_with_proxy_content_type_created(self):
  977. """
  978. A proxy model's permissions use its own content type rather than the
  979. content type of the concrete model.
  980. """
  981. opts = UserProxy._meta
  982. codename = get_permission_codename('add', opts)
  983. self.assertTrue(
  984. Permission.objects.filter(
  985. content_type__model=opts.model_name,
  986. content_type__app_label=opts.app_label,
  987. codename=codename,
  988. ).exists()
  989. )