mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2025-07-22 13:39:45 +03:00
Added twitter login test
This commit is contained in:
parent
c4e7bdc77f
commit
5276227b82
|
@ -46,7 +46,7 @@ class TwitterLoginSerializer(serializers.Serializer):
|
||||||
if not adapter_class:
|
if not adapter_class:
|
||||||
raise serializers.ValidationError('Define adapter_class in view')
|
raise serializers.ValidationError('Define adapter_class in view')
|
||||||
|
|
||||||
adapter = adapter_class()
|
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):
|
if('access_token' in attrs) and ('token_secret' in attrs):
|
||||||
|
|
|
@ -80,6 +80,7 @@ INSTALLED_APPS = [
|
||||||
'allauth.account',
|
'allauth.account',
|
||||||
'allauth.socialaccount',
|
'allauth.socialaccount',
|
||||||
'allauth.socialaccount.providers.facebook',
|
'allauth.socialaccount.providers.facebook',
|
||||||
|
'allauth.socialaccount.providers.twitter',
|
||||||
|
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'rest_framework.authtoken',
|
'rest_framework.authtoken',
|
||||||
|
|
|
@ -99,6 +99,7 @@ class BaseAPITestCase(object):
|
||||||
self.user_url = reverse('rest_user_details')
|
self.user_url = reverse('rest_user_details')
|
||||||
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')
|
||||||
|
|
||||||
def _login(self):
|
def _login(self):
|
||||||
payload = {
|
payload = {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import json
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.test.utils import override_settings
|
from django.test.utils import override_settings
|
||||||
|
@ -34,9 +36,19 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
|
||||||
client_id='123123123',
|
client_id='123123123',
|
||||||
secret='321321321',
|
secret='321321321',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
twitter_social_app = SocialApp.objects.create(
|
||||||
|
provider='twitter',
|
||||||
|
name='Twitter',
|
||||||
|
client_id='11223344',
|
||||||
|
secret='55667788',
|
||||||
|
)
|
||||||
|
|
||||||
site = Site.objects.get_current()
|
site = Site.objects.get_current()
|
||||||
social_app.sites.add(site)
|
social_app.sites.add(site)
|
||||||
|
twitter_social_app.sites.add(site)
|
||||||
self.graph_api_url = GRAPH_API_URL + '/me'
|
self.graph_api_url = GRAPH_API_URL + '/me'
|
||||||
|
self.twitter_url = 'http://twitter.com/foobarme'
|
||||||
|
|
||||||
@responses.activate
|
@responses.activate
|
||||||
def test_failed_social_auth(self):
|
def test_failed_social_auth(self):
|
||||||
|
@ -57,11 +69,24 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
|
||||||
@responses.activate
|
@responses.activate
|
||||||
def test_social_auth(self):
|
def test_social_auth(self):
|
||||||
# fake response for facebook call
|
# fake response for facebook call
|
||||||
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true}' # noqa
|
resp_body = {
|
||||||
|
"id": "123123123123",
|
||||||
|
"first_name": "John",
|
||||||
|
"gender": "male",
|
||||||
|
"last_name": "Smith",
|
||||||
|
"link": "https://www.facebook.com/john.smith",
|
||||||
|
"locale": "en_US",
|
||||||
|
"name": "John Smith",
|
||||||
|
"timezone": 2,
|
||||||
|
"updated_time": "2014-08-13T10:14:38+0000",
|
||||||
|
"username": "john.smith",
|
||||||
|
"verified": True
|
||||||
|
}
|
||||||
|
|
||||||
responses.add(
|
responses.add(
|
||||||
responses.GET,
|
responses.GET,
|
||||||
self.graph_api_url,
|
self.graph_api_url,
|
||||||
body=resp_body,
|
body=json.dumps(resp_body),
|
||||||
status=200,
|
status=200,
|
||||||
content_type='application/json'
|
content_type='application/json'
|
||||||
)
|
)
|
||||||
|
@ -80,6 +105,37 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
|
||||||
self.assertIn('key', self.response.json.keys())
|
self.assertIn('key', self.response.json.keys())
|
||||||
self.assertEqual(get_user_model().objects.all().count(), users_count + 1)
|
self.assertEqual(get_user_model().objects.all().count(), users_count + 1)
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
|
def test_twitter_social_auth(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=200,
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
|
||||||
|
users_count = get_user_model().objects.all().count()
|
||||||
|
payload = {
|
||||||
|
'access_token': 'abc123',
|
||||||
|
'token_secret': '1111222233334444'
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = self.post(self.tw_login_url, data=payload)
|
||||||
|
|
||||||
|
self.assertIn('key', self.response.json.keys())
|
||||||
|
self.assertEqual(get_user_model().objects.all().count(), users_count + 1)
|
||||||
|
|
||||||
|
# make sure that second request will not create a new user
|
||||||
|
self.post(self.tw_login_url, data=payload, status_code=200)
|
||||||
|
self.assertIn('key', self.response.json.keys())
|
||||||
|
self.assertEqual(get_user_model().objects.all().count(), users_count + 1)
|
||||||
|
|
||||||
@responses.activate
|
@responses.activate
|
||||||
@override_settings(
|
@override_settings(
|
||||||
ACCOUNT_EMAIL_VERIFICATION='mandatory',
|
ACCOUNT_EMAIL_VERIFICATION='mandatory',
|
||||||
|
@ -87,11 +143,25 @@ class TestSocialAuth(TestCase, BaseAPITestCase):
|
||||||
REST_SESSION_LOGIN=False
|
REST_SESSION_LOGIN=False
|
||||||
)
|
)
|
||||||
def test_edge_case(self):
|
def test_edge_case(self):
|
||||||
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true,"email":"%s"}' # noqa
|
resp_body = {
|
||||||
|
"id": "123123123123",
|
||||||
|
"first_name": "John",
|
||||||
|
"gender": "male",
|
||||||
|
"last_name": "Smith",
|
||||||
|
"link": "https://www.facebook.com/john.smith",
|
||||||
|
"locale": "en_US",
|
||||||
|
"name": "John Smith",
|
||||||
|
"timezone": 2,
|
||||||
|
"updated_time": "2014-08-13T10:14:38+0000",
|
||||||
|
"username": "john.smith",
|
||||||
|
"verified": True,
|
||||||
|
"email": self.EMAIL
|
||||||
|
}
|
||||||
|
|
||||||
responses.add(
|
responses.add(
|
||||||
responses.GET,
|
responses.GET,
|
||||||
self.graph_api_url,
|
self.graph_api_url,
|
||||||
body=resp_body % self.EMAIL,
|
body=json.dumps(resp_body),
|
||||||
status=200,
|
status=200,
|
||||||
content_type='application/json'
|
content_type='application/json'
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,14 +3,23 @@ from django.views.generic import TemplateView
|
||||||
from . import django_urls
|
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 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.views import LoginView
|
||||||
|
|
||||||
|
|
||||||
class FacebookLogin(SocialLoginView):
|
class FacebookLogin(SocialLoginView):
|
||||||
adapter_class = FacebookOAuth2Adapter
|
adapter_class = FacebookOAuth2Adapter
|
||||||
|
|
||||||
|
|
||||||
|
class TwitterLogin(LoginView):
|
||||||
|
adapter_class = TwitterOAuthAdapter
|
||||||
|
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)),
|
||||||
|
@ -19,5 +28,6 @@ urlpatterns += [
|
||||||
url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
|
url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
|
||||||
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'^accounts/', include('allauth.socialaccount.urls'))
|
url(r'^accounts/', include('allauth.socialaccount.urls'))
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user