feat: wire persistence into the web layer (check → archive → dossier)

This is the piece that turns a Pre-Publish-Prüfung into an actual
archived, provable record instead of a one-off form response.

extract.Client.Extract now returns Result{Facts, RawJSON} instead of
just Facts — RawJSON is the model's exact, unmodified JSON, which is
what belongs in extraction.payload (the audit trail), not a re-encoded
view through our own Facts struct. extract.ParsePayload reconstructs
Facts from a stored payload later, reusing the same parsing/validation
path Extract uses (including the enum guard), so a previously-saved
extraction can be read back exactly as it would have been the first
time.

internal/dossier.BuildContent no longer requires AssetHash: most checks
right now are caption-only (no image/video upload wired yet), and
inventing a placeholder hash for a nonexistent asset would itself be an
integrity problem in an evidence tool. Content shows "kein Asset
hinterlegt" instead.

internal/web gains a narrow Store interface (mirroring the Extractor
pattern — only the methods these handlers use, not the full
*store.Store) so its test suite stays network/DB-free via an in-memory
fake:

- POST /pruefen persists submission + extraction + findings and marks
  the submission "checked". A needsClarification result persists the
  extraction (there's something worth keeping) but no findings and no
  status change, and the template omits the archive option entirely.
- POST /veroeffentlichen re-derives Facts from the stored payload, hashes
  the canonical submission+facts+findings metadata, gets an RFC-3161
  timestamp, generates the PDF dossier to disk, and persists the
  evidence_package row before marking the submission "published".
- GET /dossier/{id} serves the generated PDF.

Tested end-to-end offline: a fake Timestamper builds a real, structurally
valid self-signed RFC-3161 response so the full check→archive→download
flow runs against an in-memory store, verifying the downloaded bytes are
an actual PDF and the dossier file lands on disk — without hitting a
real database, TSA, or the Claude API.

cmd/deklarix/main.go now wires store.Store, evidence.NewHTTPTimestamper
(TSA_URL, default FreeTSA), and DOSSIER_DIR (default "dossiers") into
web.NewServer alongside the extractor and rule set.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
noroot
2026-08-27 14:39:38 +02:00
parent 72005fc326
commit f5ad08cebd
14 changed files with 724 additions and 105 deletions

View File

@@ -1,9 +1,15 @@
package web
import (
"encoding/hex"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/netcell-it/deklarix/internal/dossier"
"github.com/netcell-it/deklarix/internal/evidence"
"github.com/netcell-it/deklarix/internal/extract"
"github.com/netcell-it/deklarix/internal/rules"
)
@@ -33,13 +39,18 @@ type findingView struct {
}
type resultData struct {
SubmissionID string
NeedsClarification bool
CanArchive bool
Findings []findingView
}
// handleCheck führt die Pre-Publish-Prüfung aus: Extraktion (Stufe 1)
// gefolgt von der Regelauswertung (Stufe 2). Speichert noch nichts —
// das ist der nächste Schritt (Verdrahtung über internal/store).
// handleCheck führt die Pre-Publish-Prüfung aus: Extraktion (Stufe 1),
// Regelauswertung (Stufe 2), und speichert Submission + Extraction +
// Findings. Die eigentliche Archivierung (Hash, Zeitstempel, Dossier)
// passiert erst in handleArchive, wenn der Beitrag tatsächlich
// veröffentlicht wurde — das sind unterschiedliche Zeitpunkte im
// Lebenszyklus (siehe CLAUDE.md, Funktion 1 vs. 2).
func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "ungültiges Formular", http.StatusBadRequest)
@@ -47,13 +58,16 @@ func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) {
}
platform := r.FormValue("platform")
postType := r.FormValue("post_type")
caption := r.FormValue("caption")
if platform == "" || caption == "" {
http.Error(w, "Plattform und Caption sind Pflichtfelder", http.StatusBadRequest)
if platform == "" || postType == "" || caption == "" {
http.Error(w, "Plattform, Beitragstyp und Caption sind Pflichtfelder", http.StatusBadRequest)
return
}
facts, err := s.extractor.Extract(r.Context(), extract.Input{
ctx := r.Context()
result, err := s.extractor.Extract(ctx, extract.Input{
Platform: platform,
Jurisdiction: "DE",
Caption: caption,
@@ -63,21 +77,180 @@ func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) {
return
}
findings, needsClarification := rules.Evaluate(s.ruleSet, facts)
sub, err := s.store.CreateSubmission(ctx, platform, postType, caption)
if err != nil {
http.Error(w, "Beitrag konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
return
}
data := resultData{NeedsClarification: needsClarification}
for _, f := range findings {
data.Findings = append(data.Findings, findingView{
RuleID: f.RuleID,
Version: f.RuleVersion,
Severity: string(f.Severity),
Title: f.Title,
Fix: f.Fix,
Sources: f.Sources,
})
ext, err := s.store.CreateExtraction(ctx, sub.ID, result.RawJSON, s.extractor.ModelVersion(), extract.PromptVersion)
if err != nil {
http.Error(w, "Extraktion konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
return
}
findings, needsClarification := rules.Evaluate(s.ruleSet, result.Facts)
data := resultData{SubmissionID: sub.ID, NeedsClarification: needsClarification}
if !needsClarification {
extractionID := ext.ID
for _, f := range findings {
if _, err := s.store.CreateFinding(ctx, sub.ID, &extractionID, f.RuleID, f.RuleVersion, string(f.Severity), f.Title, f.Fix, f.Sources); err != nil {
http.Error(w, "Finding konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
return
}
data.Findings = append(data.Findings, findingView{
RuleID: f.RuleID, Version: f.RuleVersion, Severity: string(f.Severity),
Title: f.Title, Fix: f.Fix, Sources: f.Sources,
})
}
if err := s.store.SetSubmissionStatus(ctx, sub.ID, "checked"); err != nil {
http.Error(w, "Status konnte nicht aktualisiert werden: "+err.Error(), http.StatusInternalServerError)
return
}
data.CanArchive = true
}
if err := s.templates.ExecuteTemplate(w, "result", data); err != nil {
http.Error(w, "Ergebnis konnte nicht gerendert werden", http.StatusInternalServerError)
}
}
type archivedData struct {
SubmissionID string
DossierURL string
TimestampedAt string
}
// handleArchive markiert einen geprüften Beitrag als veröffentlicht und
// archiviert ihn: Hash der Metadaten, RFC-3161-Zeitstempel, PDF-Dossier.
// Setzt eine vorherige Prüfung voraus (Submission + Extraction +
// Findings müssen bereits existieren).
func (s *Server) handleArchive(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "ungültiges Formular", http.StatusBadRequest)
return
}
submissionID := r.FormValue("submission_id")
if submissionID == "" {
http.Error(w, "submission_id ist Pflicht", http.StatusBadRequest)
return
}
ctx := r.Context()
sub, err := s.store.GetSubmission(ctx, submissionID)
if err != nil {
http.Error(w, "Beitrag nicht gefunden: "+err.Error(), http.StatusNotFound)
return
}
ext, err := s.store.GetLatestExtraction(ctx, submissionID)
if err != nil {
http.Error(w, "Extraktion nicht gefunden: "+err.Error(), http.StatusNotFound)
return
}
facts, err := extract.ParsePayload(ext.Payload, sub.Platform, "DE")
if err != nil {
http.Error(w, "gespeicherte Extraktion konnte nicht gelesen werden: "+err.Error(), http.StatusInternalServerError)
return
}
storeFindings, err := s.store.ListCurrentFindings(ctx, submissionID)
if err != nil {
http.Error(w, "Findings konnten nicht gelesen werden: "+err.Error(), http.StatusInternalServerError)
return
}
dossierFindings := make([]rules.Finding, len(storeFindings))
for i, f := range storeFindings {
dossierFindings[i] = rules.Finding{
RuleID: f.RuleID, RuleVersion: f.RuleVersion, Severity: rules.Severity(f.Severity),
Title: f.Title, Fix: f.Fix, Sources: f.Sources,
}
}
metadataHash, err := evidence.HashMetadata(struct {
SubmissionID string
Platform string
PostType string
Caption string
Facts rules.Facts
Findings []rules.Finding
}{sub.ID, sub.Platform, sub.PostType, sub.Caption, facts, dossierFindings})
if err != nil {
http.Error(w, "Metadaten-Hash fehlgeschlagen: "+err.Error(), http.StatusInternalServerError)
return
}
timestampToken, err := s.timestamper.Timestamp(ctx, metadataHash)
if err != nil {
http.Error(w, "Zeitstempel fehlgeschlagen: "+err.Error(), http.StatusBadGateway)
return
}
if err := os.MkdirAll(s.dossierDir, 0o750); err != nil {
http.Error(w, "Dossier-Verzeichnis konnte nicht angelegt werden: "+err.Error(), http.StatusInternalServerError)
return
}
dossierPath := filepath.Join(s.dossierDir, submissionID+".pdf")
dossierFile, err := os.Create(dossierPath)
if err != nil {
http.Error(w, "Dossier-Datei konnte nicht angelegt werden: "+err.Error(), http.StatusInternalServerError)
return
}
defer dossierFile.Close()
genErr := dossier.Generate(dossierFile, dossier.Data{
Submission: dossier.Submission{
Platform: sub.Platform, PostType: sub.PostType, Caption: sub.Caption, CreatedAt: sub.CreatedAt,
},
Facts: facts,
Findings: dossierFindings,
MetadataHash: metadataHash,
TimestampToken: timestampToken,
GeneratedAt: time.Now(),
})
if genErr != nil {
http.Error(w, "Dossier konnte nicht erzeugt werden: "+genErr.Error(), http.StatusInternalServerError)
return
}
if _, err := s.store.CreateEvidencePackage(ctx, submissionID, dossierPath, hex.EncodeToString(metadataHash), timestampToken); err != nil {
http.Error(w, "Evidence-Package konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
return
}
if err := s.store.SetSubmissionStatus(ctx, submissionID, "published"); err != nil {
http.Error(w, "Status konnte nicht aktualisiert werden: "+err.Error(), http.StatusInternalServerError)
return
}
timestampedAt, err := evidence.TimestampTime(timestampToken)
if err != nil {
http.Error(w, "Zeitstempel konnte nicht gelesen werden: "+err.Error(), http.StatusInternalServerError)
return
}
data := archivedData{
SubmissionID: submissionID,
DossierURL: "/dossier/" + submissionID,
TimestampedAt: timestampedAt.Format("02.01.2006 15:04:05 MST"),
}
if err := s.templates.ExecuteTemplate(w, "archived", data); err != nil {
http.Error(w, "Ergebnis konnte nicht gerendert werden", http.StatusInternalServerError)
}
}
// handleDossierDownload liefert das erzeugte PDF-Dossier eines
// archivierten Beitrags aus.
func (s *Server) handleDossierDownload(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
pkg, err := s.store.GetLatestEvidencePackage(r.Context(), id)
if err != nil {
http.Error(w, "Kein Dossier für diesen Beitrag gefunden", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/pdf")
http.ServeFile(w, r, pkg.DossierPath)
}

View File

@@ -14,8 +14,10 @@ import (
"html/template"
"net/http"
"github.com/netcell-it/deklarix/internal/evidence"
"github.com/netcell-it/deklarix/internal/extract"
"github.com/netcell-it/deklarix/internal/rules"
"github.com/netcell-it/deklarix/internal/store"
)
//go:embed templates/*.html
@@ -28,31 +30,61 @@ var staticFS embed.FS
// *extract.Client erfüllt sie; Tests injizieren einen Fake statt echte
// Claude-API-Aufrufe zu machen.
type Extractor interface {
Extract(ctx context.Context, in extract.Input) (rules.Facts, error)
Extract(ctx context.Context, in extract.Input) (extract.Result, error)
ModelVersion() string
}
// Store ist die Schnittstelle, die der Server zur Persistenz braucht —
// bewusst schmal (nur was diese Handler tatsächlich nutzen), nicht der
// komplette *store.Store. *store.Store erfüllt sie; Tests injizieren
// einen Fake statt eine echte Datenbank zu brauchen.
type Store interface {
CreateSubmission(ctx context.Context, platform, postType, caption string) (store.Submission, error)
GetSubmission(ctx context.Context, id string) (store.Submission, error)
SetSubmissionStatus(ctx context.Context, id, status string) error
CreateExtraction(ctx context.Context, submissionID string, payload []byte, modelVersion, promptVersion string) (store.Extraction, error)
GetLatestExtraction(ctx context.Context, submissionID string) (store.Extraction, error)
CreateFinding(ctx context.Context, submissionID string, extractionID *string, ruleID string, ruleVersion int, severity, title, fix string, sources []string) (store.Finding, error)
ListCurrentFindings(ctx context.Context, submissionID string) ([]store.Finding, error)
CreateEvidencePackage(ctx context.Context, submissionID, dossierPath, sha256Hex string, timestampToken []byte) (store.EvidencePackage, error)
GetLatestEvidencePackage(ctx context.Context, submissionID string) (store.EvidencePackage, error)
}
// Server bündelt Routing und Abhängigkeiten der Web-Schicht.
type Server struct {
mux *http.ServeMux
extractor Extractor
ruleSet []rules.Rule
templates *template.Template
mux *http.ServeMux
extractor Extractor
ruleSet []rules.Rule
store Store
timestamper evidence.Timestamper
dossierDir string
templates *template.Template
}
// NewServer erstellt den Server. ruleSet kommt von rules.Load und wird
// einmal beim Start geladen, nicht pro Request.
func NewServer(extractor Extractor, ruleSet []rules.Rule) (*Server, error) {
// einmal beim Start geladen, nicht pro Request. dossierDir ist das
// Verzeichnis, in das erzeugte Nachweis-Dossiers geschrieben werden.
func NewServer(extractor Extractor, ruleSet []rules.Rule, st Store, timestamper evidence.Timestamper, dossierDir string) (*Server, error) {
tmpl, err := template.ParseFS(templatesFS, "templates/*.html")
if err != nil {
return nil, fmt.Errorf("web: templates parsen: %w", err)
}
s := &Server{extractor: extractor, ruleSet: ruleSet, templates: tmpl}
s := &Server{
extractor: extractor,
ruleSet: ruleSet,
store: st,
timestamper: timestamper,
dossierDir: dossierDir,
templates: tmpl,
}
mux := http.NewServeMux()
mux.HandleFunc("GET /health", s.handleHealth)
mux.HandleFunc("GET /{$}", s.handleIndex)
mux.HandleFunc("POST /pruefen", s.handleCheck)
mux.HandleFunc("POST /veroeffentlichen", s.handleArchive)
mux.HandleFunc("GET /dossier/{id}", s.handleDossierDownload)
mux.Handle("GET /static/", http.FileServerFS(staticFS))
s.mux = mux

View File

@@ -2,27 +2,218 @@ package web_test
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/digitorus/timestamp"
"github.com/netcell-it/deklarix/internal/extract"
"github.com/netcell-it/deklarix/internal/rules"
"github.com/netcell-it/deklarix/internal/store"
"github.com/netcell-it/deklarix/internal/web"
)
// ─── Fakes ────────────────────────────────────────────────────────────
type fakeExtractor struct {
facts rules.Facts
raw []byte
err error
}
func (f fakeExtractor) Extract(ctx context.Context, in extract.Input) (rules.Facts, error) {
return f.facts, f.err
func (f fakeExtractor) Extract(ctx context.Context, in extract.Input) (extract.Result, error) {
if f.err != nil {
return extract.Result{}, f.err
}
raw := f.raw
if raw == nil {
raw = []byte(`{}`)
}
return extract.Result{Facts: f.facts, RawJSON: raw}, nil
}
func (f fakeExtractor) ModelVersion() string { return "fake-model-v0" }
// fakeStore ist eine In-Memory-Implementierung von web.Store, damit die
// Handler-Tests keine echte Postgres-Instanz brauchen.
type fakeStore struct {
mu sync.Mutex
nextID int
submissions map[string]store.Submission
extractions map[string]store.Extraction
findings map[string][]store.Finding
evidencePkgs map[string]store.EvidencePackage
}
func newFakeStore() *fakeStore {
return &fakeStore{
submissions: map[string]store.Submission{},
extractions: map[string]store.Extraction{},
findings: map[string][]store.Finding{},
evidencePkgs: map[string]store.EvidencePackage{},
}
}
func (f *fakeStore) newID() string {
f.nextID++
return fmt.Sprintf("id-%d", f.nextID)
}
func (f *fakeStore) CreateSubmission(ctx context.Context, platform, postType, caption string) (store.Submission, error) {
f.mu.Lock()
defer f.mu.Unlock()
sub := store.Submission{
ID: f.newID(), Platform: platform, PostType: postType, Caption: caption,
Status: "draft", CreatedAt: time.Now(), UpdatedAt: time.Now(),
}
f.submissions[sub.ID] = sub
return sub, nil
}
func (f *fakeStore) GetSubmission(ctx context.Context, id string) (store.Submission, error) {
f.mu.Lock()
defer f.mu.Unlock()
sub, ok := f.submissions[id]
if !ok {
return store.Submission{}, fmt.Errorf("fakeStore: submission %s nicht gefunden", id)
}
return sub, nil
}
func (f *fakeStore) SetSubmissionStatus(ctx context.Context, id, status string) error {
f.mu.Lock()
defer f.mu.Unlock()
sub, ok := f.submissions[id]
if !ok {
return fmt.Errorf("fakeStore: submission %s nicht gefunden", id)
}
sub.Status = status
f.submissions[id] = sub
return nil
}
func (f *fakeStore) CreateExtraction(ctx context.Context, submissionID string, payload []byte, modelVersion, promptVersion string) (store.Extraction, error) {
f.mu.Lock()
defer f.mu.Unlock()
ext := store.Extraction{
ID: f.newID(), SubmissionID: submissionID, Payload: payload,
ModelVersion: modelVersion, PromptVersion: promptVersion, CreatedAt: time.Now(),
}
f.extractions[submissionID] = ext
return ext, nil
}
func (f *fakeStore) GetLatestExtraction(ctx context.Context, submissionID string) (store.Extraction, error) {
f.mu.Lock()
defer f.mu.Unlock()
ext, ok := f.extractions[submissionID]
if !ok {
return store.Extraction{}, fmt.Errorf("fakeStore: keine Extraktion fuer %s", submissionID)
}
return ext, nil
}
func (f *fakeStore) CreateFinding(ctx context.Context, submissionID string, extractionID *string, ruleID string, ruleVersion int, severity, title, fix string, sources []string) (store.Finding, error) {
f.mu.Lock()
defer f.mu.Unlock()
finding := store.Finding{
ID: f.newID(), SubmissionID: submissionID, ExtractionID: extractionID,
RuleID: ruleID, RuleVersion: ruleVersion, Severity: severity,
Title: title, Fix: fix, Sources: sources, CreatedAt: time.Now(),
}
f.findings[submissionID] = append(f.findings[submissionID], finding)
return finding, nil
}
func (f *fakeStore) ListCurrentFindings(ctx context.Context, submissionID string) ([]store.Finding, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.findings[submissionID], nil
}
func (f *fakeStore) CreateEvidencePackage(ctx context.Context, submissionID, dossierPath, sha256Hex string, timestampToken []byte) (store.EvidencePackage, error) {
f.mu.Lock()
defer f.mu.Unlock()
pkg := store.EvidencePackage{
ID: f.newID(), SubmissionID: submissionID, DossierPath: dossierPath,
SHA256: sha256Hex, TimestampToken: timestampToken, CreatedAt: time.Now(),
}
f.evidencePkgs[submissionID] = pkg
return pkg, nil
}
func (f *fakeStore) GetLatestEvidencePackage(ctx context.Context, submissionID string) (store.EvidencePackage, error) {
f.mu.Lock()
defer f.mu.Unlock()
pkg, ok := f.evidencePkgs[submissionID]
if !ok {
return store.EvidencePackage{}, fmt.Errorf("fakeStore: kein evidence package fuer %s", submissionID)
}
return pkg, nil
}
// fakeTimestamper liefert einen offline erzeugten, strukturell gültigen
// (selbstsignierten) RFC-3161-Token — genug, damit evidence.TimestampTime
// ihn parsen kann, ohne eine echte TSA zu brauchen.
type fakeTimestamper struct{}
func (fakeTimestamper) Timestamp(ctx context.Context, hash []byte) ([]byte, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
now := time.Now()
certTemplate := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "deklarix-test-tsa"},
NotBefore: now.Add(-time.Hour),
NotAfter: now.Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageTimeStamping},
}
certDER, err := x509.CreateCertificate(rand.Reader, certTemplate, certTemplate, &key.PublicKey, key)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(certDER)
if err != nil {
return nil, err
}
ts := timestamp.Timestamp{
HashAlgorithm: crypto.SHA256,
HashedMessage: hash,
Time: now,
SerialNumber: big.NewInt(1),
Policy: asn1.ObjectIdentifier{1, 2, 3},
}
respDER, err := ts.CreateResponse(cert, key)
if err != nil {
return nil, err
}
parsed, err := timestamp.ParseResponse(respDER)
if err != nil {
return nil, err
}
return parsed.RawToken, nil
}
// ─── Test-Setup ───────────────────────────────────────────────────────
func loadRealRules(t *testing.T) []rules.Rule {
t.Helper()
rs, err := rules.Load(os.DirFS("../../rules"))
@@ -34,13 +225,15 @@ func loadRealRules(t *testing.T) []rules.Rule {
func newTestServer(t *testing.T, ex web.Extractor) *web.Server {
t.Helper()
s, err := web.NewServer(ex, loadRealRules(t))
s, err := web.NewServer(ex, loadRealRules(t), newFakeStore(), fakeTimestamper{}, t.TempDir())
if err != nil {
t.Fatalf("NewServer: %v", err)
}
return s
}
// ─── Tests ────────────────────────────────────────────────────────────
func TestHandleHealth(t *testing.T) {
s := newTestServer(t, fakeExtractor{})
@@ -67,7 +260,7 @@ func TestHandleIndexRendersForm(t *testing.T) {
t.Fatalf("status = %d, want 200", w.Code)
}
body := w.Body.String()
for _, want := range []string{`name="platform"`, `name="caption"`, `hx-post="/pruefen"`} {
for _, want := range []string{`name="platform"`, `name="post_type"`, `name="caption"`, `hx-post="/pruefen"`} {
if !strings.Contains(body, want) {
t.Errorf("index body missing %q\nbody: %s", want, body)
}
@@ -89,64 +282,87 @@ func TestHandleStaticServesHTMX(t *testing.T) {
}
}
func postCheck(t *testing.T, s *web.Server, form url.Values) *httptest.ResponseRecorder {
func postForm(t *testing.T, s *web.Server, path string, form url.Values) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodPost, "/pruefen", strings.NewReader(form.Encode()))
req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
s.ServeHTTP(w, req)
return w
}
func checkForm(extra ...string) url.Values {
v := url.Values{"platform": {"instagram"}, "post_type": {"reel"}, "caption": {"..."}}
for i := 0; i+1 < len(extra); i += 2 {
v.Set(extra[i], extra[i+1])
}
return v
}
func TestHandleCheckRejectsMissingFields(t *testing.T) {
s := newTestServer(t, fakeExtractor{})
w := postCheck(t, s, url.Values{"platform": {"instagram"}})
w := postForm(t, s, "/pruefen", url.Values{"platform": {"instagram"}})
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400 for missing caption", w.Code)
t.Fatalf("status = %d, want 400 for missing fields", w.Code)
}
}
func TestHandleCheckPropagatesExtractionError(t *testing.T) {
s := newTestServer(t, fakeExtractor{err: errTest})
s := newTestServer(t, fakeExtractor{err: fmt.Errorf("simulierter Extraktionsfehler")})
w := postCheck(t, s, url.Values{"platform": {"instagram"}, "caption": {"x"}})
w := postForm(t, s, "/pruefen", checkForm())
if w.Code != http.StatusBadGateway {
t.Fatalf("status = %d, want 502 when extraction fails", w.Code)
}
}
func TestHandleCheckRendersFinding(t *testing.T) {
s := newTestServer(t, fakeExtractor{facts: rules.Facts{
Platform: "instagram",
Jurisdiction: "DE",
Consideration: rules.ConsiderationPaid,
DisclosurePresent: true,
DisclosureWording: "Werbung",
DisclosureBeforeCut: false,
}})
w := postCheck(t, s, url.Values{"platform": {"instagram"}, "caption": {"Werbung, schaut mal..."}})
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", w.Code)
func TestHandleCheckPersistsSubmissionExtractionAndFindings(t *testing.T) {
fs := newFakeStore()
s, err := web.NewServer(fakeExtractor{facts: rules.Facts{
Platform: "instagram", Jurisdiction: "DE", Consideration: rules.ConsiderationPaid,
DisclosurePresent: true, DisclosureWording: "Werbung", DisclosureBeforeCut: false,
}, raw: []byte(`{"gegenleistung":"bezahlt"}`)}, loadRealRules(t), fs, fakeTimestamper{}, t.TempDir())
if err != nil {
t.Fatalf("NewServer: %v", err)
}
body := w.Body.String()
if !strings.Contains(body, "WK-004") {
t.Errorf("expected WK-004 finding in result, got: %s", body)
w := postForm(t, s, "/pruefen", checkForm())
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200, body: %s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), "WK-004") {
t.Fatalf("expected WK-004 in result, got: %s", w.Body.String())
}
if len(fs.submissions) != 1 {
t.Fatalf("expected 1 persisted submission, got %d", len(fs.submissions))
}
var subID string
for id, sub := range fs.submissions {
subID = id
if sub.Status != "checked" {
t.Errorf("submission status = %q, want checked", sub.Status)
}
}
if _, ok := fs.extractions[subID]; !ok {
t.Error("expected an extraction to be persisted")
}
if len(fs.findings[subID]) != 1 || fs.findings[subID][0].RuleID != "WK-004" {
t.Errorf("expected exactly one persisted WK-004 finding, got %+v", fs.findings[subID])
}
}
func TestHandleCheckRendersNeedsClarification(t *testing.T) {
s := newTestServer(t, fakeExtractor{facts: rules.Facts{
Platform: "instagram",
Jurisdiction: "DE",
Consideration: rules.ConsiderationUnclear,
}})
w := postCheck(t, s, url.Values{"platform": {"instagram"}, "caption": {"..."}})
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", w.Code)
func TestHandleCheckRendersNeedsClarificationAndDoesNotOfferArchive(t *testing.T) {
fs := newFakeStore()
s, err := web.NewServer(fakeExtractor{facts: rules.Facts{
Platform: "instagram", Jurisdiction: "DE", Consideration: rules.ConsiderationUnclear,
}}, loadRealRules(t), fs, fakeTimestamper{}, t.TempDir())
if err != nil {
t.Fatalf("NewServer: %v", err)
}
w := postForm(t, s, "/pruefen", checkForm())
body := w.Body.String()
if strings.Contains(body, "WK-") {
t.Errorf("expected no rule findings for an unclear extraction, got: %s", body)
@@ -154,16 +370,87 @@ func TestHandleCheckRendersNeedsClarification(t *testing.T) {
if !strings.Contains(body, "nicht sicher bestimmt") {
t.Errorf("expected a clarification message, got: %s", body)
}
if strings.Contains(body, "veroeffentlichen") || strings.Contains(body, "/veroeffentlichen") {
t.Errorf("expected no archive option when clarification is needed, got: %s", body)
}
for _, sub := range fs.submissions {
if sub.Status == "checked" {
t.Error("submission should not be marked checked when clarification is needed")
}
}
}
func TestHandleCheckCleanCaseHasNoFindings(t *testing.T) {
func TestFullCheckThenArchiveFlow(t *testing.T) {
fs := newFakeStore()
s, err := web.NewServer(fakeExtractor{facts: rules.Facts{
Platform: "instagram", Jurisdiction: "DE", Consideration: rules.ConsiderationPaid,
DisclosurePresent: true, DisclosureWording: "Werbung", DisclosureBeforeCut: false,
}, raw: []byte(`{"gegenleistung":"bezahlt","kennzeichnung_vorhanden":true,"kennzeichnung_wortlaut":"Werbung","kennzeichnung_vor_kuerzung":false}`)},
loadRealRules(t), fs, fakeTimestamper{}, t.TempDir())
if err != nil {
t.Fatalf("NewServer: %v", err)
}
checkResp := postForm(t, s, "/pruefen", checkForm())
if checkResp.Code != http.StatusOK {
t.Fatalf("check status = %d, body: %s", checkResp.Code, checkResp.Body.String())
}
if !strings.Contains(checkResp.Body.String(), `name="submission_id"`) {
t.Fatalf("expected a hidden submission_id field in the result, got: %s", checkResp.Body.String())
}
var subID string
for id := range fs.submissions {
subID = id
}
archiveResp := postForm(t, s, "/veroeffentlichen", url.Values{"submission_id": {subID}})
if archiveResp.Code != http.StatusOK {
t.Fatalf("archive status = %d, body: %s", archiveResp.Code, archiveResp.Body.String())
}
if !strings.Contains(archiveResp.Body.String(), "/dossier/"+subID) {
t.Fatalf("expected a dossier download link, got: %s", archiveResp.Body.String())
}
pkg, ok := fs.evidencePkgs[subID]
if !ok {
t.Fatal("expected an evidence package to be persisted")
}
if fs.submissions[subID].Status != "published" {
t.Errorf("submission status = %q, want published", fs.submissions[subID].Status)
}
downloadReq := httptest.NewRequest(http.MethodGet, "/dossier/"+subID, nil)
downloadReq.SetPathValue("id", subID)
downloadW := httptest.NewRecorder()
s.ServeHTTP(downloadW, downloadReq)
if downloadW.Code != http.StatusOK {
t.Fatalf("download status = %d", downloadW.Code)
}
if !strings.HasPrefix(downloadW.Body.String(), "%PDF-") {
t.Fatal("downloaded dossier does not start with the PDF header")
}
if _, err := os.Stat(pkg.DossierPath); err != nil {
t.Fatalf("dossier file does not exist on disk: %v", err)
}
}
func TestHandleArchiveRejectsUnknownSubmission(t *testing.T) {
s := newTestServer(t, fakeExtractor{})
w := postForm(t, s, "/veroeffentlichen", url.Values{"submission_id": {"does-not-exist"}})
if w.Code != http.StatusNotFound {
t.Fatalf("status = %d, want 404 for an unknown submission", w.Code)
}
}
func TestHandleCheckCleanCaseHasNoFindingsAndOffersArchive(t *testing.T) {
s := newTestServer(t, fakeExtractor{facts: rules.Facts{
Platform: "instagram",
Jurisdiction: "DE",
Consideration: rules.ConsiderationNone,
Platform: "instagram", Jurisdiction: "DE", Consideration: rules.ConsiderationNone,
}})
w := postCheck(t, s, url.Values{"platform": {"instagram"}, "caption": {"ein ganz normaler Post"}})
w := postForm(t, s, "/pruefen", checkForm())
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", w.Code)
}
@@ -174,10 +461,7 @@ func TestHandleCheckCleanCaseHasNoFindings(t *testing.T) {
if !strings.Contains(body, "Keine Kennzeichnungsrisiken") {
t.Errorf("expected the no-findings message, got: %s", body)
}
if !strings.Contains(body, "/veroeffentlichen") {
t.Errorf("expected an archive option even with no findings (still a real check), got: %s", body)
}
}
var errTest = extractError("simulierter Extraktionsfehler")
type extractError string
func (e extractError) Error() string { return string(e) }

View File

@@ -0,0 +1,13 @@
{{define "archived"}}
<p class="archiviert">
Beitrag archiviert und mit RFC-3161-Zeitstempel versehen
({{.TimestampedAt}}).
</p>
<p>
<a href="{{.DossierURL}}">Nachweis-Dossier (PDF) herunterladen</a>
</p>
<p class="disclaimer">
Dieses Dossier dokumentiert die durchgeführte Prüfung. Es ist keine
Rechtsberatung und ersetzt keine anwaltliche Prüfung im Einzelfall.
</p>
{{end}}

View File

@@ -9,6 +9,14 @@
<option value="tiktok">TikTok</option>
</select>
<label for="post_type">Beitragstyp</label>
<select id="post_type" name="post_type" required>
<option value="feed">Feed</option>
<option value="reel">Reel</option>
<option value="story">Story</option>
<option value="video">Video</option>
</select>
<label for="caption">Caption</label>
<textarea id="caption" name="caption" rows="6" required></textarea>

View File

@@ -20,6 +20,14 @@
{{else}}
<p class="keine-findings">Keine Kennzeichnungsrisiken nach aktuellem Regelwerk gefunden.</p>
{{end}}
{{if .CanArchive}}
<form hx-post="/veroeffentlichen" hx-target="#ergebnis" hx-swap="innerHTML">
<input type="hidden" name="submission_id" value="{{.SubmissionID}}">
<button type="submit">Als veröffentlicht markieren &amp; archivieren</button>
</form>
{{end}}
<p class="disclaimer">
Diese Prüfung ist keine Rechtsberatung und ersetzt keine anwaltliche
Prüfung im Einzelfall.