1
from django.contrib.auth import models
2
from django.contrib.auth.management.commands import changepassword
3
from django.core.management import call_command
4
from django.test import TestCase
5
from django.utils.six import StringIO
8
class MultiDBChangepasswordManagementCommandTestCase(TestCase):
12
self.user = models.User.objects.db_manager('other').create_user(username='joe', password='qwerty')
13
self.stdout = StringIO()
18
def test_that_changepassword_command_with_database_option_uses_given_db(self):
20
Executing the changepassword management command with a database option
21
should operate on the specified DB
23
self.assertTrue(self.user.check_password('qwerty'))
24
command = changepassword.Command()
25
command._get_pass = lambda *args: 'not qwerty'
27
command.execute("joe", database='other', stdout=self.stdout)
28
command_output = self.stdout.getvalue().strip()
30
self.assertEqual(command_output, "Changing password for user 'joe'\nPassword changed successfully for user 'joe'")
31
self.assertTrue(models.User.objects.using('other').get(username="joe").check_password("not qwerty"))
34
class MultiDBCreatesuperuserTestCase(TestCase):
37
def test_createsuperuser_command_with_database_option(self):
38
" createsuperuser command should operate on specified DB"
41
call_command("createsuperuser",
44
email="joe@somewhere.org",
48
command_output = new_io.getvalue().strip()
50
self.assertEqual(command_output, 'Superuser created successfully.')
52
u = models.User.objects.using('other').get(username="joe")
53
self.assertEqual(u.email, 'joe@somewhere.org')