From d0984d24f4559facc0c5bca16c154fb52339c3a9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Sun, 27 Feb 2011 14:24:47 -0200 Subject: [PATCH] Initial testing suite. Only twitter so far, more to come soon. Refs gh-25 --- README.rst | 16 +++++++ social_auth/tests/twitter.py | 91 +++++------------------------------- 2 files changed, 28 insertions(+), 79 deletions(-) diff --git a/README.rst b/README.rst index d224df6..a86a455 100644 --- a/README.rst +++ b/README.rst @@ -413,6 +413,22 @@ anonymous values will be used if not configured as described in their check which Apps are included in their `Google Data Protocol Directory`_ +------- +Testing +------- +To test the app just run:: + + ./manage.py test social_auth + +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:: + + TEST_TWITTER_USER = 'testing_account' + TEST_TWITTER_PASSWORD = 'password_for_testing_account' + ---- Bugs diff --git a/social_auth/tests/twitter.py b/social_auth/tests/twitter.py index 5e8986f..da15d55 100644 --- a/social_auth/tests/twitter.py +++ b/social_auth/tests/twitter.py @@ -1,41 +1,18 @@ -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 from social_auth.backends.twitter import TwitterAuth - - -SERVER_NAME = 'myapp.com' -SERVER_PORT = '8000' -USER = 'social_auth' -PASSWD = 'pass(twitter).7' - - -class SocialAuthTestsCase(unittest.TestCase): - def __init__(self, *args, **kwargs): - self.client = Client() - super(SocialAuthTestsCase, self).__init__(*args, **kwargs) - - def get_content(self, url, data=None): - """Open URL and return content""" - data = data and urllib.urlencode(data) or data - return ''.join(urllib.urlopen(url, data=data).readlines()) - - def reverse(self, name, backend): - return reverse(name, args=(backend,)) - - def make_relative(self, value): - parsed = urlparse.urlparse(value) - return urlparse.urlunparse(('', '', parsed.path, parsed.params, - parsed.query, parsed.fragment)) +from social_auth.tests.base import SocialAuthTestsCase, FormParser, RefreshParser class TwitterTestCase(SocialAuthTestsCase): + def setUp(self, *args, **kwargs): + super(TwitterTestCase, self).setUp(*args, **kwargs) + self.user = getattr(settings, 'TEST_TWITTER_USER', None) + self.passwd = getattr(settings, 'TEST_TWITTER_PASSWORD', None) + # check that user and password are setup properly + self.assertTrue(self.user) + self.assertTrue(self.passwd) + def testLogin(self): response = self.client.get(self.reverse('begin', 'twitter')) # social_auth must redirect to service page @@ -46,8 +23,8 @@ class TwitterTestCase(SocialAuthTestsCase): login_content = self.get_content(response['Location']) parser = FormParser('login_form') parser.feed(login_content) - auth = {'session[username_or_email]': USER, - 'session[password]': PASSWD} + auth = {'session[username_or_email]': self.user, + 'session[password]': self.passwd} # Check that action and values were loaded properly self.assertTrue(parser.action) @@ -82,50 +59,6 @@ class TwitterTestCase(SocialAuthTestsCase): self.assertTrue(location == login_redirect) def runTest(self): - # don't run tests if it's not enabled + # don't run tests twitter backend is not enabled if TwitterAuth.enabled: self.testLogin() - - -class TwitterParser(SGMLParser): - def feed(self, data): - SGMLParser.feed(self, data) - self.close() - return self - - -class FormParser(TwitterParser): - def __init__(self, form_id, *args, **kwargs): - TwitterParser.__init__(self, *args, **kwargs) - self.form_id = form_id - self.inside_form = False - self.action = None - self.values = {} - - def start_form(self, attributes): - attrs = dict(attributes) - if attrs.get('id') == self.form_id: - self.inside_form = True - self.action = attrs.get('action') - - def end_form(self): - self.inside_form = False - - def start_input(self, attributes): - attrs = dict(attributes) - if self.inside_form: - type, name, value = attrs.get('type'), attrs.get('name'), \ - attrs.get('value') - if name and value and type in ('text', 'hidden', 'password'): - self.values[name] = value - - -class RefreshParser(TwitterParser): - def __init__(self, *args, **kwargs): - TwitterParser.__init__(self, *args, **kwargs) - self.value = None - - def start_meta(self, attributes): - attrs = dict(attributes) - if attrs.get('http-equiv') == 'refresh': - self.value = attrs.get('content').lstrip('0;url=') -- 2.39.5