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
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'
from django.core.urlresolvers import reverse
+USER_AGENT = 'Mozilla/5.0'
+
+
class SocialAuthTestsCase(unittest.TestCase):
"""Base class for social auth tests"""
SERVER_NAME = None
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"""
--- /dev/null
+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))