mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore(coderd): add MockAuditor.Contains test helper (#10421)
* Adds a Contains() method on MockAuditor to help with asserting the presence of an audit log with specific fields. * Updates existing usages of verifyAuditWorkspaceCreated to use the new helper * Updates test referenced in PR#10396.
This commit is contained in:
+15
-25
@@ -511,7 +511,11 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
|
||||
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
|
||||
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
|
||||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
|
||||
verifyAuditWorkspaceCreated(t, auditor, workspace.Name)
|
||||
assert.True(t, auditor.Contains(t, database.AuditLog{
|
||||
ResourceType: database.ResourceTypeWorkspace,
|
||||
Action: database.AuditActionCreate,
|
||||
ResourceTarget: workspace.Name,
|
||||
}))
|
||||
})
|
||||
|
||||
t.Run("CreateFromVersionWithAuditLogs", func(t *testing.T) {
|
||||
@@ -535,7 +539,11 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
|
||||
|
||||
require.Equal(t, testWorkspaceBuild.TemplateVersionID, versionTest.ID)
|
||||
require.Equal(t, defaultWorkspaceBuild.TemplateVersionID, versionDefault.ID)
|
||||
verifyAuditWorkspaceCreated(t, auditor, defaultWorkspace.Name)
|
||||
assert.True(t, auditor.Contains(t, database.AuditLog{
|
||||
ResourceType: database.ResourceTypeWorkspace,
|
||||
Action: database.AuditActionCreate,
|
||||
ResourceTarget: defaultWorkspace.Name,
|
||||
}))
|
||||
})
|
||||
|
||||
t.Run("InvalidCombinationOfTemplateAndTemplateVersion", func(t *testing.T) {
|
||||
@@ -2741,7 +2749,11 @@ func TestWorkspaceDormant(t *testing.T) {
|
||||
Dormant: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, auditRecorder.AuditLogs(), 1)
|
||||
require.True(t, auditRecorder.Contains(t, database.AuditLog{
|
||||
Action: database.AuditActionWrite,
|
||||
ResourceType: database.ResourceTypeWorkspace,
|
||||
ResourceTarget: workspace.Name,
|
||||
}))
|
||||
|
||||
workspace = coderdtest.MustWorkspace(t, client, workspace.ID)
|
||||
require.NoError(t, err, "fetch provisioned workspace")
|
||||
@@ -2804,25 +2816,3 @@ func TestWorkspaceDormant(t *testing.T) {
|
||||
coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStop, database.WorkspaceTransitionStart)
|
||||
})
|
||||
}
|
||||
|
||||
func verifyAuditWorkspaceCreated(t *testing.T, auditor *audit.MockAuditor, workspaceName string) {
|
||||
var auditLogs []database.AuditLog
|
||||
ok := assert.Eventually(t, func() bool {
|
||||
auditLogs = auditor.AuditLogs()
|
||||
|
||||
for _, auditLog := range auditLogs {
|
||||
if auditLog.Action == database.AuditActionCreate &&
|
||||
auditLog.ResourceType == database.ResourceTypeWorkspace &&
|
||||
auditLog.ResourceTarget == workspaceName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}, testutil.WaitMedium, testutil.IntervalFast)
|
||||
|
||||
if !ok {
|
||||
for i, auditLog := range auditLogs {
|
||||
t.Logf("%d. Audit: ID=%s action=%s resourceID=%s resourceType=%s resourceTarget=%s", i+1, auditLog.ID, auditLog.Action, auditLog.ResourceID, auditLog.ResourceType, auditLog.ResourceTarget)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user