| 1 |
# test/test_methods.py |
| 2 |
# Part of ‘dput’, a Debian package upload toolkit. |
| 3 |
# |
| 4 |
# This is free software, and you are welcome to redistribute it under |
| 5 |
# certain conditions; see the end of this file for copyright |
| 6 |
# information, grant of license, and disclaimer of warranty. |
| 7 |
|
| 8 |
""" Unit tests for HTTP upload method plug-in. """ |
| 9 |
|
| 10 |
import copy |
| 11 |
import getpass |
| 12 |
import http.client |
| 13 |
import unittest.mock |
| 14 |
import urllib.parse |
| 15 |
import urllib.request |
| 16 |
|
| 17 |
import testscenarios |
| 18 |
import testtools |
| 19 |
|
| 20 |
import dput.methods.http |
| 21 |
|
| 22 |
|
| 23 |
def make_test_password_mgr( |
| 24 |
test_class=dput.methods.http.PromptingPasswordMgr, |
| 25 |
test_kwargs=None, |
| 26 |
): |
| 27 |
""" |
| 28 |
Make a password manager of specified `test_class` with `test_kwargs`. |
| 29 |
""" |
| 30 |
if test_kwargs is None: |
| 31 |
test_kwargs = { |
| 32 |
'username': "FAKE", |
| 33 |
} |
| 34 |
instance = test_class(**test_kwargs) |
| 35 |
return instance |
| 36 |
|
| 37 |
|
| 38 |
def setup_passwordmgr( |
| 39 |
testcase, |
| 40 |
passwordmgr_class=None, |
| 41 |
passwordmgr_kwargs=None, |
| 42 |
): |
| 43 |
""" Set up a password manager fixture for the `testcase`. """ |
| 44 |
if passwordmgr_kwargs is None: |
| 45 |
passwordmgr_kwargs = { |
| 46 |
'username': testcase.getUniqueString()[:40], |
| 47 |
} |
| 48 |
maker_kwargs = { |
| 49 |
'test_kwargs': passwordmgr_kwargs, |
| 50 |
} |
| 51 |
if passwordmgr_class is not None: |
| 52 |
maker_kwargs['test_class'] = passwordmgr_class |
| 53 |
testcase.test_password_mgr = make_test_password_mgr(**maker_kwargs) |
| 54 |
|
| 55 |
|
| 56 |
class PromptingPasswordMgr_TestCase(testtools.TestCase): |
| 57 |
""" Test cases for class `PromptingPasswordMgr`. """ |
| 58 |
|
| 59 |
passwordmgr_class = dput.methods.http.PromptingPasswordMgr |
| 60 |
|
| 61 |
def setUp(self): |
| 62 |
""" Set up fixtures for this test case. """ |
| 63 |
super().setUp() |
| 64 |
|
| 65 |
self.test_username = self.getUniqueString()[:40] |
| 66 |
self.test_args = { |
| 67 |
'username': self.test_username, |
| 68 |
} |
| 69 |
setup_passwordmgr( |
| 70 |
self, |
| 71 |
passwordmgr_class=self.passwordmgr_class, |
| 72 |
passwordmgr_kwargs=self.test_args) |
| 73 |
|
| 74 |
def test_instantiate(self): |
| 75 |
""" Should create a new instance of `PromptingPasswordMgr`. """ |
| 76 |
self.assertIsInstance( |
| 77 |
self.test_password_mgr, self.passwordmgr_class) |
| 78 |
|
| 79 |
def test_is_password_manager(self): |
| 80 |
""" Should create a new instance of `HTTPPasswordMgr`. """ |
| 81 |
self.assertIsInstance( |
| 82 |
self.test_password_mgr, urllib.request.HTTPPasswordMgr) |
| 83 |
|
| 84 |
def test_has_specified_username(self): |
| 85 |
""" Should have the specified `username` value. """ |
| 86 |
self.assertEqual(self.test_password_mgr.username, self.test_username) |
| 87 |
|
| 88 |
|
| 89 |
class PromptingPasswordMgr_find_user_password_TestCase( |
| 90 |
testscenarios.WithScenarios, |
| 91 |
testtools.TestCase): |
| 92 |
""" Test cases for method `PromptingPasswordMgr.find_user_password`. """ |
| 93 |
|
| 94 |
table_scenarios = [ |
| 95 |
('normal', { |
| 96 |
'passwd_entries': { |
| 97 |
"pellentesque": { |
| 98 |
( |
| 99 |
urllib.parse.SplitResult( |
| 100 |
scheme="https", |
| 101 |
netloc="lorem.example.org:443", |
| 102 |
path="/", |
| 103 |
query="", |
| 104 |
fragment="", |
| 105 |
), |
| 106 |
urllib.parse.SplitResult( |
| 107 |
scheme="https", |
| 108 |
netloc="ipsum.example.org:443", |
| 109 |
path="/", |
| 110 |
query="", |
| 111 |
fragment="", |
| 112 |
), |
| 113 |
): ("consectetur", "adipiscing"), |
| 114 |
( |
| 115 |
urllib.parse.SplitResult( |
| 116 |
scheme="https", |
| 117 |
netloc="dolor.example.org:443", |
| 118 |
path="/", |
| 119 |
query="", |
| 120 |
fragment="", |
| 121 |
), |
| 122 |
urllib.parse.SplitResult( |
| 123 |
scheme="https", |
| 124 |
netloc="sit.example.org:443", |
| 125 |
path="/", |
| 126 |
query="", |
| 127 |
fragment="", |
| 128 |
), |
| 129 |
urllib.parse.SplitResult( |
| 130 |
scheme="https", |
| 131 |
netloc="amet.example.org:443", |
| 132 |
path="/", |
| 133 |
query="", |
| 134 |
fragment="", |
| 135 |
), |
| 136 |
): ("vehicula", "faucibus"), |
| 137 |
}, |
| 138 |
"suscipit": { |
| 139 |
( |
| 140 |
urllib.parse.SplitResult( |
| 141 |
scheme="https", |
| 142 |
netloc="dictum.example.org:443", |
| 143 |
path="/", |
| 144 |
query="", |
| 145 |
fragment="", |
| 146 |
), |
| 147 |
): ("dui", "porta"), |
| 148 |
}, |
| 149 |
}, |
| 150 |
'test_username': "consectetur", |
| 151 |
'test_realm': "pellentesque", |
| 152 |
}), |
| 153 |
] |
| 154 |
|
| 155 |
authuri_scenarios = [ |
| 156 |
('authuri-match', { |
| 157 |
'test_authuri': "https://ipsum.example.org/proin", |
| 158 |
'test_realm_uris': ( |
| 159 |
urllib.parse.SplitResult( |
| 160 |
scheme="https", |
| 161 |
netloc="lorem.example.org:443", |
| 162 |
path="/", |
| 163 |
query="", |
| 164 |
fragment="", |
| 165 |
), |
| 166 |
urllib.parse.SplitResult( |
| 167 |
scheme="https", |
| 168 |
netloc="ipsum.example.org:443", |
| 169 |
path="/", |
| 170 |
query="", |
| 171 |
fragment="", |
| 172 |
), |
| 173 |
), |
| 174 |
'expected_username': "consectetur", |
| 175 |
'expected_password': "adipiscing", |
| 176 |
}), |
| 177 |
] |
| 178 |
|
| 179 |
getpass_scenarios = [ |
| 180 |
('getpass-normal', { |
| 181 |
'input_password': "efficitur", |
| 182 |
}), |
| 183 |
] |
| 184 |
|
| 185 |
scenarios = testscenarios.multiply_scenarios( |
| 186 |
table_scenarios, authuri_scenarios, getpass_scenarios) |
| 187 |
|
| 188 |
def setUp(self): |
| 189 |
""" Set up fixtures for this test case. """ |
| 190 |
super().setUp() |
| 191 |
|
| 192 |
passwordmgr_kwargs = { |
| 193 |
'username': self.test_username, |
| 194 |
} |
| 195 |
setup_passwordmgr( |
| 196 |
self, passwordmgr_kwargs=passwordmgr_kwargs) |
| 197 |
|
| 198 |
self.test_password_mgr.passwd = copy.deepcopy(self.passwd_entries) |
| 199 |
|
| 200 |
self.test_kwargs = { |
| 201 |
'realm': self.test_realm, |
| 202 |
'authuri': self.test_authuri, |
| 203 |
} |
| 204 |
|
| 205 |
self.patch_getpass_getpass(test_result=self.input_password) |
| 206 |
|
| 207 |
def patch_getpass_getpass(self, test_result=None): |
| 208 |
""" Patch the `getpass.getpass` function for this test case. """ |
| 209 |
func_patcher = unittest.mock.patch.object(getpass, 'getpass') |
| 210 |
self.mock_getpass = func_patcher.start() |
| 211 |
if test_result is not None: |
| 212 |
self.mock_getpass.return_value = test_result |
| 213 |
self.addCleanup(func_patcher.stop) |
| 214 |
|
| 215 |
def test_returns_expected_username(self): |
| 216 |
""" Should return expected username value. """ |
| 217 |
(username, __) = self.test_password_mgr.find_user_password( |
| 218 |
**self.test_kwargs) |
| 219 |
self.assertEqual(username, self.expected_username) |
| 220 |
|
| 221 |
def test_returns_stored_password_when_authuri_match(self): |
| 222 |
""" Should return stored password when `authuri` matches. """ |
| 223 |
(__, password) = self.test_password_mgr.find_user_password( |
| 224 |
**self.test_kwargs) |
| 225 |
(__, expected_password) = self.passwd_entries[ |
| 226 |
self.test_realm][self.test_realm_uris] |
| 227 |
self.assertEqual(password, expected_password) |
| 228 |
|
| 229 |
def test_returns_input_password_when_authuri_not_found(self): |
| 230 |
""" Should return input password when `authuri` not found. """ |
| 231 |
del self.test_password_mgr.passwd[ |
| 232 |
self.test_realm][self.test_realm_uris] |
| 233 |
(__, password) = self.test_password_mgr.find_user_password( |
| 234 |
**self.test_kwargs) |
| 235 |
expected_password = self.input_password |
| 236 |
self.assertEqual(password, expected_password) |
| 237 |
|
| 238 |
|
| 239 |
def make_test_authrequest( |
| 240 |
test_class=dput.methods.http.AuthHandlerHackAround, |
| 241 |
test_kwargs=None, |
| 242 |
): |
| 243 |
""" |
| 244 |
Make an authentication request of type `test_class` with `test_kwargs`. |
| 245 |
""" |
| 246 |
if test_kwargs is None: |
| 247 |
test_kwargs = { |
| 248 |
'url': "FAKE", |
| 249 |
'response_headers': object(), |
| 250 |
'password_mgr': make_test_password_mgr(), |
| 251 |
} |
| 252 |
instance = test_class(**test_kwargs) |
| 253 |
return instance |
| 254 |
|
| 255 |
|
| 256 |
def setup_authrequest( |
| 257 |
testcase, |
| 258 |
authrequest_class=None, |
| 259 |
authrequest_kwargs=None, |
| 260 |
): |
| 261 |
""" Set up an authnetication handler fixture for the `testcase`. """ |
| 262 |
maker_kwargs = {} |
| 263 |
if authrequest_class is not None: |
| 264 |
maker_kwargs['test_class'] = authrequest_class |
| 265 |
if authrequest_kwargs is not None: |
| 266 |
maker_kwargs['test_kwargs'] = authrequest_kwargs |
| 267 |
testcase.test_authrequest = make_test_authrequest(**maker_kwargs) |
| 268 |
|
| 269 |
|
| 270 |
class AuthHandlerHackAround_TestCase(testtools.TestCase): |
| 271 |
""" Test cases for class `AuthHandlerHackAround`. """ |
| 272 |
|
| 273 |
authrequest_class = dput.methods.http.AuthHandlerHackAround |
| 274 |
|
| 275 |
def setUp(self): |
| 276 |
""" Set up fixtures for this test case. """ |
| 277 |
super().setUp() |
| 278 |
|
| 279 |
setup_passwordmgr(self) |
| 280 |
|
| 281 |
self.test_url = self.getUniqueString() |
| 282 |
self.test_response_headers = { |
| 283 |
self.getUniqueString(): self.getUniqueString(), |
| 284 |
self.getUniqueString(): self.getUniqueString(), |
| 285 |
} |
| 286 |
self.test_args = { |
| 287 |
'url': self.test_url, |
| 288 |
'response_headers': self.test_response_headers, |
| 289 |
'password_mgr': self.test_password_mgr, |
| 290 |
} |
| 291 |
setup_authrequest( |
| 292 |
self, |
| 293 |
authrequest_class=self.authrequest_class, |
| 294 |
authrequest_kwargs=self.test_args) |
| 295 |
|
| 296 |
def test_instantiate(self): |
| 297 |
""" Should create a new instance of `AuthHandlerHackAround`. """ |
| 298 |
self.assertIsInstance( |
| 299 |
self.test_authrequest, self.authrequest_class) |
| 300 |
|
| 301 |
def test_has_specified_url(self): |
| 302 |
""" Should have the specified `url` value. """ |
| 303 |
self.assertEqual(self.test_authrequest.url, self.test_url) |
| 304 |
|
| 305 |
def test_has_specified_full_url(self): |
| 306 |
""" Should have the specified URL as `full_url` value. """ |
| 307 |
self.assertEqual(self.test_authrequest.full_url, self.test_url) |
| 308 |
|
| 309 |
def test_has_specified_response_headers(self): |
| 310 |
""" Should have the specified `response_headers` value. """ |
| 311 |
self.assertEqual( |
| 312 |
self.test_authrequest.response_headers, |
| 313 |
self.test_response_headers) |
| 314 |
|
| 315 |
def test_has_expected_auth_handler_types(self): |
| 316 |
""" Should have expected authentication handler types. """ |
| 317 |
expected_types = { |
| 318 |
urllib.request.HTTPBasicAuthHandler, |
| 319 |
urllib.request.HTTPDigestAuthHandler, |
| 320 |
} |
| 321 |
authhandler_types = { |
| 322 |
handler.__class__ |
| 323 |
for handler in self.test_authrequest.authhandlers} |
| 324 |
self.assertEqual(authhandler_types, expected_types) |
| 325 |
|
| 326 |
def test_has_expected_auth_handler_parents(self): |
| 327 |
""" Should have expected authentication handler parents. """ |
| 328 |
expected_parent = self.test_authrequest |
| 329 |
for handler in self.test_authrequest.authhandlers: |
| 330 |
self.assertIs(handler.parent, expected_parent) |
| 331 |
|
| 332 |
|
| 333 |
class AuthHandlerHackAround_get_header_TestCase(testtools.TestCase): |
| 334 |
""" Test cases for method `AuthHandlerHackAround.get_header`. """ |
| 335 |
|
| 336 |
def test_returns_specified_field_from_headers(self): |
| 337 |
""" Should return specified field value from the `headers` mapping. """ |
| 338 |
setup_authrequest(self) |
| 339 |
test_field_name = self.getUniqueString() |
| 340 |
test_field_value = self.getUniqueString() |
| 341 |
self.test_authrequest.headers[test_field_name] = test_field_value |
| 342 |
self.test_args = (test_field_name,) |
| 343 |
result = self.test_authrequest.get_header(*self.test_args) |
| 344 |
self.assertEqual(test_field_value, result) |
| 345 |
|
| 346 |
def test_returns_none_when_specified_field_not_found(self): |
| 347 |
""" Should return ``None`` when specified field is not found. """ |
| 348 |
setup_authrequest(self) |
| 349 |
test_field_name = self.getUniqueString() |
| 350 |
self.test_args = (test_field_name,) |
| 351 |
result = self.test_authrequest.get_header(*self.test_args) |
| 352 |
self.assertIsNone(result) |
| 353 |
|
| 354 |
def test_returns_specified_default_when_specified_field_not_found(self): |
| 355 |
""" Should return `default` value when specified field not found. """ |
| 356 |
setup_authrequest(self) |
| 357 |
test_field_name = self.getUniqueString() |
| 358 |
test_default_value = self.getUniqueString() |
| 359 |
self.test_args = { |
| 360 |
'header_name': test_field_name, |
| 361 |
'default': test_default_value, |
| 362 |
} |
| 363 |
result = self.test_authrequest.get_header(**self.test_args) |
| 364 |
self.assertEqual(test_default_value, result) |
| 365 |
|
| 366 |
|
| 367 |
class AuthHandlerHackAround_add_header_TestCase(testtools.TestCase): |
| 368 |
""" Test cases for method `AuthHandlerHackAround.add_header`. """ |
| 369 |
|
| 370 |
def test_sets_specified_field_in_headers(self): |
| 371 |
""" Should set specified field in the `headers` mapping. """ |
| 372 |
setup_authrequest(self) |
| 373 |
test_field_name = self.getUniqueString() |
| 374 |
test_field_value = self.getUniqueString() |
| 375 |
self.test_args = (test_field_name, test_field_value) |
| 376 |
self.test_authrequest.add_header(*self.test_args) |
| 377 |
self.assertEqual( |
| 378 |
self.test_authrequest.headers[test_field_name], |
| 379 |
test_field_value) |
| 380 |
|
| 381 |
|
| 382 |
class AuthHandlerHackAround_add_unredirected_header_TestCase( |
| 383 |
testtools.TestCase): |
| 384 |
""" |
| 385 |
Test cases for method `AuthHandlerHackAround.add_unredirected_header`. |
| 386 |
""" |
| 387 |
|
| 388 |
def test_sets_specified_field_in_headers(self): |
| 389 |
""" Should set specified field in the `headers` mapping. """ |
| 390 |
setup_authrequest(self) |
| 391 |
test_field_name = self.getUniqueString() |
| 392 |
test_field_value = self.getUniqueString() |
| 393 |
self.test_args = (test_field_name, test_field_value) |
| 394 |
self.test_authrequest.add_unredirected_header(*self.test_args) |
| 395 |
self.assertEqual( |
| 396 |
self.test_authrequest.headers[test_field_name], |
| 397 |
test_field_value) |
| 398 |
|
| 399 |
|
| 400 |
class AuthHandlerHackAround_get_full_url_TestCase(testtools.TestCase): |
| 401 |
""" Test cases for method `AuthHandlerHackAround.get_full_url`. """ |
| 402 |
|
| 403 |
def test_returns_specified_url(self): |
| 404 |
""" Should return the specified `url` value from initialisation. """ |
| 405 |
setup_authrequest(self) |
| 406 |
expected_url = self.test_authrequest.url |
| 407 |
result = self.test_authrequest.get_full_url() |
| 408 |
self.assertEqual(result, expected_url) |
| 409 |
|
| 410 |
|
| 411 |
class AuthHandlerHackAround_open_TestCase(testtools.TestCase): |
| 412 |
""" Test cases for method `AuthHandlerHackAround.open`. """ |
| 413 |
|
| 414 |
def test_accepts_open_args(self): |
| 415 |
""" Should accept the expected `open` arguments. """ |
| 416 |
setup_authrequest(self) |
| 417 |
test_kwargs = { |
| 418 |
'fullurl': self.getUniqueString(), |
| 419 |
'data': self.getUniqueString(), |
| 420 |
'timeout': self.getUniqueInteger(), |
| 421 |
} |
| 422 |
self.test_authrequest.open(**test_kwargs) |
| 423 |
|
| 424 |
|
| 425 |
class AuthHandlerHackAround_get_auth_headers_TestCase( |
| 426 |
testscenarios.WithScenarios, |
| 427 |
testtools.TestCase): |
| 428 |
""" Test cases for method `AuthHandlerHackAround.get_auth_headers`. """ |
| 429 |
|
| 430 |
basicauth_scenarios = [ |
| 431 |
('basicauth-okay', { |
| 432 |
'basicauth_error_effect': None, |
| 433 |
}), |
| 434 |
('basicauth-error', { |
| 435 |
'basicauth_error_effect': ValueError("lorem ipsum"), |
| 436 |
}), |
| 437 |
] |
| 438 |
|
| 439 |
digestauth_scenarios = [ |
| 440 |
('digestauth-okay', { |
| 441 |
'digestauth_error_effect': None, |
| 442 |
}), |
| 443 |
('digestauth-error', { |
| 444 |
'digestauth_error_effect': ValueError("lorem ipsum"), |
| 445 |
}), |
| 446 |
] |
| 447 |
|
| 448 |
header_effect_scenarios = [ |
| 449 |
('no-header-change', { |
| 450 |
'new_header_field': None, |
| 451 |
}), |
| 452 |
('header-add-field', { |
| 453 |
'new_header_field': ("dolor", "sit amet"), |
| 454 |
}), |
| 455 |
] |
| 456 |
|
| 457 |
scenarios = testscenarios.multiply_scenarios( |
| 458 |
basicauth_scenarios, digestauth_scenarios, header_effect_scenarios) |
| 459 |
|
| 460 |
def setUp(self): |
| 461 |
""" Set up fixtures for this test case. """ |
| 462 |
super().setUp() |
| 463 |
|
| 464 |
setup_authrequest(self) |
| 465 |
|
| 466 |
self.mock_handler_error_methods = { |
| 467 |
handler_class: self.patch_authhander_error( |
| 468 |
handler_class=handler_class, |
| 469 |
error_effect=getattr(self, error_effect_name)) |
| 470 |
for (handler_class, error_effect_name) in [ |
| 471 |
( |
| 472 |
urllib.request.HTTPBasicAuthHandler, |
| 473 |
'basicauth_error_effect'), |
| 474 |
( |
| 475 |
urllib.request.HTTPDigestAuthHandler, |
| 476 |
'digestauth_error_effect'), |
| 477 |
]} |
| 478 |
|
| 479 |
def patch_authhander_error(self, handler_class, error_effect): |
| 480 |
""" Patch the `http_error_401` method of the `handler_class`. """ |
| 481 |
def side_effect(*args, **kwargs): |
| 482 |
if self.new_header_field is not None: |
| 483 |
(field_name, field_value) = self.new_header_field |
| 484 |
self.test_authrequest.headers[field_name] = field_value |
| 485 |
if error_effect is not None: |
| 486 |
raise error_effect |
| 487 |
|
| 488 |
func_patcher = unittest.mock.patch.object( |
| 489 |
handler_class, 'http_error_401', |
| 490 |
side_effect=side_effect, |
| 491 |
) |
| 492 |
mock_method = func_patcher.start() |
| 493 |
self.addCleanup(func_patcher.stop) |
| 494 |
|
| 495 |
return mock_method |
| 496 |
|
| 497 |
def test_calls_basicauthhandler_error_with_expected_args(self): |
| 498 |
""" |
| 499 |
Should call `HTTPBasicAuthHandler.http_error_401` with expected args. |
| 500 |
""" |
| 501 |
self.test_authrequest.get_auth_headers() |
| 502 |
mock_method = self.mock_handler_error_methods[ |
| 503 |
urllib.request.HTTPBasicAuthHandler] |
| 504 |
expected_request = self.test_authrequest |
| 505 |
expected_status_code = http.client.UNAUTHORIZED |
| 506 |
expected_response_headers = self.test_authrequest.response_headers |
| 507 |
mock_method.assert_called_with( |
| 508 |
expected_request, |
| 509 |
unittest.mock.ANY, |
| 510 |
expected_status_code, |
| 511 |
unittest.mock.ANY, |
| 512 |
expected_response_headers, |
| 513 |
) |
| 514 |
|
| 515 |
def test_returns_accumulated_header_fields(self): |
| 516 |
""" Should return accumulated header fields. """ |
| 517 |
result = self.test_authrequest.get_auth_headers() |
| 518 |
expected_header_fields = self.test_authrequest.headers |
| 519 |
self.assertEqual(result, expected_header_fields) |
| 520 |
|
| 521 |
|
| 522 |
# Copyright © 2015–2026 Ben Finney <[email protected]> |
| 523 |
# |
| 524 |
# This is free software: you may copy, modify, and/or distribute this work |
| 525 |
# under the terms of the GNU General Public License as published by the |
| 526 |
# Free Software Foundation; version 3 of that license or any later version. |
| 527 |
# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details. |
| 528 |
|
| 529 |
|
| 530 |
# Local variables: |
| 531 |
# coding: utf-8 |
| 532 |
# mode: python |
| 533 |
# End: |
| 534 |
# vim: fileencoding=utf-8 filetype=python : |