Skip to content

Commit

Permalink
[refactoring] smart_bytes exists in django 1.8 (#139)
Browse files Browse the repository at this point in the history
* smart_bytes exists in django 1.8

* undo sneak-in commit

* nits
  • Loading branch information
Peter Bengtsson authored Jun 12, 2017
1 parent 70cc070 commit 02502ca
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Django of your choice. Here is such an example:
$ virtualenv -p /path/to/bin/python3.5 venv
$ source venv
(venv) $ pip install Django==1.11.1
(venv) $ pip install Django==1.11.2
(venv) $ pip install -r tests/requirements.txt
(venv) $ DJANGO_SETTINGS_MODULE=tests.settings django-admin.py test
Expand Down
6 changes: 1 addition & 5 deletions mozilla_django_oidc/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import logging
import requests

try:
from django.utils.encoding import smart_bytes
except ImportError:
from django.utils.encoding import smart_str as smart_bytes
from django.utils.encoding import smart_text
from django.utils.encoding import smart_bytes, smart_text
from django.contrib.auth import get_user_model
from django.core.exceptions import SuspiciousOperation
from django.core.urlresolvers import reverse
Expand Down
5 changes: 3 additions & 2 deletions mozilla_django_oidc/middleware.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging
import time
try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode
except ImportError:
# Python < 3
from urllib import urlencode

import django
from django.core.urlresolvers import reverse
Expand Down
5 changes: 3 additions & 2 deletions mozilla_django_oidc/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import time
try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode
except ImportError:
# Python < 3
from urllib import urlencode

from django.core.exceptions import SuspiciousOperation
from django.core.urlresolvers import reverse
Expand Down
29 changes: 1 addition & 28 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,9 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo
'redirect_uri': 'http://testserver/callback/',
}
self.assertEqual(User.objects.all().count(), 0)
result = self.backend.authenticate(request=auth_request)
self.backend.authenticate(request=auth_request)
self.assertEqual(User.objects.all().count(), 1)
user = User.objects.all()[0]
self.assertEquals(user, result)

self.assertEquals(user.email, '[email protected]')
self.assertEquals(user.username, 'username_algo')

Expand All @@ -215,31 +213,6 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo
headers={'Authorization': 'Bearer access_granted'}
)

@patch('mozilla_django_oidc.auth.requests')
@patch('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token')
def test_successful_authentication_no_email(self, token_mock, request_mock):
"""What happens if the auth "works" but it doesn't have an email?"""
auth_request = RequestFactory().get('/foo', {'code': 'foo',
'state': 'bar'})
auth_request.session = {}

token_mock.return_value = True
get_json_mock = Mock()
get_json_mock.json.return_value = {
'nickname': 'a_username',
'email': ''
}
request_mock.get.return_value = get_json_mock
post_json_mock = Mock()
post_json_mock.json.return_value = {
'id_token': 'id_token',
'access_token': 'access_granted'
}
request_mock.post.return_value = post_json_mock
result = self.backend.authenticate(request=auth_request)
assert result is None
self.assertEqual(User.objects.all().count(), 0)

def test_authenticate_no_code_no_state(self):
"""Test authenticate with wrong parameters."""

Expand Down
1 change: 1 addition & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
try:
from urllib.parse import parse_qs
except ImportError:
# Python < 3
from urlparse import parse_qs

from mock import patch
Expand Down
5 changes: 3 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
try:
from urlparse import parse_qs, urlparse
except ImportError:
from urllib.parse import parse_qs, urlparse
except ImportError:
# Python < 3
from urlparse import parse_qs, urlparse

from mock import patch

Expand Down

0 comments on commit 02502ca

Please sign in to comment.