@@ -420,6 +420,56 @@ func TestCreate(t *testing.T) {
420420 assert .Equal (t , schema .UserPrincipal , got .PrincipalType )
421421 })
422422
423+ t .Run ("audit record target name uses title when set" , func (t * testing.T ) {
424+ repo , relationSvc , _ , projectSvc , _ , _ , auditRepo , _ , svc := newTestService (t )
425+ userID := uuid .New ().String ()
426+
427+ projectSvc .EXPECT ().Get (mock .Anything , testProject .ID ).Return (testProject , nil )
428+
429+ repo .EXPECT ().Create (mock .Anything , mock .Anything ).Return (resource.Resource {
430+ ID : uuid .New ().String (), Name : "aoi-uuid-1" , Title : "My AOI" , NamespaceID : "resource/item" ,
431+ ProjectID : testProject .ID , PrincipalID : userID , PrincipalType : schema .UserPrincipal ,
432+ }, nil )
433+
434+ relationSvc .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (nil )
435+ relationSvc .EXPECT ().Create (mock .Anything , mock .Anything ).Return (relation.Relation {}, nil ).Times (2 )
436+
437+ auditRepo .EXPECT ().Create (mock .Anything , mock .MatchedBy (func (r auditmodels.AuditRecord ) bool {
438+ return r .Target != nil && r .Target .Name == "My AOI"
439+ })).Return (auditmodels.AuditRecord {}, nil )
440+
441+ _ , err := svc .Create (ctx , resource.Resource {
442+ Name : "aoi-uuid-1" , Title : "My AOI" , NamespaceID : "resource/item" , ProjectID : testProject .ID ,
443+ PrincipalID : userID , PrincipalType : schema .UserPrincipal ,
444+ })
445+ assert .NoError (t , err )
446+ })
447+
448+ t .Run ("audit record target name falls back to name when title empty" , func (t * testing.T ) {
449+ repo , relationSvc , _ , projectSvc , _ , _ , auditRepo , _ , svc := newTestService (t )
450+ userID := uuid .New ().String ()
451+
452+ projectSvc .EXPECT ().Get (mock .Anything , testProject .ID ).Return (testProject , nil )
453+
454+ repo .EXPECT ().Create (mock .Anything , mock .Anything ).Return (resource.Resource {
455+ ID : uuid .New ().String (), Name : "res-slug" , NamespaceID : "resource/item" ,
456+ ProjectID : testProject .ID , PrincipalID : userID , PrincipalType : schema .UserPrincipal ,
457+ }, nil )
458+
459+ relationSvc .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (nil )
460+ relationSvc .EXPECT ().Create (mock .Anything , mock .Anything ).Return (relation.Relation {}, nil ).Times (2 )
461+
462+ auditRepo .EXPECT ().Create (mock .Anything , mock .MatchedBy (func (r auditmodels.AuditRecord ) bool {
463+ return r .Target != nil && r .Target .Name == "res-slug"
464+ })).Return (auditmodels.AuditRecord {}, nil )
465+
466+ _ , err := svc .Create (ctx , resource.Resource {
467+ Name : "res-slug" , NamespaceID : "resource/item" , ProjectID : testProject .ID ,
468+ PrincipalID : userID , PrincipalType : schema .UserPrincipal ,
469+ })
470+ assert .NoError (t , err )
471+ })
472+
423473 t .Run ("explicit principal skips authn lookup" , func (t * testing.T ) {
424474 repo , relationSvc , _ , projectSvc , _ , _ , auditRepo , _ , svc := newTestService (t )
425475 userID := uuid .New ().String ()
0 commit comments