Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <[email protected]>
  • Loading branch information
TG1999 committed Mar 21, 2023
1 parent a17af5d commit 3134cc5
Show file tree
Hide file tree
Showing 8 changed files with 4,421 additions and 1 deletion.
31 changes: 31 additions & 0 deletions vulnerabilities/tests/test_apache_kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
# See https://aboutcode.org for more information about nexB OSS projects.
#

import json
import os
from unittest.mock import patch

import pytest

from vulnerabilities.importer import AdvisoryData
from vulnerabilities.importers.apache_kafka import ApacheKafkaImporter
from vulnerabilities.improvers.default import DefaultImprover
from vulnerabilities.improvers.valid_versions import ApacheKafkaImprover
from vulnerabilities.tests import util_tests

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -32,6 +37,32 @@ def test_to_advisory():
util_tests.check_results_against_json(result, expected_file)


@patch("vulnerabilities.improvers.valid_versions.ApacheKafkaImprover.get_package_versions")
def test_apache_tomcat_improver(mock_response):
advisory_file = os.path.join(TEST_DATA, f"to-advisory-apache_kafka-expected.json")
with open(advisory_file) as exp:
advisories = [AdvisoryData.from_dict(adv) for adv in (json.load(exp))]
mock_response.return_value = [
"1.1.0",
"1.1.1",
"1.1.2",
"1.1.3",
"1.1.4",
"1.1.5",
"1.1.6",
"1.1.7",
"1.1.8",
]
improvers = [ApacheKafkaImprover(), DefaultImprover()]
result = []
for improver in improvers:
for advisory in advisories:
inference = [data.to_dict() for data in improver.get_inferences(advisory)]
result.extend(inference)
expected_file = os.path.join(TEST_DATA, f"apache-kafka-improver-expected.json")
util_tests.check_results_against_json(result, expected_file)


def to_advisory_changed_cve():
with open(os.path.join(TEST_DATA, "cve-list-changed-cve.html")) as f:
raw_data = f.read()
Expand Down
34 changes: 33 additions & 1 deletion vulnerabilities/tests/test_apache_tomcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
# See https://aboutcode.org for more information about nexB OSS projects.
#

import json
import os
from unittest import TestCase
from unittest.mock import patch

from univers.version_constraint import VersionConstraint
from univers.version_range import ApacheVersionRange
from univers.version_range import MavenVersionRange
from univers.versions import MavenVersion
from univers.versions import SemverVersion

from vulnerabilities.importer import AdvisoryData
from vulnerabilities.importers.apache_tomcat import ApacheTomcatImporter
from vulnerabilities.importers.apache_tomcat import extract_tomcat_advisory_data_from_page
from vulnerabilities.importers.apache_tomcat import to_version_ranges_apache
from vulnerabilities.importers.apache_tomcat import to_version_ranges_maven
from vulnerabilities.improvers.default import DefaultImprover
from vulnerabilities.improvers.valid_versions import ApacheTomcatImprover
from vulnerabilities.tests import util_tests

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -39,6 +43,34 @@ def test_method_extract_advisories_from_page():
util_tests.check_results_against_json(results, expected_file)


@patch("vulnerabilities.improvers.valid_versions.ApacheTomcatImprover.get_package_versions")
def test_apache_tomcat_improver(mock_response):
advisory_file = os.path.join(
TEST_DATA, f"parse-apache_tomcat-selected-advisories-expected.json"
)
with open(advisory_file) as exp:
advisories = [AdvisoryData.from_dict(adv) for adv in (json.load(exp))]
mock_response.return_value = [
"1.1.0",
"1.1.1",
"1.1.2",
"1.1.3",
"1.1.4",
"1.1.5",
"1.1.6",
"1.1.7",
"1.1.8",
]
improvers = [ApacheTomcatImprover(), DefaultImprover()]
result = []
for improver in improvers:
for advisory in advisories:
inference = [data.to_dict() for data in improver.get_inferences(advisory)]
result.extend(inference)
expected_file = os.path.join(TEST_DATA, f"apache-tomcat-improver-expected.json")
util_tests.check_results_against_json(result, expected_file)


def test_extract_advisories_from_page():
page = """
<h3 id="Fixed_in_Apache_Tomcat_10.0.5"><span class="pull-right">6 April 2021</span> Fixed in Apache Tomcat 10.0.5</h3><div class="text">
Expand Down
Loading

0 comments on commit 3134cc5

Please sign in to comment.