Built, not deployed. `07-backup-architecture.md` §8.2f parts 3-4 and decision 309. - store: the hub's own record of which apps a box runs, by the hub's clock (offsite_app_presence, fed by every controller report); it goes with the household. - offsitekeys: window-open takes the box's due end records and answers ended_allowed / ended_refused. An app is agreed only after 30 days of absence in the hub's record; an id only when the hub's own listing holds it, uploaded before that absence. The cap (max_remove) is unchanged; the close keeps only agreed ids from removed_for_ended_apps and allows them beside the cap. - The 07:10 audit explains the ended apps' vanished files by name and still raises for every other vanished file. - notify: offsite_end_scheduled is a household receipt — one mail per press to the household's own address, whatever its switches; the operator is not mailed. - Two operator-only hub records: offsite_ended_app_removed (info), offsite_ended_app_refused (warning). Tests TestD72_*, TestD84_*; 44 mutations seen red, 5 after a redo (documentation/audits/build-release-4-2026-11-15/A/redproof-hub-*.txt). Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
93 lines
3.8 KiB
Go
93 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dooplex.hu/admin/felhom-hub/internal/notify"
|
|
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
|
|
)
|
|
|
|
// D72 / D84 (`07-backup-architecture.md` §8.2f): the wire. RED-PROOFS:
|
|
// documentation/audits/build-release-4-2026-11-15/A/redproof-hub-api.txt.
|
|
|
|
// A controller report feeds the hub's OWN app record: the app list, and the apps named in pending end records. The
|
|
// box's dates in `ended_apps` never become the hub's date.
|
|
func TestD72_ReportFeedsTheHubsOwnAppRecord(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())
|
|
}
|
|
}
|
|
post(`{"customer_id":"c1","stacks":{"deployed":["vikunja","immich"],"available":[]}}`)
|
|
before := time.Now().Add(-time.Minute)
|
|
// vikunja is gone; the box also holds an end record for an app the hub never saw, dated two years ago.
|
|
post(`{"customer_id":"c1","stacks":{"deployed":["immich"],"available":[]},"offsite":{"enabled":true,"ended_apps":[{"app":"oldapp","removed_at":"2024-01-01T00:00:00Z","ends_at":"2024-01-31T00:00:00Z"}]}}`)
|
|
m, err := st.AppPresences("c1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !m["immich"].Present {
|
|
t.Fatalf("immich is listed: %+v", m)
|
|
}
|
|
for _, app := range []string{"vikunja", "oldapp"} {
|
|
p, ok := m[app]
|
|
if !ok || p.Present || p.AbsentSince.Before(before) || p.AbsentSince.After(time.Now().Add(time.Minute)) {
|
|
t.Fatalf("%s must be absent since NOW by the hub's clock (the box's 2024 date is not read): %+v ok=%v", app, p, ok)
|
|
}
|
|
}
|
|
// A report with no `stacks` object is not an empty app list: nothing becomes absent.
|
|
post(`{"customer_id":"c1"}`)
|
|
if m2, _ := st.AppPresences("c1"); !m2["immich"].Present {
|
|
t.Fatalf("a report without an app list made immich absent: %+v", m2)
|
|
}
|
|
}
|
|
|
|
// The window-open request carries the box's due end records to the service, whole.
|
|
func TestD84_WindowOpenCarriesTheEndedClaims(t *testing.T) {
|
|
h, st, _ := newTestHandler(t)
|
|
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "pp"})
|
|
f := &fakeKeySvc{}
|
|
h.SetOffsiteKeyService(f)
|
|
id := strings.Repeat("ab", 32)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/offsite/window-open/c1",
|
|
strings.NewReader(`{"count_before":20,"ended":[{"app":"vikunja","removed_at":"2026-10-01T00:00:00Z","ends_at":"2026-10-31T00:00:00Z","snapshot_ids":["`+id+`"]}]}`))
|
|
req.Header.Set("Authorization", "Bearer ckey")
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("window-open → %d (%s)", rr.Code, rr.Body.String())
|
|
}
|
|
if len(f.gotEnded) != 1 || f.gotEnded[0].App != "vikunja" || len(f.gotEnded[0].SnapshotIDs) != 1 || f.gotEnded[0].SnapshotIDs[0] != id {
|
|
t.Fatalf("the service did not receive the claim: %+v", f.gotEnded)
|
|
}
|
|
}
|
|
|
|
// The household's receipt is a type a box may send; the two hub-made records are not — no box can mint one.
|
|
func TestD84_EventTypes(t *testing.T) {
|
|
if !allowedEventTypes["offsite_end_scheduled"] {
|
|
t.Fatal("offsite_end_scheduled must be accepted from a box (POST /event would answer 400)")
|
|
}
|
|
if notify.IsOperatorOnly("offsite_end_scheduled") {
|
|
t.Fatal("the household's receipt must not be operator-only")
|
|
}
|
|
for _, et := range []string{"offsite_ended_app_removed", "offsite_ended_app_refused"} {
|
|
if allowedEventTypes[et] {
|
|
t.Fatalf("%s is hub-made: a box must not be able to POST it", et)
|
|
}
|
|
if !notify.IsOperatorOnly(et) {
|
|
t.Fatalf("%s must be operator-only", et)
|
|
}
|
|
}
|
|
}
|