-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #932 from TG1999/all_vulnerable_purls
Add API endpoint to get all vulnerable packages #929
- Loading branch information
Showing
5 changed files
with
110 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# VulnerableCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
# See https://github.com/nexB/vulnerablecode for support or download. | ||
# See https://aboutcode.org for more information about nexB OSS projects. | ||
# | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase | ||
from packageurl import PackageURL | ||
|
||
from vulnerabilities.models import Package | ||
from vulnerabilities.models import PackageRelatedVulnerability | ||
from vulnerabilities.models import Vulnerability | ||
|
||
User = get_user_model() | ||
|
||
|
||
class TestPackageModel(TestCase): | ||
def setUp(self): | ||
vuln1 = Vulnerability.objects.create( | ||
summary="test-vuln", | ||
) | ||
vuln2 = Vulnerability.objects.create( | ||
summary="test-vuln1", | ||
) | ||
for i in range(0, 10): | ||
query_kwargs = dict( | ||
type="generic", | ||
namespace="nginx", | ||
name="test", | ||
version=str(i), | ||
qualifiers={}, | ||
subpath="", | ||
) | ||
vuln_package = Package.objects.create(**query_kwargs) | ||
# Attaching same package to 2 vulnerabilities | ||
PackageRelatedVulnerability.objects.create( | ||
package=vuln_package, | ||
vulnerability=vuln1, | ||
fix=False, | ||
) | ||
PackageRelatedVulnerability.objects.create( | ||
package=vuln_package, | ||
vulnerability=vuln2, | ||
fix=False, | ||
) | ||
|
||
def test_get_vulnerable_packages(self): | ||
vuln_packages = Package.objects.vulnerable() | ||
assert vuln_packages.count() == 10 | ||
vuln_purls = [pkg.purl for pkg in vuln_packages.only(*PackageURL._fields)] | ||
assert vuln_purls == [ | ||
"pkg:generic/nginx/test@0", | ||
"pkg:generic/nginx/test@1", | ||
"pkg:generic/nginx/test@2", | ||
"pkg:generic/nginx/test@3", | ||
"pkg:generic/nginx/test@4", | ||
"pkg:generic/nginx/test@5", | ||
"pkg:generic/nginx/test@6", | ||
"pkg:generic/nginx/test@7", | ||
"pkg:generic/nginx/test@8", | ||
"pkg:generic/nginx/test@9", | ||
] |