From 0b2997a19d92df0d485d2937f5d3ac39c28b9331 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Fri, 25 Mar 2011 02:55:05 -0300 Subject: [PATCH] Twitter tests using urllib2 module. Refs gh-25 --- social_auth/tests/base.py | 22 ++++++++++++++++++---- social_auth/tests/twitter.py | 10 +++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/social_auth/tests/base.py b/social_auth/tests/base.py index 3bd40a6..4255c7f 100644 --- a/social_auth/tests/base.py +++ b/social_auth/tests/base.py @@ -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""" diff --git a/social_auth/tests/twitter.py b/social_auth/tests/twitter.py index da15d55..2d16f84 100644 --- a/social_auth/tests/twitter.py +++ b/social_auth/tests/twitter.py @@ -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() -- 2.39.5