Esempio n. 1
0
    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')
        parser.feed(response.content)
        # Check that action and values were loaded properly
        self.assertTrue(parser.action)
        self.assertTrue(parser.values)
        content = self.get_content(parser.action,
                                   parser.values,
                                   use_cookies=True)

        parser = FormParserByID('gaia_loginform')
        parser.feed(content)
        auth = {'Email': self.user, 'Passwd': self.passwd}
        parser.values.update(auth)
        # Check that action and values were loaded properly
        self.assertTrue(parser.action)
        self.assertTrue(parser.values)

        content = self.get_content(parser.action,
                                   parser.values,
                                   use_cookies=True)
        parser = RefreshParser()
        parser.feed(content)

        # approved?
        result = self.get_redirect(parser.value, use_cookies=True)
        if result.headers.get('Location', ''):  # approved?
            # damn, google has a hell of redirects :-(
            result = self.get_redirect(result.headers['Location'],
                                       use_cookies=True)
            result = self.get_redirect(result.headers['Location'],
                                       use_cookies=True)
            result = self.get_redirect(result.headers['Location'],
                                       use_cookies=True)

        # app was not approved
        if self.SERVER_NAME not in result.headers.get('Location', ''):
            content = self.get_content(parser.value, use_cookies=True)
            parser = FormParser()
            parser.feed(content)
            parser.values['submit_true'] = 'yes'
            parser.values['remember_choices'] = 'yes'
            result = self.get_redirect(parser.action,
                                       parser.values,
                                       use_cookies=True)

        response = self.client.get(
            self.make_relative(result.headers['Location']))
        self.assertTrue(setting('LOGIN_REDIRECT_URL') in \
                            self.make_relative(response['Location']))
Esempio n. 2
0
    def test_login_succeful(self):
        response = self.client.get(self.reverse('socialauth_begin', 'twitter'))
        # 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
        login_content = self.get_content(response['Location'])
        parser = FormParserByID('login_form')
        parser.feed(login_content)
        auth = {
            'session[username_or_email]': self.user,
            'session[password]': self.passwd
        }

        # Check that action and values were loaded properly
        self.assertTrue(parser.action)
        self.assertTrue(parser.values)

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

        # 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 'login_form' in content:
            # authorization form post, returns redirect_page
            parser = FormParserByID('login_form').feed(content)
            self.assertTrue(parser.action)
            self.assertTrue(parser.values)
            parser.values.update(auth)
            redirect_page = self.get_content(parser.action, data=parser.values)
        else:
            redirect_page = content

        parser = RefreshParser()
        parser.feed(redirect_page)
        self.assertTrue(parser.value)

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