|
| 1 | +import inspect |
| 2 | +from unittest.mock import patch |
| 3 | + |
1 | 4 | import pytest |
| 5 | + |
2 | 6 | from vulnerabilities import importers |
| 7 | +from vulnerabilities.data_source import Advisory |
3 | 8 | from vulnerabilities.importer_yielder import IMPORTER_REGISTRY |
4 | 9 |
|
| 10 | +MAX_ADVISORIES = 1 |
| 11 | + |
| 12 | + |
| 13 | +class MaxAdvisoriesCreatedInterrupt(BaseException): |
| 14 | + # Inheriting BaseException is intentional because the function being tested might catch Exception |
| 15 | + pass |
| 16 | + |
5 | 17 |
|
6 | 18 | @pytest.mark.webtest |
7 | 19 | @pytest.mark.parametrize( |
8 | 20 | ("data_source", "config"), |
9 | 21 | ((data["data_source"], data["data_source_cfg"]) for data in IMPORTER_REGISTRY), |
10 | 22 | ) |
11 | 23 | def test_updated_advisories(data_source, config): |
12 | | - |
13 | 24 | if not data_source == "GitHubAPIDataSource": |
14 | 25 | data_src = getattr(importers, data_source) |
15 | | - data_src = data_src(batch_size=1, config=config) |
16 | | - with data_src: |
17 | | - for i in data_src.updated_advisories(): |
| 26 | + data_src = data_src(batch_size=MAX_ADVISORIES, config=config) |
| 27 | + advisory_counter = 0 |
| 28 | + |
| 29 | + def patched_advisory(*args, **kwargs): |
| 30 | + nonlocal advisory_counter |
| 31 | + |
| 32 | + if advisory_counter >= MAX_ADVISORIES: |
| 33 | + raise MaxAdvisoriesCreatedInterrupt |
| 34 | + |
| 35 | + advisory_counter += 1 |
| 36 | + return Advisory(*args, **kwargs) |
| 37 | + |
| 38 | + module = inspect.getmodule(data_src) |
| 39 | + module_members = [m[0] for m in inspect.getmembers(module)] |
| 40 | + advisory_class = f"{module.__name__}.Advisory" |
| 41 | + if "Advisory" not in module_members: |
| 42 | + advisory_class = "vulnerabilities.data_source.Advisory" |
| 43 | + |
| 44 | + # Either |
| 45 | + # 1) Advisory class is successfully patched and MaxAdvisoriesCreatedInterrupt is thrown when |
| 46 | + # an importer tries to create an Advisory or |
| 47 | + # 2) Importer somehow bypasses the patch / handles BaseException internally, then |
| 48 | + # updated_advisories is required to return non zero advisories |
| 49 | + with patch(advisory_class, side_effect=patched_advisory): |
| 50 | + try: |
| 51 | + with data_src: |
| 52 | + assert len(list(data_src.updated_advisories())) > 0 |
| 53 | + except MaxAdvisoriesCreatedInterrupt: |
18 | 54 | pass |
0 commit comments