Permalink
Browse files

Dropping httplib in favor of requests (closes #113)

  • Loading branch information...
pennersr committed Dec 22, 2012
1 parent 9f94502 commit 93eca00adbcfd33deaf8cfd93b1871817b8cbddb
@@ -1,14 +1,15 @@
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
from allauth.socialaccount import providers
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)
@@ -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', ''),
@@ -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',
@@ -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')
@@ -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,
@@ -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:
@@ -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)
-
-
@@ -7,15 +7,15 @@
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
from models import SocialApp
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,
View
@@ -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):
View
@@ -1,4 +1,4 @@
django>=1.4
oauth2
python-openid
-
+requests>==1.0.3

0 comments on commit 93eca00

Please sign in to comment.