generated from aboutcode-org/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reference: #247 Signed-off-by: John M. Horan <[email protected]>
- Loading branch information
1 parent
ff53939
commit da8d816
Showing
3 changed files
with
510 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import json | ||
import os | ||
|
||
from click.testing import CliRunner | ||
from commoncode.testcase import FileBasedTesting | ||
from django.test import TestCase | ||
from fetchcode.package_versions import PackageVersion, router, versions | ||
|
||
import purlcli | ||
from packagedb.models import Package | ||
|
||
|
||
class TestPURLCLI(FileBasedTesting): | ||
test_data_dir = os.path.join(os.path.dirname(__file__), "data") | ||
|
||
# QUESTION: These four validate tests call validate_purls(), which queries the validate endpoint. Is that what we want here? I think we might be able to mock the API if we pass the API to the validate_purls() function instead of defining the API inside the function as we do now. | ||
|
||
def test_validate_purl(self): | ||
test_purls = [ | ||
"pkg:nginx/[email protected]?os=windows", | ||
|
@@ -92,3 +98,92 @@ def test_validate_purl_strip(self): | |
] | ||
|
||
self.assertEqual(validated_purls, expected_results) | ||
|
||
def test_versions(self): | ||
purls = ["pkg:pypi/fetchcode"] | ||
abc = purlcli.list_versions(purls) | ||
|
||
print(f"\nabc = {abc}") | ||
|
||
for purl_list in abc: | ||
for p in purl_list: | ||
# print(PackageVersion.to_dict(p)) | ||
p_dict = PackageVersion.to_dict(p) | ||
# print(f"\np_dict = {p_dict}") | ||
# print(f"\ntype(p_dict) = {type(p_dict)}") | ||
|
||
json_p_dict = json.dumps(p_dict) | ||
# print(f"\njson_p_dict = {json_p_dict}") | ||
# print(f"\ntype(json_p_dict) = {type(json_p_dict)}") | ||
|
||
|
||
# 2024-01-08 Monday 17:55:15. Based on test_api.py's class PurlValidateApiTestCase(TestCase). | ||
class TestPURLCLI_API(TestCase): | ||
def setUp(self): | ||
self.package_data = { | ||
"type": "npm", | ||
"namespace": "", | ||
"name": "foobar", | ||
"version": "1,1.0", | ||
"qualifiers": "", | ||
"subpath": "", | ||
"download_url": "", | ||
"filename": "Foo.zip", | ||
"sha1": "testsha1", | ||
"md5": "testmd5", | ||
"size": 101, | ||
} | ||
self.package = Package.objects.create(**self.package_data) | ||
self.package.refresh_from_db() | ||
|
||
def test_api_purl_validation(self): | ||
data1 = { | ||
"purl": "pkg:npm/[email protected]", | ||
"check_existence": True, | ||
} | ||
response1 = self.client.get(f"/api/validate/", data=data1) | ||
|
||
print(f"\nresponse1 = {response1}") | ||
print(f"\nresponse1.data = {response1.data}") | ||
print(f"") | ||
|
||
data2 = { | ||
"purl": "pkg:npm/[email protected]", | ||
"check_existence": True, | ||
} | ||
response2 = self.client.get(f"/api/validate/", data=data2) | ||
|
||
print(f"\nresponse2 = {response2}") | ||
print(f"\nresponse2.data = {response2.data}") | ||
print(f"") | ||
|
||
self.assertEquals(True, response1.data["valid"]) | ||
self.assertEquals(True, response1.data["exists"]) | ||
self.assertEquals( | ||
"The provided Package URL is valid, and the package exists in the upstream repo.", | ||
response1.data["message"], | ||
) | ||
|
||
self.assertEquals(False, response2.data["valid"]) | ||
self.assertEquals( | ||
"The provided PackageURL is not valid.", response2.data["message"] | ||
) | ||
|
||
# ZZZ: 2024-01-08 Monday 18:54:51. Some exploring: | ||
|
||
data3 = { | ||
"purl": "pkg:npm/ogdendunes", | ||
"check_existence": True, | ||
} | ||
response3 = self.client.get(f"/api/validate/", data=data3) | ||
|
||
print(f"\nresponse3 = {response3}") | ||
print(f"\nresponse3.data = {response3.data}") | ||
print(f"") | ||
|
||
self.assertEqual(True, response3.data["valid"]) | ||
self.assertEqual(False, response3.data["exists"]) | ||
self.assertEqual( | ||
"The provided Package URL is valid but does not exist in the upstream repo.", | ||
response3.data["message"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.