|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +from unittest.mock import patch |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from vulnerabilities.models import AdvisoryV2 |
| 15 | +from vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash import ( |
| 16 | + ComputeAdvisoryContentHash, |
| 17 | +) |
| 18 | + |
| 19 | +pytestmark = pytest.mark.django_db |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def advisory_factory(): |
| 24 | + def _create(count, with_hash=False, start=0): |
| 25 | + objs = [] |
| 26 | + for i in range(start, start + count): |
| 27 | + objs.append( |
| 28 | + AdvisoryV2( |
| 29 | + summary=f"summary {i}", |
| 30 | + advisory_content_hash="existing_hash" if with_hash else None, |
| 31 | + unique_content_id=f"unique_id_{i}", |
| 32 | + advisory_id=f"ADV-{i}", |
| 33 | + datasource_id="ds", |
| 34 | + avid=f"ds/ADV-{i}", |
| 35 | + url=f"https://example.com/ADV-{i}", |
| 36 | + ) |
| 37 | + ) |
| 38 | + return AdvisoryV2.objects.bulk_create(objs) |
| 39 | + |
| 40 | + return _create |
| 41 | + |
| 42 | + |
| 43 | +def run_pipeline(): |
| 44 | + pipeline = ComputeAdvisoryContentHash() |
| 45 | + pipeline.compute_advisory_content_hash() |
| 46 | + |
| 47 | + |
| 48 | +@patch( |
| 49 | + "vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash.compute_advisory_content" |
| 50 | +) |
| 51 | +def test_pipeline_updates_only_missing_hash(mock_compute, advisory_factory): |
| 52 | + advisory_factory(3, with_hash=False, start=0) |
| 53 | + advisory_factory(2, with_hash=True, start=100) |
| 54 | + |
| 55 | + mock_compute.return_value = "new_hash" |
| 56 | + |
| 57 | + run_pipeline() |
| 58 | + |
| 59 | + updated = AdvisoryV2.objects.filter(advisory_content_hash="new_hash").count() |
| 60 | + untouched = AdvisoryV2.objects.filter(advisory_content_hash="existing_hash").count() |
| 61 | + |
| 62 | + assert updated == 3 |
| 63 | + assert untouched == 2 |
| 64 | + assert mock_compute.call_count == 3 |
| 65 | + |
| 66 | + |
| 67 | +@patch( |
| 68 | + "vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash.compute_advisory_content" |
| 69 | +) |
| 70 | +def test_pipeline_bulk_update_batches(mock_compute, advisory_factory): |
| 71 | + advisory_factory(6000, with_hash=False) |
| 72 | + |
| 73 | + mock_compute.return_value = "batch_hash" |
| 74 | + |
| 75 | + run_pipeline() |
| 76 | + |
| 77 | + assert AdvisoryV2.objects.filter(advisory_content_hash="batch_hash").count() == 6000 |
| 78 | + |
| 79 | + assert mock_compute.call_count == 6000 |
| 80 | + |
| 81 | + |
| 82 | +@patch( |
| 83 | + "vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash.compute_advisory_content" |
| 84 | +) |
| 85 | +def test_pipeline_no_advisories(mock_compute): |
| 86 | + run_pipeline() |
| 87 | + |
| 88 | + assert mock_compute.call_count == 0 |
0 commit comments