Ticket #16017: ticket16017.patch

File ticket16017.patch, 3.6 KB (added by Will Hardy, 13 years ago)

Added unit test for this fix

  • django/contrib/auth/management/__init__.py

    diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py
    index 9966849..83cb47b 100644
    a b def get_system_username():  
    8383    :returns: The username as a unicode string, or an empty string if the
    8484        username could not be determined.
    8585    """
    86     try:
    87         return getpass.getuser().decode(locale.getdefaultlocale()[1])
    88     except (ImportError, KeyError, UnicodeDecodeError):
    89         # KeyError will be raised by os.getpwuid() (called by getuser())
    90         # if there is no corresponding entry in the /etc/passwd file
    91         # (a very restricted chroot environment, for example).
    92         # UnicodeDecodeError - preventive treatment for non-latin Windows.
    93         return u''
     86    default_locale = locale.getdefaultlocale()[1]
     87    if default_locale:
     88        try:
     89            return getpass.getuser().decode(default_locale)
     90        except (ImportError, KeyError, UnicodeDecodeError):
     91            # KeyError will be raised by os.getpwuid() (called by getuser())
     92            # if there is no corresponding entry in the /etc/passwd file
     93            # (a very restricted chroot environment, for example).
     94            # UnicodeDecodeError - preventive treatment for non-latin Windows.
     95            pass
     96    return u''
    9497
    9598
    9699def get_default_username(check_db=True):
  • django/contrib/auth/tests/basic.py

    diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py
    index 6a3b656..fbee587 100644
    a b  
     1import locale
     2from StringIO import StringIO
     3
    14from django.test import TestCase
    25from django.contrib.auth.models import User, AnonymousUser
    36from django.core.management import call_command
    4 from StringIO import StringIO
     7from django.contrib.auth.management.commands import createsuperuser
    58
    69class BasicTestCase(TestCase):
    710    def test_user(self):
    class BasicTestCase(TestCase):  
    9396        self.assertEqual(u.email, 'joe@somewhere.org')
    9497        self.assertFalse(u.has_usable_password())
    9598
     99    def test_createsuperuser_nolocale(self):
     100        """ Check that createsuperuser does not break when no locale is set.
     101            See ticket #16017.
     102        """
     103
     104        try:
     105            old_getdefaultlocale = locale.getdefaultlocale
     106            old_getpass = createsuperuser.getpass
     107
     108            # Temporarily remove locale information
     109            locale.getdefaultlocale = lambda: (None, None)
     110
     111            # Temporarily replace getpass to allow interactive code to be used
     112            # non-interactively
     113            class mock_getpass: pass
     114            mock_getpass.getpass = staticmethod(lambda p=None: "nopasswd")
     115            createsuperuser.getpass = mock_getpass
     116
     117            # Call the command in this new environment
     118            new_io = StringIO()
     119            call_command("createsuperuser",
     120                interactive=True,
     121                username="nolocale@somewhere.org",
     122                email="nolocale@somewhere.org",
     123                stdout=new_io
     124            )
     125        except TypeError, e:
     126            self.fail("createsuperuser failed when without locale information")
     127
     128        finally:
     129            # Re-apply locale and getpass information
     130            createsuperuser.getpass = old_getpass
     131            locale.getdefaultlocale = old_getdefaultlocale
     132
     133        # If we were successful, a user would have been created
     134        u = User.objects.get(username="nolocale@somewhere.org")
     135        self.assertEqual(u.email, 'nolocale@somewhere.org')
     136
Back to Top