2012-03-10 17:36:41 +00:00
|
|
|
from django.contrib.auth import models
|
|
|
|
from django.contrib.auth.management.commands import changepassword
|
|
|
|
from django.core.management import call_command
|
|
|
|
from django.test import TestCase
|
2012-08-07 13:41:54 +00:00
|
|
|
from django.utils.six import StringIO
|
2012-03-10 17:36:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MultiDBChangepasswordManagementCommandTestCase(TestCase):
|
|
|
|
multi_db = True
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.user = models.User.objects.db_manager('other').create_user(username='joe', password='qwerty')
|
|
|
|
|
|
|
|
def test_that_changepassword_command_with_database_option_uses_given_db(self):
|
|
|
|
"""
|
|
|
|
Executing the changepassword management command with a database option
|
|
|
|
should operate on the specified DB
|
|
|
|
"""
|
|
|
|
self.assertTrue(self.user.check_password('qwerty'))
|
|
|
|
command = changepassword.Command()
|
|
|
|
command._get_pass = lambda *args: 'not qwerty'
|
|
|
|
|
2014-10-21 17:15:10 +00:00
|
|
|
out = StringIO()
|
|
|
|
command.execute(username="joe", database='other', stdout=out)
|
|
|
|
command_output = out.getvalue().strip()
|
2012-03-10 17:36:41 +00:00
|
|
|
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertEqual(
|
|
|
|
command_output,
|
|
|
|
"Changing password for user 'joe'\nPassword changed successfully for user 'joe'"
|
|
|
|
)
|
2012-03-10 17:36:41 +00:00
|
|
|
self.assertTrue(models.User.objects.using('other').get(username="joe").check_password("not qwerty"))
|
|
|
|
|
|
|
|
|
|
|
|
class MultiDBCreatesuperuserTestCase(TestCase):
|
|
|
|
multi_db = True
|
|
|
|
|
|
|
|
def test_createsuperuser_command_with_database_option(self):
|
|
|
|
" createsuperuser command should operate on specified DB"
|
|
|
|
new_io = StringIO()
|
|
|
|
|
2013-12-08 17:20:06 +00:00
|
|
|
call_command(
|
|
|
|
"createsuperuser",
|
2012-03-10 17:36:41 +00:00
|
|
|
interactive=False,
|
|
|
|
username="joe",
|
|
|
|
email="joe@somewhere.org",
|
|
|
|
database='other',
|
|
|
|
stdout=new_io
|
|
|
|
)
|
|
|
|
command_output = new_io.getvalue().strip()
|
|
|
|
|
|
|
|
self.assertEqual(command_output, 'Superuser created successfully.')
|
|
|
|
|
|
|
|
u = models.User.objects.using('other').get(username="joe")
|
|
|
|
self.assertEqual(u.email, 'joe@somewhere.org')
|
|
|
|
|
|
|
|
new_io.close()
|