From 5d610a5b82dbaf68a7471939794699ed6524072f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Fri, 25 Mar 2011 05:02:13 -0300 Subject: [PATCH] Facebook login testing. Refs gh-25 --- README.rst | 8 +++-- doc/testing.rst | 8 +++-- social_auth/tests/__init__.py | 1 + social_auth/tests/base.py | 17 +++++++-- social_auth/tests/facebook.py | 65 +++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 social_auth/tests/facebook.py diff --git a/README.rst b/README.rst index 54b3438..fe5ea9d 100644 --- a/README.rst +++ b/README.rst @@ -456,12 +456,16 @@ To test the app just run:: This will run a bunch of tests, so far only login process is tested, more will come eventually. -User accounts on the different sites are needed to run test, here's how it's -configured:: +User accounts on the different sites are needed to run tests, configure the +credentials in the following way:: + # twitter testing TEST_TWITTER_USER = 'testing_account' TEST_TWITTER_PASSWORD = 'password_for_testing_account' + # facebook testing + TEST_FACEBOOK_USER = 'testing_account' + TEST_FACEBOOK_PASSWORD = 'password_for_testing_account' ---- Bugs diff --git a/doc/testing.rst b/doc/testing.rst index 392b609..869a0f6 100644 --- a/doc/testing.rst +++ b/doc/testing.rst @@ -10,8 +10,12 @@ To test the app just run:: This will run a bunch of tests, so far only login process is tested, more will come eventually. -User accounts on the different sites are needed to run test, here's how it's -configured:: +User accounts on the different sites are needed to run tests, configure the +credentials in the following way:: TEST_TWITTER_USER = 'testing_account' TEST_TWITTER_PASSWORD = 'password_for_testing_account' + + # facebook testing + TEST_FACEBOOK_USER = 'testing_account' + TEST_FACEBOOK_PASSWORD = 'password_for_testing_account' diff --git a/social_auth/tests/__init__.py b/social_auth/tests/__init__.py index 196a8ca..d5fc463 100644 --- a/social_auth/tests/__init__.py +++ b/social_auth/tests/__init__.py @@ -1 +1,2 @@ from social_auth.tests.twitter import * +from social_auth.tests.facebook import * diff --git a/social_auth/tests/base.py b/social_auth/tests/base.py index 4255c7f..0104a07 100644 --- a/social_auth/tests/base.py +++ b/social_auth/tests/base.py @@ -9,6 +9,9 @@ from django.test.client import Client from django.core.urlresolvers import reverse +USER_AGENT = 'Mozilla/5.0' + + class SocialAuthTestsCase(unittest.TestCase): """Base class for social auth tests""" SERVER_NAME = None @@ -20,18 +23,26 @@ class SocialAuthTestsCase(unittest.TestCase): client_kwargs['SERVER_NAME'] = self.SERVER_NAME if self.SERVER_PORT: client_kwargs['SERVER_PORT'] = self.SERVER_PORT + self.jar = None self.client = Client(**client_kwargs) - self.jar = cookielib.CookieJar() super(SocialAuthTestsCase, self).__init__(*args, **kwargs) def get_content(self, url, data=None, use_cookies=False): """Return content for given url, if data is not None, then a POST request will be issued, otherwise GET will be used""" data = data and urllib.urlencode(data, doseq=True) or data + request = urllib2.Request(url) agent = urllib2.build_opener() + if use_cookies: - agent.add_handler(urllib2.HTTPCookieProcessor(self.jar)) - return ''.join(agent.open(url, data=data).readlines()) + agent.add_handler(urllib2.HTTPCookieProcessor(self.get_jar())) + request.add_header('User-Agent', USER_AGENT) + return ''.join(agent.open(request, data=data).readlines()) + + def get_jar(self): + if not self.jar: + self.jar = cookielib.CookieJar() + return self.jar def reverse(self, name, backend): """Reverses backend URL by name""" diff --git a/social_auth/tests/facebook.py b/social_auth/tests/facebook.py new file mode 100644 index 0000000..d2f7d31 --- /dev/null +++ b/social_auth/tests/facebook.py @@ -0,0 +1,65 @@ +import re +import urllib + +from django.conf import settings + +from social_auth.tests.base import SocialAuthTestsCase, FormParser + + +class FacebookTestCase(SocialAuthTestsCase): + SERVER_NAME = 'myapp.com' + SERVER_PORT = '8000' + + def setUp(self, *args, **kwargs): + super(FacebookTestCase, self).setUp(*args, **kwargs) + self.user = getattr(settings, 'TEST_FACEBOOK_USER', None) + self.passwd = getattr(settings, 'TEST_FACEBOOK_PASSWORD', None) + # check that user and password are setup properly + self.assertTrue(self.user) + self.assertTrue(self.passwd) + + +REDIRECT_RE = re.compile('window.location.replace\("(.*)"\);') + +class FacebookTestLogin(FacebookTestCase): + def test_login_succeful(self): + response = self.client.get(self.reverse('begin', 'facebook')) + # social_auth must redirect to service page + self.assertEqual(response.status_code, 302) + + # Open first redirect page, it contains user login form because + # we don't have cookie to send to twitter + parser = FormParser('login_form') + parser.feed(self.get_content(response['Location'], use_cookies=True)) + auth = {'email': self.user, + 'pass': self.passwd} + + # Check that action and values were loaded properly + self.assertTrue(parser.action) + self.assertTrue(parser.values) + + # Post login form, will return authorization or redirect page + parser.values.update(auth) + content = self.get_content(parser.action, parser.values, + use_cookies=True) + + # If page contains a form#login_form, then we are in the app + # authorization page because the app is not authorized yet, + # otherwise the app already gained permission and twitter sends + # a page that redirects to redirect_url + if 'uiserver_form' in content: + # authorization form post, returns redirect_page + parser = FormParser('uiserver_form').feed(content) + self.assertTrue(parser.action) + self.assertTrue(parser.values) + parser.values.update(auth) + redirect_page = self.get_content(parser.action, parser.values, + use_cookies=True) + else: + redirect_page = content + + url = REDIRECT_RE.findall(redirect_page) + self.assertTrue(url) + # URL comes on JS scaped, needs to be clean first, any better way? + url = url[0].replace('\u0025', '%').replace('\\', '') + response = self.client.get(self.make_relative(url)) -- 2.39.5