2

Improve requests log filters and sorting

This commit is contained in:
2026-03-12 18:42:12 +01:00
parent 200fc83831
commit fef2237c49
7 changed files with 589 additions and 89 deletions

View File

@@ -330,3 +330,123 @@ func TestStoreOverviewLeaderboardsUseTrafficFromRawJSON(t *testing.T) {
t.Fatalf("expected filtered top urls to be empty, got %+v", filtered.TopURLs)
}
}
func TestStoreListEventsSupportsFiltersAndSorting(t *testing.T) {
t.Parallel()
dbPath := filepath.Join(t.TempDir(), "blocker.db")
db, err := Open(dbPath)
if err != nil {
t.Fatalf("open store: %v", err)
}
defer db.Close()
ctx := context.Background()
baseTime := time.Date(2025, 3, 12, 18, 0, 0, 0, time.UTC)
events := []*model.Event{
{
SourceName: "main",
ProfileName: "main",
OccurredAt: baseTime,
RemoteIP: "198.51.100.10",
ClientIP: "203.0.113.10",
Host: "example.test",
Method: "GET",
URI: "/wp-login.php",
Path: "/wp-login.php",
Status: 404,
UserAgent: "curl/8.0",
Decision: model.DecisionActionReview,
DecisionReason: "php_path",
DecisionReasons: []string{"php_path"},
RawJSON: `{"status":404}`,
},
{
SourceName: "main",
ProfileName: "main",
OccurredAt: baseTime.Add(10 * time.Second),
RemoteIP: "198.51.100.11",
ClientIP: "203.0.113.11",
Host: "example.test",
Method: "GET",
URI: "/xmlrpc.php",
Path: "/xmlrpc.php",
Status: 401,
UserAgent: "curl/8.0",
Decision: model.DecisionActionReview,
DecisionReason: "php_path",
DecisionReasons: []string{"php_path"},
RawJSON: `{"status":401}`,
},
{
SourceName: "main",
ProfileName: "main",
OccurredAt: baseTime.Add(20 * time.Second),
RemoteIP: "198.51.100.12",
ClientIP: "203.0.113.20",
Host: "example.test",
Method: "POST",
URI: "/xmlrpc.php",
Path: "/xmlrpc.php",
Status: 403,
UserAgent: "curl/8.0",
Decision: model.DecisionActionReview,
DecisionReason: "unexpected_post",
DecisionReasons: []string{"unexpected_post"},
RawJSON: `{"status":403}`,
},
{
SourceName: "gitea",
ProfileName: "gitea",
OccurredAt: baseTime.Add(30 * time.Second),
RemoteIP: "198.51.100.13",
ClientIP: "203.0.113.30",
Host: "git.example.test",
Method: "GET",
URI: "/install.php",
Path: "/install.php",
Status: 404,
UserAgent: "curl/8.0",
Decision: model.DecisionActionReview,
DecisionReason: "suspicious_path_prefix:/install.php",
DecisionReasons: []string{"suspicious_path_prefix:/install.php"},
RawJSON: `{"status":404}`,
},
}
for _, event := range events {
if err := db.RecordEvent(ctx, event); err != nil {
t.Fatalf("record event %+v: %v", event, err)
}
}
for _, ip := range []string{"203.0.113.10", "203.0.113.11"} {
if err := db.SaveInvestigation(ctx, model.IPInvestigation{
IP: ip,
UpdatedAt: baseTime,
Bot: &model.BotMatch{Name: "Googlebot", ProviderID: "google_official", Verified: true},
}); err != nil {
t.Fatalf("save investigation for %s: %v", ip, err)
}
}
items, err := db.ListEvents(ctx, baseTime.Add(-time.Minute), 10, model.EventListOptions{
Source: "main",
Method: "GET",
StatusFilter: "4xx",
State: string(model.IPStateReview),
BotFilter: "known",
SortBy: "status",
SortDesc: false,
})
if err != nil {
t.Fatalf("list events with filters: %v", err)
}
if len(items) != 2 {
t.Fatalf("expected 2 filtered items, got %+v", items)
}
if items[0].ClientIP != "203.0.113.11" || items[0].Status != 401 {
t.Fatalf("unexpected first filtered row: %+v", items[0])
}
if items[1].ClientIP != "203.0.113.10" || items[1].Status != 404 {
t.Fatalf("unexpected second filtered row: %+v", items[1])
}
}