from django.test import TestCase
from dojo.models import Test
from dojo.tools.harbor_vulnerability.parser import HarborVulnerabilityParser


class TestHarborVulnerabilityParser(TestCase):

    def test_parse_file_with_no_vuln_has_no_findings(self):
        testfile = open("dojo/unittests/scans/harbor_vulnerability/harbor-0-vuln.json")
        parser = HarborVulnerabilityParser()
        findings = parser.get_findings(testfile, Test())
        self.assertEqual(0, len(findings))

    # Sample with One Test
    # + also verify data with one test
    def test_parse_file_with_one_vuln_has_one_findings(self):
        testfile = open("dojo/unittests/scans/harbor_vulnerability/harbor-1-vuln.json")
        parser = HarborVulnerabilityParser()
        findings = parser.get_findings(testfile, Test())
        self.assertEqual(1, len(findings))

        findings = findings[0]
        self.assertEqual(findings.title, "CVE-YYYY-NNN - package (exploitable-version)")
        self.assertEqual(
            findings.description,
            "This is a sample description for sample description from Harbor API.",
        )
        self.assertEqual(
            findings.severity, "Informational"
        )  # Negligible is translated to Informational

    # Sample with Multiple Test
    def test_parse_file_with_multiple_vuln_has_multiple_findings(self):
        testfile = open("dojo/unittests/scans/harbor_vulnerability/harbor-5-vuln.json")
        parser = HarborVulnerabilityParser()
        findings = parser.get_findings(testfile, Test())
        self.assertEqual(5, len(findings))
