Phase 10, part 8: the audit log, and who made what
Sign-ins and failed ones, everything admins do to users, connections, posting accounts, each post published and email sent, pull requests and deletions go in an audit log (schema 36), kept two years; admins read it under Settings → Activity. Drafts, published posts and outreach emails show who made them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"supermarket/internal/audit"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/secrets"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
func TestActivityRecords(t *testing.T) {
|
||||
s, h := newServerEdition(t)
|
||||
owner := addUser(t, s, "owner@example.com", users.Owner, nil)
|
||||
owner.Name = "Olu"
|
||||
users.SaveProfile(s.DB, owner)
|
||||
oc := sessionFor(t, s, owner)
|
||||
|
||||
// A failed sign-in, a connection, a deletion.
|
||||
signin(h, "203.0.113.4", url.Values{"email": {"owner@example.com"}, "password": {"nope nope nope"}})
|
||||
as(h, oc, http.MethodPost, "/settings/sources", url.Values{secrets.HunterAPIKey: {"hk"}})
|
||||
e := &inventory.Entry{Name: "Rival", Relationship: "competitor"}
|
||||
inventory.Save(s.DB, e)
|
||||
if rec := as(h, oc, http.MethodPost, "/market/"+itoa64(e.ID)+"/delete", url.Values{}); rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("delete: %d", rec.Code)
|
||||
}
|
||||
|
||||
entries, _ := audit.List(s.DB, audit.Filter{}, 50)
|
||||
byAction := map[string][]audit.Entry{}
|
||||
for _, e := range entries {
|
||||
byAction[e.Action] = append(byAction[e.Action], e)
|
||||
}
|
||||
if f := byAction["signin-failed"]; len(f) != 1 || f[0].IP != "203.0.113.4" || f[0].UserID != owner.ID {
|
||||
t.Errorf("failed sign-in: %+v", f)
|
||||
}
|
||||
if c := byAction["connected"]; len(c) != 1 || c[0].Target != "Hunter.io" || c[0].UserName != "Olu" {
|
||||
t.Errorf("connection: %+v", c)
|
||||
}
|
||||
if d := byAction["deleted"]; len(d) != 1 || !strings.Contains(d[0].Target, "/market/") {
|
||||
t.Errorf("deletions: %+v", d)
|
||||
}
|
||||
// Filters.
|
||||
body := as(h, oc, http.MethodGet, "/settings/activity?action=signin-failed", nil).Body.String()
|
||||
if !strings.Contains(body, "failed to sign in") || strings.Contains(body, "Hunter.io") {
|
||||
t.Errorf("filtered: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func itoa64(n int64) string { return strconv.FormatInt(n, 10) }
|
||||
Reference in New Issue
Block a user