1. Peter Sanchez
  2. django-impersonate

Commits

Peter Sanchez  committed 454e67d

Updating tests to work with Python 3

  • Participants
  • Parent commits ee1fab1
  • Branches py3k

Comments (0)

Files changed (1)

File impersonate/tests.py

View file
  • Ignore whitespace
         is_superuser = False
         is_staff = False
 '''
-import urllib
 import factory
-from urlparse import urlsplit
+from django.utils import six
 from django.test import TestCase
 from django.utils import unittest
 from django.http import HttpResponse
 from django.conf.urls.defaults import patterns, url, include
 
 try:
+    from urllib.parse import urlencode, urlsplit
+except ImportError:
+    from urllib import urlencode
+    from urlparse import urlsplit
+
+try:
     # Django 1.5 check
     from django.contrib.auth import get_user_model
 except ImportError:
     return User.objects.all()
 
 
-class UserFactory(factory.Factory):
-    FACTORY_FOR = User
+if six.PY3:
+    # Temporary until factory_boy gets Py3k support
+    class UserFactory(object):
+        @staticmethod
+        def create(**kwargs):
+            password = kwargs.pop('password', None)
+            kwargs['email'] = \
+                '{0}@test-email.com'.format(kwargs['username']).lower()
+            user = User.objects.create(**kwargs)
+            if password:
+                user.set_password(password)
+                user.save()
+            return user
+else:
+    class UserFactory(factory.Factory):
+        FACTORY_FOR = User
 
-    email = factory.LazyAttribute(
-        lambda a: '{0}@test-email.com'.format(a.username).lower()
-    )
+        email = factory.LazyAttribute(
+            lambda a: '{0}@test-email.com'.format(a.username).lower()
+        )
 
-    @classmethod
-    def _prepare(cls, create, **kwargs):
-        password = kwargs.pop('password', None)
-        user = super(UserFactory, cls)._prepare(create, **kwargs)
-        if password:
-            user.set_password(password)
-            if create:
-                user.save()
-        return user
+        @classmethod
+        def _prepare(cls, create, **kwargs):
+            password = kwargs.pop('password', None)
+            user = super(UserFactory, cls)._prepare(create, **kwargs)
+            if password:
+                user.set_password(password)
+                if create:
+                    user.save()
+            return user
 
 
 class TestImpersonation(TestCase):
         self.client.login(username=user, password=passwd)
         url = reverse('impersonate-start', args=[user_id_to_impersonate])
         if qwargs:
-            url += '?{0}'.format(urllib.urlencode(qwargs))
+            url += '?{0}'.format(urlencode(qwargs))
         return self.client.get(url)
 
     def _redirect_check(self, response, new_path):
         # Don't allow impersonated users to use restricted URI's
         with self.settings(IMPERSONATE_URI_EXCLUSIONS=r'^test-view/'):
             response = self.client.get(reverse('impersonate-test'))
-            self.assertEqual(('user1' in response.content), True) # NOT user4
+            self.assertEqual(('user1' in str(response.content)), True) # !user4
 
         self.client.logout()
 
         with self.settings(
                 IMPERSONATE_CUSTOM_ALLOW='impersonate.tests.test_allow2'):
             response = self.client.get(reverse('impersonate-test'))
-            self.assertEqual(('user1' in response.content), True) # NOT user4
+            self.assertEqual(('user1' in str(response.content)), True) # !user4
 
     @override_settings(
         IMPERSONATE_CUSTOM_USER_QUERYSET='impersonate.tests.test_qs')