from django.test import TestCase
from dojo.models import Test
from dojo.tools.github_vulnerability.parser import GithubVulnerabilityParser


class TestGithubVulnerabilityParser(TestCase):
    def test_parse_without_file_has_no_findings(self):
        parser = GithubVulnerabilityParser(None, Test())
        self.assertEqual(0, len(parser.items))

    # sample with zero vulnerability
    def test_parse_file_with_no_vuln_has_no_findings(self):
        testfile = open("dojo/unittests/scans/github_vulnerability/github-0-vuln.json")
        parser = GithubVulnerabilityParser(testfile, Test())
        self.assertEqual(0, len(parser.items))

    # sample with one vulnerability
    def test_parse_file_with_one_vuln_has_one_findings(self):
        testfile = open("dojo/unittests/scans/github_vulnerability/github-1-vuln.json")
        parser = GithubVulnerabilityParser(testfile, Test())
        self.assertEqual(1, len(parser.items))

        findings = parser.items[0]
        self.assertEqual(findings.title, "package")
        self.assertEqual(
            findings.description,
            "This is a sample description for sample description from Github API.",
        )
        self.assertEqual(
            findings.severity, "Critical"
        )

    # sample with five vulnerability
    def test_parse_file_with_multiple_vuln_has_multiple_findings(self):
        testfile = open("dojo/unittests/scans/github_vulnerability/github-5-vuln.json")
        parser = GithubVulnerabilityParser(testfile, Test())
        self.assertEqual(5, len(parser.items))
