diff --git a/allauth/socialaccount/providers/facebook/views.py b/allauth/socialaccount/providers/facebook/views.py index d22f34632f..071638e72b 100644 --- a/allauth/socialaccount/providers/facebook/views.py +++ b/allauth/socialaccount/providers/facebook/views.py @@ -1,6 +1,8 @@ from django.utils.cache import patch_response_headers from django.shortcuts import render +import requests + from allauth.socialaccount.models import SocialAccount, SocialLogin, SocialToken from allauth.socialaccount.helpers import complete_social_login from allauth.socialaccount.helpers import render_authentication_error @@ -8,7 +10,6 @@ from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter, OAuth2LoginView, OAuth2CallbackView) -from allauth.socialaccount import requests from forms import FacebookConnectForm from provider import FacebookProvider @@ -20,7 +21,7 @@ def fb_complete_login(app, token): resp = requests.get('https://graph.facebook.com/me', params={ 'access_token': token.token }) - extra_data = resp.json + extra_data = resp.json() email = valid_email_or_none(extra_data.get('email')) uid = extra_data['id'] user = User(email=email) diff --git a/allauth/socialaccount/providers/github/views.py b/allauth/socialaccount/providers/github/views.py index 8d8ad084ec..ce57eb7ba0 100644 --- a/allauth/socialaccount/providers/github/views.py +++ b/allauth/socialaccount/providers/github/views.py @@ -1,7 +1,8 @@ +import requests + from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter, OAuth2LoginView, OAuth2CallbackView) -from allauth.socialaccount import requests from allauth.socialaccount.models import SocialAccount, SocialLogin from allauth.utils import get_user_model @@ -18,7 +19,7 @@ class GitHubOAuth2Adapter(OAuth2Adapter): def complete_login(self, request, app, token): resp = requests.get(self.profile_url, params={ 'access_token': token.token }) - extra_data = resp.json + extra_data = resp.json() uid = str(extra_data['id']) user = User(username=extra_data.get('login', ''), email=extra_data.get('email', ''), diff --git a/allauth/socialaccount/providers/google/views.py b/allauth/socialaccount/providers/google/views.py index 826269fd77..b0af3860d4 100644 --- a/allauth/socialaccount/providers/google/views.py +++ b/allauth/socialaccount/providers/google/views.py @@ -1,8 +1,9 @@ +import requests + from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter, OAuth2LoginView, OAuth2CallbackView) -from allauth.socialaccount import requests from allauth.socialaccount.models import SocialLogin, SocialAccount from allauth.utils import get_user_model @@ -20,7 +21,7 @@ def complete_login(self, request, app, token): resp = requests.get(self.profile_url, { 'access_token': token.token, 'alt': 'json' }) - extra_data = resp.json + extra_data = resp.json() # extra_data is something of the form: # # {u'family_name': u'Penners', u'name': u'Raymond Penners', diff --git a/allauth/socialaccount/providers/oauth2/client.py b/allauth/socialaccount/providers/oauth2/client.py index 1ab1808309..71fdd33f1e 100644 --- a/allauth/socialaccount/providers/oauth2/client.py +++ b/allauth/socialaccount/providers/oauth2/client.py @@ -1,7 +1,7 @@ import urllib import urlparse +import requests -from allauth.socialaccount import requests class OAuth2Error(Exception): pass @@ -47,7 +47,7 @@ def get_access_token(self, code): access_token = None if resp.status_code == 200: if resp.headers['content-type'].split(';')[0] == 'application/json': - data = resp.json + data = resp.json() else: data = dict(urlparse.parse_qsl(resp.content)) access_token = data.get('access_token') diff --git a/allauth/socialaccount/providers/persona/views.py b/allauth/socialaccount/providers/persona/views.py index dd3c5d574a..23e799042b 100644 --- a/allauth/socialaccount/providers/persona/views.py +++ b/allauth/socialaccount/providers/persona/views.py @@ -1,6 +1,7 @@ +import requests + from allauth.socialaccount.helpers import complete_social_login from allauth.socialaccount.helpers import render_authentication_error -from allauth.socialaccount import requests from allauth.socialaccount.models import SocialAccount, SocialLogin from allauth.utils import get_user_model @@ -14,9 +15,9 @@ def persona_login(request): resp = requests.post('https://verifier.login.persona.org/verify', { 'assertion': assertion, 'audience': audience }) - if resp.json['status'] != 'okay': + if resp.json()['status'] != 'okay': return render_authentication_error(request) - email = resp.json['email'] + email = resp.json()['email'] user = User(email=email) extra_data = resp.json account = SocialAccount(uid=email, diff --git a/allauth/socialaccount/providers/soundcloud/views.py b/allauth/socialaccount/providers/soundcloud/views.py index a650caa15f..74f35e1886 100644 --- a/allauth/socialaccount/providers/soundcloud/views.py +++ b/allauth/socialaccount/providers/soundcloud/views.py @@ -1,7 +1,8 @@ +import requests + from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter, OAuth2LoginView, OAuth2CallbackView) -from allauth.socialaccount import requests from allauth.socialaccount.models import SocialAccount, SocialLogin from allauth.utils import get_user_model @@ -18,7 +19,7 @@ class SoundCloudOAuth2Adapter(OAuth2Adapter): def complete_login(self, request, app, token): resp = requests.get(self.profile_url, params={ 'oauth_token': token.token }) - extra_data = resp.json + extra_data = resp.json() uid = str(extra_data['id']) name_parts = extra_data.get('full_name', '').split(' ', 1) if len(name_parts) == 2: diff --git a/allauth/socialaccount/requests.py b/allauth/socialaccount/requests.py deleted file mode 100644 index bb2d4cc50c..0000000000 --- a/allauth/socialaccount/requests.py +++ /dev/null @@ -1,57 +0,0 @@ -import urllib -import httplib2 - -class Response(object): - def __init__(self, status_code, content, headers={}): - self.status_code = status_code - self.content = content - self.headers = headers - - @classmethod - def from_httplib(cls, resp, content): - return Response(resp.status, - content, - headers=dict(resp.iteritems())) - - - @property - def json(self): - import json - return json.loads(self.content) - -_mocked_responses = [] - -def mock_next_request(resp): - global _mocked_responses - _mocked_responses.append(resp) - -def _mockable_request(f): - def new_f(*args, **kwargs): - global _mocked_responses - if _mocked_responses: - return _mocked_responses.pop(0) - return f(*args, **kwargs) - return new_f - -@_mockable_request -def get(url, params={}): - global _mocked_responses - if _mocked_responses: - return _mocked_responses.pop(0) - client = httplib2.Http() - query = urllib.urlencode(params) - if query: - url += '?' + query - resp, content = client.request(url, 'GET') - return Response.from_httplib(resp, content) - -@_mockable_request -def post(url, params): - client = httplib2.Http() - headers = { 'content-type': 'application/x-www-form-urlencoded' } - resp, content = client.request(url, 'POST', - body=urllib.urlencode(params), - headers=headers) - return Response.from_httplib(resp, content) - - diff --git a/allauth/socialaccount/tests.py b/allauth/socialaccount/tests.py index 39ba8f9655..94563adb65 100644 --- a/allauth/socialaccount/tests.py +++ b/allauth/socialaccount/tests.py @@ -7,7 +7,7 @@ from django.test.utils import override_settings import providers -from allauth.socialaccount import requests +from allauth.tests import MockedResponse, mocked_response from providers.oauth2.provider import OAuth2Provider @@ -15,7 +15,7 @@ mocked_oauth_responses = { - 'google': requests.Response(200, """ + 'google': MockedResponse(200, """ {"family_name": "Penners", "name": "Raymond Penners", "picture": "https://lh5.googleusercontent.com/-GOFYGBVOdBQ/AAAAAAAAAAI/AAAAAAAAAGM/WzRfPkv4xbo/photo.jpg", "locale": "nl", "gender": "male", @@ -48,15 +48,14 @@ def test_login(self): warnings.warn("Cannot test provider %s, no oauth mock" % self.provider.id) return - requests.mock_next_request \ - (requests.Response(200, - '{"access_token":"testac"}', - {'content-type': - 'application/json'})) - requests.mock_next_request(resp_mock) - resp = self.client.get(complete_url, - { 'code': 'test' }) - self.assertRedirects(resp, reverse('socialaccount_signup')) + with mocked_response(MockedResponse(200, + '{"access_token":"testac"}', + {'content-type': + 'application/json'}), + resp_mock): + resp = self.client.get(complete_url, + { 'code': 'test' }) + self.assertRedirects(resp, reverse('socialaccount_signup')) impl = { 'setUp': setUp, diff --git a/allauth/tests.py b/allauth/tests.py index d74e21fd74..8f5b19076f 100644 --- a/allauth/tests.py +++ b/allauth/tests.py @@ -1,9 +1,40 @@ # -*- coding: utf-8 -*- +import requests from django.test import TestCase import utils +class MockedResponse(object): + def __init__(self, status_code, content, headers={}): + self.status_code = status_code + self.content = content + self.headers = headers + + def json(self): + import json + return json.loads(self.content) + +class mocked_response: + def __init__(self, *responses): + self.responses = list(responses) + + def __enter__(self): + self.orig_get = requests.get + self.orig_post = requests.post + + def mockable_request(f): + def new_f(*args, **kwargs): + if self.responses: + return self.responses.pop(0) + return f(*args, **kwargs) + return new_f + requests.get = mockable_request(requests.get) + requests.post = mockable_request(requests.post) + + def __exit__(self, type, value, traceback): + requests.get = self.orig_get + requests.post = self.orig_post class BasicTests(TestCase): diff --git a/requirements.txt b/requirements.txt index 45a769be0c..676baa38c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ django>=1.4 oauth2 python-openid - +requests>==1.0.3