Skip to content

Commit

Permalink
Refactor purlcli.py validate, create tests #247
Browse files Browse the repository at this point in the history
Reference: #247

Signed-off-by: John M. Horan <[email protected]>
  • Loading branch information
johnmhoran committed Jan 4, 2024
1 parent 67e05a4 commit ff53939
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
94 changes: 94 additions & 0 deletions packagedb/tests/test_purlcli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import os

from click.testing import CliRunner
from commoncode.testcase import FileBasedTesting

import purlcli


class TestPURLCLI(FileBasedTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), "data")

def test_validate_purl(self):
test_purls = [
"pkg:nginx/[email protected]?os=windows",
"pkg:maven/com.fasterxml.jackson.core/[email protected]",
]

validated_purls = purlcli.validate_purls(test_purls)

expected_results = [
{
"valid": True,
"exists": None,
"message": "The provided PackageURL is valid, but `check_existence` is not supported for this package type.",
"purl": "pkg:nginx/[email protected]?os=windows",
},
{
"valid": True,
"exists": True,
"message": "The provided Package URL is valid, and the package exists in the upstream repo.",
"purl": "pkg:maven/com.fasterxml.jackson.core/[email protected]",
},
]

self.assertEqual(validated_purls, expected_results)

def test_validate_purl_empty(self):
test_purls = []

validated_purls = purlcli.validate_purls(test_purls)

expected_results = []

self.assertEqual(validated_purls, expected_results)

def test_validate_purl_invalid(self):
test_purls = [
"foo",
]

validated_purls = purlcli.validate_purls(test_purls)

expected_results = [
{
"valid": False,
"exists": None,
"message": "The provided PackageURL is not valid.",
"purl": "foo",
}
]

self.assertEqual(validated_purls, expected_results)

def test_validate_purl_strip(self):
test_purls = [
"pkg:nginx/[email protected]?os=windows",
" pkg:nginx/[email protected]?os=windows",
"pkg:nginx/[email protected]?os=windows ",
]

validated_purls = purlcli.validate_purls(test_purls)

expected_results = [
{
"valid": True,
"exists": None,
"message": "The provided PackageURL is valid, but `check_existence` is not supported for this package type.",
"purl": "pkg:nginx/[email protected]?os=windows",
},
{
"valid": True,
"exists": None,
"message": "The provided PackageURL is valid, but `check_existence` is not supported for this package type.",
"purl": "pkg:nginx/[email protected]?os=windows",
},
{
"valid": True,
"exists": None,
"message": "The provided PackageURL is valid, but `check_existence` is not supported for this package type.",
"purl": "pkg:nginx/[email protected]?os=windows",
},
]

self.assertEqual(validated_purls, expected_results)
13 changes: 10 additions & 3 deletions purlcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ def validate(purls, output, file):
"""
if (purls and file) or not (purls or file):
raise click.UsageError("Use either purls or file but not both.")
api_query = "https://public.purldb.io/api/validate/"
validated_purls = []

if file:
purls = file.read().splitlines(False)

validated_purls = validate_purls(purls)

json.dump(validated_purls, output, indent=4)


def validate_purls(purls):
api_query = "https://public.purldb.io/api/validate/"
validated_purls = []
for purl in purls:
purl = purl.strip()
if not purl:
Expand All @@ -52,7 +59,7 @@ def validate(purls, output, file):
results = response.json()
validated_purls.append(results)

json.dump(validated_purls, output, indent=4)
return validated_purls


if __name__ == "__main__":
Expand Down

0 comments on commit ff53939

Please sign in to comment.