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

« back to all changes in this revision

Viewing changes to social_core/tests/backends/test_dummy.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
import json
 
2
import datetime
 
3
import time
 
4
 
 
5
from httpretty import HTTPretty
 
6
 
 
7
from ...actions import do_disconnect
 
8
from ...backends.oauth import BaseOAuth2
 
9
from ...exceptions import AuthForbidden
 
10
 
 
11
from ..models import User
 
12
from .oauth import OAuth2Test
 
13
 
 
14
 
 
15
class DummyOAuth2(BaseOAuth2):
 
16
    name = 'dummy'
 
17
    AUTHORIZATION_URL = 'http://dummy.com/oauth/authorize'
 
18
    ACCESS_TOKEN_URL = 'http://dummy.com/oauth/access_token'
 
19
    REVOKE_TOKEN_URL = 'https://dummy.com/oauth/revoke'
 
20
    REVOKE_TOKEN_METHOD = 'GET'
 
21
    GET_ALL_EXTRA_DATA = False
 
22
    EXTRA_DATA = [
 
23
        ('id', 'id'),
 
24
        ('expires', 'expires'),
 
25
        ('empty', 'empty', True),
 
26
        'url'
 
27
    ]
 
28
 
 
29
    def get_user_details(self, response):
 
30
        """Return user details from Github account"""
 
31
        return {'username': response.get('username'),
 
32
                'email': response.get('email', ''),
 
33
                'first_name': response.get('first_name', ''),
 
34
                'last_name': response.get('last_name', '')}
 
35
 
 
36
    def user_data(self, access_token, *args, **kwargs):
 
37
        """Loads user data from service"""
 
38
        return self.get_json('http://dummy.com/user', params={
 
39
            'access_token': access_token
 
40
        })
 
41
 
 
42
 
 
43
class Dummy2OAuth2(DummyOAuth2):
 
44
    GET_ALL_EXTRA_DATA = True
 
45
 
 
46
 
 
47
class DummyOAuth2Test(OAuth2Test):
 
48
    backend_path = 'social_core.tests.backends.test_dummy.DummyOAuth2'
 
49
    user_data_url = 'http://dummy.com/user'
 
50
    expected_username = 'foobar'
 
51
    access_token_body = json.dumps({
 
52
        'access_token': 'foobar',
 
53
        'token_type': 'bearer'
 
54
    })
 
55
    user_data_body = json.dumps({
 
56
        'id': 1,
 
57
        'username': 'foobar',
 
58
        'url': 'http://dummy.com/user/foobar',
 
59
        'first_name': 'Foo',
 
60
        'last_name': 'Bar',
 
61
        'email': 'foo@bar.com'
 
62
    })
 
63
 
 
64
    def test_login(self):
 
65
        self.do_login()
 
66
 
 
67
    def test_partial_pipeline(self):
 
68
        self.do_partial_pipeline()
 
69
 
 
70
    def test_tokens(self):
 
71
        user = self.do_login()
 
72
        self.assertEqual(user.social[0].access_token, 'foobar')
 
73
 
 
74
    def test_revoke_token(self):
 
75
        self.strategy.set_settings({
 
76
            'SOCIAL_AUTH_REVOKE_TOKENS_ON_DISCONNECT': True
 
77
        })
 
78
        self.do_login()
 
79
        user = User.get(self.expected_username)
 
80
        user.password = 'password'
 
81
        HTTPretty.register_uri(self._method(self.backend.REVOKE_TOKEN_METHOD),
 
82
                               self.backend.REVOKE_TOKEN_URL,
 
83
                               status=200)
 
84
        do_disconnect(self.backend, user)
 
85
 
 
86
 
 
87
class WhitelistEmailsTest(DummyOAuth2Test):
 
88
    def test_valid_login(self):
 
89
        self.strategy.set_settings({
 
90
            'SOCIAL_AUTH_WHITELISTED_EMAILS': ['foo@bar.com']
 
91
        })
 
92
        self.do_login()
 
93
 
 
94
    def test_invalid_login(self):
 
95
        self.strategy.set_settings({
 
96
            'SOCIAL_AUTH_WHITELISTED_EMAILS': ['foo2@bar.com']
 
97
        })
 
98
        with self.assertRaises(AuthForbidden):
 
99
            self.do_login()
 
100
 
 
101
 
 
102
class WhitelistDomainsTest(DummyOAuth2Test):
 
103
    def test_valid_login(self):
 
104
        self.strategy.set_settings({
 
105
            'SOCIAL_AUTH_WHITELISTED_DOMAINS': ['bar.com']
 
106
        })
 
107
        self.do_login()
 
108
 
 
109
    def test_invalid_login(self):
 
110
        self.strategy.set_settings({
 
111
            'SOCIAL_AUTH_WHITELISTED_EMAILS': ['bar2.com']
 
112
        })
 
113
        with self.assertRaises(AuthForbidden):
 
114
            self.do_login()
 
115
 
 
116
 
 
117
DELTA = datetime.timedelta(days=1)
 
118
 
 
119
 
 
120
class ExpirationTimeTest(DummyOAuth2Test):
 
121
    user_data_body = json.dumps({
 
122
        'id': 1,
 
123
        'username': 'foobar',
 
124
        'url': 'http://dummy.com/user/foobar',
 
125
        'first_name': 'Foo',
 
126
        'last_name': 'Bar',
 
127
        'email': 'foo@bar.com',
 
128
        'expires': time.mktime((datetime.datetime.utcnow() +
 
129
                                DELTA).timetuple())
 
130
    })
 
131
 
 
132
    def test_expires_time(self):
 
133
        user = self.do_login()
 
134
        social = user.social[0]
 
135
        expiration = social.expiration_timedelta()
 
136
        self.assertEqual(expiration <= DELTA, True)
 
137
 
 
138
 
 
139
class AllExtraDataTest(DummyOAuth2Test):
 
140
    backend_path = 'social_core.tests.backends.test_dummy.Dummy2OAuth2'
 
141
    access_token_body = json.dumps({
 
142
        'access_token': 'foobar',
 
143
        'token_type': 'bearer'
 
144
    })
 
145
    user_data_body = json.dumps({
 
146
        'id': 1,
 
147
        'username': 'foobar',
 
148
        'url': 'http://dummy.com/user/foobar',
 
149
        'first_name': 'Foo',
 
150
        'last_name': 'Bar',
 
151
        'email': 'foo@bar.com',
 
152
        'not_normally_in_extra_data': 'value'
 
153
    })
 
154
 
 
155
    def test_get_all_extra_data(self):
 
156
        user = self.do_login()
 
157
        social = user.social[0]
 
158
        self.assertIn('not_normally_in_extra_data', social.extra_data)
 
159
        self.assertEqual(len(social.extra_data), 10)  # Includes auth_time.