Statistics
| Branch: | Tag: | Revision:

root / social_auth / tests / twitter.py @ b5a2ce2a

History | View | Annotate | Download (3.1 kB)

1
from social_auth.utils import setting
2
from social_auth.tests.base import SocialAuthTestsCase, FormParserByID, \
3
                                   RefreshParser
4
from django.test.utils import override_settings
5

    
6

    
7
class TwitterTestCase(SocialAuthTestsCase):
8

    
9
    name = 'twitter'
10

    
11
    def setUp(self, *args, **kwargs):
12
        super(TwitterTestCase, self).setUp(*args, **kwargs)
13
        self.user = setting('TEST_TWITTER_USER')
14
        self.passwd = setting('TEST_TWITTER_PASSWORD')
15
        # check that user and password are setup properly
16
        self.assertTrue(self.user)
17
        self.assertTrue(self.passwd)
18

    
19

    
20
class TwitterTestLogin(TwitterTestCase):
21
    @override_settings(SOCIAL_AUTH_PIPELINE = (
22
        'social_auth.backends.pipeline.social.social_auth_user',
23
        'social_auth.backends.pipeline.associate.associate_by_email',
24
        'social_auth.backends.pipeline.user.get_username',
25
        'social_auth.backends.pipeline.misc.save_status_to_session',
26
        'social_auth.backends.pipeline.social.associate_user',
27
        'social_auth.backends.pipeline.social.load_extra_data',
28
        'social_auth.backends.pipeline.user.update_user_details',
29
        ))
30
    def test_login_succeful(self):
31
        response = self.client.get(self.reverse('socialauth_begin', 'twitter'))
32
        # social_auth must redirect to service page
33
        self.assertEqual(response.status_code, 302)
34

    
35
        # Open first redirect page, it contains user login form because
36
        # we don't have cookie to send to twitter
37
        login_content = self.get_content(response['Location'])
38
        parser = FormParserByID('oauth_form')
39
        parser.feed(login_content)
40
        auth = {'session[username_or_email]': self.user,
41
                'session[password]': self.passwd}
42

    
43
        # Check that action and values were loaded properly
44
        self.assertTrue(parser.action)
45
        self.assertTrue(parser.values)
46

    
47
        # Post login form, will return authorization or redirect page
48
        parser.values.update(auth)
49
        content = self.get_content(parser.action, data=parser.values)
50

    
51
        # If page contains a form#login_form, then we are in the app
52
        # authorization page because the app is not authorized yet,
53
        # otherwise the app already gained permission and twitter sends
54
        # a page that redirects to redirect_url
55
        if 'login_form' in content:
56
            # authorization form post, returns redirect_page
57
            parser = FormParserByID('login_form').feed(content)
58
            self.assertTrue(parser.action)
59
            self.assertTrue(parser.values)
60
            parser.values.update(auth)
61
            redirect_page = self.get_content(parser.action, data=parser.values)
62
        else:
63
            redirect_page = content
64

    
65
        parser = RefreshParser()
66
        parser.feed(redirect_page)
67
        self.assertTrue(parser.value)
68

    
69
        response = self.client.get(self.make_relative(parser.value))
70
        self.assertEqual(response.status_code, 302)
71
        location = self.make_relative(response['Location'])
72
        login_redirect = setting('LOGIN_REDIRECT_URL')
73
        self.assertTrue(location == login_redirect)