Files
felhom.eu/hub/internal/api/end_notice_review_test.go
T

172 lines
7.2 KiB
Go

package api
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/offsitekeys"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
// THE INDEPENDENT REVIEW OF THE NOTICE LOCK (2026-11-15 night). One test per finding, each seen red before its fix:
// documentation/audits/build-release-4b-2026-11-15/A/redproof-review-<n>.txt.
// Finding 3. A box could put a link-shaped word into the household's subject — `www.evil-bank.example.com`, five a
// day: the app-record's name rule allows dots and capitals, and a notice row was made for a name the hub never saw
// installed. A name that reaches a household's mail must be a plain app slug (store.ValidNoticeApp): lower-case
// letters, digits, `-`, `_`; no dot. Anything else: no row, no mail, never agreed.
func TestReview3_ALinkShapedNameNeverReachesTheHousehold(t *testing.T) {
r := newNoticeRig(t, household)
for _, name := range []string{"www.evil-bank.example.com", "evil.example", "Felhom-Support", "a.b", "x..y"} {
r.report(pressed(name, "2026-11-15T10:00:00Z"))
if got := r.to(household); len(got) != 0 {
t.Fatalf("%q reached the household: subject %q", name, got[0].subject)
}
if _, ok := r.notice(name); ok {
t.Fatalf("%q has a notice row", name)
}
}
// positive control: a plain slug is told
r.report(pressed("paperless-ngx", "2026-11-15T10:00:00Z"))
if got := r.to(household); len(got) != 1 || !strings.Contains(got[0].subject, "paperless-ngx: ") {
t.Fatalf("a plain app name must be told: %+v", got)
}
}
// The slug rule fits every app the catalog has (read from the sibling repository when it is there).
func TestReview3_EveryCatalogAppNameIsANoticeName(t *testing.T) {
dir := filepath.Join("..", "..", "..", "..", "app-catalog-felhom.eu", "templates")
entries, err := os.ReadDir(dir)
if err != nil {
t.Skipf("the catalog repository is not beside this one (%v)", err)
}
n := 0
for _, e := range entries {
if !e.IsDir() {
continue
}
if _, err := os.Stat(filepath.Join(dir, e.Name(), ".felhom.yml")); err != nil {
continue
}
n++
if !store.ValidNoticeApp(e.Name()) {
t.Errorf("the catalog app %q is not a notice name — its household could never be told, and its copies never deleted", e.Name())
}
}
if n < 50 {
t.Fatalf("only %d catalog apps read — the test stopped reading the catalog", n)
}
}
// Finding 4. The told time was the WALK's start: with many rows, the last one was „told” minutes before its mail was
// accepted — and the 30 days counted from before the mail. The told time is taken AFTER the send has returned.
func TestReview4_ToldIsTheTimeAfterTheSendReturned(t *testing.T) {
r := newNoticeRig(t, household)
r.fail = errors.New("resend: 503")
r.report(repPressed) // the row exists, nobody told
r.mu.Lock()
r.fail = nil
r.mu.Unlock()
r.d.SetSendFnForTest(func(to, subject, body string, _ map[string]string) error {
time.Sleep(1200 * time.Millisecond) // the mail service takes its time
return nil
})
start := time.Now().Add(2 * time.Hour) // the walk's clock at its start
r.h.endNotices("c1", start)
n, _ := r.notice("vikunja")
if !n.Told() {
t.Fatalf("fixture: not told: %+v", n)
}
if n.ToldAt.Unix() < start.Unix()+1 {
t.Fatalf("told at %s — the walk started at %s and the send took 1.2 s: the told time must be after the send returned, never the walk's start",
n.ToldAt.Format(time.RFC3339), start.UTC().Format(time.RFC3339))
}
}
// Finding 2. A send that ERRORS AFTER the mail service accepted it (a client timeout) was mailed again every hour,
// and it was outside the daily limit, because the limit counted only `sent` rows: 12 mails in 12 hours. Now the
// limit counts every try that reached the mail service (`sent` + `failed`), and the wait before the next try grows
// with the tries (1 h, 2 h, 4 h … at most 24 h). The operator's in-place mail has the same shape and the same
// fix. The „stuck” line after 3 days still comes.
func TestReview2_ASendThatErrorsAfterItWasAcceptedIsNotMailedEveryHour(t *testing.T) {
for _, c := range []struct{ name, email, to string }{
{"the household's notice", household, household},
{"the operator's in-place notice", "", operator},
} {
t.Run(c.name, func(t *testing.T) {
r := newNoticeRig(t, c.email)
r.d.SetSendFnForTest(func(to, subject, body string, _ map[string]string) error {
r.mu.Lock()
defer r.mu.Unlock()
r.mails = append(r.mails, noticeMail{to, subject, body}) // the mail WENT …
return errors.New("context deadline exceeded") // … and the client gave up waiting
})
count := func() int {
n := 0
for _, m := range r.to(c.to) {
if !strings.Contains(m.subject, "offsite_end_notice_stuck") && !strings.Contains(m.subject, "offsite_end_claim_unseen") {
n++
}
}
return n
}
r.report(repPressed)
now := time.Now()
for i := 1; i <= 11; i++ { // a report every hour for twelve hours
r.h.endNotices("c1", now.Add(time.Duration(i)*61*time.Minute))
}
if n := count(); n > 4 {
t.Fatalf("12 hours of a send that errors after it was accepted: %d mails, want at most 4 (tries at 0, 1, 3 and 7 hours)", n)
}
for i := 12; i <= 100; i++ { // … and four days of it
r.h.endNotices("c1", now.Add(time.Duration(i)*61*time.Minute))
}
if n := count(); n > 5 {
t.Fatalf("four days of it: %d mails — the daily limit must count a try that failed (all of them are inside this test's real 24 hours)", n)
}
if n, _ := r.notice("vikunja"); n.Told() {
t.Fatalf("a send that returned an error counts as told: %+v", n)
}
if e := r.events(offsitekeys.EventEndNoticeStuck); len(e) != 1 {
t.Fatalf("the operator's line after 3 days: %d, want 1", len(e))
}
})
}
}
// Finding 7. The guard `n.Told() ||` in endNoticeStuck had no test: without it the suite stayed green. A row that
// WAS told to the operator (the household had no address then) and whose later household mail waits for the daily
// limit has told somebody — the operator must NOT get the line „the notice has not gone out”.
func TestReview7_ARowToldToTheOperatorNeverGetsTheStuckLine(t *testing.T) {
r := newNoticeRig(t, "")
r.report(repPressed)
if n, _ := r.notice("vikunja"); n.ToldTo != store.EndNoticeOperator {
t.Fatalf("fixture: the operator was not told: %+v", n)
}
// five days pass; the household now has an address, and its daily limit is used up by other notices
old := time.Now().UTC().Add(-5 * 24 * time.Hour)
if err := r.st.SetEndNoticeForTest("c1", "vikunja", "2026-11-15T10:00:00Z", old, old, store.EndNoticeOperator); err != nil {
t.Fatal(err)
}
if err := r.st.SaveNotificationPrefs("c1", household, []string{}, 6); err != nil {
t.Fatal(err)
}
for i := 0; i < 5; i++ {
if err := r.st.LogNotification("c1", "offsite_end_scheduled", "info", "offsite_end_scheduled other", "sent", "", "customer"); err != nil {
t.Fatal(err)
}
}
r.report(repPressed)
n, _ := r.notice("vikunja")
if n.ToldTo != store.EndNoticeOperator || !n.Told() || !strings.Contains(n.LastReason, "daily limit") {
t.Fatalf("fixture: the household's mail must have waited for the limit, the operator's notice standing: %+v", n)
}
if e := r.events(offsitekeys.EventEndNoticeStuck); len(e) != 0 {
t.Fatalf("a row that was told to the operator got the „told nobody” line: %+v", e)
}
}