@@ -665,12 +665,13 @@ func TestDelete(t *testing.T) {
665665 Organization : organization.Organization {ID : uuid .New ().String (), Title : "Org One" },
666666 }
667667 res := resource.Resource {ID : "r1" , Name : "res-1" , Title : "Resource One" , NamespaceID : "resource/item" , ProjectID : proj .ID }
668+ lookup := project.Filter {ProjectIDs : []string {proj .ID }, IncludeDisabled : true }
668669
669670 t .Run ("deletes relations then the row, and writes the audit record" , func (t * testing.T ) {
670671 repo , relationSvc , _ , projectSvc , _ , _ , auditRepo , _ , svc := newTestService (t )
671672
672673 repo .EXPECT ().GetByID (mock .Anything , "r1" ).Return (res , nil )
673- projectSvc .EXPECT ().Get (mock .Anything , proj . ID ).Return (proj , nil )
674+ projectSvc .EXPECT ().List (mock .Anything , lookup ).Return ([]project. Project { proj } , nil )
674675 relationSvc .EXPECT ().Delete (mock .Anything , relation.Relation {
675676 Object : relation.Object {ID : "r1" , Namespace : "resource/item" },
676677 }).Return (nil )
@@ -719,7 +720,7 @@ func TestDelete(t *testing.T) {
719720 repo , relationSvc , _ , projectSvc , _ , _ , auditRepo , _ , svc := newTestService (t )
720721
721722 repo .EXPECT ().GetByID (mock .Anything , "r1" ).Return (res , nil )
722- projectSvc .EXPECT ().Get (mock .Anything , proj . ID ).Return (proj , nil )
723+ projectSvc .EXPECT ().List (mock .Anything , lookup ).Return ([]project. Project { proj } , nil )
723724 relationSvc .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (relation .ErrNotExist )
724725 repo .EXPECT ().Delete (mock .Anything , "r1" ).Return (nil )
725726 auditRepo .EXPECT ().Create (mock .Anything , mock .AnythingOfType ("models.AuditRecord" )).
@@ -733,7 +734,7 @@ func TestDelete(t *testing.T) {
733734 repo , relationSvc , _ , projectSvc , _ , _ , _ , _ , svc := newTestService (t )
734735
735736 repo .EXPECT ().GetByID (mock .Anything , "r1" ).Return (res , nil )
736- projectSvc .EXPECT ().Get (mock .Anything , proj . ID ).Return (proj , nil )
737+ projectSvc .EXPECT ().List (mock .Anything , lookup ).Return ([]project. Project { proj } , nil )
737738 relationSvc .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (errors .New ("spicedb down" ))
738739
739740 err := svc .Delete (ctx , "resource/item" , "r1" )
@@ -744,13 +745,40 @@ func TestDelete(t *testing.T) {
744745 repo , relationSvc , _ , projectSvc , _ , _ , _ , _ , svc := newTestService (t )
745746
746747 repo .EXPECT ().GetByID (mock .Anything , "r1" ).Return (res , nil )
747- projectSvc .EXPECT ().Get (mock .Anything , proj . ID ).Return (proj , nil )
748+ projectSvc .EXPECT ().List (mock .Anything , lookup ).Return ([]project. Project { proj } , nil )
748749 relationSvc .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (nil )
749750 repo .EXPECT ().Delete (mock .Anything , "r1" ).Return (errors .New ("db down" ))
750751
751752 err := svc .Delete (ctx , "resource/item" , "r1" )
752753 assert .ErrorContains (t , err , "db down" )
753754 })
755+
756+ t .Run ("deletes a resource of a disabled project" , func (t * testing.T ) {
757+ repo , relationSvc , _ , projectSvc , _ , _ , auditRepo , _ , svc := newTestService (t )
758+ disabled := proj
759+ disabled .State = project .Disabled
760+
761+ repo .EXPECT ().GetByID (mock .Anything , "r1" ).Return (res , nil )
762+ projectSvc .EXPECT ().List (mock .Anything , lookup ).Return ([]project.Project {disabled }, nil )
763+ relationSvc .EXPECT ().Delete (mock .Anything , mock .Anything ).Return (nil )
764+ repo .EXPECT ().Delete (mock .Anything , "r1" ).Return (nil )
765+ auditRepo .EXPECT ().Create (mock .Anything , mock .MatchedBy (func (rec auditmodels.AuditRecord ) bool {
766+ return rec .Resource .ID == proj .ID
767+ })).Return (auditmodels.AuditRecord {}, nil )
768+
769+ err := svc .Delete (ctx , "resource/item" , "r1" )
770+ assert .NoError (t , err )
771+ })
772+
773+ t .Run ("deletes nothing when the project is gone" , func (t * testing.T ) {
774+ repo , _ , _ , projectSvc , _ , _ , _ , _ , svc := newTestService (t )
775+
776+ repo .EXPECT ().GetByID (mock .Anything , "r1" ).Return (res , nil )
777+ projectSvc .EXPECT ().List (mock .Anything , lookup ).Return ([]project.Project {}, nil )
778+
779+ err := svc .Delete (ctx , "resource/item" , "r1" )
780+ assert .ErrorIs (t , err , project .ErrNotExist )
781+ })
754782}
755783
756784func TestPurge (t * testing.T ) {
0 commit comments