|
| 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 | +from decimal import Decimal |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from vulnerabilities.models import AdvisorySeverity |
| 15 | +from vulnerabilities.models import AdvisoryV2 |
| 16 | +from vulnerabilities.models import AdvisoryWeakness |
| 17 | +from vulnerabilities.models import PackageV2 |
| 18 | +from vulnerabilities.pipelines.v2_improvers.compute_package_risk import ComputePackageRiskPipeline |
| 19 | +from vulnerabilities.severity_systems import CVSSV3 |
| 20 | +from vulnerabilities.severity_systems import GENERIC |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.django_db |
| 24 | +def test_simple_risk_pipeline(): |
| 25 | + pkg = PackageV2.objects.create(type="pypi", name="foo", version="2.3.0") |
| 26 | + assert PackageV2.objects.count() == 1 |
| 27 | + |
| 28 | + adv = AdvisoryV2( |
| 29 | + advisory_id="VCID-Existing", |
| 30 | + summary="vulnerability description here", |
| 31 | + datasource_id="ds", |
| 32 | + avid="ds/VCID-Existing", |
| 33 | + unique_content_id="ajkef", |
| 34 | + url="https://test.com", |
| 35 | + date_collected=datetime.now(), |
| 36 | + ) |
| 37 | + adv.save() |
| 38 | + |
| 39 | + severity1 = AdvisorySeverity.objects.create( |
| 40 | + url="https://nvd.nist.gov/vuln/detail/CVE-xxxx-xxx1", |
| 41 | + scoring_system=CVSSV3.identifier, |
| 42 | + scoring_elements="CVSS:3.0/AV:P/AC:H/PR:H/UI:R/S:C/C:H/I:H/A:N/E:H/RL:O/RC:R/CR:H/MAC:H/MC:L", |
| 43 | + value="6.5", |
| 44 | + ) |
| 45 | + |
| 46 | + severity2 = AdvisorySeverity.objects.create( |
| 47 | + url="https://nvd.nist.gov/vuln/detail/CVE-xxxx-xxx1", |
| 48 | + scoring_system=GENERIC.identifier, |
| 49 | + value="MODERATE", # 6.9 |
| 50 | + ) |
| 51 | + adv.severities.add(severity1) |
| 52 | + adv.severities.add(severity2) |
| 53 | + |
| 54 | + weaknesses = AdvisoryWeakness.objects.create(cwe_id=119) |
| 55 | + adv.weaknesses.add(weaknesses) |
| 56 | + |
| 57 | + adv.affecting_packages.add(pkg) |
| 58 | + adv.save() |
| 59 | + |
| 60 | + improver = ComputePackageRiskPipeline() |
| 61 | + improver.execute() |
| 62 | + |
| 63 | + assert pkg.risk_score is None |
| 64 | + |
| 65 | + improver = ComputePackageRiskPipeline() |
| 66 | + improver.execute() |
| 67 | + |
| 68 | + pkg = PackageV2.objects.get(type="pypi", name="foo", version="2.3.0") |
| 69 | + assert pkg.risk_score == Decimal("3.1") |
0 commit comments