Debian Codegraph @

tests/test_google_auth_httplib2.py

raw symbols SCIP
1 # Copyright 2016 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import http.client
16 import io
17 from unittest import mock
18
19 import httplib2
20
21 import google_auth_httplib2
22 from tests import compliance
23
24
25 class MockHttp(object):
26 def __init__(self, responses, headers=None):
27 self.responses = responses
28 self.requests = []
29 self.headers = headers or {}
30 self.add_certificate = mock.Mock(return_value=None)
31
32 def request(
33 self,
34 url,
35 method="GET",
36 body=None,
37 headers=None,
38 redirections=httplib2.DEFAULT_MAX_REDIRECTS,
39 connection_type=None,
40 ):
41 self.requests.append(
42 (method, url, body, headers, redirections, connection_type)
43 )
44 return self.responses.pop(0)
45
46
47 class MockResponse(object):
48 def __init__(self, status=http.client.OK, data=b""):
49 self.status = status
50 self.data = data
51
52 def __iter__(self):
53 yield self
54 yield self.data
55
56
57 class TestRequestResponse(compliance.RequestResponseTests):
58 def make_request(self):
59 http = httplib2.Http()
60 return google_auth_httplib2.Request(http)
61
62 def test_timeout(self):
63 url = "http://example.com"
64 http = MockHttp(responses=[MockResponse()])
65 request = google_auth_httplib2.Request(http)
66 request(url=url, method="GET", timeout=5)
67
68 assert http.requests[0] == (
69 "GET",
70 url,
71 None,
72 None,
73 httplib2.DEFAULT_MAX_REDIRECTS,
74 None,
75 )
76
77
78 def test__make_default_http():
79 http = google_auth_httplib2._make_default_http()
80 assert isinstance(http, httplib2.Http)
81
82
83 class MockCredentials(object):
84 def __init__(self, token="token"):
85 self.token = token
86
87 def apply(self, headers):
88 headers["authorization"] = self.token
89
90 def before_request(self, request, method, url, headers):
91 self.apply(headers)
92
93 def refresh(self, request):
94 self.token += "1"
95
96
97 class TestAuthorizedHttp(object):
98 TEST_URL = "http://example.com"
99
100 def test_authed_http_defaults(self):
101 authed_http = google_auth_httplib2.AuthorizedHttp(mock.sentinel.credentials)
102
103 assert authed_http.credentials == mock.sentinel.credentials
104 assert isinstance(authed_http.http, httplib2.Http)
105
106 def test_close(self):
107 with mock.patch("httplib2.Http.close", autospec=True) as close:
108 authed_http = google_auth_httplib2.AuthorizedHttp(mock.sentinel.credentials)
109 authed_http.close()
110
111 close.assert_called_once()
112
113 def test_connections(self):
114 authed_http = google_auth_httplib2.AuthorizedHttp(mock.sentinel.credentials)
115
116 assert authed_http.connections == authed_http.http.connections
117
118 authed_http.connections = mock.sentinel.connections
119 assert authed_http.http.connections == mock.sentinel.connections
120
121 def test_follow_redirects(self):
122 auth_http = google_auth_httplib2.AuthorizedHttp(mock.sentinel.credentials)
123
124 assert auth_http.follow_redirects == auth_http.http.follow_redirects
125
126 mock_follow_redirects = mock.sentinel.follow_redirects
127 auth_http.follow_redirects = mock_follow_redirects
128 assert auth_http.http.follow_redirects == mock_follow_redirects
129
130 def test_timeout(self):
131 authed_http = google_auth_httplib2.AuthorizedHttp(mock.sentinel.credentials)
132
133 assert authed_http.timeout == authed_http.http.timeout
134
135 authed_http.timeout = mock.sentinel.timeout
136 assert authed_http.http.timeout == mock.sentinel.timeout
137
138 def test_redirect_codes(self):
139 authed_http = google_auth_httplib2.AuthorizedHttp(mock.sentinel.credentials)
140
141 assert authed_http.redirect_codes == authed_http.http.redirect_codes
142
143 authed_http.redirect_codes = mock.sentinel.redirect_codes
144 assert authed_http.http.redirect_codes == mock.sentinel.redirect_codes
145
146 def test_add_certificate(self):
147 authed_http = google_auth_httplib2.AuthorizedHttp(
148 mock.sentinel.credentials, http=MockHttp(MockResponse())
149 )
150
151 authed_http.add_certificate("key", "cert", "domain", password="password")
152 authed_http.http.add_certificate.assert_called_once_with(
153 "key", "cert", "domain", password="password"
154 )
155
156 def test_request_no_refresh(self):
157 mock_credentials = mock.Mock(wraps=MockCredentials())
158 mock_response = MockResponse()
159 mock_http = MockHttp([mock_response])
160
161 authed_http = google_auth_httplib2.AuthorizedHttp(
162 mock_credentials, http=mock_http
163 )
164
165 response, data = authed_http.request(self.TEST_URL)
166
167 assert response == mock_response
168 assert data == mock_response.data
169 assert mock_credentials.before_request.called
170 assert not mock_credentials.refresh.called
171 assert mock_http.requests == [
172 (
173 "GET",
174 self.TEST_URL,
175 None,
176 {"authorization": "token"},
177 httplib2.DEFAULT_MAX_REDIRECTS,
178 None,
179 )
180 ]
181
182 def test_request_refresh(self):
183 mock_credentials = mock.Mock(wraps=MockCredentials())
184 mock_final_response = MockResponse(status=http.client.OK)
185 # First request will 401, second request will succeed.
186 mock_http = MockHttp(
187 [MockResponse(status=http.client.UNAUTHORIZED), mock_final_response]
188 )
189
190 authed_http = google_auth_httplib2.AuthorizedHttp(
191 mock_credentials, http=mock_http
192 )
193
194 response, data = authed_http.request(self.TEST_URL)
195
196 assert response == mock_final_response
197 assert data == mock_final_response.data
198 assert mock_credentials.before_request.call_count == 2
199 assert mock_credentials.refresh.called
200 assert mock_http.requests == [
201 (
202 "GET",
203 self.TEST_URL,
204 None,
205 {"authorization": "token"},
206 httplib2.DEFAULT_MAX_REDIRECTS,
207 None,
208 ),
209 (
210 "GET",
211 self.TEST_URL,
212 None,
213 {"authorization": "token1"},
214 httplib2.DEFAULT_MAX_REDIRECTS,
215 None,
216 ),
217 ]
218
219 def test_request_stream_body(self):
220 mock_credentials = mock.Mock(wraps=MockCredentials())
221 mock_response = MockResponse()
222 # Refresh is needed to cover the resetting of the body position.
223 mock_http = MockHttp(
224 [MockResponse(status=http.client.UNAUTHORIZED), mock_response]
225 )
226
227 body = io.StringIO("body")
228 body.seek(1)
229
230 authed_http = google_auth_httplib2.AuthorizedHttp(
231 mock_credentials, http=mock_http
232 )
233
234 response, data = authed_http.request(self.TEST_URL, method="POST", body=body)
235
236 assert response == mock_response
237 assert data == mock_response.data
238 assert mock_http.requests == [
239 (
240 "POST",
241 self.TEST_URL,
242 body,
243 {"authorization": "token"},
244 httplib2.DEFAULT_MAX_REDIRECTS,
245 None,
246 ),
247 (
248 "POST",
249 self.TEST_URL,
250 body,
251 {"authorization": "token1"},
252 httplib2.DEFAULT_MAX_REDIRECTS,
253 None,
254 ),
255 ]
256
257 def test_request_positional_args(self):
258 """Verifies that clients can pass args to request as positioanls."""
259 mock_credentials = mock.Mock(wraps=MockCredentials())
260 mock_response = MockResponse()
261 mock_http = MockHttp([mock_response])
262
263 authed_http = google_auth_httplib2.AuthorizedHttp(
264 mock_credentials, http=mock_http
265 )
266
267 response, data = authed_http.request(
268 self.TEST_URL, "GET", None, None, httplib2.DEFAULT_MAX_REDIRECTS, None
269 )
270
271 assert response == mock_response
272 assert data == mock_response.data
273 assert mock_credentials.before_request.called
274 assert not mock_credentials.refresh.called
275 assert mock_http.requests == [
276 (
277 "GET",
278 self.TEST_URL,
279 None,
280 {"authorization": "token"},
281 httplib2.DEFAULT_MAX_REDIRECTS,
282 None,
283 )
284 ]