diff --git a/src/authentic2_auth_fc/app_settings.py b/src/authentic2_auth_fc/app_settings.py index bd16a0e..9ce9a26 100644 --- a/src/authentic2_auth_fc/app_settings.py +++ b/src/authentic2_auth_fc/app_settings.py @@ -108,6 +108,10 @@ class AppSettings(object): def scopes(self): return self._setting('SCOPES', []) + @property + def requests_proxies(self): + return self._setting('REQUESTS_PROXIES', {}) + import sys diff --git a/src/authentic2_auth_fc/utils.py b/src/authentic2_auth_fc/utils.py index 5182007..2e99dde 100644 --- a/src/authentic2_auth_fc/utils.py +++ b/src/authentic2_auth_fc/utils.py @@ -182,4 +182,6 @@ def requests_retry_session( adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) + # set proxies + session.proxies.update(app_settings.requests_proxies) return session diff --git a/tests/test_auth_fc.py b/tests/test_auth_fc.py index 77461ef..7a132d6 100644 --- a/tests/test_auth_fc.py +++ b/tests/test_auth_fc.py @@ -2,11 +2,14 @@ import pytest import urlparse import httmock +import mock import json import base64 from jwcrypto import jwk, jwt import datetime +import requests + from django.core.urlresolvers import reverse from django.contrib.auth import get_user_model from django.utils.timezone import now @@ -14,6 +17,7 @@ from django.utils.timezone import now from authentic2.utils import timestamp_from_datetime from authentic2_auth_fc import models +from authentic2_auth_fc.utils import requests_retry_session User = get_user_model() @@ -214,3 +218,21 @@ def test_login_email_is_unique_and_already_linked(app, fc_settings, caplog): assert 'is already used' in str(response) assert User.objects.count() == 1 assert '_auth_user_id' not in app.session + + +def test_requests_proxies_support(app, fc_settings, caplog): + session = requests_retry_session() + assert session.proxies == {} + other_session = requests.Session() + other_session.proxies = {'http': 'http://example.net'} + session = requests_retry_session(session=other_session) + assert session is other_session + assert session.proxies == {'http': 'http://example.net'} + fc_settings.A2_FC_REQUESTS_PROXIES = {'https': 'http://pubproxy.com/api/proxy'} + session = requests_retry_session() + assert session.proxies == {'https': 'http://pubproxy.com/api/proxy'} + + with mock.patch('authentic2_auth_fc.utils.requests.Session.send') as mocked_send: + mocked_send.return_value = mock.Mock(status_code=200, content='whatever') + session.get('https://example.net/') + assert mocked_send.call_args[1]['proxies'] == {'https': 'http://pubproxy.com/api/proxy'}