]> git.parisson.com Git - django-social-auth.git/commitdiff
Twitter tests using urllib2 module. Refs gh-25
authorMatías Aguirre <matiasaguirre@gmail.com>
Fri, 25 Mar 2011 05:55:05 +0000 (02:55 -0300)
committerMatías Aguirre <matiasaguirre@gmail.com>
Fri, 25 Mar 2011 05:55:05 +0000 (02:55 -0300)
social_auth/tests/base.py
social_auth/tests/twitter.py

index 3bd40a6592e6e5ed46e6349fa1ae0c5ff68b0b76..4255c7f20951e85aec13f552c9054b7dcc5deb2e 100644 (file)
@@ -1,3 +1,5 @@
+import urllib2
+import cookielib
 import urllib
 import urlparse
 import unittest
@@ -9,15 +11,27 @@ from django.core.urlresolvers import reverse
 
 class SocialAuthTestsCase(unittest.TestCase):
     """Base class for social auth tests"""
+    SERVER_NAME = None
+    SERVER_PORT = None
+
     def __init__(self, *args, **kwargs):
-        self.client = Client()
+        client_kwargs = {}
+        if self.SERVER_NAME:
+            client_kwargs['SERVER_NAME'] = self.SERVER_NAME
+        if self.SERVER_PORT:
+            client_kwargs['SERVER_PORT'] = self.SERVER_PORT
+        self.client = Client(**client_kwargs)
+        self.jar = cookielib.CookieJar()
         super(SocialAuthTestsCase, self).__init__(*args, **kwargs)
 
-    def get_content(self, url, data=None):
+    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) or data
-        return ''.join(urllib.urlopen(url, data=data).readlines())
+        data = data and urllib.urlencode(data, doseq=True) or data
+        agent = urllib2.build_opener()
+        if use_cookies:
+            agent.add_handler(urllib2.HTTPCookieProcessor(self.jar))
+        return ''.join(agent.open(url, data=data).readlines())
 
     def reverse(self, name, backend):
         """Reverses backend URL by name"""
index da15d55bebe1dfe3518e5ff98cee74ba55b9ea48..2d16f846f62ec16f832c80820361ea2934a6597f 100644 (file)
@@ -1,6 +1,5 @@
 from django.conf import settings
 
-from social_auth.backends.twitter import TwitterAuth
 from social_auth.tests.base import SocialAuthTestsCase, FormParser, RefreshParser
 
 
@@ -13,7 +12,9 @@ class TwitterTestCase(SocialAuthTestsCase):
         self.assertTrue(self.user)
         self.assertTrue(self.passwd)
 
-    def testLogin(self):
+
+class TwitterTestLogin(TwitterTestCase):
+    def test_login_succeful(self):
         response = self.client.get(self.reverse('begin', 'twitter'))
         # social_auth must redirect to service page
         self.assertEqual(response.status_code, 302)
@@ -57,8 +58,3 @@ class TwitterTestCase(SocialAuthTestsCase):
         location = self.make_relative(response['Location'])
         login_redirect = getattr(settings, 'LOGIN_REDIRECT_URL', '')
         self.assertTrue(location == login_redirect)
-
-    def runTest(self):
-        # don't run tests twitter backend is not enabled
-        if TwitterAuth.enabled:
-            self.testLogin()