mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2025-07-22 05:29:46 +03:00
Added more tests for twitter social login
This commit is contained in:
parent
d34bea1f11
commit
f2360d6845
|
@ -49,11 +49,8 @@ class TwitterLoginSerializer(serializers.Serializer):
|
||||||
adapter = adapter_class(request)
|
adapter = adapter_class(request)
|
||||||
app = adapter.get_provider().get_app(request)
|
app = adapter.get_provider().get_app(request)
|
||||||
|
|
||||||
if('access_token' in attrs) and ('token_secret' in attrs):
|
access_token = attrs.get('access_token')
|
||||||
access_token = attrs.get('access_token')
|
token_secret = attrs.get('token_secret')
|
||||||
token_secret = attrs.get('token_secret')
|
|
||||||
else:
|
|
||||||
raise serializers.ValidationError('Incorrect input. access_token and token_secret are required.')
|
|
||||||
|
|
||||||
request.session['oauth_api.twitter.com_access_token'] = {
|
request.session['oauth_api.twitter.com_access_token'] = {
|
||||||
'oauth_token': access_token,
|
'oauth_token': access_token,
|
||||||
|
|
|
@ -100,6 +100,8 @@ class BaseAPITestCase(object):
|
||||||
self.veirfy_email_url = reverse('rest_verify_email')
|
self.veirfy_email_url = reverse('rest_verify_email')
|
||||||
self.fb_login_url = reverse('fb_login')
|
self.fb_login_url = reverse('fb_login')
|
||||||
self.tw_login_url = reverse('tw_login')
|
self.tw_login_url = reverse('tw_login')
|
||||||
|
self.tw_login_no_view_url = reverse('tw_login_no_view')
|
||||||
|
self.tw_login_no_adapter_url = reverse('tw_login_no_adapter')
|
||||||
|
|
||||||
def _login(self):
|
def _login(self):
|
||||||
payload = {
|
payload = {
|
||||||
|
|
|
@ -145,6 +145,79 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
|
||||||
def test_twitter_social_auth_without_auto_singup(self):
|
def test_twitter_social_auth_without_auto_singup(self):
|
||||||
self._twitter_social_auth()
|
self._twitter_social_auth()
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
|
def test_twitter_social_auth_request_error(self):
|
||||||
|
# fake response for twitter call
|
||||||
|
resp_body = {
|
||||||
|
"id": "123123123123",
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.add(
|
||||||
|
responses.GET,
|
||||||
|
'https://api.twitter.com/1.1/account/verify_credentials.json',
|
||||||
|
body=json.dumps(resp_body),
|
||||||
|
status=400,
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
|
||||||
|
users_count = get_user_model().objects.all().count()
|
||||||
|
payload = {
|
||||||
|
'access_token': 'abc123',
|
||||||
|
'token_secret': '1111222233334444'
|
||||||
|
}
|
||||||
|
|
||||||
|
self.post(self.tw_login_url, data=payload, status_code=400)
|
||||||
|
self.assertNotIn('key', self.response.json.keys())
|
||||||
|
self.assertEqual(get_user_model().objects.all().count(), users_count)
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
|
def test_twitter_social_auth_no_view_in_context(self):
|
||||||
|
# fake response for twitter call
|
||||||
|
resp_body = {
|
||||||
|
"id": "123123123123",
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.add(
|
||||||
|
responses.GET,
|
||||||
|
'https://api.twitter.com/1.1/account/verify_credentials.json',
|
||||||
|
body=json.dumps(resp_body),
|
||||||
|
status=400,
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
|
||||||
|
users_count = get_user_model().objects.all().count()
|
||||||
|
payload = {
|
||||||
|
'access_token': 'abc123',
|
||||||
|
'token_secret': '1111222233334444'
|
||||||
|
}
|
||||||
|
|
||||||
|
self.post(self.tw_login_no_view_url, data=payload, status_code=400)
|
||||||
|
self.assertEqual(get_user_model().objects.all().count(), users_count)
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
|
def test_twitter_social_auth_no_adapter(self):
|
||||||
|
# fake response for twitter call
|
||||||
|
resp_body = {
|
||||||
|
"id": "123123123123",
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.add(
|
||||||
|
responses.GET,
|
||||||
|
'https://api.twitter.com/1.1/account/verify_credentials.json',
|
||||||
|
body=json.dumps(resp_body),
|
||||||
|
status=400,
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
|
||||||
|
users_count = get_user_model().objects.all().count()
|
||||||
|
payload = {
|
||||||
|
'access_token': 'abc123',
|
||||||
|
'token_secret': '1111222233334444'
|
||||||
|
}
|
||||||
|
|
||||||
|
self.post(self.tw_login_no_adapter_url, data=payload, status_code=400)
|
||||||
|
self.assertEqual(get_user_model().objects.all().count(), users_count)
|
||||||
|
|
||||||
@responses.activate
|
@responses.activate
|
||||||
@override_settings(
|
@override_settings(
|
||||||
ACCOUNT_EMAIL_VERIFICATION='mandatory',
|
ACCOUNT_EMAIL_VERIFICATION='mandatory',
|
||||||
|
|
|
@ -5,6 +5,8 @@ from . import django_urls
|
||||||
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
||||||
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
|
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
|
||||||
|
|
||||||
|
from rest_framework.decorators import api_view
|
||||||
|
|
||||||
from rest_auth.urls import urlpatterns
|
from rest_auth.urls import urlpatterns
|
||||||
from rest_auth.registration.views import SocialLoginView
|
from rest_auth.registration.views import SocialLoginView
|
||||||
from rest_auth.social_serializers import TwitterLoginSerializer
|
from rest_auth.social_serializers import TwitterLoginSerializer
|
||||||
|
@ -19,6 +21,23 @@ class TwitterLogin(SocialLoginView):
|
||||||
serializer_class = TwitterLoginSerializer
|
serializer_class = TwitterLoginSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class TwitterLoginSerializerFoo(TwitterLoginSerializer):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['POST'])
|
||||||
|
def twitter_login_view(request):
|
||||||
|
serializer = TwitterLoginSerializerFoo(
|
||||||
|
data={'access_token': '11223344', 'token_secret': '55667788'},
|
||||||
|
context={'request': request}
|
||||||
|
)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
|
||||||
|
|
||||||
|
class TwitterLoginNoAdapter(SocialLoginView):
|
||||||
|
serializer_class = TwitterLoginSerializer
|
||||||
|
|
||||||
|
|
||||||
urlpatterns += [
|
urlpatterns += [
|
||||||
url(r'^rest-registration/', include('rest_auth.registration.urls')),
|
url(r'^rest-registration/', include('rest_auth.registration.urls')),
|
||||||
url(r'^test-admin/', include(django_urls)),
|
url(r'^test-admin/', include(django_urls)),
|
||||||
|
@ -28,5 +47,7 @@ urlpatterns += [
|
||||||
name='account_confirm_email'),
|
name='account_confirm_email'),
|
||||||
url(r'^social-login/facebook/$', FacebookLogin.as_view(), name='fb_login'),
|
url(r'^social-login/facebook/$', FacebookLogin.as_view(), name='fb_login'),
|
||||||
url(r'^social-login/twitter/$', TwitterLogin.as_view(), name='tw_login'),
|
url(r'^social-login/twitter/$', TwitterLogin.as_view(), name='tw_login'),
|
||||||
|
url(r'^social-login/twitter-no-view/$', twitter_login_view, name='tw_login_no_view'),
|
||||||
|
url(r'^social-login/twitter-no-adapter/$', TwitterLoginNoAdapter.as_view(), name='tw_login_no_adapter'),
|
||||||
url(r'^accounts/', include('allauth.socialaccount.urls'))
|
url(r'^accounts/', include('allauth.socialaccount.urls'))
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user