File: test_utils.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 (149 lines) | stat: -rw-r--r-- 4,958 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import sys
import unittest

from mock import Mock
from sure import expect

from social.utils import sanitize_redirect, user_is_authenticated, \
                         user_is_active, slugify, build_absolute_uri, \
                         partial_pipeline_data


PY3 = sys.version_info[0] == 3


class SanitizeRedirectTest(unittest.TestCase):
    def test_none_redirect(self):
        expect(sanitize_redirect('myapp.com', None)).to.equal(None)

    def test_empty_redirect(self):
        expect(sanitize_redirect('myapp.com', '')).to.equal(None)

    def test_dict_redirect(self):
        expect(sanitize_redirect('myapp.com', {})).to.equal(None)

    def test_invalid_redirect(self):
        expect(sanitize_redirect('myapp.com',
                                 {'foo': 'bar'})).to.equal(None)

    def test_wrong_path_redirect(self):
        expect(sanitize_redirect(
            'myapp.com',
            'http://notmyapp.com/path/'
        )).to.equal(None)

    def test_valid_absolute_redirect(self):
        expect(sanitize_redirect(
            'myapp.com',
            'http://myapp.com/path/'
        )).to.equal('http://myapp.com/path/')

    def test_valid_relative_redirect(self):
        expect(sanitize_redirect('myapp.com', '/path/')).to.equal('/path/')


class UserIsAuthenticatedTest(unittest.TestCase):
    def test_user_is_none(self):
        expect(user_is_authenticated(None)).to.equal(False)

    def test_user_is_not_none(self):
        expect(user_is_authenticated(object())).to.equal(True)

    def test_user_has_is_authenticated(self):
        class User(object):
            is_authenticated = True
        expect(user_is_authenticated(User())).to.equal(True)

    def test_user_has_is_authenticated_callable(self):
        class User(object):
            def is_authenticated(self):
                return True
        expect(user_is_authenticated(User())).to.equal(True)


class UserIsActiveTest(unittest.TestCase):
    def test_user_is_none(self):
        expect(user_is_active(None)).to.equal(False)

    def test_user_is_not_none(self):
        expect(user_is_active(object())).to.equal(True)

    def test_user_has_is_active(self):
        class User(object):
            is_active = True
        expect(user_is_active(User())).to.equal(True)

    def test_user_has_is_active_callable(self):
        class User(object):
            def is_active(self):
                return True
        expect(user_is_active(User())).to.equal(True)


class SlugifyTest(unittest.TestCase):
    def test_slugify_formats(self):
        if PY3:
            expect(slugify('FooBar')).to.equal('foobar')
            expect(slugify('Foo Bar')).to.equal('foo-bar')
            expect(slugify('Foo (Bar)')).to.equal('foo-bar')
        else:
            expect(slugify('FooBar'.decode('utf-8'))).to.equal('foobar')
            expect(slugify('Foo Bar'.decode('utf-8'))).to.equal('foo-bar')
            expect(slugify('Foo (Bar)'.decode('utf-8'))).to.equal('foo-bar')


class BuildAbsoluteURITest(unittest.TestCase):
    def setUp(self):
        self.host = 'http://foobar.com'

    def tearDown(self):
        self.host = None

    def test_path_none(self):
        expect(build_absolute_uri(self.host)).to.equal(self.host)

    def test_path_empty(self):
        expect(build_absolute_uri(self.host, '')).to.equal(self.host)

    def test_path_http(self):
        expect(build_absolute_uri(self.host, 'http://barfoo.com')) \
              .to.equal('http://barfoo.com')

    def test_path_https(self):
        expect(build_absolute_uri(self.host, 'https://barfoo.com')) \
              .to.equal('https://barfoo.com')

    def test_host_ends_with_slash_and_path_starts_with_slash(self):
        expect(build_absolute_uri(self.host + '/', '/foo/bar')) \
              .to.equal('http://foobar.com/foo/bar')

    def test_absolute_uri(self):
        expect(build_absolute_uri(self.host, '/foo/bar')) \
              .to.equal('http://foobar.com/foo/bar')


class PartialPipelineData(unittest.TestCase):
    def test_kwargs_included_in_result(self):
        backend = self._backend()
        kwargitem = ('foo', 'bar')
        _, xkwargs = partial_pipeline_data(backend, None,
                                           *(), **dict([kwargitem]))
        xkwargs.should.have.key(kwargitem[0]).being.equal(kwargitem[1])

    def test_update_user(self):
        user = object()
        backend = self._backend(session_kwargs={'user': None})
        _, xkwargs = partial_pipeline_data(backend, user)
        xkwargs.should.have.key('user').being.equal(user)

    def _backend(self, session_kwargs=None):
        strategy = Mock()
        strategy.request = None
        strategy.session_get.return_value = object()
        strategy.partial_from_session.return_value = \
            (0, 'mock-backend', [], session_kwargs or {})

        backend = Mock()
        backend.name = 'mock-backend'
        backend.strategy = strategy
        return backend