2828
2929from django .apps import apps
3030from django .core .exceptions import SuspiciousFileOperation
31+ from django .http .response import Http404
3132from django .test import TestCase
3233from django .test import override_settings
3334from django .urls import reverse
3435from django .urls .exceptions import NoReverseMatch
3536
3637import requests
3738
39+ from scanpipe .forms import BaseProjectActionForm
3840from scanpipe .models import CodebaseRelation
3941from scanpipe .models import CodebaseResource
4042from scanpipe .models import DiscoveredDependency
4850from scanpipe .tests import dependency_data2
4951from scanpipe .tests import make_dependency
5052from scanpipe .tests import make_package
53+ from scanpipe .tests import make_project
5154from scanpipe .tests import make_resource_file
5255from scanpipe .tests import package_data1
5356from scanpipe .tests import package_data2
57+ from scanpipe .views import ProjectActionView
5458from scanpipe .views import ProjectCodebaseView
5559from scanpipe .views import ProjectDetailView
5660
@@ -147,7 +151,7 @@ def test_scanpipe_views_project_list_state_of_filters_in_search_form(self):
147151 def test_scanpipe_views_project_list_filters_exclude_page (self , mock_paginate_by ):
148152 url = reverse ("project_list" )
149153 # Create another project to enable pagination
150- Project . objects . create ( name = "project2" )
154+ make_project ( )
151155 mock_paginate_by .return_value = 1
152156
153157 data = {"page" : "2" }
@@ -162,6 +166,34 @@ def test_scanpipe_views_project_list_filters_exclude_page(self, mock_paginate_by
162166 expected = '<a href="?sort=" class="dropdown-item is-active">Newest</a>'
163167 self .assertContains (response , expected )
164168
169+ def test_scanpipe_views_project_list_modal_forms_include_url_query (self ):
170+ url = reverse ("project_list" )
171+ response = self .client .get (url )
172+
173+ expected = '<input type="hidden" name="url_query" value="">'
174+ self .assertContains (response , expected , html = True )
175+
176+ url_query = "name=search_value"
177+ response = self .client .get (url + "?" + url_query )
178+ expected = f'<input type="hidden" name="url_query" value="{ url_query } ">'
179+ self .assertContains (response , expected , html = True )
180+
181+ @mock .patch ("scanpipe.views.ProjectListView.get_paginate_by" )
182+ def test_scanpipe_views_project_list_modal_forms_include_show_on_all_checked (
183+ self , mock_paginate_by
184+ ):
185+ url = reverse ("project_list" )
186+ # Create another project to enable pagination
187+ make_project ()
188+ mock_paginate_by .return_value = 1
189+ response = self .client .get (url )
190+ expected = '<div class="show-on-all-checked">'
191+ self .assertContains (response , expected )
192+
193+ mock_paginate_by .return_value = 2
194+ response = self .client .get (url )
195+ self .assertNotContains (response , expected )
196+
165197 def test_scanpipe_views_project_actions_view (self ):
166198 url = reverse ("project_action" )
167199 response = self .client .get (url )
@@ -198,6 +230,37 @@ def test_scanpipe_views_project_action_report_view(self):
198230 response = self .client .post (url , data = data , follow = True )
199231 self .assertEqual ("report.xlsx" , response .filename )
200232
233+ def test_scanpipe_views_project_action_view_get_project_queryset (self ):
234+ queryset = ProjectActionView .get_project_queryset (
235+ selected_project_ids = [self .project1 .uuid ],
236+ action_form = None ,
237+ )
238+ self .assertQuerySetEqual (queryset , [self .project1 ])
239+
240+ # No project selection, no select_across
241+ form_data = {"select_across" : 0 }
242+ action_form = BaseProjectActionForm (data = form_data )
243+ action_form .full_clean ()
244+ with self .assertRaises (Http404 ):
245+ ProjectActionView .get_project_queryset (
246+ selected_project_ids = None ,
247+ action_form = action_form ,
248+ )
249+
250+ # select_across, no active filters
251+ form_data = {"select_across" : 1 }
252+ action_form = BaseProjectActionForm (data = form_data )
253+ action_form .full_clean ()
254+ self .assertQuerySetEqual (queryset , [self .project1 ])
255+
256+ # select_across, active filters
257+ make_project ()
258+ self .assertEqual (2 , Project .objects .count ())
259+ form_data = {"select_across" : 1 , "url_query" : f"name={ self .project1 .name } " }
260+ action_form = BaseProjectActionForm (data = form_data )
261+ action_form .full_clean ()
262+ self .assertQuerySetEqual (queryset , [self .project1 ])
263+
201264 def test_scanpipe_views_project_details_is_archived (self ):
202265 url = self .project1 .get_absolute_url ()
203266 expected1 = "WARNING: This project is archived and read-only."
0 commit comments