package server_test import ( "bytes" "context" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "io" "mime/multipart" "net/http" "strings" "testing" "git.breakpilot.com/platform/tenant-registry/internal/storage" "git.breakpilot.com/platform/tenant-registry/internal/store" ) func upload(t *testing.T, h *testHarness, tenantID string, fields map[string]string, filename, content string) (*http.Response, []byte) { t.Helper() var buf bytes.Buffer mw := multipart.NewWriter(&buf) for _, k := range []string{"title", "description"} { if v, ok := fields[k]; ok { _ = mw.WriteField(k, v) } } if filename != "" { fw, _ := mw.CreateFormFile("file", filename) _, _ = io.WriteString(fw, content) } _ = mw.Close() req, _ := http.NewRequest("POST", h.srv.URL+"/v1/admin/tenants/"+tenantID+"/documents", &buf) req.Header.Set("Content-Type", mw.FormDataContentType()) resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } defer func() { _ = resp.Body.Close() }() raw, _ := io.ReadAll(resp.Body) return resp, raw } func TestDocuments_uploadPublishDownloadDelete(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { objs := storage.NewMemory() h.app.Objects = objs content := "%PDF-1.7 conformity" resp, raw := upload(t, h, h.tenant.ID, map[string]string{"title": "CRA-Dossier", "description": "Q3"}, "../../Prüfbericht.pdf", content) if resp.StatusCode != 201 { t.Fatalf("upload: %d %s", resp.StatusCode, raw) } var d store.Document _ = json.Unmarshal(raw, &d) sum := sha256.Sum256([]byte(content)) if d.Status != "draft" || d.Filename != "Prüfbericht.pdf" || d.SizeBytes != int64(len(content)) || d.SHA256 != hex.EncodeToString(sum[:]) || d.ContentType != "application/pdf" { t.Fatalf("stored: %+v", d) } if len(objs.Objects) != 1 || strings.Contains(string(raw), "object_key") { t.Fatalf("object not stored or key leaked: %d %s", len(objs.Objects), raw) } // drafts are invisible to the tenant _, raw = h.do("GET", "/v1/tenants/"+h.tenant.ID+"/documents", nil) if !strings.Contains(string(raw), `"items":[]`) { t.Fatalf("draft visible: %s", raw) } if resp, _ := h.do("GET", "/v1/tenants/"+h.tenant.ID+"/documents/"+d.ID+"/download", nil); resp.StatusCode != 404 { t.Fatalf("draft downloadable: %d", resp.StatusCode) } // staff see it _, raw = h.do("GET", "/v1/admin/tenants/"+h.tenant.ID+"/documents", nil) if !strings.Contains(string(raw), d.ID) { t.Fatalf("admin list: %s", raw) } resp, raw = h.do("POST", "/v1/admin/documents/"+d.ID+"/publish", nil) if resp.StatusCode != 200 || !strings.Contains(string(raw), `"status":"published"`) { t.Fatalf("publish: %d %s", resp.StatusCode, raw) } if resp, _ := h.do("POST", "/v1/admin/documents/"+d.ID+"/publish", nil); resp.StatusCode != 200 { t.Fatalf("publish twice: %d", resp.StatusCode) } _, raw = h.do("GET", "/v1/tenants/"+h.tenant.ID+"/documents", nil) if !strings.Contains(string(raw), d.ID) { t.Fatalf("published not listed: %s", raw) } resp, raw = h.do("GET", "/v1/tenants/"+h.tenant.ID+"/documents/"+d.ID+"/download", nil) if resp.StatusCode != 200 || !strings.Contains(string(raw), `"url":"memory://`) || !strings.Contains(string(raw), "Pr%C3%BCfbericht.pdf") { t.Fatalf("download: %d %s", resp.StatusCode, raw) } // another tenant cannot reach it other := newTenant(t, h, "other-docs") if resp, _ := h.do("GET", "/v1/tenants/"+other.ID+"/documents/"+d.ID+"/download", nil); resp.StatusCode != 404 { t.Fatalf("cross-tenant download: %d", resp.StatusCode) } items, _, _ := h.store.ListAudit(context.Background(), store.AuditFilter{TenantID: h.tenant.ID}) seen := map[string]bool{} for _, a := range items { seen[a.Action] = true } for _, a := range []string{"document.uploaded", "document.published", "document.downloaded"} { if !seen[a] { t.Fatalf("audit %s missing: %v", a, seen) } } if resp, _ := h.do("DELETE", "/v1/admin/documents/"+d.ID, nil); resp.StatusCode != 204 { t.Fatalf("delete: %d", resp.StatusCode) } if len(objs.Objects) != 0 { t.Fatal("object not deleted") } if resp, _ := h.do("DELETE", "/v1/admin/documents/"+d.ID, nil); resp.StatusCode != 404 { t.Fatalf("delete twice: %d", resp.StatusCode) } if resp, _ := h.do("POST", "/v1/admin/documents/not-a-uuid/publish", nil); resp.StatusCode != 404 { t.Fatalf("publish unknown: %d", resp.StatusCode) } }) } func TestDocuments_uploadRules(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { objs := storage.NewMemory() h.app.Objects = objs if resp, _ := upload(t, h, h.tenant.ID, nil, "tool.exe", "MZ"); resp.StatusCode != 415 { t.Fatalf("exe: %d", resp.StatusCode) } if resp, _ := upload(t, h, h.tenant.ID, map[string]string{"title": "x"}, "", ""); resp.StatusCode != 400 { t.Fatalf("no file: %d", resp.StatusCode) } // the title defaults to the file name without extension resp, raw := upload(t, h, h.tenant.ID, nil, "Risikobewertung.docx", "PK") if resp.StatusCode != 201 || !strings.Contains(string(raw), `"title":"Risikobewertung"`) { t.Fatalf("default title: %d %s", resp.StatusCode, raw) } if resp, _ := upload(t, h, h.tenant.ID, map[string]string{"title": strings.Repeat("x", 201)}, "a.pdf", "x"); resp.StatusCode != 400 { t.Fatalf("long title: %d", resp.StatusCode) } if len(objs.Objects) != 1 { t.Fatalf("rejected upload left an object: %d", len(objs.Objects)) } // over the size limit h.app.Cfg.DocumentsMaxBytes = 4 if resp, _ := upload(t, h, h.tenant.ID, nil, "big.pdf", "123456789"); resp.StatusCode != 413 { t.Fatalf("too large: %d", resp.StatusCode) } h.app.Cfg.DocumentsMaxBytes = 0 // storage failure objs.FailPut = errors.New("down") if resp, _ := upload(t, h, h.tenant.ID, nil, "a.pdf", "x"); resp.StatusCode != 502 { t.Fatalf("storage down: %d", resp.StatusCode) } objs.FailPut = nil if resp, _ := upload(t, h, "00000000-0000-0000-0000-00000000dead", nil, "a.pdf", "x"); resp.StatusCode != 404 { t.Fatalf("unknown tenant: %d", resp.StatusCode) } req, _ := http.NewRequest("POST", h.srv.URL+"/v1/admin/tenants/"+h.tenant.ID+"/documents", strings.NewReader("{}")) req.Header.Set("Content-Type", "application/json") if resp, err := http.DefaultClient.Do(req); err != nil || resp.StatusCode != 400 { t.Fatalf("not multipart: %v %v", resp, err) } // not configured h.app.Objects = nil if resp, _ := upload(t, h, h.tenant.ID, nil, "a.pdf", "x"); resp.StatusCode != 503 { t.Fatalf("unconfigured: %d", resp.StatusCode) } }) }