Skip to content

Audit date filter/kira pilot #4845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
attempt to add test
  • Loading branch information
Kira-Pilot committed Nov 1, 2022
commit c3f04f5be91ce51e909242528779b995409b7bb4
5 changes: 4 additions & 1 deletion coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ func (api *API) generateFakeAuditLog(rw http.ResponseWriter, r *http.Request) {
if params.ResourceID == uuid.Nil {
params.ResourceID = uuid.New()
}
if params.Time.IsZero() {
params.Time = time.Now()
}

_, err = api.Database.InsertAuditLog(ctx, database.InsertAuditLogParams{
ID: uuid.New(),
Time: time.Now(),
Time: params.Time,
UserID: user.ID,
Ip: ipNet,
UserAgent: r.UserAgent(),
Expand Down
8 changes: 8 additions & 0 deletions coderd/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coderd_test
import (
"context"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -54,12 +55,14 @@ func TestAuditLogsFilter(t *testing.T) {
err := client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
Action: codersdk.AuditActionCreate,
ResourceType: codersdk.ResourceTypeTemplate,
Time: time.Date(2022, 8, 15, 14, 30, 45, 100, time.UTC), // 2022-8-15 14:30:45
})
require.NoError(t, err)
err = client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
Action: codersdk.AuditActionCreate,
ResourceType: codersdk.ResourceTypeUser,
ResourceID: userResourceID,
Time: time.Date(2022, 8, 16, 14, 30, 45, 100, time.UTC), // 2022-8-16 14:30:45
})
require.NoError(t, err)

Expand Down Expand Up @@ -127,6 +130,11 @@ func TestAuditLogsFilter(t *testing.T) {
SearchQuery: "action:invalid",
ExpectedResult: 3,
},
{
Name: "FilterWithDateFrom",
SearchQuery: "action:create date_from:2022-08-15 date_to:2022-08-15",
ExpectedResult: 1,
},
}

for _, testCase := range testCases {
Expand Down
1 change: 1 addition & 0 deletions codersdk/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type CreateTestAuditLogRequest struct {
Action AuditAction `json:"action,omitempty"`
ResourceType ResourceType `json:"resource_type,omitempty"`
ResourceID uuid.UUID `json:"resource_id,omitempty"`
Time time.Time `json:"time,omitempty"`
}

// AuditLogs retrieves audit logs from the given page.
Expand Down