Skip to content

Commit

Permalink
Add tests for improver
Browse files Browse the repository at this point in the history
Signed-off-by: Hritik Vijay <[email protected]>
  • Loading branch information
Hritik14 authored and TG1999 committed Aug 31, 2023
1 parent 6cc8fd7 commit c58cf36
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vulnerabilities/improver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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,
Expand Down
70 changes: 70 additions & 0 deletions vulnerabilities/tests/test_improver.py
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit c58cf36

Please sign in to comment.