Skip to content

Commit

Permalink
Respect Google Provider's HTTP reponse
Browse files Browse the repository at this point in the history
 - if google responds to you with  a 401 where your token is no good,
   raise an `HTTPError` (resp.raise_for_status) so that you can handle
   the exception corretly.  Otherwise you just get a 500 stauts because
   of a `KeyError`.
  • Loading branch information
yarbelk authored and Ishaan Singal committed Apr 30, 2015
1 parent af2f32a commit 8705514
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
36 changes: 35 additions & 1 deletion allauth/socialaccount/providers/google/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.core import mail
from django.core.urlresolvers import reverse

try:
from importlib import import_module
Expand All @@ -16,14 +17,18 @@
from allauth.socialaccount.tests import create_oauth2_tests
from allauth.account import app_settings as account_settings
from allauth.account.models import EmailConfirmation, EmailAddress
from allauth.socialaccount.models import SocialAccount
from allauth.socialaccount.models import SocialAccount, SocialToken
from allauth.socialaccount.providers import registry
from allauth.tests import MockedResponse
from allauth.account.signals import user_signed_up
from allauth.account.adapter import get_adapter

from requests.exceptions import HTTPError

from .provider import GoogleProvider

import mock


@override_settings(SOCIALACCOUNT_AUTO_SIGNUP=True,
ACCOUNT_SIGNUP_FORM_CLASS=None,
Expand Down Expand Up @@ -51,6 +56,35 @@ def get_mocked_response(self,
given_name,
(repr(verified_email).lower())))

def test_google_compelete_login_401(self):
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter

class LessMockedResponse(MockedResponse):
def raise_for_status(self):
if self.status_code != 200:
raise HTTPError(None)
request = RequestFactory().get(reverse(self.provider.id + '_login'),
dict(process='login'))

adapter = GoogleOAuth2Adapter()
app = adapter.get_provider().get_app(request)
token = SocialToken(token='some_token')
response_with_401 = LessMockedResponse(401, """
{"error": {
"errors": [{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization" } ],
"code": 401,
"message": "Invalid Credentials" }
}""")
with mock.patch('allauth.socialaccount.providers.google.views.requests') as patched_requests:
patched_requests.get.return_value = response_with_401
with self.assertRaises(HTTPError):
adapter.complete_login(request, app, token)

def test_username_based_on_email(self):
first_name = '明'
last_name = '小'
Expand Down
1 change: 1 addition & 0 deletions allauth/socialaccount/providers/google/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url,
params={'access_token': token.token,
'alt': 'json'})
resp.raise_for_status()
extra_data = resp.json()
login = self.get_provider() \
.sociallogin_from_response(request,
Expand Down

0 comments on commit 8705514

Please sign in to comment.