The pending list is across workspaces, oldest first; entries that keep waiting in one workspace (no receiving workflow, not booked) could fill every page and hide those of all others. GET /v1/internal/intake/pending takes after=<entry id>, so the reader pages on with the last id it got. Each item says whether its report's capture is settled (capture_settled), so a reader paging on can hold a supplement back until its capture is handed over, also when the capture was on an earlier page. Without after the route answers as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
197 lines
9.9 KiB
Go
197 lines
9.9 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// intakeFixture: the role fixture with the stage rules this slice adds.
|
|
func intakeFixture(t *testing.T) *roleFixture {
|
|
t.Helper()
|
|
f := newRoleFixture(t)
|
|
f.s.Authz = ParseAuthzRules("portal-svc:tenants,users;portal-stage:role-dataset.user,intake.user;werkmeister-svc:tenants.read,intake")
|
|
return f
|
|
}
|
|
|
|
func (f *roleFixture) intake(sub string) string {
|
|
return "/v1/tenants/" + f.tenant.ID + "/intake/reports" + sub
|
|
}
|
|
|
|
const secretText = "Steuerung X-1 antwortet auf Port 502 ohne Anmeldung"
|
|
|
|
// A person reports with a description only; the report gets the next
|
|
// reference, which the confirmation shows. Later information goes to the
|
|
// same reference. Only the reporter sees the report, and only its state.
|
|
func TestIntakeReportAndSupplement(t *testing.T) {
|
|
f := intakeFixture(t)
|
|
year := time.Now().UTC().Format("2006")
|
|
|
|
code, out, raw := f.do(t, "staff", "POST", f.intake(""), `{"danger":"no","what":" `+secretText+` "}`)
|
|
if code != http.StatusCreated || out["reference"] != "SH-"+year+"-00001" || out["about"] != "unknown" || out["received_at"] == nil {
|
|
t.Fatalf("report: %d %s", code, raw)
|
|
}
|
|
ref := out["reference"].(string)
|
|
if code, out, raw := f.do(t, "ceo", "POST", f.intake(""), `{"danger":"yes","about":"product","what":"x","where":"Halle 2","contact":"Durchwahl 12"}`); code != http.StatusCreated || out["reference"] != "SH-"+year+"-00002" {
|
|
t.Fatalf("second report: %d %s", code, raw)
|
|
}
|
|
|
|
code, out, raw = f.do(t, "staff", "GET", f.intake("/"+ref), "")
|
|
if code != http.StatusOK || out["handover"] != "" || len(out["entries"].([]any)) != 1 || strings.Contains(raw, "Port 502") {
|
|
t.Fatalf("own report: %d %s", code, raw)
|
|
}
|
|
for _, who := range []string{"ceo", "itadmin", "stranger"} {
|
|
if code, _, raw := f.do(t, who, "GET", f.intake("/"+ref), ""); code != http.StatusNotFound {
|
|
t.Fatalf("%s reads the report: %d %s", who, code, raw)
|
|
}
|
|
if code, _, _ := f.do(t, who, "POST", f.intake("/"+ref+"/supplements"), `{"what":"y"}`); code != http.StatusNotFound {
|
|
t.Fatalf("%s adds to it: %d", who, code)
|
|
}
|
|
}
|
|
if code, out, _ := f.do(t, "ceo", "GET", f.intake(""), ""); code != http.StatusOK || len(out["items"].([]any)) != 1 {
|
|
t.Fatalf("ceo's own list: %d %v", code, out)
|
|
}
|
|
|
|
code, out, raw = f.do(t, "staff", "POST", f.intake("/"+ref+"/supplements"), `{"what":"Auch Port 102 offen"}`)
|
|
if code != http.StatusCreated || out["reference"] != ref || out["entry"].(map[string]any)["kind"] != "supplement" {
|
|
t.Fatalf("supplement: %d %s", code, raw)
|
|
}
|
|
code, out, _ = f.do(t, "staff", "GET", f.intake(""), "")
|
|
items := out["items"].([]any)
|
|
if code != http.StatusOK || len(items) != 1 || items[0].(map[string]any)["reference"] != ref || items[0].(map[string]any)["entries"] != nil {
|
|
t.Fatalf("one report, not two: %d %v", code, out)
|
|
}
|
|
if code, _, _ := f.do(t, "staff", "POST", f.intake("/SH-"+year+"-00099/supplements"), `{"what":"y"}`); code != http.StatusNotFound {
|
|
t.Fatalf("unknown reference: %d", code)
|
|
}
|
|
}
|
|
|
|
func TestIntakeReportIsChecked(t *testing.T) {
|
|
f := intakeFixture(t)
|
|
long := strings.Repeat("x", 501)
|
|
for name, c := range map[string]struct {
|
|
as, body, err string
|
|
code int
|
|
}{
|
|
"no danger": {"staff", `{"what":"x"}`, "invalid_danger", http.StatusBadRequest},
|
|
"bad danger": {"staff", `{"danger":"vielleicht","what":"x"}`, "invalid_danger", http.StatusBadRequest},
|
|
"bad about": {"staff", `{"danger":"no","about":"cloud","what":"x"}`, "invalid_about", http.StatusBadRequest},
|
|
"no what": {"staff", `{"danger":"no","what":" "}`, "invalid_what", http.StatusBadRequest},
|
|
"long what": {"staff", `{"danger":"no","what":"` + strings.Repeat("x", 20001) + `"}`, "invalid_what", http.StatusBadRequest},
|
|
"long where": {"staff", `{"danger":"no","what":"x","where":"` + long + `"}`, "invalid_where", http.StatusBadRequest},
|
|
"not json": {"staff", `{`, "invalid_body", http.StatusBadRequest},
|
|
"service": {"werkmeister", `{"danger":"no","what":"x"}`, "FORBIDDEN_CLIENT", http.StatusForbidden},
|
|
"other tenant": {"stranger", `{"danger":"no","what":"x"}`, "not_found", http.StatusNotFound},
|
|
} {
|
|
code, _, raw := f.do(t, c.as, "POST", f.intake(""), c.body)
|
|
if code != c.code || !strings.Contains(raw, c.err) {
|
|
t.Fatalf("%s: %d %s", name, code, raw)
|
|
}
|
|
}
|
|
if code, _, raw := f.do(t, "staff", "POST", f.intake("/x/supplements"), `{"what":""}`); code != http.StatusBadRequest || !strings.Contains(raw, "invalid_what") {
|
|
t.Fatalf("empty supplement: %d %s", code, raw)
|
|
}
|
|
// Without the intake.user grant the portal's sign-in client reaches no
|
|
// intake route.
|
|
f.s.Authz = ParseAuthzRules("portal-stage:role-dataset.user")
|
|
if code, _, raw := f.do(t, "staff", "POST", f.intake(""), `{"danger":"no","what":"x"}`); code != http.StatusForbidden || !strings.Contains(raw, "FORBIDDEN_CLIENT") {
|
|
t.Fatalf("without grant: %d %s", code, raw)
|
|
}
|
|
// Without rules (dev) a service token still cannot report.
|
|
f.s.Authz = nil
|
|
if code, _, raw := f.do(t, "werkmeister", "POST", f.intake(""), `{"danger":"no","what":"x"}`); code != http.StatusForbidden || !strings.Contains(raw, "user_token_required") {
|
|
t.Fatalf("service without rules: %d %s", code, raw)
|
|
}
|
|
}
|
|
|
|
// Werkmeister reads what is not settled, without the text, and says what
|
|
// became of each entry; the reporter sees the handover state.
|
|
func TestIntakeIsTakenOver(t *testing.T) {
|
|
f := intakeFixture(t)
|
|
_, out, _ := f.do(t, "staff", "POST", f.intake(""), `{"danger":"unknown","about":"product","what":"`+secretText+`"}`)
|
|
ref := out["reference"].(string)
|
|
f.do(t, "staff", "POST", f.intake("/"+ref+"/supplements"), `{"what":"Nachtrag zu Port 502"}`)
|
|
|
|
code, out, raw := f.do(t, "werkmeister", "GET", "/v1/internal/intake/pending?limit=10", "")
|
|
items, _ := out["items"].([]any)
|
|
if code != http.StatusOK || len(items) != 2 || strings.Contains(raw, "Port 502") {
|
|
t.Fatalf("pending: %d %s", code, raw)
|
|
}
|
|
first := items[0].(map[string]any)
|
|
if first["reference"] != ref || first["kind"] != "capture" || first["about"] != "product" || first["danger"] != "unknown" || first["tenant_id"] != f.tenant.ID {
|
|
t.Fatalf("pending capture: %v", first)
|
|
}
|
|
entry := "/v1/internal/intake/entries/" + itoa(int(first["entry_id"].(float64))) + "/receipts"
|
|
for _, c := range []struct {
|
|
body string
|
|
code int
|
|
}{
|
|
{`{"status":"waiting","reason":"target_unknown"}`, http.StatusCreated},
|
|
{`{"status":"waiting","reason":"target_unknown"}`, http.StatusOK},
|
|
{`{"status":"delivered","handoff_id":"h-1"}`, http.StatusCreated},
|
|
{`{"status":"processed","handoff_id":"h-1","run_id":"r-1"}`, http.StatusCreated},
|
|
{`{"status":"processed","handoff_id":"h-1","run_id":"r-1"}`, http.StatusConflict},
|
|
{`{"status":"refused"}`, http.StatusConflict},
|
|
} {
|
|
if code, _, raw := f.do(t, "werkmeister", "POST", entry, c.body); code != c.code {
|
|
t.Fatalf("%s: %d %s", c.body, code, raw)
|
|
}
|
|
}
|
|
_, out, _ = f.do(t, "werkmeister", "GET", "/v1/internal/intake/pending", "")
|
|
if items := out["items"].([]any); len(items) != 1 || items[0].(map[string]any)["kind"] != "supplement" || items[0].(map[string]any)["capture_settled"] != true {
|
|
t.Fatalf("pending after processing: %v", out)
|
|
}
|
|
last := itoa(int(out["items"].([]any)[0].(map[string]any)["entry_id"].(float64)))
|
|
if _, out, _ := f.do(t, "werkmeister", "GET", "/v1/internal/intake/pending?after="+last, ""); len(out["items"].([]any)) != 0 {
|
|
t.Fatalf("after the last: %v", out)
|
|
}
|
|
_, out, raw = f.do(t, "staff", "GET", f.intake("/"+ref), "")
|
|
receipts := out["entries"].([]any)[0].(map[string]any)["receipts"].([]any)
|
|
if out["handover"] != "processed" || len(receipts) != 3 || receipts[0].(map[string]any)["reason"] != "target_unknown" {
|
|
t.Fatalf("reporter's view: %s", raw)
|
|
}
|
|
|
|
for name, c := range map[string]struct {
|
|
as, method, path, body string
|
|
code int
|
|
}{
|
|
"bad status": {"werkmeister", "POST", entry, `{"status":"done"}`, http.StatusBadRequest},
|
|
"bad status2": {"werkmeister", "POST", "/v1/internal/intake/entries/2/receipts", `{"status":"done"}`, http.StatusBadRequest},
|
|
"long reason": {"werkmeister", "POST", "/v1/internal/intake/entries/2/receipts", `{"status":"waiting","reason":"` + strings.Repeat("r", 81) + `"}`, http.StatusBadRequest},
|
|
"unknown": {"werkmeister", "POST", "/v1/internal/intake/entries/99/receipts", `{"status":"waiting"}`, http.StatusNotFound},
|
|
"not an id": {"werkmeister", "POST", "/v1/internal/intake/entries/x/receipts", `{"status":"waiting"}`, http.StatusNotFound},
|
|
"not json": {"werkmeister", "POST", "/v1/internal/intake/entries/2/receipts", `{`, http.StatusBadRequest},
|
|
"limit": {"werkmeister", "GET", "/v1/internal/intake/pending?limit=201", "", http.StatusBadRequest},
|
|
"after": {"werkmeister", "GET", "/v1/internal/intake/pending?after=-1", "", http.StatusBadRequest},
|
|
"after2": {"werkmeister", "GET", "/v1/internal/intake/pending?after=x", "", http.StatusBadRequest},
|
|
"user": {"staff", "GET", "/v1/internal/intake/pending", "", http.StatusForbidden},
|
|
"portal svc": {"portal", "GET", "/v1/internal/intake/pending", "", http.StatusForbidden},
|
|
} {
|
|
if code, _, raw := f.do(t, c.as, c.method, c.path, c.body); code != c.code {
|
|
t.Fatalf("%s: %d %s", name, code, raw)
|
|
}
|
|
}
|
|
f.s.Authz = nil
|
|
if code, _, raw := f.do(t, "staff", "GET", "/v1/internal/intake/pending", ""); code != http.StatusForbidden || !strings.Contains(raw, "service_token_required") {
|
|
t.Fatalf("user without rules: %d %s", code, raw)
|
|
}
|
|
if code, _, _ := f.do(t, "staff", "POST", "/v1/internal/intake/entries/2/receipts", `{"status":"waiting"}`); code != http.StatusForbidden {
|
|
t.Fatalf("user receipt without rules: %d", code)
|
|
}
|
|
}
|
|
|
|
func TestIntakePaths(t *testing.T) {
|
|
for path, want := range map[string]bool{
|
|
"/v1/tenants/t/intake/reports": true, "/v1/tenants/t/intake": true,
|
|
"/v1/tenants/t/role-dataset": false, "/v1/internal/intake/pending": false, "/v1/tenants/t": false,
|
|
} {
|
|
if isIntakeUserPath(path) != want {
|
|
t.Fatalf("%s", path)
|
|
}
|
|
}
|
|
if routeGroup("GET", "/v1/internal/intake/pending") != "intake" {
|
|
t.Fatal("route group")
|
|
}
|
|
}
|