Files
meshcore-analyzer/internal/users/stats_test.go
T
efitenandClaude Opus 5.5 18a634c115 feat: admin dashboard with overview and audit log (user management part C) (#2138)
Part C of #2128: an admin area with an overview, the user table and a
global audit log. Off by default like the rest of user management: with
`userManagement` off nothing changes, and non-admins get no new UI.

This is the first of 4 stacked PRs (C, D, E, then a small export/backup
follow-up). Each later one contains this branch; review them in order.

## The situation

- An admin could manage users one by one in `#/admin/users`, but nothing
showed whether the instance needs attention: accounts stuck in
activation, bouncing mail, someone guessing a password, an MQTT source
that dropped.
- The audit log existed (`internal/users/audit.go`) but could only be
read per user, and logins were not recorded.

## What this PR adds

**Store (`internal/users`)**
- Schema v3: an index on `audit_log(at)`.
- `AuditList` with filters (action or group prefix like `user.login.*`,
user as actor or target, period) and keyset pagination; `PruneAudit`;
`Stats` for the user figures.

**Server**
- `GET /api/admin/stats` (typed struct): accounts by status, admins,
registrations and active users over 7 and 30 days, logins and failed
logins in 24 hours, mail by final status, and the attention items
computed server-side.
- `GET /api/admin/audit`: filtered, newest first, `next` cursor.
- `GET /api/admin/users` gains `bouncing=1`.
- Logins are audited as `user.login` and `user.login.failed` (reason
`wrong_password`, `pending` or `disabled`). An unknown address writes no
row. The writes are asynchronous, so the login response does not wait on
`users.db`. Login rows are pruned after 90 days.

**Frontend**
- `#/admin?tab=overview|users|audit`: `admin.js` (tab shell),
`admin-overview.js` ("Needs attention", Users card, System card from the
existing health, MQTT and observer endpoints), `admin-audit.js` (filters
in the URL, "Load more"). `admin-users.js` becomes the Users tab; the
old `#/admin/users` link rewrites to it.
- `/api/healthz` is read on open and on Refresh only, not on the
60-second timer, because it walks every packet under a read lock.

Spec:
[`docs/specs/2026-10-07-admin-dashboard-design.md`](https://github.com/efiten/CoreScope/blob/feat/admin-dashboard/docs/specs/2026-10-07-admin-dashboard-design.md).

## Verification

- `internal/users` and `cmd/server`: `go vet` and `go test` pass
locally, 26 new Go tests. One upstream test,
`TestSaveGeoFilterPreservesFileMode`, also fails on Windows on plain
`master` (file modes) and is unrelated.
- `sh test-all.sh` exits 0; the XSS gate in diff mode passes.
- User-management E2E with the `e2etest` build: 13 of 13 steps locally.
- Running on our staging and production instance since 7 October 2026.

## Not in this PR

- Restricting existing pages (perf, MQTT status) to logged-in users or
admins. The admin area adds no access mechanism of its own, so that
stays a later router rule plus endpoint check.
- Server-enforced customizer tab restrictions (#1508).
- The 24-hour, 5-attempt and 10-minute attention thresholds are
constants for now (AGENTS.md rule 8: customizer later).

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 21:38:30 +02:00

126 lines
3.8 KiB
Go

package users
import (
"fmt"
"reflect"
"testing"
"time"
)
func TestStatsCountsEveryFigure(t *testing.T) {
st, clk := newTestStore(t)
now := clk.Now() // 2026-10-06 12:00 UTC
at := func(d time.Duration) { clk.t = now.Add(d) }
day := 24 * time.Hour
must := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
at(-48 * time.Hour)
mustCreate(t, st, "stuck@example.org", "Stuck") // pending for more than 24 hours
at(-2 * time.Hour)
mustCreate(t, st, "fresh@example.org", "Fresh") // pending, recent
at(-40 * day)
act := mustCreate(t, st, "act@example.org", "Act")
adm := mustCreate(t, st, "adm@example.org", "Adm")
dis := mustCreate(t, st, "dis@example.org", "Dis")
for _, u := range []*User{act, adm, dis} {
must(st.Activate(u.ID, RoleUser, nil))
}
must(st.SetRole(adm.ID, RoleAdmin))
must(st.SetStatus(dis.ID, StatusDisabled))
must(st.SetEmailBouncing(dis.ID, true))
at(-3 * day)
must(st.TouchLogin(act.ID)) // active within 7 days
at(-10 * day)
_, _, err := st.CreateSession(adm.ID, 30*day, "") // active within 30 days only
must(err)
for _, d := range []time.Duration{-time.Hour, -3 * day, -20 * day, -40 * day} {
at(d)
mustAudit(t, st, nil, "user.register", nil, nil)
}
aid, mid := act.ID, adm.ID
at(-30 * time.Hour)
mustAudit(t, st, nil, "user.login", &aid, nil)
at(-25 * time.Hour)
mustAudit(t, st, nil, "user.login.failed", &mid, nil)
at(-time.Hour)
mustAudit(t, st, nil, "user.login", &aid, nil)
at(-2 * time.Hour)
for i := 0; i < 5; i++ {
mustAudit(t, st, nil, "user.login.failed", &mid, map[string]string{"reason": "wrong_password"})
}
for i := 0; i < 4; i++ {
mustAudit(t, st, nil, "user.login.failed", &aid, map[string]string{"reason": "wrong_password"})
}
at(-day)
for i, ev := range []string{"delivered", "opened", "hard_bounce", "soft_bounce", "blocked", "spam", "", "deferred", "error"} {
id := fmt.Sprintf("m%d", i)
_, err := st.LogMail(&aid, "act@example.org", "activate", id)
must(err)
if ev != "" {
_, _, err := st.RecordMailEvent(id, ev, clk.Now(), "")
must(err)
}
}
at(-8 * day)
_, err = st.LogMail(&aid, "act@example.org", "activate", "old")
must(err)
_, _, err = st.RecordMailEvent("old", "delivered", clk.Now(), "")
must(err)
at(0)
got, err := st.Stats(now)
if err != nil {
t.Fatal(err)
}
perDay := got.NewPerDay
got.NewPerDay = nil
want := Stats{Total: 5, Active: 2, Pending: 2, Disabled: 1, Admins: 1, StuckPending: 1, Bouncing: 1,
New7d: 2, New30d: 3, Active7d: 1, Active30d: 2, Logins24h: 1, FailedLogins24h: 9,
Mail7d: MailCounts{Delivered: 2, Bounced: 2, Blocked: 1, Spam: 1, Pending: 2, Other: 1},
Guessing: []GuessedAccount{{UserID: adm.ID, DisplayName: "Adm", Failed: 5}}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Stats =\n%+v\nwant\n%+v", got, want)
}
if len(perDay) != 30 || perDay[0].Day != "2026-09-07" || perDay[29].Day != "2026-10-06" {
t.Fatalf("NewPerDay bounds = %d entries, %+v .. %+v", len(perDay), perDay[0], perDay[len(perDay)-1])
}
nonZero := map[string]int{}
for _, d := range perDay {
if d.Count != 0 {
nonZero[d.Day] = d.Count
}
}
if !reflect.DeepEqual(nonZero, map[string]int{"2026-09-16": 1, "2026-10-03": 1, "2026-10-06": 1}) {
t.Fatalf("NewPerDay non-zero days = %v", nonZero)
}
}
func TestStatsOnEmptyDatabase(t *testing.T) {
st, clk := newTestStore(t)
got, err := st.Stats(clk.Now())
if err != nil {
t.Fatal(err)
}
if got.Total != 0 || got.New30d != 0 || got.Active30d != 0 || got.FailedLogins24h != 0 || got.Mail7d != (MailCounts{}) {
t.Fatalf("empty Stats = %+v", got)
}
if got.Guessing == nil || len(got.Guessing) != 0 {
t.Fatalf("Guessing = %#v; want an empty, non-nil slice", got.Guessing)
}
if len(got.NewPerDay) != 30 {
t.Fatalf("NewPerDay has %d entries; want 30", len(got.NewPerDay))
}
for _, d := range got.NewPerDay {
if d.Count != 0 {
t.Fatalf("NewPerDay = %+v; want zeros", got.NewPerDay)
}
}
}