]> git.parisson.com Git - django-social-auth.git/commitdiff
fixed tests: default pipelines, fixed twitter&facebook, respect for SOCIAL_AUTH_ENABL...
authorAndrii Kostenko <andrey@kostenko.name>
Mon, 9 Apr 2012 12:45:57 +0000 (16:45 +0400)
committerAndrii Kostenko <andrey@kostenko.name>
Mon, 9 Apr 2012 12:45:57 +0000 (16:45 +0400)
social_auth/tests/base.py
social_auth/tests/facebook.py
social_auth/tests/google.py
social_auth/tests/twitter.py

index 26695eae1b9da9d1c9b8069400449c0a4072cda4..f5bed808a38eae9500248498c4593f3c1c843865 100644 (file)
@@ -5,6 +5,7 @@ import urllib
 import urlparse
 import unittest
 from sgmllib import SGMLParser
+from django.conf import settings
 
 from django.test.client import Client
 from django.core.urlresolvers import reverse
@@ -29,6 +30,25 @@ class SocialAuthTestsCase(unittest.TestCase):
         self.client = Client(**client_kwargs)
         super(SocialAuthTestsCase, self).__init__(*args, **kwargs)
 
+    def setUp(self):
+        from social_auth import backends
+        self.old_PIPELINE = backends.PIPELINE
+        backends.PIPELINE = (
+                'social_auth.backends.pipeline.social.social_auth_user',
+                'social_auth.backends.pipeline.associate.associate_by_email',
+                'social_auth.backends.pipeline.user.get_username',
+                'social_auth.backends.pipeline.user.create_user',
+                'social_auth.backends.pipeline.social.associate_user',
+                'social_auth.backends.pipeline.social.load_extra_data',
+                'social_auth.backends.pipeline.user.update_user_details',
+                )
+        super(SocialAuthTestsCase, self).setUp()
+
+    def tearDown(self):
+        from social_auth import backends
+        backends.PIPELINE = self.old_PIPELINE
+        super(SocialAuthTestsCase, self).tearDown()
+
     def test_backend_cache(self):
         """Ensure that the backend for the testcase gets cached."""
         try:
@@ -36,10 +56,13 @@ class SocialAuthTestsCase(unittest.TestCase):
         except AttributeError:
             pass
         else:
+            if self.name not in settings.SOCIAL_AUTH_ENABLED_BACKENDS:
+                # this backend is not enabled (for example, google-openid/google-oauth2)
+                return
             from social_auth import backends
             backends.BACKENDS = {}
             self.client.get(self.reverse('socialauth_begin', self.name))
-            self.assertTrue(self.name in backends.BACKENDS)
+            self.assertTrue(self.name in backends.BACKENDSCACHE)
 
     def get_content(self, url, data=None, use_cookies=False):
         """Return content for given url, if data is not None, then a POST
index d0a9601097611d06c209713fe9f7cd279b8628e8..7e007497f5be13e0a384e6657caaf0a227100181 100644 (file)
@@ -2,11 +2,17 @@ import re
 
 from social_auth.utils import setting
 from social_auth.tests.base import SocialAuthTestsCase, FormParserByID
+from django.contrib.sites.models import Site
 
 
 class FacebookTestCase(SocialAuthTestsCase):
     SERVER_NAME = 'myapp.com'
     SERVER_PORT = '8000'
+
+    def __init__(self, methodName='runTest'):
+        self.SERVER_NAME = Site.objects.get_current()
+        super(FacebookTestCase, self).__init__(methodName)
+
     name = 'facebook'
 
     def setUp(self, *args, **kwargs):
@@ -20,18 +26,20 @@ class FacebookTestCase(SocialAuthTestsCase):
 
 REDIRECT_RE = re.compile('window.location.replace\("(.*)"\);')
 
-
 class FacebookTestLogin(FacebookTestCase):
     def test_login_succeful(self):
-        response = self.client.get(self.reverse('socialauth_begin',
-                                                'facebook'))
+        """
+
+        """
+        response = self.client.get('http://%s%s' % (self.SERVER_NAME, self.reverse('socialauth_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 = FormParserByID('login_form')
-        parser.feed(self.get_content(response['Location'], use_cookies=True))
+        content = self.get_content(response['Location'], use_cookies=True)
+        parser.feed(content)
         auth = {'email': self.user,
                 'pass': self.passwd}
 
@@ -41,26 +49,36 @@ class FacebookTestLogin(FacebookTestCase):
 
         # Post login form, will return authorization or redirect page
         parser.values.update(auth)
-        content = self.get_content(parser.action, parser.values,
+        redirect = self.get_redirect(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:
+        if 'login_form' in content:
             # authorization form post, returns redirect_page
-            parser = FormParserByID('uiserver_form').feed(content)
+            parser = FormParserByID('login_form')
+            parser.feed(content)
             self.assertTrue(parser.action)
             self.assertTrue(parser.values)
             parser.values.update(auth)
-            redirect_page = self.get_content(parser.action, parser.values,
+            redirect = self.get_redirect(parser.action, parser.values,
                                              use_cookies=True)
+            redirect_page = redirect.read()
         else:
-            redirect_page = content
+            redirect = self.get_redirect(redirect.headers['Location'],
+                use_cookies=True)
+            redirect_page = redirect.read()
+
+        if 'uiserver_form' in redirect_page:
+            # authorization form post, returns redirect_page
+            parser = FormParserByID('uiserver_form')
+            parser.feed(redirect_page)
+            self.assertTrue(parser.action)
+            self.assertTrue(parser.values)
+            parser.values.update(auth)
+            redirect = self.get_redirect(parser.action, parser.values,
+                use_cookies=True)
+
 
-        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))
+        self.assertTrue(setting('LOGIN_REDIRECT_URL') in self.make_relative(redirect.headers['Location']))
index d7c3d9a35ced292dfedb68f20dc7c1ee14f11d63..0f35968c03d0590bf2a5762d4b6e08cbae15d725 100644 (file)
@@ -3,7 +3,7 @@ import re
 from social_auth.utils import setting
 from social_auth.tests.base import SocialAuthTestsCase, FormParserByID, \
                                    FormParser, RefreshParser
-
+from django.conf import settings
 
 class GoogleTestCase(SocialAuthTestsCase):
 
@@ -26,6 +26,8 @@ class GoogleOpenIdTestLogin(GoogleTestCase):
     SERVER_PORT = '8000'
 
     def test_login_succeful(self):
+        if self.name not in settings.SOCIAL_AUTH_ENABLED_BACKENDS:
+            self.skipTest('Google OpenID is not enabled')
         response = self.client.get(self.reverse('socialauth_begin', 'google'))
 
         parser = FormParserByID('openid_message')
index ee5cc2342e5c82f75d522249a41f2706b58c952e..30b588c97bac6b85af14389c30cce8c3a0cf50ca 100644 (file)
@@ -25,7 +25,7 @@ class TwitterTestLogin(TwitterTestCase):
         # Open first redirect page, it contains user login form because
         # we don't have cookie to send to twitter
         login_content = self.get_content(response['Location'])
-        parser = FormParserByID('login_form')
+        parser = FormParserByID('oauth_form')
         parser.feed(login_content)
         auth = {'session[username_or_email]': self.user,
                 'session[password]': self.passwd}