|
| 1 | +import gc |
1 | 2 | import json |
2 | 3 | import os |
3 | 4 | import shutil |
4 | 5 | import tempfile |
5 | 6 | import unittest |
| 7 | +import weakref |
6 | 8 | from datetime import datetime, timezone |
7 | 9 | from types import SimpleNamespace |
| 10 | +from unittest import mock |
8 | 11 | from urllib.parse import quote as urlescape |
9 | 12 |
|
10 | 13 | import pytest |
11 | 14 | import requests |
12 | 15 | from mako.lookup import TemplateLookup |
| 16 | +from pyramid import httpexceptions as exc |
13 | 17 | from pyramid.testing import DummyRequest |
14 | 18 | from pyuppsala import etree |
15 | 19 | from wsgi_intercept.interceptor import RequestsInterceptor, UrllibInterceptor |
16 | 20 |
|
17 | 21 | from pyff.api import mkapp, search_handler |
18 | 22 | from pyff.constants import config |
| 23 | +from pyff.resource import ResourceException |
19 | 24 | from pyff.samlmd import iter_entities |
20 | 25 | from pyff.test import SignerTestCase |
21 | 26 | from pyff.test.test_pipeline import PipeLineTest |
@@ -356,3 +361,110 @@ def test_entity_id_without_trailing_slash_is_not_found(self): |
356 | 361 | with RequestsInterceptor(self.app, host='127.0.0.1', port=80) as url: |
357 | 362 | assert requests.post(f'{url}/api/call/update').status_code == 200 |
358 | 363 | assert self._entity_ids(url, '/entities/https:/sp.example.com/saml2/metadata') == set() |
| 364 | + |
| 365 | + |
| 366 | +class _PipelineResult(list): |
| 367 | + """A pipeline result that can be weakly referenced, to observe when it is freed""" |
| 368 | + |
| 369 | + |
| 370 | +class PyFFAPITestErrorResponses(PipeLineTest): |
| 371 | + """ |
| 372 | + Errors while processing a request must have the right status, and must not keep the pipeline result - often |
| 373 | + the whole selected metadata tree - alive after the request: cf. issue #283. |
| 374 | + """ |
| 375 | + |
| 376 | + mdx = None |
| 377 | + app = None |
| 378 | + |
| 379 | + @classmethod |
| 380 | + def setUpClass(cls): |
| 381 | + SignerTestCase.setUpClass() |
| 382 | + config.local_copy_dir = tempfile.mkdtemp() |
| 383 | + cls.mdx = tempfile.NamedTemporaryFile('w').name |
| 384 | + with open(cls.mdx, "w") as fd: |
| 385 | + fd.write( |
| 386 | + f""" |
| 387 | +- when update: |
| 388 | + - load: |
| 389 | + - {os.path.join(cls.datadir, 'metadata', 'test01.xml')} |
| 390 | +- when request: |
| 391 | + - select |
| 392 | + - pipe: |
| 393 | + - when accept application/xml: |
| 394 | + - emit application/xml |
| 395 | + - break |
| 396 | +""" |
| 397 | + ) |
| 398 | + cls._app = mkapp(cls.mdx) |
| 399 | + cls.app = lambda *args, **kwargs: cls._app |
| 400 | + |
| 401 | + @classmethod |
| 402 | + def tearDownClass(cls): |
| 403 | + SignerTestCase.tearDownClass() |
| 404 | + if os.path.exists(cls.mdx): |
| 405 | + os.unlink(cls.mdx) |
| 406 | + if os.path.exists(config.local_copy_dir): |
| 407 | + shutil.rmtree(config.local_copy_dir) |
| 408 | + |
| 409 | + def _request_with_pipeline_result(self, url, fail=False, error=None): |
| 410 | + """ |
| 411 | + GET /entities as text/plain with the pipeline replaced by one producing a weakly referenceable result, |
| 412 | + then failing if ``fail`` (with a RuntimeError) or with the ``error`` factory's exception, raised while a |
| 413 | + KeyError is being handled. The garbage collector is off, so only reference counting can free the result. |
| 414 | +
|
| 415 | + :return: the HTTP status and whether the result was freed once the request was done |
| 416 | + """ |
| 417 | + refs = [] |
| 418 | + |
| 419 | + def process(*args, **kwargs): |
| 420 | + result = _PipelineResult(['<md:EntitiesDescriptor/>']) |
| 421 | + refs.append(weakref.ref(result)) |
| 422 | + if error is not None: |
| 423 | + try: |
| 424 | + raise KeyError("first failure") |
| 425 | + except KeyError: |
| 426 | + raise error() |
| 427 | + if fail: |
| 428 | + raise RuntimeError("pipeline failed") |
| 429 | + return result |
| 430 | + |
| 431 | + gc.collect() |
| 432 | + gc.disable() |
| 433 | + try: |
| 434 | + with mock.patch.object(self._app.registry.plumbings[0], 'process', process): |
| 435 | + status = requests.get(f'{url}/entities', headers={'Accept': 'text/plain'}).status_code |
| 436 | + return status, refs[0]() is None |
| 437 | + finally: |
| 438 | + gc.enable() |
| 439 | + |
| 440 | + def test_unacceptable_type_is_406(self): |
| 441 | + """No pipeline branch and no format matches text/plain: that is 406 Not Acceptable, not a server error""" |
| 442 | + with RequestsInterceptor(self.app, host='127.0.0.1', port=80) as url: |
| 443 | + assert requests.post(f'{url}/api/call/update').status_code == 200 |
| 444 | + r = requests.get(f'{url}/entities', headers={'Accept': 'text/plain'}) |
| 445 | + assert r.status_code == 406 |
| 446 | + |
| 447 | + def test_406_frees_the_pipeline_result(self): |
| 448 | + with RequestsInterceptor(self.app, host='127.0.0.1', port=80) as url: |
| 449 | + status, freed = self._request_with_pipeline_result(url, fail=False) |
| 450 | + assert status == 406 |
| 451 | + assert freed, "the pipeline result outlived a 406 response" |
| 452 | + |
| 453 | + def test_500_frees_the_pipeline_result(self): |
| 454 | + with RequestsInterceptor(self.app, host='127.0.0.1', port=80) as url: |
| 455 | + status, freed = self._request_with_pipeline_result(url, fail=True) |
| 456 | + assert status == 500 |
| 457 | + assert freed, "the pipeline result outlived a 500 response" |
| 458 | + |
| 459 | + def test_http_error_raised_while_handling_another_frees_the_pipeline_result(self): |
| 460 | + """An HTTP error chained to another exception must not keep the result alive through __context__""" |
| 461 | + with RequestsInterceptor(self.app, host='127.0.0.1', port=80) as url: |
| 462 | + status, freed = self._request_with_pipeline_result(url, error=exc.HTTPNotFound) |
| 463 | + assert status == 404 |
| 464 | + assert freed, "the pipeline result outlived a chained 404 response" |
| 465 | + |
| 466 | + def test_409_frees_the_pipeline_result(self): |
| 467 | + with RequestsInterceptor(self.app, host='127.0.0.1', port=80) as url: |
| 468 | + status, freed = self._request_with_pipeline_result(url, error=lambda: ResourceException("conflict")) |
| 469 | + assert status == 409 |
| 470 | + assert freed, "the pipeline result outlived a 409 response" |
0 commit comments