Skip to content

Commit

Permalink
Add unit tests #95
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Aug 30, 2024
1 parent ec9d975 commit 25a8430
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
30 changes: 30 additions & 0 deletions product_portfolio/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from product_portfolio.models import ProductRelationStatus
from product_portfolio.models import ProductStatus
from product_portfolio.models import ScanCodeProject
from product_portfolio.tests import make_product
from product_portfolio.views import ManageComponentGridView
from workflow.models import Request
from workflow.models import RequestTemplate
Expand Down Expand Up @@ -271,6 +272,35 @@ def test_product_portfolio_detail_view_tab_dependency_view(self):
response = self.client.get(url)
self.assertContains(response, "4 results")

def test_product_portfolio_detail_view_tab_vulnerability_view(self):
self.client.login(username="nexb_user", password="secret")
url = self.product1.get_url("tab_vulnerabilities")

with self.assertMaxQueries(9):
response = self.client.get(url)
self.assertContains(response, "0 results")

p1 = make_package(self.dataspace, is_vulnerable=True)
p2 = make_package(self.dataspace, is_vulnerable=True)
p3 = make_package(self.dataspace, is_vulnerable=True)
p4 = make_package(self.dataspace, is_vulnerable=True)
product1 = make_product(self.dataspace, inventory=[p1, p2, p3, p4])

self.assertEqual(4, product1.packages.count())
self.assertEqual(4, product1.packages.vulnerable().count())

url = product1.get_url("tab_vulnerabilities")
with self.assertMaxQueries(10):
response = self.client.get(url)
self.assertContains(response, "4 results")

def test_product_portfolio_detail_view_tab_vulnerability_view_filters(self):
self.client.login(username="nexb_user", password="secret")
url = self.product1.get_url("tab_vulnerabilities")
response = self.client.get(url)
expected = "?vulnerabilities-max_score=#vulnerabilities"
self.assertContains(response, expected)

def test_product_portfolio_detail_view_object_type_filter_in_inventory_tab(self):
self.client.login(username="nexb_user", password="secret")

Expand Down
32 changes: 15 additions & 17 deletions product_portfolio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
from product_portfolio.models import ScanCodeProject


class BaseProductView: # TODO: Rename this, it is a mixin
class BaseProductViewMixin:
model = Product
slug_url_kwarg = ("name", "version")

Expand Down Expand Up @@ -227,7 +227,7 @@ def get_extra_add_urls(self):

class ProductDetailsView(
LoginRequiredMixin,
BaseProductView,
BaseProductViewMixin,
ObjectDetailsView,
):
template_name = "product_portfolio/product_details.html"
Expand Down Expand Up @@ -685,7 +685,7 @@ def get_context_data(self, **kwargs):

class ProductTabInventoryView(
LoginRequiredMixin,
BaseProductView,
BaseProductViewMixin,
PreviousNextPaginationMixin,
TabContentView,
):
Expand Down Expand Up @@ -915,7 +915,7 @@ def inject_scan_data(scancodeio, feature_grouped, dataspace_uuid):

class ProductTabCodebaseView(
LoginRequiredMixin,
BaseProductView,
BaseProductViewMixin,
PreviousNextPaginationMixin,
TabContentView,
):
Expand Down Expand Up @@ -996,7 +996,7 @@ def has_any_values(field_name):

class ProductTabDependenciesView(
LoginRequiredMixin,
BaseProductView,
BaseProductViewMixin,
PreviousNextPaginationMixin,
TableHeaderMixin,
TabContentView,
Expand Down Expand Up @@ -1075,12 +1075,11 @@ def get_context_data(self, **kwargs):

class ProductTabVulnerabilitiesView(
LoginRequiredMixin,
BaseProductView,
BaseProductViewMixin,
PreviousNextPaginationMixin,
TableHeaderMixin,
TabContentView,
):
# TODO: check queries: assertMax
template_name = "product_portfolio/tabs/tab_vulnerabilities.html"
paginate_by = 50
query_dict_page_param = "vulnerabilities-page"
Expand Down Expand Up @@ -1108,7 +1107,6 @@ def get_context_data(self, **kwargs):
"-min_score",
)

# TODO: Add missing anchor
self.filterset = self.filterset_class(
self.request.GET,
queryset=vulnerability_qs,
Expand Down Expand Up @@ -1147,7 +1145,7 @@ def get_context_data(self, **kwargs):

class ProductTabImportsView(
LoginRequiredMixin,
BaseProductView,
BaseProductViewMixin,
TabContentView,
):
template_name = "product_portfolio/tabs/tab_imports.html"
Expand Down Expand Up @@ -1328,7 +1326,7 @@ class ProductAddView(

class ProductUpdateView(
LicenseDataForBuilderMixin,
BaseProductView,
BaseProductViewMixin,
DataspacedUpdateView,
):
form_class = ProductForm
Expand All @@ -1346,7 +1344,7 @@ def get_success_url(self):
return super().get_success_url()


class ProductDeleteView(BaseProductView, DataspacedDeleteView):
class ProductDeleteView(BaseProductViewMixin, DataspacedDeleteView):
permission_required = "product_portfolio.delete_product"

def get_queryset(self):
Expand Down Expand Up @@ -1583,7 +1581,7 @@ class AttributionView(
LoginRequiredMixin,
DataspaceScopeMixin,
GetDataspacedObjectMixin,
BaseProductView,
BaseProductViewMixin,
DetailView,
):
template_name = "product_portfolio/attribution/base.html"
Expand Down Expand Up @@ -1822,15 +1820,15 @@ def get_context_data(self, **kwargs):
return context


class ProductSendAboutFilesView(BaseProductView, SendAboutFilesView):
class ProductSendAboutFilesView(BaseProductViewMixin, SendAboutFilesView):
pass


class ProductExportSPDXDocumentView(BaseProductView, ExportSPDXDocumentView):
class ProductExportSPDXDocumentView(BaseProductViewMixin, ExportSPDXDocumentView):
pass


class ProductExportCycloneDXBOMView(BaseProductView, ExportCycloneDXBOMView):
class ProductExportCycloneDXBOMView(BaseProductViewMixin, ExportCycloneDXBOMView):
pass


Expand Down Expand Up @@ -1925,7 +1923,7 @@ class BaseProductManageGridView(
LicenseDataForBuilderMixin,
GetDataspacedObjectMixin,
PermissionRequiredMixin,
BaseProductView,
BaseProductViewMixin,
FormSetView,
):
"""A base view for managing product relationship through a grid."""
Expand Down Expand Up @@ -2248,7 +2246,7 @@ class BaseProductImportFormView(
PermissionRequiredMixin,
GetDataspacedObjectMixin,
DataspacedModelFormMixin,
BaseProductView,
BaseProductViewMixin,
FormView,
):
permission_required = "product_portfolio.change_product"
Expand Down

0 comments on commit 25a8430

Please sign in to comment.