@@ -319,7 +319,7 @@ def test_scanpipe_management_command_batch_create_global_webhook(
319
319
def test_scanpipe_management_command_add_input_file (self ):
320
320
out = StringIO ()
321
321
322
- project = Project . objects . create (name = "my_project" )
322
+ project = make_project (name = "my_project" )
323
323
parent_path = Path (__file__ ).parent
324
324
options = [
325
325
"--input-file" ,
@@ -346,7 +346,7 @@ def test_scanpipe_management_command_add_input_file(self):
346
346
call_command ("add-input" , * options , stdout = out )
347
347
348
348
def test_scanpipe_management_command_add_input_url (self ):
349
- project = Project . objects . create (name = "my_project" )
349
+ project = make_project (name = "my_project" )
350
350
options = [
351
351
"--input-url" ,
352
352
"https://example.com/archive.zip" ,
@@ -368,7 +368,7 @@ def test_scanpipe_management_command_add_input_url(self):
368
368
def test_scanpipe_management_command_add_input_copy_codebase (self ):
369
369
out = StringIO ()
370
370
371
- project = Project . objects . create (name = "my_project" )
371
+ project = make_project (name = "my_project" )
372
372
373
373
options = ["--copy-codebase" , "non-existing" , "--project" , project .name ]
374
374
expected = "non-existing not found"
@@ -394,7 +394,7 @@ def test_scanpipe_management_command_add_input_copy_codebase(self):
394
394
def test_scanpipe_management_command_add_pipeline (self ):
395
395
out = StringIO ()
396
396
397
- project = Project . objects . create (name = "my_project" )
397
+ project = make_project (name = "my_project" )
398
398
399
399
pipelines = [
400
400
self .pipeline_name ,
@@ -431,7 +431,7 @@ def test_scanpipe_management_command_add_pipeline(self):
431
431
def test_scanpipe_management_command_add_webhook (self ):
432
432
out = StringIO ()
433
433
434
- project = Project . objects . create (name = "my_project" )
434
+ project = make_project (name = "my_project" )
435
435
436
436
options = ["https://example.com/webhook" ]
437
437
expected = "the following arguments are required: --project"
@@ -486,7 +486,7 @@ def test_scanpipe_management_command_show_pipeline(self):
486
486
"analyze_root_filesystem_or_vm_image" ,
487
487
]
488
488
489
- project = Project . objects . create (name = "my_project" )
489
+ project = make_project (name = "my_project" )
490
490
for pipeline_name in pipeline_names :
491
491
project .add_pipeline (pipeline_name )
492
492
@@ -513,7 +513,7 @@ def test_scanpipe_management_command_show_pipeline(self):
513
513
self .assertEqual (expected , out .getvalue ())
514
514
515
515
def test_scanpipe_management_command_execute (self ):
516
- project = Project . objects . create (name = "my_project" )
516
+ project = make_project (name = "my_project" )
517
517
options = ["--project" , project .name ]
518
518
519
519
out = StringIO ()
@@ -557,7 +557,7 @@ def test_scanpipe_management_command_execute(self):
557
557
self .assertEqual ("" , run3 .task_output )
558
558
559
559
def test_scanpipe_management_command_execute_project_function (self ):
560
- project = Project . objects . create (name = "my_project" )
560
+ project = make_project (name = "my_project" )
561
561
562
562
expected = "No pipelines to run on project my_project"
563
563
with self .assertRaisesMessage (CommandError , expected ):
@@ -584,7 +584,7 @@ def test_scanpipe_management_command_execute_project_function(self):
584
584
self .assertIsNone (returned_value )
585
585
586
586
def test_scanpipe_management_command_status (self ):
587
- project = Project . objects . create (name = "my_project" )
587
+ project = make_project (name = "my_project" )
588
588
run = project .add_pipeline (self .pipeline_name )
589
589
590
590
options = ["--project" , project .name , "--no-color" ]
@@ -657,9 +657,9 @@ def test_scanpipe_management_command_list_pipelines(self):
657
657
self .assertIn ("(addon)" , output )
658
658
659
659
def test_scanpipe_management_command_list_project (self ):
660
- project1 = Project . objects . create (name = "project1" )
661
- project2 = Project . objects . create (name = "project2" )
662
- project3 = Project . objects . create (name = "archived" , is_archived = True )
660
+ project1 = make_project (name = "project1" )
661
+ project2 = make_project (name = "project2" )
662
+ project3 = make_project (name = "archived" , is_archived = True )
663
663
664
664
options = []
665
665
out = StringIO ()
@@ -686,7 +686,7 @@ def test_scanpipe_management_command_list_project(self):
686
686
self .assertIn (project3 .name , output )
687
687
688
688
def test_scanpipe_management_command_output (self ):
689
- project = Project . objects . create (name = "my_project" )
689
+ project = make_project (name = "my_project" )
690
690
make_package (project , package_url = "pkg:generic/name@1.0" )
691
691
692
692
out = StringIO ()
@@ -754,7 +754,7 @@ def test_scanpipe_management_command_output(self):
754
754
self .assertIn ('"specVersion": "1.5",' , out_value )
755
755
756
756
def test_scanpipe_management_command_delete_project (self ):
757
- project = Project . objects . create (name = "my_project" )
757
+ project = make_project (name = "my_project" )
758
758
work_path = project .work_path
759
759
self .assertTrue (work_path .exists ())
760
760
@@ -770,7 +770,7 @@ def test_scanpipe_management_command_delete_project(self):
770
770
self .assertFalse (work_path .exists ())
771
771
772
772
def test_scanpipe_management_command_archive_project (self ):
773
- project = Project . objects . create (name = "my_project" )
773
+ project = make_project (name = "my_project" )
774
774
(project .input_path / "input_file" ).touch ()
775
775
(project .codebase_path / "codebase_file" ).touch ()
776
776
self .assertEqual (1 , len (Project .get_root_content (project .input_path )))
@@ -797,7 +797,7 @@ def test_scanpipe_management_command_archive_project(self):
797
797
self .assertEqual (0 , len (Project .get_root_content (project .codebase_path )))
798
798
799
799
def test_scanpipe_management_command_reset_project (self ):
800
- project = Project . objects . create (name = "my_project" )
800
+ project = make_project (name = "my_project" )
801
801
project .add_pipeline ("analyze_docker_image" )
802
802
CodebaseResource .objects .create (project = project , path = "filename.ext" )
803
803
DiscoveredPackage .objects .create (project = project )
@@ -833,8 +833,8 @@ def test_scanpipe_management_command_reset_project(self):
833
833
self .assertEqual (0 , len (Project .get_root_content (project .codebase_path )))
834
834
835
835
def test_scanpipe_management_command_flush_projects (self ):
836
- project1 = Project . objects . create ( name = "project1" )
837
- project2 = Project . objects . create ( name = "project2" )
836
+ project1 = make_project ( "project1" )
837
+ project2 = make_project ( "project2" )
838
838
ten_days_ago = timezone .now () - datetime .timedelta (days = 10 )
839
839
project2 .update (created_date = ten_days_ago )
840
840
@@ -846,14 +846,58 @@ def test_scanpipe_management_command_flush_projects(self):
846
846
self .assertEqual (expected , out_value )
847
847
self .assertEqual (project1 , Project .objects .get ())
848
848
849
- Project .objects .create (name = "project2" )
849
+ make_project ("project2" )
850
+ out = StringIO ()
851
+ options = ["--no-color" , "--no-input" , "--dry-run" ]
852
+ call_command ("flush-projects" , * options , stdout = out )
853
+ out_value = out .getvalue ().strip ()
854
+ expected = "2 projects would be deleted:\n - project2\n - project1"
855
+ self .assertEqual (expected , out_value )
856
+
850
857
out = StringIO ()
851
858
options = ["--no-color" , "--no-input" ]
852
859
call_command ("flush-projects" , * options , stdout = out )
853
860
out_value = out .getvalue ().strip ()
854
861
expected = "2 projects and their related data have been removed."
855
862
self .assertEqual (expected , out_value )
856
863
864
+ def test_scanpipe_management_command_flush_projects_filters (self ):
865
+ label1 = "label1"
866
+ label2 = "label2"
867
+ make_project ("project1" , labels = [label1 ])
868
+ make_project ("project2" , labels = [label1 , label2 ])
869
+ make_project ("project3" , pipelines = ["scan_single_package" ])
870
+
871
+ base_options = ["--no-color" , "--no-input" , "--dry-run" ]
872
+
873
+ out = StringIO ()
874
+ options = base_options + ["--label" , label1 ]
875
+ call_command ("flush-projects" , * options , stdout = out )
876
+ out_value = out .getvalue ().strip ()
877
+ expected = "2 projects would be deleted:\n - project2\n - project1"
878
+ self .assertEqual (expected , out_value )
879
+
880
+ out = StringIO ()
881
+ options = base_options + ["--label" , label2 ]
882
+ call_command ("flush-projects" , * options , stdout = out )
883
+ out_value = out .getvalue ().strip ()
884
+ expected = "1 projects would be deleted:\n - project2"
885
+ self .assertEqual (expected , out_value )
886
+
887
+ out = StringIO ()
888
+ options = base_options + ["--label" , label1 , "--label" , label2 ]
889
+ call_command ("flush-projects" , * options , stdout = out )
890
+ out_value = out .getvalue ().strip ()
891
+ expected = "2 projects would be deleted:\n - project2\n - project1"
892
+ self .assertEqual (expected , out_value )
893
+
894
+ out = StringIO ()
895
+ options = base_options + ["--pipeline" , "scan_single_package" ]
896
+ call_command ("flush-projects" , * options , stdout = out )
897
+ out_value = out .getvalue ().strip ()
898
+ expected = "1 projects would be deleted:\n - project3"
899
+ self .assertEqual (expected , out_value )
900
+
857
901
def test_scanpipe_management_command_create_user (self ):
858
902
out = StringIO ()
859
903
@@ -1123,7 +1167,7 @@ def test_scanpipe_management_command_purldb_scan_queue_worker_continue_after_fai
1123
1167
)
1124
1168
1125
1169
def test_scanpipe_management_command_check_compliance (self ):
1126
- project = Project . objects . create (name = "my_project" )
1170
+ project = make_project (name = "my_project" )
1127
1171
1128
1172
out = StringIO ()
1129
1173
options = ["--project" , project .name ]
@@ -1169,9 +1213,8 @@ def test_scanpipe_management_command_check_compliance(self):
1169
1213
self .assertEqual (expected , out_value )
1170
1214
1171
1215
def test_scanpipe_management_command_report (self ):
1172
- project1 = make_project ("project1" )
1173
1216
label1 = "label1"
1174
- project1 . labels . add ( label1 )
1217
+ project1 = make_project ( "project1" , labels = [ label1 ] )
1175
1218
make_resource_file (project1 , path = "file.ext" , status = flag .REQUIRES_REVIEW )
1176
1219
make_project ("project2" )
1177
1220
0 commit comments