Files
tenant-registry/internal/server/notify_test.go
T
Sharang ParnerkarandClaude Opus 5.5 dc44eb22bb
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 6m6s
ci / image (pull_request) Skipped
feat(authz): the new bundles are assignable (RBAC step 4)
PSIRT, DEVELOPMENT, QUALITY, PRIVACY, PURCHASING, AUDITOR and EXTERNAL
can be given to a member (catalogue 2026-11-11.1). Invitations and role
changes accept exactly the catalogue's assignable bundles.

The registry's own checks read the catalogue instead of role lists:
notification recipients, managed services and Prüfwerk settings. For
today's six bundles nothing changes (pinned against the 2026-11-10
tables); AUDITOR now reads managed services, and the new bundles get
the mails their capabilities name. Individual capabilities still do not
count in the registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
2026-11-11 21:29:33 +01:00

176 lines
7.6 KiB
Go

package server_test
import (
"context"
"encoding/json"
"errors"
"slices"
"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()))
}
})
}
// RBAC step 4: recipients follow the catalogue, so the new bundles hear
// what their capabilities say: PSIRT about findings, AUDITOR about
// documents, EXTERNAL about neither.
func TestNotify_newBundles(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
h.app.Objects = storage.NewMemory()
rec := &mail.Recorder{}
h.app.Mail = rec
h.kcMock.Members["acme"] = []keycloak.OrgMember{
{UserID: "p", Email: "psirt@acme.test", OrgRoles: []string{"PSIRT"}, Enabled: true},
{UserID: "a", Email: "audit@acme.test", OrgRoles: []string{"AUDITOR"}, Enabled: true},
{UserID: "x", Email: "ext@acme.test", OrgRoles: []string{"EXTERNAL"}, Enabled: true},
}
body := map[string]any{"tenant_id": h.tenant.ID, "kind": "findings.new", "idempotency_key": "scan-1",
"data": map[string]any{"target_name": "Pumpstation", "new_count": 1, "critical": 0, "high": 1, "url": "https://pruefwerk-stage.breakpilot.com/targets/t1"}}
if resp, raw := h.do("POST", "/v1/notifications", body); resp.StatusCode != 202 || !strings.Contains(string(raw), `"queued":1`) {
t.Fatalf("findings: %d %s", resp.StatusCode, raw)
}
_, 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)
if _, err := h.app.DrainMail(context.Background()); err != nil {
t.Fatal(err)
}
got := []string{}
for _, m := range rec.Messages() {
got = append(got, m.To+" "+strings.Fields(m.Subject)[0])
}
slices.Sort(got)
if len(got) != 2 || !strings.HasPrefix(got[0], "audit@acme.test") || !strings.HasPrefix(got[1], "psirt@acme.test 1") {
t.Fatalf("recipients: %v", got)
}
})
}