Files
tenant-registry/internal/server/notify_test.go
T
Sharang ParnerkarandClaude Opus 5.5 6880f3afc2
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 23m15s
ci / image (pull_request) Skipped
feat(notify): branded notification mail through an outbox
Publishing a document now mails the workspace members who may read
documents, and products can POST /v1/notifications (route group
"notify") to report new findings, which reach IT_ADMIN, OWNER, CXO
and LEGAL. Demo tenants and tenants without an organization get
nothing.

Every mail is a mail_outbox row (migration 0009), one per recipient,
idempotent per key, sent by a worker with backoff and parked with its
error after twelve attempts. Messages are rendered when sent from
embedded German and English templates in the BreakPilot layout, as
multipart text + HTML, through the SMTP relay (SMTP_*).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-23 19:45:14 +02:00

139 lines
5.9 KiB
Go

package server_test
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/keycloak"
"git.breakpilot.com/platform/tenant-registry/internal/mail"
"git.breakpilot.com/platform/tenant-registry/internal/storage"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
func seedMembers(h *testHarness, slug string) {
h.kcMock.Members[slug] = []keycloak.OrgMember{
{UserID: "u1", Email: "IT@acme.test", OrgRoles: []string{"IT_ADMIN"}, Enabled: true},
{UserID: "u2", Email: "legal@acme.test", OrgRoles: []string{"LEGAL"}, Enabled: true},
{UserID: "u3", Email: "dev@acme.test", OrgRoles: []string{"USER"}, Enabled: true},
{UserID: "u4", Email: "gone@acme.test", OrgRoles: []string{"IT_ADMIN"}, Enabled: false},
{UserID: "u5", Email: "fin@acme.test", OrgRoles: []string{"FINANCE"}, Enabled: true},
}
}
func TestNotify_documentPublishedMailsTheReaders(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
h.app.Objects = storage.NewMemory()
rec := &mail.Recorder{}
h.app.Mail = rec
h.app.Cfg.PortalURL = "https://app-stage.breakpilot.com"
seedMembers(h, "acme")
_, raw := upload(t, h, h.tenant.ID, map[string]string{"title": "CRA-Dossier"}, "dossier.pdf", "%PDF")
var d store.Document
_ = json.Unmarshal(raw, &d)
h.do("POST", "/v1/admin/documents/"+d.ID+"/publish", nil)
n, err := h.app.DrainMail(context.Background())
if err != nil || n != 3 {
t.Fatalf("drained %d, %v", n, err)
}
to := map[string]bool{}
for _, m := range rec.Messages() {
to[m.To] = true
if !strings.Contains(m.Subject, "CRA-Dossier") || !strings.Contains(m.Text, "https://app-stage.breakpilot.com/acme/documents") {
t.Fatalf("message: %+v", m)
}
}
if !to["it@acme.test"] || !to["legal@acme.test"] || !to["fin@acme.test"] || to["dev@acme.test"] || to["gone@acme.test"] {
t.Fatalf("recipients: %v", to)
}
// nothing left, and nothing new for a second publish
h.do("POST", "/v1/admin/documents/"+d.ID+"/publish", nil)
if n, _ := h.app.DrainMail(context.Background()); n != 0 {
t.Fatalf("second drain: %d", n)
}
})
}
func TestNotify_productFindings(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
rec := &mail.Recorder{}
h.app.Mail = rec
seedMembers(h, "acme")
body := map[string]any{"tenant_id": h.tenant.ID, "kind": "findings.new", "idempotency_key": "scan-42",
"data": map[string]any{"target_name": "Pumpstation", "new_count": 3, "critical": 1, "high": 2, "url": "https://pruefwerk-stage.breakpilot.com/targets/t1"}}
resp, raw := h.do("POST", "/v1/notifications", body)
if resp.StatusCode != 202 || !strings.Contains(string(raw), `"queued":2`) {
t.Fatalf("queue: %d %s", resp.StatusCode, raw)
}
if resp, raw := h.do("POST", "/v1/notifications", body); resp.StatusCode != 202 || !strings.Contains(string(raw), `"queued":0`) {
t.Fatalf("repeat: %d %s", resp.StatusCode, raw)
}
if _, err := h.app.DrainMail(context.Background()); err != nil {
t.Fatal(err)
}
msgs := rec.Messages()
if len(msgs) != 2 || msgs[0].Subject != "3 neue Befunde in Pumpstation" || !strings.Contains(msgs[0].HTML, "https://pruefwerk-stage.breakpilot.com/targets/t1") {
t.Fatalf("messages: %+v", msgs)
}
for _, bad := range []map[string]any{
{"tenant_id": h.tenant.ID, "kind": "document.published", "idempotency_key": "x", "data": map[string]any{"target_name": "a", "new_count": 1}},
{"tenant_id": h.tenant.ID, "kind": "findings.new", "data": map[string]any{"target_name": "a", "new_count": 1}},
{"tenant_id": h.tenant.ID, "kind": "findings.new", "idempotency_key": "y", "data": map[string]any{"target_name": "a", "new_count": 0}},
{"tenant_id": h.tenant.ID, "kind": "findings.new", "idempotency_key": "z", "data": map[string]any{"new_count": 2}},
} {
if resp, raw := h.do("POST", "/v1/notifications", bad); resp.StatusCode != 400 {
t.Fatalf("accepted %v: %d %s", bad, resp.StatusCode, raw)
}
}
if resp, _ := h.do("POST", "/v1/notifications", map[string]any{"tenant_id": "00000000-0000-0000-0000-00000000dead", "kind": "findings.new",
"idempotency_key": "k", "data": map[string]any{"target_name": "a", "new_count": 1}}); resp.StatusCode != 404 {
t.Fatalf("unknown tenant: %d", resp.StatusCode)
}
})
}
func TestNotify_demoAndOrglessTenantsGetNothing(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
h.app.Mail = &mail.Recorder{}
demo := newTenant(t, h, "demo-co")
if _, err := h.store.UpdateTenant(context.Background(), demo.ID, store.TenantUpdate{Status: ptr("demo")}); err != nil {
t.Fatal(err)
}
seedMembers(h, "demo-co")
for _, id := range []string{demo.ID, newTenant(t, h, "no-org-3").ID} {
resp, raw := h.do("POST", "/v1/notifications", map[string]any{"tenant_id": id, "kind": "findings.new", "idempotency_key": "k-" + id,
"data": map[string]any{"target_name": "a", "new_count": 1}})
if resp.StatusCode != 202 || !strings.Contains(string(raw), `"queued":0`) {
t.Fatalf("%s: %d %s", id, resp.StatusCode, raw)
}
}
})
}
func TestNotify_retriesWithBackoff(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
rec := &mail.Recorder{Fail: errors.New("relay down")}
h.app.Mail = rec
seedMembers(h, "acme")
h.do("POST", "/v1/notifications", map[string]any{"tenant_id": h.tenant.ID, "kind": "findings.new", "idempotency_key": "r1",
"data": map[string]any{"target_name": "a", "new_count": 1}})
if n, err := h.app.DrainMail(context.Background()); err != nil || n != 2 {
t.Fatalf("first attempt: %d %v", n, err)
}
// not due again right away
if n, _ := h.app.DrainMail(context.Background()); n != 0 {
t.Fatalf("retried immediately: %d", n)
}
rec.Fail = nil
h.app.Now = func() time.Time { return time.Now().Add(2 * time.Minute) }
if n, _ := h.app.DrainMail(context.Background()); n != 2 || len(rec.Messages()) != 2 {
t.Fatalf("after backoff: %d sent %d", n, len(rec.Messages()))
}
})
}