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":"x"},{"app":"_shares"},{"app":"traefik"}]}}`) for _, ev := range told() { if strings.Contains(ev.Message, "") || 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") } }