Files
felhom.eu/hub/internal/offsite/account_record_test.go
T
admin e5c643d73d
gates / gates (push) Successful in 2m0s
hub (unreleased): the record of an off-site account outlives an untick; RESET and delete take the account down (R-1375, D71); runbook: a household or a tester leaves (D74)
An untick alone takes nothing down (pinned first). Tests run the real save and RESET over the repository's fake
provider. Not deployed.

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-14 17:56:29 +01:00

52 lines
2.6 KiB
Go

package offsite
import (
"strings"
"testing"
"time"
)
// D71 (R-1375): the four cases of KeepAccountRecord, as a table. The web tests (TestD71_*) prove the consequence
// through the real save and the real RESET; this pins the rule itself.
func TestKeepAccountRecord_FourCases(t *testing.T) {
now := time.Date(2026, 11, 14, 20, 0, 0, 0, time.UTC)
const (
provisioned = `{"git":{"username":"x"},"offsite":{"enabled":true,"type":"shared","host":"u1-sub3.example","user":"u1-sub3","port":23}}`
choiceOnly = `{"offsite":{"enabled":true,"type":"shared","quota_gb":100}}` // ticked, never provisioned (or after a RESET)
withRecord = `{"offsite_account":{"type":"shared","user":"u1-sub3","host":"u1-sub3.example","unticked_at":"2026-11-01T00:00:00Z"}}`
unticked = `{"git":{"username":"x"}}`
)
for _, c := range []struct {
name, prev, next string
wantUser, wantAt string // "" = no record
keepsOther string
}{
{"ticked and provisioned, then unticked: a record from the old descriptor", provisioned, unticked, "u1-sub3", "2026-11-14T20:00:00Z", `"git"`},
{"a record, still unticked: carried unchanged (the form does not know it)", withRecord, unticked, "u1-sub3", "2026-11-01T00:00:00Z", `"git"`},
{"ticked again: no record", withRecord, provisioned, "", "", `"offsite"`},
{"a config that carries BOTH a ticked tier and a record: the record goes (the descriptor is the record)", withRecord,
`{"offsite":{"enabled":true,"type":"shared","user":"u1-sub3"},"offsite_account":{"type":"shared","user":"u1-sub3"}}`, "", "", `"offsite"`},
{"ticked, never provisioned, then unticked: no account was made, no record", choiceOnly, unticked, "", "", `"git"`},
{"never ticked: nothing", `{}`, unticked, "", "", `"git"`},
{"an empty previous config", ``, `{}`, "", "", ``},
} {
out, err := KeepAccountRecord(c.prev, c.next, now)
if err != nil {
t.Fatalf("%s: %v", c.name, err)
}
rec := ReadAccountRecord(out)
switch {
case c.wantUser == "" && rec != nil:
t.Errorf("%s: a record %+v, want none (%s)", c.name, rec, out)
case c.wantUser != "" && (rec == nil || rec.User != c.wantUser || rec.UntickedAt != c.wantAt || rec.Type != "shared"):
t.Errorf("%s: record %+v, want user %s unticked_at %s (%s)", c.name, rec, c.wantUser, c.wantAt, out)
}
if c.keepsOther != "" && !strings.Contains(out, c.keepsOther) {
t.Errorf("%s: the other keys were lost: %s", c.name, out)
}
}
if _, err := KeepAccountRecord(provisioned, `{not json`, now); err == nil {
t.Error("a config that cannot be read must be an error (the save is then refused), not a silent loss of the record")
}
}