diff options
author | Victor Stinner <vstinner@redhat.com> | 2015-08-27 14:32:29 +0200 |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2015-08-27 16:07:10 +0200 |
commit | d95e2f05ef9036ba28988cfc16ebbfe47d920fbf (patch) | |
tree | 88dc32ab13f47dec0ce3902826f34876f93662ad | |
parent | 829cbee14e200fd127ec4f0e0f4d743102d8e9d1 (diff) | |
download | django_openstack_auth-d95e2f05ef9036ba28988cfc16ebbfe47d920fbf.tar.gz |
Fix Python 3 issues
* Replace unicode() with six.text_type
* Replace basestring with six.string_types
* Add unit tests for User.has_perms()
This change is required to port Horizon to Python 3.
Partial-Implements: blueprint porting-python3
Change-Id: I028a37d51ba1eda69336d4c81a47606f7c66f83f
-rw-r--r-- | openstack_auth/tests/tests.py | 21 | ||||
-rw-r--r-- | openstack_auth/user.py | 3 | ||||
-rw-r--r-- | openstack_auth/views.py | 3 |
3 files changed, 25 insertions, 2 deletions
diff --git a/openstack_auth/tests/tests.py b/openstack_auth/tests/tests.py index 467f814..39ecfa0 100644 --- a/openstack_auth/tests/tests.py +++ b/openstack_auth/tests/tests.py @@ -936,6 +936,27 @@ class PolicyLoaderTestCase(test.TestCase): self.assertIsNone(policy._ENFORCER) +class PermTestCase(test.TestCase): + def test_has_perms(self): + testuser = user.User(id=1, roles=[]) + + def has_perm(perm, obj=None): + return perm in ('perm1', 'perm3') + + with mock.patch.object(testuser, 'has_perm', side_effect=has_perm): + self.assertFalse(testuser.has_perms(['perm2'])) + + # perm1 AND perm3 + self.assertFalse(testuser.has_perms(['perm1', 'perm2'])) + + # perm1 AND perm3 + self.assertTrue(testuser.has_perms(['perm1', 'perm3'])) + + # perm1 AND (perm2 OR perm3) + perm_list = ['perm1', ('perm2', 'perm3')] + self.assertTrue(testuser.has_perms(perm_list)) + + class PolicyTestCase(test.TestCase): _roles = [] diff --git a/openstack_auth/user.py b/openstack_auth/user.py index fc63d3f..72c8638 100644 --- a/openstack_auth/user.py +++ b/openstack_auth/user.py @@ -18,6 +18,7 @@ from django.conf import settings from django.contrib.auth import models from keystoneclient.common import cms as keystone_cms from keystoneclient import exceptions as keystone_exceptions +import six from openstack_auth import utils @@ -389,7 +390,7 @@ class User(models.AbstractBaseUser, models.AnonymousUser): if not perm_list: return True for perm in perm_list: - if isinstance(perm, basestring): + if isinstance(perm, six.string_types): # check that the permission matches if not self.has_perm(perm, obj): return False diff --git a/openstack_auth/views.py b/openstack_auth/views.py index 2047f8b..1ef4710 100644 --- a/openstack_auth/views.py +++ b/openstack_auth/views.py @@ -30,6 +30,7 @@ from django.views.decorators.csrf import csrf_protect # noqa from django.views.decorators.debug import sensitive_post_parameters # noqa from keystoneclient.auth import token_endpoint from keystoneclient import exceptions as keystone_exceptions +import six from openstack_auth import exceptions from openstack_auth import forms @@ -140,7 +141,7 @@ def websso(request): request.user = auth.authenticate(request=request, auth_url=auth_url, token=token) except exceptions.KeystoneAuthException as exc: - msg = 'Login failed: %s' % unicode(exc) + msg = 'Login failed: %s' % six.text_type(exc) res = django_http.HttpResponseRedirect(settings.LOGIN_URL) res.set_cookie('logout_reason', msg, max_age=10) return res |