2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-03-05 12:17:55 +08:00
|
|
|
from StringIO import StringIO
|
|
|
|
|
2011-05-09 09:10:40 +08:00
|
|
|
from django.contrib.auth import models, management
|
2012-03-05 12:17:55 +08:00
|
|
|
from django.contrib.auth.management.commands import changepassword
|
2012-05-27 02:50:44 +08:00
|
|
|
from django.core.management.base import CommandError
|
2012-03-05 12:17:55 +08:00
|
|
|
from django.test import TestCase
|
2011-05-09 09:10:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GetDefaultUsernameTestCase(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._getpass_getuser = management.get_system_username
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
management.get_system_username = self._getpass_getuser
|
|
|
|
|
|
|
|
def test_simple(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
management.get_system_username = lambda: 'joe'
|
2011-05-09 09:10:40 +08:00
|
|
|
self.assertEqual(management.get_default_username(), 'joe')
|
|
|
|
|
|
|
|
def test_existing(self):
|
|
|
|
models.User.objects.create(username='joe')
|
2012-06-08 00:08:47 +08:00
|
|
|
management.get_system_username = lambda: 'joe'
|
2011-05-09 09:10:40 +08:00
|
|
|
self.assertEqual(management.get_default_username(), '')
|
|
|
|
self.assertEqual(
|
|
|
|
management.get_default_username(check_db=False), 'joe')
|
|
|
|
|
|
|
|
def test_i18n(self):
|
|
|
|
# 'Julia' with accented 'u':
|
2012-06-08 00:08:47 +08:00
|
|
|
management.get_system_username = lambda: 'J\xfalia'
|
2011-05-09 09:10:40 +08:00
|
|
|
self.assertEqual(management.get_default_username(), 'julia')
|
2012-03-05 12:17:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ChangepasswordManagementCommandTestCase(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.user = models.User.objects.create_user(username='joe', password='qwerty')
|
|
|
|
self.stdout = StringIO()
|
|
|
|
self.stderr = StringIO()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.stdout.close()
|
|
|
|
self.stderr.close()
|
|
|
|
|
|
|
|
def test_that_changepassword_command_changes_joes_password(self):
|
|
|
|
" Executing the changepassword management command should change joe's password "
|
|
|
|
self.assertTrue(self.user.check_password('qwerty'))
|
|
|
|
command = changepassword.Command()
|
|
|
|
command._get_pass = lambda *args: 'not qwerty'
|
|
|
|
|
|
|
|
command.execute("joe", stdout=self.stdout)
|
|
|
|
command_output = self.stdout.getvalue().strip()
|
|
|
|
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(command_output, "Changing password for user 'joe'\nPassword changed successfully for user 'joe'")
|
2012-03-05 12:17:55 +08:00
|
|
|
self.assertTrue(models.User.objects.get(username="joe").check_password("not qwerty"))
|
|
|
|
|
|
|
|
def test_that_max_tries_exits_1(self):
|
|
|
|
"""
|
|
|
|
A CommandError should be thrown by handle() if the user enters in
|
2012-05-27 02:50:44 +08:00
|
|
|
mismatched passwords three times.
|
2012-03-05 12:17:55 +08:00
|
|
|
"""
|
|
|
|
command = changepassword.Command()
|
|
|
|
command._get_pass = lambda *args: args or 'foo'
|
|
|
|
|
2012-05-27 02:50:44 +08:00
|
|
|
with self.assertRaises(CommandError):
|
|
|
|
command.execute("joe", stdout=self.stdout, stderr=self.stderr)
|