add social medial authentication tests, separate test urls

This commit is contained in:
Mateusz Sikora 2014-10-08 12:19:15 +02:00
parent f134d8b1d6
commit 4088081707
5 changed files with 100 additions and 35 deletions

View File

@ -36,6 +36,18 @@ MIDDLEWARE_CLASSES = [
'django.contrib.messages.middleware.MessageMiddleware' 'django.contrib.messages.middleware.MessageMiddleware'
] ]
TEMPLATE_CONTEXT_PROCESSORS = [
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.media',
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.static',
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
]
INSTALLED_APPS = [ INSTALLED_APPS = [
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
@ -48,6 +60,8 @@ INSTALLED_APPS = [
'allauth', 'allauth',
'allauth.account', 'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'rest_framework', 'rest_framework',
'rest_framework.authtoken', 'rest_framework.authtoken',

22
rest_auth/test_urls.py Normal file
View File

@ -0,0 +1,22 @@
from django.conf.urls import patterns, url, include
from django.views.generic import TemplateView
from django.contrib.auth.tests import urls
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from .urls import urlpatterns
from .registration.views import SocialLogin
class FacebookLogin(SocialLogin):
adapter_class = FacebookOAuth2Adapter
urlpatterns += patterns('',
url(r'^rest-registration/', include('registration.urls')),
url(r'^test-admin/', include(urls)),
url(r'^account-email-verification-sent/$', TemplateView.as_view(),
name='account_email_verification_sent'),
url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
name='account_confirm_email'),
url(r'^social-login/facebook/$', FacebookLogin.as_view(), name='fb_login')
)

View File

@ -9,8 +9,11 @@ from django.contrib.auth.models import User
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core import mail from django.core import mail
from django.test.utils import override_settings from django.test.utils import override_settings
from django.contrib.sites.models import Site
from allauth.socialaccount.models import SocialApp
import responses
from rest_framework.serializers import _resolve_model
from rest_framework import status from rest_framework import status
@ -121,6 +124,8 @@ class APITestCase1(TestCase, BaseAPITestCase):
- custom registration: backend defined - custom registration: backend defined
""" """
urls = 'rest_auth.test_urls'
USERNAME = 'person' USERNAME = 'person'
PASS = 'person' PASS = 'person'
EMAIL = "person1@world.com" EMAIL = "person1@world.com"
@ -338,3 +343,44 @@ class APITestCase1(TestCase, BaseAPITestCase):
# try to login again # try to login again
self._login() self._login()
self._logout() self._logout()
class TestSocialAuth(TestCase, BaseAPITestCase):
urls = 'rest_auth.test_urls'
def setUp(self):
social_app = SocialApp.objects.create(
provider='facebook',
name='Facebook',
client_id='123123123',
secret='321321321',
)
site = Site.objects.get_current()
social_app.sites.add(site)
self.fb_login_url = reverse('fb_login')
@responses.activate
def test_failed_social_auth(self):
# fake response
responses.add(responses.GET, 'https://graph.facebook.com/me',
body='', status=400, content_type='application/json')
payload = {
'access_token': 'abc123'
}
self.post(self.fb_login_url, data=payload, status_code=400)
@responses.activate
def test_social_auth(self):
# 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}'
responses.add(responses.GET, 'https://graph.facebook.com/me',
body=resp_body, status=200, content_type='application/json')
payload = {
'access_token': 'abc123'
}
self.post(self.fb_login_url, data=payload, status_code=200)
self.assertIn('key', self.response.json.keys())

View File

@ -1,36 +1,18 @@
from django.conf import settings from django.conf.urls import patterns, url
from django.conf.urls import patterns, url, include
from django.views.generic import TemplateView
from rest_auth.views import Login, Logout, UserDetails, \ from rest_auth.views import (Login, Logout, UserDetails, PasswordChange,
PasswordChange, PasswordReset, PasswordResetConfirm PasswordReset, PasswordResetConfirm)
urlpatterns = patterns('',
urlpatterns = patterns('rest_auth.views',
# URLs that do not require a session or valid token # URLs that do not require a session or valid token
url(r'^password/reset/$', PasswordReset.as_view(), url(r'^password/reset/$', PasswordReset.as_view(),
name='rest_password_reset'), name='rest_password_reset'),
url(r'^password/reset/confirm/$', url(r'^password/reset/confirm/$', PasswordResetConfirm.as_view(),
PasswordResetConfirm.as_view( name='rest_password_reset_confirm'),
), name='rest_password_reset_confirm'),
url(r'^login/$', Login.as_view(), name='rest_login'), url(r'^login/$', Login.as_view(), name='rest_login'),
# URLs that require a user to be logged in with a valid session / token.
# URLs that require a user to be logged in with a valid
# session / token.
url(r'^logout/$', Logout.as_view(), name='rest_logout'), url(r'^logout/$', Logout.as_view(), name='rest_logout'),
url(r'^user/$', UserDetails.as_view(), url(r'^user/$', UserDetails.as_view(), name='rest_user_details'),
name='rest_user_details'),
url(r'^password/change/$', PasswordChange.as_view(), url(r'^password/change/$', PasswordChange.as_view(),
name='rest_password_change'), name='rest_password_change'),
) )
if getattr(settings, 'IS_TEST', False):
from django.contrib.auth.tests import urls
urlpatterns += patterns('',
url(r'^rest-registration/', include('registration.urls')),
url(r'^test-admin/', include(urls)),
url(r'^account-email-verification-sent/$', TemplateView.as_view(),
name='account_email_verification_sent'),
url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
name='account_confirm_email'),
)

View File

@ -31,6 +31,7 @@ setup(
'Django>=1.5.0', 'Django>=1.5.0',
'django-allauth>=0.18.0', 'django-allauth>=0.18.0',
'djangorestframework>=2.3.13', 'djangorestframework>=2.3.13',
'responses>=0.2.2'
], ],
test_suite='rest_auth.runtests.runtests', test_suite='rest_auth.runtests.runtests',
include_package_data=True, include_package_data=True,