From f2360d68459204f0cd95e31964ee22fc71d9d97a Mon Sep 17 00:00:00 2001 From: mariodev Date: Tue, 5 Jul 2016 22:10:04 +0200 Subject: [PATCH] Added more tests for twitter social login --- rest_auth/social_serializers.py | 7 +--- rest_auth/tests/test_base.py | 2 + rest_auth/tests/test_social.py | 73 +++++++++++++++++++++++++++++++++ rest_auth/tests/urls.py | 21 ++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) diff --git a/rest_auth/social_serializers.py b/rest_auth/social_serializers.py index 3386444..6e06be5 100644 --- a/rest_auth/social_serializers.py +++ b/rest_auth/social_serializers.py @@ -49,11 +49,8 @@ class TwitterLoginSerializer(serializers.Serializer): adapter = adapter_class(request) app = adapter.get_provider().get_app(request) - if('access_token' in attrs) and ('token_secret' in attrs): - access_token = attrs.get('access_token') - token_secret = attrs.get('token_secret') - else: - raise serializers.ValidationError('Incorrect input. access_token and token_secret are required.') + access_token = attrs.get('access_token') + token_secret = attrs.get('token_secret') request.session['oauth_api.twitter.com_access_token'] = { 'oauth_token': access_token, diff --git a/rest_auth/tests/test_base.py b/rest_auth/tests/test_base.py index 1bb5482..faaf7bb 100644 --- a/rest_auth/tests/test_base.py +++ b/rest_auth/tests/test_base.py @@ -100,6 +100,8 @@ class BaseAPITestCase(object): self.veirfy_email_url = reverse('rest_verify_email') self.fb_login_url = reverse('fb_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): payload = { diff --git a/rest_auth/tests/test_social.py b/rest_auth/tests/test_social.py index 19a3ed2..9356acd 100644 --- a/rest_auth/tests/test_social.py +++ b/rest_auth/tests/test_social.py @@ -145,6 +145,79 @@ class TestSocialAuth(TestCase, BaseAPITestCase): def test_twitter_social_auth_without_auto_singup(self): 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 @override_settings( ACCOUNT_EMAIL_VERIFICATION='mandatory', diff --git a/rest_auth/tests/urls.py b/rest_auth/tests/urls.py index be124a0..c33d390 100644 --- a/rest_auth/tests/urls.py +++ b/rest_auth/tests/urls.py @@ -5,6 +5,8 @@ from . import django_urls from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter +from rest_framework.decorators import api_view + from rest_auth.urls import urlpatterns from rest_auth.registration.views import SocialLoginView from rest_auth.social_serializers import TwitterLoginSerializer @@ -19,6 +21,23 @@ class TwitterLogin(SocialLoginView): 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 += [ url(r'^rest-registration/', include('rest_auth.registration.urls')), url(r'^test-admin/', include(django_urls)), @@ -28,5 +47,7 @@ urlpatterns += [ name='account_confirm_email'), 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-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')) ]