@@ -789,6 +789,9 @@ func TestAuthorizedAuditLogs(t *testing.T) {
789
789
}))
790
790
}
791
791
792
+ // This map is a simple way to insert a given number of organizations
793
+ // and audit logs for each organization.
794
+ // map[orgID][]AuditLogID
792
795
orgAuditLogs := map [uuid.UUID ][]uuid.UUID {
793
796
uuid .New (): {uuid .New (),uuid .New ()},
794
797
uuid .New (): {uuid .New (),uuid .New ()},
@@ -828,46 +831,55 @@ func TestAuthorizedAuditLogs(t *testing.T) {
828
831
t .Run ("NoAccess" ,func (t * testing.T ) {
829
832
t .Parallel ()
830
833
831
- siteAuditorCtx := dbauthz .As (ctx , rbac.Subject {
834
+ // Given: A user who is a member of 0 organizations
835
+ memberCtx := dbauthz .As (ctx , rbac.Subject {
832
836
FriendlyName :"member" ,
833
837
ID :uuid .NewString (),
834
838
Roles : rbac.Roles {memberRole },
835
839
Scope :rbac .ScopeAll ,
836
840
})
837
841
838
- logs ,err := db .GetAuditLogsOffset (siteAuditorCtx , database.GetAuditLogsOffsetParams {})
842
+ // When: The user queries for audit logs
843
+ logs ,err := db .GetAuditLogsOffset (memberCtx , database.GetAuditLogsOffsetParams {})
839
844
require .NoError (t ,err )
845
+ // Then: No logs returned
840
846
require .Len (t ,logs ,0 ,"no logs should be returned" )
841
847
})
842
848
843
849
t .Run ("SiteWideAuditor" ,func (t * testing.T ) {
844
850
t .Parallel ()
845
851
852
+ // Given: A site wide auditor
846
853
siteAuditorCtx := dbauthz .As (ctx , rbac.Subject {
847
854
FriendlyName :"owner" ,
848
855
ID :uuid .NewString (),
849
856
Roles : rbac.Roles {auditorRole },
850
857
Scope :rbac .ScopeAll ,
851
858
})
852
859
860
+ // When: the auditor queries for audit logs
853
861
logs ,err := db .GetAuditLogsOffset (siteAuditorCtx , database.GetAuditLogsOffsetParams {})
854
862
require .NoError (t ,err )
863
+ // Then: All logs are returned
855
864
require .ElementsMatch (t ,auditOnlyIDs (allLogs ),auditOnlyIDs (logs ))
856
865
})
857
866
858
867
t .Run ("SingleOrgAuditor" ,func (t * testing.T ) {
859
868
t .Parallel ()
860
869
861
870
orgID := orgIDs [0 ]
862
- siteAuditorCtx := dbauthz .As (ctx , rbac.Subject {
871
+ // Given: An organization scoped auditor
872
+ orgAuditCtx := dbauthz .As (ctx , rbac.Subject {
863
873
FriendlyName :"org-auditor" ,
864
874
ID :uuid .NewString (),
865
875
Roles : rbac.Roles {orgAuditorRoles (t ,orgID )},
866
876
Scope :rbac .ScopeAll ,
867
877
})
868
878
869
- logs ,err := db .GetAuditLogsOffset (siteAuditorCtx , database.GetAuditLogsOffsetParams {})
879
+ // When: The auditor queries for audit logs
880
+ logs ,err := db .GetAuditLogsOffset (orgAuditCtx , database.GetAuditLogsOffsetParams {})
870
881
require .NoError (t ,err )
882
+ // Then: Only the logs for the organization are returned
871
883
require .ElementsMatch (t ,orgAuditLogs [orgID ],auditOnlyIDs (logs ))
872
884
})
873
885
@@ -876,30 +888,36 @@ func TestAuthorizedAuditLogs(t *testing.T) {
876
888
877
889
first := orgIDs [0 ]
878
890
second := orgIDs [1 ]
879
- siteAuditorCtx := dbauthz .As (ctx , rbac.Subject {
891
+ // Given: A user who is an auditor for two organizations
892
+ multiOrgAuditCtx := dbauthz .As (ctx , rbac.Subject {
880
893
FriendlyName :"org-auditor" ,
881
894
ID :uuid .NewString (),
882
895
Roles : rbac.Roles {orgAuditorRoles (t ,first ),orgAuditorRoles (t ,second )},
883
896
Scope :rbac .ScopeAll ,
884
897
})
885
898
886
- logs ,err := db .GetAuditLogsOffset (siteAuditorCtx , database.GetAuditLogsOffsetParams {})
899
+ // When: The user queries for audit logs
900
+ logs ,err := db .GetAuditLogsOffset (multiOrgAuditCtx , database.GetAuditLogsOffsetParams {})
887
901
require .NoError (t ,err )
902
+ // Then: All logs for both organizations are returned
888
903
require .ElementsMatch (t ,append (orgAuditLogs [first ],orgAuditLogs [second ]... ),auditOnlyIDs (logs ))
889
904
})
890
905
891
906
t .Run ("ErroneousOrg" ,func (t * testing.T ) {
892
907
t .Parallel ()
893
908
894
- siteAuditorCtx := dbauthz .As (ctx , rbac.Subject {
909
+ // Given: A user who is an auditor for an organization that has 0 logs
910
+ userCtx := dbauthz .As (ctx , rbac.Subject {
895
911
FriendlyName :"org-auditor" ,
896
912
ID :uuid .NewString (),
897
913
Roles : rbac.Roles {orgAuditorRoles (t ,uuid .New ())},
898
914
Scope :rbac .ScopeAll ,
899
915
})
900
916
901
- logs ,err := db .GetAuditLogsOffset (siteAuditorCtx , database.GetAuditLogsOffsetParams {})
917
+ // When: The user queries for audit logs
918
+ logs ,err := db .GetAuditLogsOffset (userCtx , database.GetAuditLogsOffsetParams {})
902
919
require .NoError (t ,err )
920
+ // Then: No logs are returned
903
921
require .Len (t ,logs ,0 ,"no logs should be returned" )
904
922
})
905
923
}