@@ -473,9 +473,12 @@ def has_permission(self, request, view):
473473 # Raise an explicit drf exception here
474474 raise ValidationError (e )
475475 if engagement := converted_dict .get ("engagement" ):
476- # Validate the resolved engagement's parent chain matches any provided names
477- if (product_name := converted_dict .get ("product_name" )) and engagement .product .name != product_name :
478- msg = f'The resolved engagement is associated with product "{ engagement .product .name } ", not with product "{ product_name } "'
476+ # Validate the resolved engagement's parent chain matches any provided identifiers
477+ if (product := converted_dict .get ("product" )) and engagement .product_id != product .id :
478+ msg = f'The resolved engagement is associated with product "{ engagement .product .name } ", not with product "{ converted_dict .get ("product_name" )} "'
479+ raise ValidationError (msg )
480+ if (engagement_name := converted_dict .get ("engagement_name" )) and engagement .name != engagement_name :
481+ msg = f'The resolved engagement is named "{ engagement .name } ", not "{ engagement_name } "'
479482 raise ValidationError (msg )
480483 return user_has_permission (
481484 request .user , engagement , Permissions .Import_Scan_Result ,
@@ -777,11 +780,18 @@ def has_permission(self, request, view):
777780 raise ValidationError (e )
778781
779782 if test := converted_dict .get ("test" ):
780- # Validate the resolved test's parent chain matches any provided names
781- if (product_name := converted_dict .get ("product_name" )) and test .engagement .product .name != product_name :
783+ # Validate the resolved test's parent chain matches any provided identifiers
784+ if (product := converted_dict .get ("product" )) and test .engagement .product_id != product .id :
785+ msg = f'The resolved test is associated with product "{ test .engagement .product .name } ", not with product "{ converted_dict .get ("product_name" )} "'
786+ raise ValidationError (msg )
787+ if (engagement := converted_dict .get ("engagement" )) and test .engagement_id != engagement .id :
788+ msg = f'The resolved test is associated with engagement "{ test .engagement .name } ", not with engagement "{ converted_dict .get ("engagement_name" )} "'
789+ raise ValidationError (msg )
790+ # Also validate by name when the objects were not resolved (e.g. names that match no existing record)
791+ if not converted_dict .get ("product" ) and (product_name := converted_dict .get ("product_name" )) and test .engagement .product .name != product_name :
782792 msg = f'The resolved test is associated with product "{ test .engagement .product .name } ", not with product "{ product_name } "'
783793 raise ValidationError (msg )
784- if (engagement_name := converted_dict .get ("engagement_name" )) and test .engagement .name != engagement_name :
794+ if not converted_dict . get ( "engagement" ) and (engagement_name := converted_dict .get ("engagement_name" )) and test .engagement .name != engagement_name :
785795 msg = f'The resolved test is associated with engagement "{ test .engagement .name } ", not with engagement "{ engagement_name } "'
786796 raise ValidationError (msg )
787797 return user_has_permission (
0 commit comments