diff --git a/vulnerabilities/improver.py b/vulnerabilities/improver.py index 9b46cce0a..a7554001f 100644 --- a/vulnerabilities/improver.py +++ b/vulnerabilities/improver.py @@ -78,7 +78,7 @@ def to_dict(self): """ return { "vulnerability_id": self.vulnerability_id, - "aliases": [alias for alias in self.aliases], + "aliases": self.aliases, "confidence": self.confidence, "summary": self.summary, "affected_purls": [affected_purl.to_dict() for affected_purl in self.affected_purls], @@ -91,7 +91,7 @@ def to_dict(self): def from_advisory_data(cls, advisory_data, confidence, fixed_purl, affected_purls=None): """ Return an Inference object while keeping the same values as of advisory_data - for vulnerability_id, summary and references + for aliases, summary and references """ return cls( aliases=advisory_data.aliases, diff --git a/vulnerabilities/tests/test_improver.py b/vulnerabilities/tests/test_improver.py new file mode 100644 index 000000000..ed524a991 --- /dev/null +++ b/vulnerabilities/tests/test_improver.py @@ -0,0 +1,70 @@ +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# VulnerableCode is a trademark of nexB Inc. +# SPDX-License-Identifier: Apache-2.0 +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. +# See https://github.com/nexB/vulnerablecode for support or download. +# See https://aboutcode.org for more information about nexB OSS projects. +# + +import pytest + +from vulnerabilities.importer import AdvisoryData +from vulnerabilities.importer import Reference +from vulnerabilities.improver import MAX_CONFIDENCE +from vulnerabilities.improver import Inference +from vulnerabilities.improver import PackageURL + + +def test_empty_inference_raises_exception(): + with pytest.raises(AssertionError): + Inference() + + +def test_inference_to_dict_method_with_vulnerability_id(): + inference = Inference(vulnerability_id="vulcoid-1337") + expected = { + "vulnerability_id": "vulcoid-1337", + "aliases": [], + "confidence": MAX_CONFIDENCE, + "summary": "", + "affected_purls": [], + "fixed_purl": None, + "references": [], + } + assert expected == inference.to_dict() + + +def test_inference_to_dict_method_with_purls(): + purl = PackageURL(type="dummy", namespace="rick", name="jalebi", version="1") + inference = Inference(affected_purls=[purl], fixed_purl=purl) + expected = { + "vulnerability_id": None, + "aliases": [], + "confidence": MAX_CONFIDENCE, + "summary": "", + "affected_purls": [purl.to_dict()], + "fixed_purl": purl.to_dict(), + "references": [], + } + assert expected == inference.to_dict() + + +def test_inference_to_dict_method_with_versionless_purls_raises_exception(): + versionless_purl = PackageURL(type="dummy", namespace="rick", name="gulabjamun") + with pytest.raises(AssertionError): + Inference(affected_purls=[versionless_purl], fixed_purl=versionless_purl) + + +def test_inference_from_advisory_data(): + aliases = ["lalmohan", "gulabjamun"] + summary = "really tasty sweets" + references = [Reference(url="http://localhost")] + advisory_data = AdvisoryData(aliases=aliases, summary=summary, references=references) + fixed_purl = PackageURL(name="mithai", version="1", type="sweets") + inference = Inference.from_advisory_data( + advisory_data=advisory_data, fixed_purl=fixed_purl, confidence=MAX_CONFIDENCE + ) + assert inference == Inference( + aliases=aliases, summary=summary, references=references, fixed_purl=fixed_purl + )