-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine the CDX methods implementation and add tests #98
Signed-off-by: tdruez <[email protected]>
- Loading branch information
Showing
8 changed files
with
188 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# DejaCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: AGPL-3.0-only | ||
# See https://github.com/aboutcode-org/dejacode for support or download. | ||
# See https://aboutcode.org for more information about AboutCode FOSS projects. | ||
# | ||
|
||
from django import forms | ||
from django.core.exceptions import ValidationError | ||
|
||
from crispy_forms.helper import FormHelper | ||
|
||
from dje.forms import DataspacedModelForm | ||
from product_portfolio.models import ProductPackage | ||
from vulnerabilities.models import VulnerabilityAnalysis | ||
|
||
|
||
class VulnerabilityAnalysisForm(DataspacedModelForm): | ||
responses = forms.MultipleChoiceField( | ||
choices=VulnerabilityAnalysis.Response.choices, | ||
widget=forms.CheckboxSelectMultiple, | ||
required=False, | ||
) | ||
|
||
class Meta: | ||
model = VulnerabilityAnalysis | ||
fields = [ | ||
"product_package", | ||
"vulnerability", | ||
"state", | ||
"justification", | ||
"responses", | ||
"detail", | ||
] | ||
widgets = { | ||
"product_package": forms.widgets.HiddenInput, | ||
"vulnerability": forms.widgets.HiddenInput, | ||
"detail": forms.Textarea(attrs={"rows": 3}), | ||
} | ||
|
||
def __init__(self, user, *args, **kwargs): | ||
super().__init__(user, *args, **kwargs) | ||
|
||
responses_model_field = self._meta.model._meta.get_field("responses") | ||
self.fields["responses"].help_text = responses_model_field.help_text | ||
|
||
product_package_field = self.fields["product_package"] | ||
perms = ["view_product", "change_product"] | ||
product_package_field.queryset = ProductPackage.objects.product_secured(user, perms=perms) | ||
|
||
@property | ||
def helper(self): | ||
helper = FormHelper() | ||
helper.form_method = "post" | ||
helper.form_id = "product-vulnerability-analysis-form" | ||
helper.form_tag = False | ||
helper.modal_title = "Vulnerability analysis" | ||
helper.modal_id = "vulnerability-analysis-modal" | ||
return helper | ||
|
||
def clean(self): | ||
main_fields = ["state", "justification", "responses", "detail"] | ||
if not any(self.cleaned_data.get(field_name) for field_name in main_fields): | ||
raise ValidationError( | ||
"At least one of state, justification, responses or detail must be provided." | ||
) |
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,50 @@ | ||
# | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# DejaCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: AGPL-3.0-only | ||
# See https://github.com/aboutcode-org/dejacode for support or download. | ||
# See https://aboutcode.org for more information about AboutCode FOSS projects. | ||
# | ||
|
||
from pathlib import Path | ||
|
||
from django.test import TestCase | ||
|
||
from dje.models import Dataspace | ||
from dje.tests import create_superuser | ||
from product_portfolio.tests import make_product | ||
from product_portfolio.tests import make_product_package | ||
from vulnerabilities.forms import VulnerabilityAnalysisForm | ||
from vulnerabilities.tests import make_vulnerability | ||
|
||
|
||
class VulnerabilitiesFormsTestCase(TestCase): | ||
data = Path(__file__).parent / "data" | ||
|
||
def setUp(self): | ||
self.dataspace = Dataspace.objects.create(name="nexB") | ||
self.super_user = create_superuser("super_user", self.dataspace) | ||
|
||
def test_vulnerability_forms_vulnerability_analysis_save(self): | ||
vulnerability1 = make_vulnerability(dataspace=self.dataspace) | ||
product_package1 = make_product_package(make_product(self.dataspace)) | ||
|
||
data = { | ||
"product_package": product_package1, | ||
"vulnerability": vulnerability1, | ||
} | ||
|
||
form = VulnerabilityAnalysisForm(user=self.super_user, data=data) | ||
self.assertFalse(form.is_valid()) | ||
msg = "At least one of state, justification, responses or detail must be provided." | ||
self.assertEqual({"__all__": [msg]}, form.errors) | ||
|
||
data["detail"] = "Analysis detail" | ||
form = VulnerabilityAnalysisForm(user=self.super_user, data=data) | ||
self.assertTrue(form.is_valid()) | ||
analysis = form.save() | ||
self.assertEqual(vulnerability1, analysis.vulnerability) | ||
self.assertEqual(product_package1, analysis.product_package) | ||
self.assertEqual(product_package1.product, analysis.product) | ||
self.assertEqual(product_package1.package, analysis.package) | ||
self.assertEqual(data["detail"], analysis.detail) |
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