@@ -405,13 +405,122 @@ def setUp(self):
405405 token = Token .objects .get (user__username = "admin" )
406406 self .client = APIClient ()
407407 self .client .credentials (HTTP_AUTHORIZATION = "Token " + token .key )
408+ self .admin = User .objects .get (username = "admin" )
409+ self .base_url = "/api/v2/findings/"
410+
411+ def _minimal_create_payload (self , title : str ):
412+ return {
413+ "test" : 3 ,
414+ "found_by" : [],
415+ "title" : title ,
416+ "date" : "2020-05-20" ,
417+ "cwe" : 1 ,
418+ "severity" : "High" ,
419+ "description" : "TEST finding for notification" ,
420+ "mitigation" : "MITIGATION" ,
421+ "impact" : "HIGH" ,
422+ "references" : "" ,
423+ "active" : True ,
424+ "verified" : False ,
425+ "false_p" : False ,
426+ "duplicate" : False ,
427+ "out_of_scope" : False ,
428+ "under_review" : False ,
429+ "under_defect_review" : False ,
430+ "numerical_severity" : "S0" ,
431+ }
408432
409433 @patch ("dojo.notifications.helper.NotificationManager._process_notifications" )
410434 def test_auditlog_on (self , mock ):
411435 prod_type = Product_Type .objects .create (name = "notif prod type API" )
412436 self .client .delete (reverse ("product_type-detail" , args = (prod_type .pk ,)), format = "json" )
413437 self .assertEqual (mock .call_args_list [- 1 ].kwargs ["description" ], 'The product type "notif prod type API" was deleted by admin' )
414438
439+ @patch ("dojo.api_v2.serializers.create_notification" )
440+ def test_create_calls_notification_with_auto_assigned_reporter (self , mock_create_notification ):
441+ """Test that create_notification is called when creating a finding without explicit reporter."""
442+ payload = self ._minimal_create_payload ("Finding with auto-assigned reporter notification" )
443+
444+ response = self .client .post (self .base_url , payload , format = "json" )
445+ self .assertEqual (201 , response .status_code , response .content [:1000 ])
446+
447+ # Verify notification was called
448+ mock_create_notification .assert_called_once ()
449+ call_args = mock_create_notification .call_args
450+
451+ # Check the notification parameters
452+ self .assertEqual (call_args [1 ]["event" ], "finding_added" )
453+ self .assertEqual (call_args [1 ]["title" ], "Addition of Finding With Auto-Assigned Reporter Notification" )
454+ self .assertEqual (
455+ call_args [1 ]["description" ],
456+ f'Finding "Finding With Auto-Assigned Reporter Notification" was added by { self .admin } ' ,
457+ )
458+ self .assertEqual (call_args [1 ]["icon" ], "exclamation-triangle" )
459+
460+ # Verify the finding was created successfully
461+ created_id = response .data .get ("id" )
462+ self .assertIsNotNone (created_id )
463+ created_finding = Finding .objects .get (id = created_id )
464+ self .assertEqual (created_finding .reporter , self .admin )
465+
466+ @patch ("dojo.api_v2.serializers.create_notification" )
467+ def test_create_calls_notification_with_explicit_reporter (self , mock_create_notification ):
468+ """Test that create_notification is called when creating a finding with explicit reporter."""
469+ # Create another user to use as explicit reporter
470+ explicit_reporter = User .
objects .
create (
username = "explicit_reporter" ,
email = "[email protected] " )
471+
472+ payload = self ._minimal_create_payload ("Finding with explicit reporter notification" )
473+ payload ["reporter" ] = explicit_reporter .id
474+
475+ response = self .client .post (self .base_url , payload , format = "json" )
476+ self .assertEqual (201 , response .status_code , response .content [:1000 ])
477+
478+ # Verify notification was called
479+ mock_create_notification .assert_called_once ()
480+ call_args = mock_create_notification .call_args
481+
482+ # Check the notification parameters
483+ self .assertEqual (call_args [1 ]["event" ], "finding_added" )
484+ self .assertEqual (call_args [1 ]["title" ], "Addition of Finding With Explicit Reporter Notification" )
485+ self .assertEqual (
486+ call_args [1 ]["description" ],
487+ f'Finding "Finding With Explicit Reporter Notification" was added by { explicit_reporter } ' ,
488+ )
489+ self .assertEqual (call_args [1 ]["icon" ], "exclamation-triangle" )
490+
491+ # Verify the finding was created with explicit reporter
492+ created_id = response .data .get ("id" )
493+ self .assertIsNotNone (created_id )
494+ created_finding = Finding .objects .get (id = created_id )
495+ self .assertEqual (created_finding .reporter , explicit_reporter )
496+
497+ @patch ("dojo.api_v2.serializers.create_notification" )
498+ def test_notification_parameters_are_correct (self , mock_create_notification ):
499+ """Test that all notification parameters are properly formatted and passed."""
500+ payload = self ._minimal_create_payload ("Test Finding for Parameter Validation" )
501+
502+ response = self .client .post (self .base_url , payload , format = "json" )
503+ self .assertEqual (201 , response .status_code , response .content [:1000 ])
504+
505+ # Get the created finding to verify URL formation
506+ created_id = response .data .get ("id" )
507+ created_finding = Finding .objects .get (id = created_id )
508+
509+ # Verify notification was called with correct parameters
510+ mock_create_notification .assert_called_once ()
511+ call_args = mock_create_notification .call_args
512+
513+ # Verify all required parameters exist
514+ self .assertEqual (call_args [1 ]["event" ], "finding_added" )
515+ self .assertEqual (call_args [1 ]["title" ], "Addition of Test Finding for Parameter Validation" )
516+ self .assertEqual (
517+ call_args [1 ]["description" ],
518+ f'Finding "Test Finding for Parameter Validation" was added by { self .admin } ' ,
519+ )
520+ self .assertEqual (call_args [1 ]["url" ], f"/finding/{ created_finding .id } " )
521+ self .assertEqual (call_args [1 ]["icon" ], "exclamation-triangle" )
522+ self .assertEqual (call_args [1 ]["finding" ], created_finding )
523+
415524
416525class TestNotificationWebhooks (DojoTestCase ):
417526 fixtures = ["dojo_testdata.json" ]
0 commit comments