Files
felhom.eu/hub/internal/api/d84_unseen_claim_test.go
T
admin 875c339cf5 hub: review fix 1 — a box that lies about its app list cannot have the store agreed in one window unseen (D84 narrowed)
The hub's 30-day check rests on the box's own reports: a box could name an app
that never existed, or stop listing a living one, wait 30 days, and get every
snapshot agreed beside the cap, with no operator signal.

- One window agrees to at most the size of its own cap, from the hub's OWN
  count of the store; the oldest files first; the rest waits for the next
  window. A narrowing of D84, decided by CC unattended — the operator may
  reverse it (07 §8.2f holds the question, both options and their cost).
- offsite_end_claim_unseen (warning, mailed, operator-only): a box first names
  an app the hub never saw installed — once per household and app, at most 8
  per household, 30 days before anything can be agreed.
- offsite_ended_large_agreement (warning, mailed, operator-only): a request
  for more than half of the store waits one week; that window agrees to none.
- 07 §8.2f: the limit rewritten (no honest ended app is needed, only 30 days),
  what bounds it, what is still open. 08 §6.5: the two events.

Also carries the red-proof outputs of the controller's review fixes 2 and 3
(felhom-controller 3928c3d and the next commit).

Tests seen red on bb226f23; 15 mutations seen red (redproof-review-1.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 14:41:58 +01:00

101 lines
4.7 KiB
Go

package api
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/notify"
"gitea.dooplex.hu/admin/felhom-hub/internal/offsitekeys"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
// Review of 2026-11-15, finding 1(b): the moment a box FIRST names, in a pending end record, an app the hub never saw
// in its app list, the operator is warned — once per (household, app), 30 days before the hub could agree to
// anything. An app the hub saw installed and then saw leave is the ordinary case and raises nothing.
// RED-PROOF: documentation/audits/build-release-4-2026-11-15/A/redproof-review-1.txt.
func TestD84_AnAppTheHubNeverSawIsToldToTheOperatorOnce(t *testing.T) {
h, st, _ := newTestHandler(t)
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "pp"})
post := func(body string) {
t.Helper()
req := httptest.NewRequest(http.MethodPost, "/api/v1/report", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer ckey")
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("report → %d (%s)", rr.Code, rr.Body.String())
}
}
told := func() []store.Event {
t.Helper()
evs, err := st.GetEventsByType("c1", offsitekeys.EventEndedClaimUnseen, time.Now().Add(-time.Hour))
if err != nil {
t.Fatal(err)
}
return evs
}
post(`{"customer_id":"c1","stacks":{"deployed":["vikunja","immich"]}}`)
// vikunja is removed with „delete backup data”: the hub SAW it installed — the ordinary case, no warning.
post(`{"customer_id":"c1","stacks":{"deployed":["immich"]},"offsite":{"enabled":true,"ended_apps":[{"app":"vikunja"}]}}`)
if e := told(); len(e) != 0 {
t.Fatalf("an app the hub saw installed raised the never-seen warning: %+v", e)
}
// „ghost” was never in the app list.
body := `{"customer_id":"c1","stacks":{"deployed":["immich"]},"offsite":{"enabled":true,"ended_apps":[{"app":"vikunja"},{"app":"ghost","removed_at":"2024-01-01T00:00:00Z","ends_at":"2024-01-31T00:00:00Z"}]}}`
post(body)
e := told()
if len(e) != 1 {
t.Fatalf("want ONE warning for the app the hub never saw, got %d", len(e))
}
day := time.Now().UTC().Add(offsitekeys.EndedAppAfter).Format("2006-01-02")
if e[0].Severity != "warning" || !strings.Contains(e[0].Message, "c1") || !strings.Contains(e[0].Message, `"ghost"`) || !strings.Contains(e[0].Message, day) {
t.Fatalf("the warning must be a warning and name the household, the app and the day the hub would agree (%s): %+v", day, e[0])
}
if !strings.Contains(e[0].DetailsJSON, `"subject":"ghost"`) {
t.Fatalf("no per-app subject in the details: %s", e[0].DetailsJSON)
}
// the same record in every later report: told ONCE
for i := 0; i < 5; i++ {
post(body)
}
if e := told(); len(e) != 1 {
t.Fatalf("the warning was repeated: %d", len(e))
}
// a box that invents many names: the marks are capped per household
var many []string
for i := 0; i < 40; i++ {
many = append(many, fmt.Sprintf(`{"app":"inv-%d"}`, i))
}
post(`{"customer_id":"c1","stacks":{"deployed":["immich"]},"offsite":{"enabled":true,"ended_apps":[` + strings.Join(many, ",") + `]}}`)
if n := len(told()); n > 8 {
t.Fatalf("40 invented names gave %d operator warnings — a box must not be able to flood the operator (limit 8 per household)", n)
}
// … and in the NEXT report 40 more new names: the limit is the household's, not one report's
many = many[:0]
for i := 0; i < 40; i++ {
many = append(many, fmt.Sprintf(`{"app":"more-%d"}`, i))
}
post(`{"customer_id":"c1","stacks":{"deployed":["immich"]},"offsite":{"enabled":true,"ended_apps":[` + strings.Join(many, ",") + `]}}`)
if n := len(told()); n != 8 {
t.Fatalf("80 invented names over two reports gave %d operator warnings, want exactly the household's limit 8", n)
}
// a name that is not an app name is never told (and never stored)
post(`{"customer_id":"c1","stacks":{"deployed":["immich"]},"offsite":{"enabled":true,"ended_apps":[{"app":"<b>x</b>"},{"app":"_shares"},{"app":"traefik"}]}}`)
for _, ev := range told() {
if strings.Contains(ev.Message, "<b>") || strings.Contains(ev.Message, "_shares") || strings.Contains(ev.Message, "traefik") {
t.Fatalf("an invalid or reserved name reached an operator message: %s", ev.Message)
}
}
// hub-made: no box can POST it, the household never gets it, and each app is its own mail
if allowedEventTypes[offsitekeys.EventEndedClaimUnseen] || allowedEventTypes[offsitekeys.EventEndedLargeAgreement] {
t.Fatal("a box must not be able to POST the hub's own warnings")
}
if !notify.IsOperatorOnly(offsitekeys.EventEndedClaimUnseen) {
t.Fatal("offsite_end_claim_unseen must be operator-only")
}
}