| 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') |