~ubuntu-branches/debian/sid/social-auth-core/sid

« back to all changes in this revision

Viewing changes to social_core/tests/strategy.py

  • Committer: Package Import Robot
  • Author(s): Andre Bianchi
  • Date: 2018-02-22 19:49:12 UTC
  • Revision ID: package-import@ubuntu.com-20180222194912-4lqv8mlhnqc4ncd3
Tags: upstream-1.7.0
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from ..strategy import BaseStrategy, BaseTemplateStrategy
 
2
 
 
3
 
 
4
TEST_URI = 'http://myapp.com'
 
5
TEST_HOST = 'myapp.com'
 
6
 
 
7
 
 
8
class Redirect(object):
 
9
    def __init__(self, url):
 
10
        self.url = url
 
11
 
 
12
 
 
13
class TestTemplateStrategy(BaseTemplateStrategy):
 
14
    def render_template(self, tpl, context):
 
15
        return tpl
 
16
 
 
17
    def render_string(self, html, context):
 
18
        return html
 
19
 
 
20
 
 
21
class TestStrategy(BaseStrategy):
 
22
    DEFAULT_TEMPLATE_STRATEGY = TestTemplateStrategy
 
23
 
 
24
    def __init__(self, storage, tpl=None):
 
25
        self._request_data = {}
 
26
        self._settings = {}
 
27
        self._session = {}
 
28
        super(TestStrategy, self).__init__(storage, tpl)
 
29
 
 
30
    def redirect(self, url):
 
31
        return Redirect(url)
 
32
 
 
33
    def get_setting(self, name):
 
34
        """Return value for given setting name"""
 
35
        return self._settings[name]
 
36
 
 
37
    def html(self, content):
 
38
        """Return HTTP response with given content"""
 
39
        return content
 
40
 
 
41
    def render_html(self, tpl=None, html=None, context=None):
 
42
        """Render given template or raw html with given context"""
 
43
        return tpl or html
 
44
 
 
45
    def request_data(self, merge=True):
 
46
        """Return current request data (POST or GET)"""
 
47
        return self._request_data
 
48
 
 
49
    def request_host(self):
 
50
        """Return current host value"""
 
51
        return TEST_HOST
 
52
 
 
53
    def request_is_secure(self):
 
54
        """ Is the request using HTTPS? """
 
55
        return False
 
56
 
 
57
    def request_path(self):
 
58
        """ path of the current request """
 
59
        return ''
 
60
 
 
61
    def request_port(self):
 
62
        """ Port in use for this request """
 
63
        return 80
 
64
 
 
65
    def request_get(self):
 
66
        """ Request GET data """
 
67
        return self._request_data.copy()
 
68
 
 
69
    def request_post(self):
 
70
        """ Request POST data """
 
71
        return self._request_data.copy()
 
72
 
 
73
    def session_get(self, name, default=None):
 
74
        """Return session value for given key"""
 
75
        return self._session.get(name, default)
 
76
 
 
77
    def session_set(self, name, value):
 
78
        """Set session value for given key"""
 
79
        self._session[name] = value
 
80
 
 
81
    def session_pop(self, name):
 
82
        """Pop session value for given key"""
 
83
        return self._session.pop(name, None)
 
84
 
 
85
    def build_absolute_uri(self, path=None):
 
86
        """Build absolute URI with given (optional) path"""
 
87
        path = path or ''
 
88
        if path.startswith('http://') or path.startswith('https://'):
 
89
            return path
 
90
        return TEST_URI + path
 
91
 
 
92
    def set_settings(self, values):
 
93
        self._settings.update(values)
 
94
 
 
95
    def set_request_data(self, values, backend):
 
96
        self._request_data.update(values)
 
97
        backend.data = self._request_data
 
98
 
 
99
    def remove_from_request_data(self, name):
 
100
        self._request_data.pop(name, None)
 
101
 
 
102
    def authenticate(self, *args, **kwargs):
 
103
        user = super(TestStrategy, self).authenticate(*args, **kwargs)
 
104
        if isinstance(user, self.storage.user.user_model()):
 
105
            self.session_set('username', user.username)
 
106
        return user
 
107
 
 
108
    def get_pipeline(self, backend=None):
 
109
        return self.setting(
 
110
            'PIPELINE',
 
111
            (
 
112
                'social_core.pipeline.social_auth.social_details',
 
113
                'social_core.pipeline.social_auth.social_uid',
 
114
                'social_core.pipeline.social_auth.auth_allowed',
 
115
                'social_core.pipeline.social_auth.social_user',
 
116
                'social_core.pipeline.user.get_username',
 
117
                'social_core.pipeline.social_auth.associate_by_email',
 
118
                'social_core.pipeline.user.create_user',
 
119
                'social_core.pipeline.social_auth.associate_user',
 
120
                'social_core.pipeline.social_auth.load_extra_data',
 
121
                'social_core.pipeline.user.user_details'
 
122
            ),
 
123
            backend
 
124
        )