def test_get_user_info(self): with patch('requests.get') as fake: fake.return_value.status_code = 401 fake.return_value.text = 'Unauthorized request' response = get_user_info('http://example.com/info', 'qwerty') self.assertEqual(response.status, '401 Unauthorized') self.assertEqual(response.message, 'Unauthorized request') with patch('requests.get') as fake: fake.return_value.status_code = 200 fake.return_value.json = lambda: { 'name': 'John', 'surname': 'Doe', } response = get_user_info('http://example.com/info', 'qwerty') self.assertEqual(response, { 'name': 'John', 'surname': 'Doe', })
def facebook_callback(request): settings = request.registry.settings access_token = oauth2_step2( request=request, token_uri=settings['facebook_access_token_url'], client_id=settings['facebook_app_id'], client_secret=settings['facebook_app_secret'], redirect_url=request.route_url('facebook_callback'), scope=_get_scope(), ) info = get_user_info(settings['facebook_basic_information_url'], access_token) user_id = info['id'] info['screen_name'] = info['name'] return register_or_update(request, 'facebook', user_id, info, request.route_path('home'))
def google_callback(request): settings = request.registry.settings access_token = oauth2_step2( request=request, token_uri=settings['google_token_uri'], client_id=settings['google_client_id'], client_secret=settings['google_client_secret'], redirect_url=request.route_url('google_callback'), scope=_get_scope(), ) info = get_user_info(settings['google_user_info_uri'], access_token) user_id = info['id'] new_info = { 'screen_name': info.get('name', ''), 'first_name': info.get('given_name', ''), 'last_name': info.get('family_name', ''), 'email': info.get('email', ''), } return register_or_update(request, 'google', user_id, new_info, request.route_path('home'))