]> git.parisson.com Git - django-social-auth.git/commitdiff
Facebook login testing. Refs gh-25
authorMatías Aguirre <matiasaguirre@gmail.com>
Fri, 25 Mar 2011 08:02:13 +0000 (05:02 -0300)
committerMatías Aguirre <matiasaguirre@gmail.com>
Fri, 25 Mar 2011 08:02:13 +0000 (05:02 -0300)
README.rst
doc/testing.rst
social_auth/tests/__init__.py
social_auth/tests/base.py
social_auth/tests/facebook.py [new file with mode: 0644]

index 54b3438883f0e55e617ec7a57eb1ef2fd08c0448..fe5ea9dec629201c2f6d8560b0f746fac1d3668d 100644 (file)
@@ -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
index 392b609251e5fb95cae5e95d2ff8e79ebd997b77..869a0f65028aae3666b903782124188954505eaa 100644 (file)
@@ -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'
index 196a8ca4532a25dc3515a700c66f855f64144abb..d5fc463e7c3f05009ee7b5612a369bc5acf408ab 100644 (file)
@@ -1 +1,2 @@
 from social_auth.tests.twitter import *
+from social_auth.tests.facebook import *
index 4255c7f20951e85aec13f552c9054b7dcc5deb2e..0104a079e4bbfef3013278b28b9fbc188e206000 100644 (file)
@@ -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 (file)
index 0000000..d2f7d31
--- /dev/null
@@ -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))