/usr/lib/python3/dist-packages/social/tests/actions/actions.py is in python3-social-auth 1:0.2.21+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import json
import requests
import unittest2 as unittest
from httpretty import HTTPretty
from social.utils import parse_qs, module_member
from social.p3 import urlparse
from social.actions import do_auth, do_complete
from social.tests.models import TestStorage, User, TestUserSocialAuth, \
TestNonce, TestAssociation
from social.tests.strategy import TestStrategy
class BaseActionTest(unittest.TestCase):
user_data_url = 'https://api.github.com/user'
login_redirect_url = '/success'
expected_username = 'foobar'
access_token_body = json.dumps({
'access_token': 'foobar',
'token_type': 'bearer'
})
user_data_body = json.dumps({
'login': 'foobar',
'id': 1,
'avatar_url': 'https://github.com/images/error/foobar_happy.gif',
'gravatar_id': 'somehexcode',
'url': 'https://api.github.com/users/foobar',
'name': 'monalisa foobar',
'company': 'GitHub',
'blog': 'https://github.com/blog',
'location': 'San Francisco',
'email': 'foo@bar.com',
'hireable': False,
'bio': 'There once was...',
'public_repos': 2,
'public_gists': 1,
'followers': 20,
'following': 0,
'html_url': 'https://github.com/foobar',
'created_at': '2008-01-14T04:33:35Z',
'type': 'User',
'total_private_repos': 100,
'owned_private_repos': 100,
'private_gists': 81,
'disk_usage': 10000,
'collaborators': 8,
'plan': {
'name': 'Medium',
'space': 400,
'collaborators': 10,
'private_repos': 20
}
})
def __init__(self, *args, **kwargs):
self.strategy = None
super(BaseActionTest, self).__init__(*args, **kwargs)
def setUp(self):
HTTPretty.enable()
User.reset_cache()
TestUserSocialAuth.reset_cache()
TestNonce.reset_cache()
TestAssociation.reset_cache()
Backend = module_member('social.backends.github.GithubOAuth2')
self.strategy = self.strategy or TestStrategy(TestStorage)
self.backend = Backend(self.strategy, redirect_uri='/complete/github')
self.user = None
def tearDown(self):
self.backend = None
self.strategy = None
self.user = None
User.reset_cache()
User.set_active(True)
TestUserSocialAuth.reset_cache()
TestNonce.reset_cache()
TestAssociation.reset_cache()
HTTPretty.disable()
def do_login(self, after_complete_checks=True, user_data_body=None,
expected_username=None):
self.strategy.set_settings({
'SOCIAL_AUTH_GITHUB_KEY': 'a-key',
'SOCIAL_AUTH_GITHUB_SECRET': 'a-secret-key',
'SOCIAL_AUTH_LOGIN_REDIRECT_URL': self.login_redirect_url,
'SOCIAL_AUTH_AUTHENTICATION_BACKENDS': (
'social.backends.github.GithubOAuth2',
)
})
start_url = do_auth(self.backend).url
target_url = self.strategy.build_absolute_uri(
'/complete/github/?code=foobar'
)
start_query = parse_qs(urlparse(start_url).query)
location_url = target_url + ('?' in target_url and '&' or '?') + \
'state=' + start_query['state']
location_query = parse_qs(urlparse(location_url).query)
HTTPretty.register_uri(HTTPretty.GET, start_url, status=301,
location=location_url)
HTTPretty.register_uri(HTTPretty.GET, location_url, status=200,
body='foobar')
response = requests.get(start_url)
self.assertEqual(response.url, location_url)
self.assertEqual(response.text, 'foobar')
HTTPretty.register_uri(HTTPretty.POST,
uri=self.backend.ACCESS_TOKEN_URL,
status=200,
body=self.access_token_body or '',
content_type='text/json')
if self.user_data_url:
user_data_body = user_data_body or self.user_data_body or ''
HTTPretty.register_uri(HTTPretty.GET, self.user_data_url,
body=user_data_body,
content_type='text/json')
self.strategy.set_request_data(location_query, self.backend)
def _login(backend, user, social_user):
backend.strategy.session_set('username', user.username)
redirect = do_complete(self.backend, user=self.user, login=_login)
if after_complete_checks:
self.assertEqual(self.strategy.session_get('username'),
expected_username or self.expected_username)
self.assertEqual(redirect.url, self.login_redirect_url)
return redirect
def do_login_with_partial_pipeline(self, before_complete=None):
self.strategy.set_settings({
'SOCIAL_AUTH_GITHUB_KEY': 'a-key',
'SOCIAL_AUTH_GITHUB_SECRET': 'a-secret-key',
'SOCIAL_AUTH_LOGIN_REDIRECT_URL': self.login_redirect_url,
'SOCIAL_AUTH_AUTHENTICATION_BACKENDS': (
'social.backends.github.GithubOAuth2',
),
'SOCIAL_AUTH_PIPELINE': (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.partial.save_status_to_session',
'social.tests.pipeline.ask_for_password',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.tests.pipeline.set_password',
'social.pipeline.user.user_details'
)
})
start_url = do_auth(self.backend).url
target_url = self.strategy.build_absolute_uri(
'/complete/github/?code=foobar'
)
start_query = parse_qs(urlparse(start_url).query)
location_url = target_url + ('?' in target_url and '&' or '?') + \
'state=' + start_query['state']
location_query = parse_qs(urlparse(location_url).query)
HTTPretty.register_uri(HTTPretty.GET, start_url, status=301,
location=location_url)
HTTPretty.register_uri(HTTPretty.GET, location_url, status=200,
body='foobar')
response = requests.get(start_url)
self.assertEqual(response.url, location_url)
self.assertEqual(response.text, 'foobar')
HTTPretty.register_uri(HTTPretty.GET,
uri=self.backend.ACCESS_TOKEN_URL,
status=200,
body=self.access_token_body or '',
content_type='text/json')
if self.user_data_url:
HTTPretty.register_uri(HTTPretty.GET, self.user_data_url,
body=self.user_data_body or '',
content_type='text/json')
self.strategy.set_request_data(location_query, self.backend)
def _login(backend, user, social_user):
backend.strategy.session_set('username', user.username)
redirect = do_complete(self.backend, user=self.user, login=_login)
url = self.strategy.build_absolute_uri('/password')
self.assertEqual(redirect.url, url)
HTTPretty.register_uri(HTTPretty.GET, redirect.url, status=200,
body='foobar')
HTTPretty.register_uri(HTTPretty.POST, redirect.url, status=200)
password = 'foobar'
requests.get(url)
requests.post(url, data={'password': password})
data = parse_qs(HTTPretty.last_request.body)
self.assertEqual(data['password'], password)
self.strategy.session_set('password', data['password'])
if before_complete:
before_complete()
redirect = do_complete(self.backend, user=self.user, login=_login)
self.assertEqual(self.strategy.session_get('username'),
self.expected_username)
self.assertEqual(redirect.url, self.login_redirect_url)
|