|
| 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 | + |
| 11 | +import tempfile |
| 12 | +from datetime import datetime |
| 13 | +from datetime import timedelta |
| 14 | +from pathlib import Path |
| 15 | +from unittest.mock import patch |
| 16 | + |
| 17 | +from django.test import TestCase |
| 18 | +from git import Repo |
| 19 | +from packageurl import PackageURL |
| 20 | +from univers.version_range import VersionRange |
| 21 | + |
| 22 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 23 | +from vulnerabilities.importer import AffectedPackageV2 |
| 24 | +from vulnerabilities.pipelines import insert_advisory_v2 |
| 25 | +from vulnerabilities.pipelines.exporters.federate_vulnerabilities import ( |
| 26 | + FederatePackageVulnerabilities, |
| 27 | +) |
| 28 | +from vulnerabilities.tests import util_tests |
| 29 | +from vulnerabilities.tests.pipelines import TestLogger |
| 30 | + |
| 31 | +TEST_DATA = ( |
| 32 | + Path(__file__).parent.parent.parent / "test_data" / "exporters" / "federate_vulnerabilities" |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +class TestFederatePackageVulnerabilities(TestCase): |
| 37 | + def setUp(self): |
| 38 | + self.logger = TestLogger() |
| 39 | + |
| 40 | + advisory = AdvisoryDataV2( |
| 41 | + summary="Test advisory", |
| 42 | + aliases=["CVE-2025-0001"], |
| 43 | + references=[], |
| 44 | + severities=[], |
| 45 | + weaknesses=[], |
| 46 | + affected_packages=[ |
| 47 | + AffectedPackageV2( |
| 48 | + package=PackageURL.from_string("pkg:npm/foobar"), |
| 49 | + affected_version_range=VersionRange.from_string("vers:npm/<=1.2.3"), |
| 50 | + fixed_version_range=VersionRange.from_string("vers:npm/1.2.4"), |
| 51 | + introduced_by_commit_patches=[], |
| 52 | + fixed_by_commit_patches=[], |
| 53 | + ), |
| 54 | + AffectedPackageV2( |
| 55 | + package=PackageURL.from_string("pkg:npm/foobar"), |
| 56 | + affected_version_range=VersionRange.from_string("vers:npm/<=3.2.3"), |
| 57 | + fixed_version_range=VersionRange.from_string("vers:npm/3.2.4"), |
| 58 | + introduced_by_commit_patches=[], |
| 59 | + fixed_by_commit_patches=[], |
| 60 | + ), |
| 61 | + ], |
| 62 | + patches=[], |
| 63 | + advisory_id="ADV-123", |
| 64 | + date_published=datetime.now() - timedelta(days=10), |
| 65 | + url="https://example.com/advisory/1", |
| 66 | + ) |
| 67 | + insert_advisory_v2( |
| 68 | + advisory=advisory, |
| 69 | + pipeline_id="test_pipeline_v2", |
| 70 | + ) |
| 71 | + |
| 72 | + @patch( |
| 73 | + "vulnerabilities.pipelines.exporters.federate_vulnerabilities.FederatePackageVulnerabilities.clone_vulnerabilities_repo" |
| 74 | + ) |
| 75 | + @patch("vulnerabilities.pipes.federatedcode.commit_and_push_changes") |
| 76 | + @patch("vulnerabilities.pipes.federatedcode.check_federatedcode_configured_and_available") |
| 77 | + def test_vulnerabilities_federation_v2(self, mock_check_fed, mock_commit, mock_clone): |
| 78 | + mock_check_fed.return_value = None |
| 79 | + mock_commit.return_value = None |
| 80 | + mock_clone.__name__ = "clone_vulnerabilities_repo" |
| 81 | + |
| 82 | + working_dir = Path(tempfile.mkdtemp()) |
| 83 | + print(working_dir) |
| 84 | + |
| 85 | + pipeline = FederatePackageVulnerabilities() |
| 86 | + pipeline.repo = Repo.init(working_dir) |
| 87 | + pipeline.log = self.logger.write |
| 88 | + pipeline.execute() |
| 89 | + print(self.logger.getvalue()) |
| 90 | + |
| 91 | + result_purl_yml = next(working_dir.rglob("purls.yml")) |
| 92 | + result_vulnerabilities_yml = next(working_dir.rglob("vulnerabilities.yml")) |
| 93 | + result_advisory_yml = next(working_dir.rglob("ADV-123.yml")) |
| 94 | + |
| 95 | + expected_purl_yml = TEST_DATA / "purls-expected.yml" |
| 96 | + expected_vulnerabilities_yml = TEST_DATA / "vulnerabilities-expected.yml" |
| 97 | + expected_advisory_yml = TEST_DATA / "ADV-123-expected.yml" |
| 98 | + |
| 99 | + util_tests.check_results_and_expected_files(result_purl_yml, expected_purl_yml) |
| 100 | + util_tests.check_results_and_expected_files( |
| 101 | + result_vulnerabilities_yml, expected_vulnerabilities_yml |
| 102 | + ) |
| 103 | + util_tests.check_results_and_expected_files(result_advisory_yml, expected_advisory_yml) |
0 commit comments