|
| 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 | +from datetime import datetime |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from vulnerabilities.models import AdvisoryReference |
| 14 | +from vulnerabilities.models import AdvisoryV2 |
| 15 | +from vulnerabilities.models import ImpactedPackage |
| 16 | +from vulnerabilities.models import PackageCommitPatch |
| 17 | +from vulnerabilities.models import PackageV2 |
| 18 | +from vulnerabilities.pipelines.v2_improvers.reference_collect_commits import ( |
| 19 | + CollectReferencesFixCommitsPipeline, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.django_db |
| 24 | +def test_is_vcs_url_already_processed_true(): |
| 25 | + advisory = AdvisoryV2.objects.create( |
| 26 | + advisory_id="CVE-2025-9999", |
| 27 | + datasource_id="test-ds", |
| 28 | + avid="test-ds/CVE-2025-9999", |
| 29 | + url="https://example.com/advisory/CVE-2025-9999", |
| 30 | + unique_content_id="11111", |
| 31 | + date_collected=datetime.now(), |
| 32 | + ) |
| 33 | + package = PackageV2.objects.create( |
| 34 | + type="bar", |
| 35 | + name="foo", |
| 36 | + version="1.0", |
| 37 | + ) |
| 38 | + impact = ImpactedPackage.objects.create(advisory=advisory) |
| 39 | + impact.affecting_packages.add(package) |
| 40 | + package_commit_patch = PackageCommitPatch.objects.create( |
| 41 | + vcs_url="https://github.com/user/repo/commit/6bd301819f8f69331a55ae2336c8b111fc933f3d", |
| 42 | + commit_hash="6bd301819f8f69331a55ae2336c8b111fc933f3d", |
| 43 | + ) |
| 44 | + impact.fixed_by_package_commit_patches.add(package_commit_patch) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.django_db |
| 48 | +def test_collect_fix_commits_pipeline_creates_entry(): |
| 49 | + advisory = AdvisoryV2.objects.create( |
| 50 | + advisory_id="CVE-2025-1000", |
| 51 | + datasource_id="test-ds", |
| 52 | + avid="test-ds/CVE-2025-1000", |
| 53 | + url="https://example.com/advisory/CVE-2025-1000", |
| 54 | + unique_content_id="11111", |
| 55 | + date_collected=datetime.now(), |
| 56 | + ) |
| 57 | + package = PackageV2.objects.create( |
| 58 | + type="foo", |
| 59 | + name="testpkg", |
| 60 | + version="1.0", |
| 61 | + ) |
| 62 | + reference = AdvisoryReference.objects.create( |
| 63 | + url="https://github.com/test/testpkg/commit/6bd301819f8f69331a55ae2336c8b111fc933f3d" |
| 64 | + ) |
| 65 | + impact = ImpactedPackage.objects.create(advisory=advisory) |
| 66 | + impact.affecting_packages.add(package) |
| 67 | + advisory.references.add(reference) |
| 68 | + |
| 69 | + pipeline = CollectReferencesFixCommitsPipeline() |
| 70 | + pipeline.collect_and_store_fix_commits() |
| 71 | + |
| 72 | + package_commit_patch = PackageCommitPatch.objects.all() |
| 73 | + |
| 74 | + assert package_commit_patch.count() == 1 |
| 75 | + fix = package_commit_patch.first() |
| 76 | + assert fix.commit_hash == "6bd301819f8f69331a55ae2336c8b111fc933f3d" |
| 77 | + assert fix.vcs_url == "https://github.com/test/testpkg" |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.django_db |
| 81 | +def test_collect_fix_commits_pipeline_skips_non_commit_urls(): |
| 82 | + advisory = AdvisoryV2.objects.create( |
| 83 | + advisory_id="CVE-2025-2000", |
| 84 | + datasource_id="test-ds", |
| 85 | + avid="test-ds/CVE-2025-2000", |
| 86 | + url="https://example.com/advisory/CVE-2025-2000", |
| 87 | + unique_content_id="11111", |
| 88 | + date_collected=datetime.now(), |
| 89 | + ) |
| 90 | + package = PackageV2.objects.create( |
| 91 | + type="pypi", |
| 92 | + name="otherpkg", |
| 93 | + version="2.0", |
| 94 | + ) |
| 95 | + impact = ImpactedPackage.objects.create(advisory=advisory) |
| 96 | + impact.affecting_packages.add(package) |
| 97 | + |
| 98 | + reference = AdvisoryReference.objects.create( |
| 99 | + url="https://github.com/test/testpkg/issues/12" |
| 100 | + ) # invalid reference 1 |
| 101 | + advisory.references.add(reference) |
| 102 | + |
| 103 | + pipeline = CollectReferencesFixCommitsPipeline() |
| 104 | + pipeline.collect_and_store_fix_commits() |
| 105 | + assert PackageCommitPatch.objects.count() == 0 |
0 commit comments