File: strategy.py

package info (click to toggle)
python-social-auth 0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,828 kB
  • ctags: 3,245
  • sloc: python: 12,867; makefile: 119; sh: 3
file content (99 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from social.strategies.base import BaseStrategy, BaseTemplateStrategy


TEST_URI = 'http://myapp.com'
TEST_HOST = 'myapp.com'


class Redirect(object):
    def __init__(self, url):
        self.url = url


class TestTemplateStrategy(BaseTemplateStrategy):
    def render_template(self, tpl, context):
        return tpl

    def render_string(self, html, context):
        return html


class TestStrategy(BaseStrategy):
    DEFAULT_TEMPLATE_STRATEGY = TestTemplateStrategy

    def __init__(self, storage, tpl=None):
        self._request_data = {}
        self._settings = {}
        self._session = {}
        super(TestStrategy, self).__init__(storage, tpl)

    def redirect(self, url):
        return Redirect(url)

    def get_setting(self, name):
        """Return value for given setting name"""
        return self._settings[name]

    def html(self, content):
        """Return HTTP response with given content"""
        return content

    def render_html(self, tpl=None, html=None, context=None):
        """Render given template or raw html with given context"""
        return tpl or html

    def request_data(self, merge=True):
        """Return current request data (POST or GET)"""
        return self._request_data

    def request_host(self):
        """Return current host value"""
        return TEST_HOST

    def session_get(self, name, default=None):
        """Return session value for given key"""
        return self._session.get(name, default)

    def session_set(self, name, value):
        """Set session value for given key"""
        self._session[name] = value

    def session_pop(self, name):
        """Pop session value for given key"""
        return self._session.pop(name, None)

    def build_absolute_uri(self, path=None):
        """Build absolute URI with given (optional) path"""
        path = path or ''
        if path.startswith('http://') or path.startswith('https://'):
            return path
        return TEST_URI + path

    def set_settings(self, values):
        self._settings.update(values)

    def set_request_data(self, values, backend):
        self._request_data.update(values)
        backend.data = self._request_data

    def remove_from_request_data(self, name):
        self._request_data.pop(name, None)

    def authenticate(self, *args, **kwargs):
        user = super(TestStrategy, self).authenticate(*args, **kwargs)
        if isinstance(user, self.storage.user.user_model()):
            self.session_set('username', user.username)
        return user

    def get_pipeline(self):
        return self.setting('PIPELINE', (
            'social.pipeline.social_auth.social_details',
            'social.pipeline.social_auth.social_uid',
            'social.pipeline.social_auth.auth_allowed',
            'social.pipeline.social_auth.social_user',
            'social.pipeline.user.get_username',
            'social.pipeline.social_auth.associate_by_email',
            'social.pipeline.user.create_user',
            'social.pipeline.social_auth.associate_user',
            'social.pipeline.social_auth.load_extra_data',
            'social.pipeline.user.user_details'))