Ticket #16017: malformedlocale2.diff

File malformedlocale2.diff, 5.1 KB (added by Chris Wesseling, 13 years ago)

mock getpass wasn't cleanup up

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

    diff -r 8a52d5651cc2 django/contrib/auth/management/__init__.py
    a b  
    8383    :returns: The username as a unicode string, or an empty string if the
    8484        username could not be determined.
    8585    """
     86    default_username = u''
    8687    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''
     88        default_locale = locale.getdefaultlocale()[1]
     89    except ValueError:
     90        # ValueError - unknown or broken locale like 'UTF-8'
     91        return default_username
     92
     93    if default_locale:
     94        try:
     95            return getpass.getuser().decode(default_locale)
     96        except (ImportError, KeyError, UnicodeDecodeError):
     97            # KeyError will be raised by os.getpwuid() (called by getuser())
     98            # if there is no corresponding entry in the /etc/passwd file
     99            # (a very restricted chroot environment, for example).
     100            # UnicodeDecodeError - preventive treatment for non-latin Windows.
     101            pass
     102    return default_username
    94103
    95104
    96105def get_default_username(check_db=True):
  • django/contrib/auth/tests/basic.py

    diff -r 8a52d5651cc2 django/contrib/auth/tests/basic.py
    a b  
     1import locale
     2import os
     3from StringIO import StringIO
     4
    15from django.test import TestCase
    26from django.contrib.auth.models import User, AnonymousUser
    37from django.core.management import call_command
    4 from StringIO import StringIO
     8from django.contrib.auth.management.commands import createsuperuser
    59
    610class BasicTestCase(TestCase):
    711    def test_user(self):
     
    9397        self.assertEqual(u.email, 'joe@somewhere.org')
    9498        self.assertFalse(u.has_usable_password())
    9599
     100    def test_createsuperuser_nolocale(self):
     101        """ Check that createsuperuser does not break when no locale is set.
     102
     103        See ticket #16017.
     104        TODO: check if this needs to be skipped on non *NIX platforms
     105        """
     106
     107        orgenv = {}
     108        for envvar in (key for key in os.environ.keys() if
     109                key.startswith('LC_') or key in ('LANG', 'LANGUAGE')):
     110            orgenv[envvar] = os.environ[envvar]
     111            os.environ[envvar] = 'C'
     112
     113        new_io = StringIO()
     114        class mock_getpass: pass
     115        mock_getpass.getpass = staticmethod(lambda
     116                    p=None: "nopasswd")
     117        org_getpass = createsuperuser.getpass
     118        createsuperuser.getpass = mock_getpass
     119        try:
     120            # Call the command in this new environment
     121            call_command("createsuperuser",
     122                interactive=True,
     123                username="nolocale",
     124                email="nolocale@example.com",
     125                stdout=new_io
     126            )
     127        except:
     128            self.fail("createsuperuser failed when without locale information")
     129        finally:
     130            for envvar, val in orgenv.items():
     131                os.environ[envvar] = val
     132            createsuperuser.getpass = org_getpass
     133
     134        # If we were successful, a user would have been created
     135        u = User.objects.get(username="nolocale")
     136        self.assertEqual(u.email, 'nolocale@example.com')
     137
     138    def test_createsuperuser_unknownlocale(self):
     139        """ Check that createsuperuser does not break when an unknown locale
     140        is set. Seen on a Mac at the DjangoCon EU sprint 2011.
     141
     142        See ticket #16017.
     143        """
     144
     145        orgenv = {}
     146        for envvar in (key for key in os.environ.keys() if
     147                key.startswith('LC_') or key in ('LANG', 'LANGUAGE')):
     148            orgenv[envvar] = os.environ[envvar]
     149            os.environ[envvar] = 'UTF-8'
     150
     151        new_io = StringIO()
     152        class mock_getpass: pass
     153        mock_getpass.getpass = staticmethod(lambda
     154                    p=None: "nopasswd")
     155        org_getpass = createsuperuser.getpass
     156        createsuperuser.getpass = mock_getpass
     157        try:
     158            # Call the command in this new environment
     159            call_command("createsuperuser",
     160                interactive=True,
     161                username="weirdlocale",
     162                email="weirdlocale@example.com",
     163                stdout=new_io
     164            )
     165        except:
     166            self.fail("createsuperuser failed when with unkown locale information")
     167        finally:
     168            for envvar, val in orgenv.items():
     169                os.environ[envvar] = val
     170            createsuperuser.getpass = org_getpass
     171
     172        # If we were successful, a user would have been created
     173        u = User.objects.get(username="weirdlocale")
     174        self.assertEqual(u.email, 'weirdlocale@example.com')
Back to Top