5
from httpretty import HTTPretty
7
from ...actions import do_disconnect
8
from ...backends.oauth import BaseOAuth2
9
from ...exceptions import AuthForbidden
11
from ..models import User
12
from .oauth import OAuth2Test
15
class DummyOAuth2(BaseOAuth2):
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
24
('expires', 'expires'),
25
('empty', 'empty', True),
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', '')}
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
43
class Dummy2OAuth2(DummyOAuth2):
44
GET_ALL_EXTRA_DATA = True
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'
55
user_data_body = json.dumps({
58
'url': 'http://dummy.com/user/foobar',
61
'email': 'foo@bar.com'
67
def test_partial_pipeline(self):
68
self.do_partial_pipeline()
70
def test_tokens(self):
71
user = self.do_login()
72
self.assertEqual(user.social[0].access_token, 'foobar')
74
def test_revoke_token(self):
75
self.strategy.set_settings({
76
'SOCIAL_AUTH_REVOKE_TOKENS_ON_DISCONNECT': True
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,
84
do_disconnect(self.backend, user)
87
class WhitelistEmailsTest(DummyOAuth2Test):
88
def test_valid_login(self):
89
self.strategy.set_settings({
90
'SOCIAL_AUTH_WHITELISTED_EMAILS': ['foo@bar.com']
94
def test_invalid_login(self):
95
self.strategy.set_settings({
96
'SOCIAL_AUTH_WHITELISTED_EMAILS': ['foo2@bar.com']
98
with self.assertRaises(AuthForbidden):
102
class WhitelistDomainsTest(DummyOAuth2Test):
103
def test_valid_login(self):
104
self.strategy.set_settings({
105
'SOCIAL_AUTH_WHITELISTED_DOMAINS': ['bar.com']
109
def test_invalid_login(self):
110
self.strategy.set_settings({
111
'SOCIAL_AUTH_WHITELISTED_EMAILS': ['bar2.com']
113
with self.assertRaises(AuthForbidden):
117
DELTA = datetime.timedelta(days=1)
120
class ExpirationTimeTest(DummyOAuth2Test):
121
user_data_body = json.dumps({
123
'username': 'foobar',
124
'url': 'http://dummy.com/user/foobar',
127
'email': 'foo@bar.com',
128
'expires': time.mktime((datetime.datetime.utcnow() +
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)
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'
145
user_data_body = json.dumps({
147
'username': 'foobar',
148
'url': 'http://dummy.com/user/foobar',
151
'email': 'foo@bar.com',
152
'not_normally_in_extra_data': 'value'
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.