1
+ import os
1
2
import re
2
3
import unittest
3
4
8
9
from atlassian_jwt_auth .key import (
9
10
HTTPSPublicKeyRetriever ,
10
11
HTTPSMultiRepositoryPublicKeyRetriever ,
12
+ PEM_FILE_TYPE ,
11
13
)
12
14
from atlassian_jwt_auth .tests import utils
13
15
14
16
17
+ def get_expected_and_os_proxies_dict (proxy_location ):
18
+ """ returns expected proxy & environmental
19
+ proxy dictionary based upon the provided proxy location.
20
+ """
21
+ expected_proxies = {
22
+ 'http' : proxy_location ,
23
+ 'https' : proxy_location ,
24
+ }
25
+ os_proxy_dict = {
26
+ 'HTTP_PROXY' : proxy_location ,
27
+ 'HTTPS_PROXY' : proxy_location
28
+ }
29
+ return expected_proxies , os_proxy_dict
30
+
31
+
15
32
class BaseHTTPSPublicKeyRetrieverTest (object ):
16
33
""" tests for the HTTPSPublicKeyRetriever class. """
17
34
@@ -39,6 +56,21 @@ def test_https_public_key_retriever_does_not_support_none_url(self):
39
56
with self .assertRaises (ValueError ):
40
57
self .create_retriever (None )
41
58
59
+ def test_https_public_key_retriever_session_uses_env_proxy (self ):
60
+ """ tests that the underlying session makes use of environmental
61
+ proxy configured.
62
+ """
63
+ proxy_location = 'https://example.proxy'
64
+ expected_proxies , proxy_dict = get_expected_and_os_proxies_dict (
65
+ proxy_location )
66
+ with mock .patch .dict (os .environ , proxy_dict , clear = True ):
67
+ retriever = self .create_retriever (self .base_url )
68
+ key_retrievers = [retriever ]
69
+ if isinstance (retriever , HTTPSMultiRepositoryPublicKeyRetriever ):
70
+ key_retrievers = retriever ._retrievers
71
+ for key_retriever in key_retrievers :
72
+ self .assertEqual (key_retriever ._proxies , expected_proxies )
73
+
42
74
def test_https_public_key_retriever_supports_https_url (self ):
43
75
""" tests that HTTPSPublicKeyRetriever supports https://
44
76
base urls.
@@ -55,6 +87,48 @@ def test_retrieve(self, mock_get_method):
55
87
retriever .retrieve ('example/eg' ),
56
88
self ._public_key_pem )
57
89
90
+ @mock .patch .object (requests .Session , 'get' )
91
+ def test_retrieve_with_proxy (self , mock_get_method ):
92
+ """ tests that the retrieve method works as expected when a proxy
93
+ should be used.
94
+ """
95
+ proxy_location = 'https://example.proxy'
96
+ key_id = 'example/eg'
97
+ expected_proxies , proxy_dict = get_expected_and_os_proxies_dict (
98
+ proxy_location )
99
+ _setup_mock_response_for_retriever (
100
+ mock_get_method , self ._public_key_pem )
101
+ with mock .patch .dict (os .environ , proxy_dict , clear = True ):
102
+ retriever = self .create_retriever (self .base_url )
103
+ retriever .retrieve (key_id )
104
+ mock_get_method .assert_called_once_with (
105
+ '%s/%s' % (self .base_url , key_id ),
106
+ headers = {'accept' : PEM_FILE_TYPE },
107
+ proxies = expected_proxies
108
+ )
109
+
110
+ @mock .patch .object (requests .Session , 'get' )
111
+ def test_retrieve_with_proxy_explicitly_set (self , mock_get_method ):
112
+ """ tests that the retrieve method works as expected when a proxy
113
+ should be used and has been explicitly provided.
114
+ """
115
+ proxy_location = 'https://example.proxy'
116
+ explicit_proxy_location = 'https://explicit.proxy'
117
+ key_id = 'example/eg'
118
+ _ , proxy_dict = get_expected_and_os_proxies_dict (proxy_location )
119
+ expected_proxies , _ = get_expected_and_os_proxies_dict (
120
+ explicit_proxy_location )
121
+ _setup_mock_response_for_retriever (
122
+ mock_get_method , self ._public_key_pem )
123
+ with mock .patch .dict (os .environ , proxy_dict , clear = True ):
124
+ retriever = self .create_retriever (self .base_url )
125
+ retriever .retrieve (key_id , proxies = expected_proxies )
126
+ mock_get_method .assert_called_once_with (
127
+ '%s/%s' % (self .base_url , key_id ),
128
+ headers = {'accept' : PEM_FILE_TYPE },
129
+ proxies = expected_proxies
130
+ )
131
+
58
132
@mock .patch .object (requests .Session , 'get' )
59
133
def test_retrieve_with_charset_in_content_type_h (self , mock_get_method ):
60
134
""" tests that the retrieve method works expected when there is
0 commit comments