Skip to content

Commit

Permalink
Merge branch 'master' of github.com:flavors/django-graphql-social-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
mongkok committed Sep 23, 2018
2 parents 47d4c05 + 3a0d1da commit 48b1788
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
9 changes: 7 additions & 2 deletions graphql_social_auth/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ def wrapper(cls, root, info, provider, access_token, **kwargs):
user = backend.do_auth(access_token)

if user is None:
raise exceptions.GraphQLSocialAuthError(_('Invalid token'))
raise exceptions.InvalidTokenError(_('Invalid token'))

user_model = strategy.storage.user.user_model()

if not isinstance(user, user_model):
msg = _('`{}` is not a user instance').format(type(user).__name__)
raise exceptions.DoAuthError(msg, user)

if not issubclass(cls, mixins.JSONWebTokenMixin):
login(info.context, user)
Expand All @@ -43,7 +49,6 @@ def on_resolve(payload):

result = f(cls, root, info, social, **kwargs)

# Improved mutation with thenable check
if is_thenable(result):
return Promise.resolve(result).then(on_resolve)
return on_resolve(result)
Expand Down
13 changes: 12 additions & 1 deletion graphql_social_auth/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@

class GraphQLSocialAuthError(Exception):
"""Raise GraphQL Social exception"""
"""Raise GraphQL Social Exception"""


class InvalidTokenError(GraphQLSocialAuthError):
"""Raise Invalid Token Exception"""


class DoAuthError(GraphQLSocialAuthError):

def __init__(self, message, result):
super().__init__(message)
self.result = result
Binary file modified graphql_social_auth/locale/es/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion graphql_social_auth/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Django GraphQL Social Auth\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-01-17 23:52+0530\n"
"POT-Creation-Date: 2018-09-16 23:17+0700\n"
"PO-Revision-Date: 2018-01-15 02:30+0530\n"
"Last-Translator: mongkok <[email protected]>\n"
"Language-Team: Spanish\n"
Expand All @@ -26,3 +26,7 @@ msgstr "Proveedor no encontrado"
#: decorators.py:25
msgid "Invalid token"
msgstr "Token inválido"

#: decorators.py:30
msgid "`{}` is not a user instance"
msgstr "`{}` no es una instancia de usuario"
17 changes: 14 additions & 3 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import MagicMock, Mock
from unittest.mock import MagicMock, Mock, patch

from django.test import TestCase, override_settings

Expand All @@ -22,13 +22,24 @@ def wrapped(cls, root, info, provider, *args):

@social_auth_mock
@override_settings(SOCIAL_AUTH_PIPELINE=[])
def test_psa_user_not_found(self, *args):
def test_psa_invalid_token(self, *args):

@decorators.social_auth
def wrapped(cls, root, info, provider, *args):
"""Social Auth decorated function"""

with self.assertRaises(exceptions.GraphQLSocialAuthError):
with self.assertRaises(exceptions.InvalidTokenError):
wrapped(self, None, Mock(), 'google-oauth2', 'token')

@social_auth_mock
@patch('social_core.backends.oauth.BaseOAuth2.do_auth')
def test_psa_do_auth_error(self, *args):

@decorators.social_auth
def wrapped(cls, root, info, provider, *args):
"""Social Auth decorated function"""

with self.assertRaises(exceptions.DoAuthError):
wrapped(self, None, Mock(), 'google-oauth2', 'token')

@social_auth_mock
Expand Down

0 comments on commit 48b1788

Please sign in to comment.