CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
package autoruleapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/dms/internal/tag"
|
|
"gitea.perlbach24.de/scripte/nexarch/dms/internal/testenv"
|
|
)
|
|
|
|
func TestAPI_MaintainRulesWithoutCode(t *testing.T) {
|
|
e := testenv.New(t)
|
|
tg, err := tag.Create(t.Context(), e.Pool, "rechnung", "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
NewServer(e.Pool).RegisterRoutes(mux)
|
|
srv := httptest.NewServer(mux)
|
|
t.Cleanup(srv.Close)
|
|
|
|
do := func(method, path string, body any) (int, []byte) {
|
|
b, _ := json.Marshal(body)
|
|
req, _ := http.NewRequest(method, srv.URL+path, bytes.NewReader(b))
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
var buf bytes.Buffer
|
|
_, _ = buf.ReadFrom(resp.Body)
|
|
return resp.StatusCode, buf.Bytes()
|
|
}
|
|
caller := "?caller=" + e.UserID
|
|
rule := map[string]any{"name": "Rechnung", "priority": 10, "enabled": true, "filename_pattern": "(?i)rechnung", "add_tag_ids": []string{tg.ID}}
|
|
|
|
if status, _ := do(http.MethodPost, "/auto-rules", rule); status != http.StatusBadRequest {
|
|
t.Errorf("ohne caller: %d, want 400", status)
|
|
}
|
|
status, body := do(http.MethodPost, "/auto-rules"+caller, rule)
|
|
if status != http.StatusCreated {
|
|
t.Fatalf("anlegen: %d %s", status, body)
|
|
}
|
|
var created map[string]any
|
|
_ = json.Unmarshal(body, &created)
|
|
id := created["id"].(string)
|
|
|
|
bad := map[string]any{"name": "kaputt", "filename_pattern": "([", "add_tag_ids": []string{tg.ID}}
|
|
if status, _ := do(http.MethodPost, "/auto-rules"+caller, bad); status != http.StatusUnprocessableEntity {
|
|
t.Errorf("ungueltiger regex: %d, want 422", status)
|
|
}
|
|
|
|
rule["priority"] = 5
|
|
if status, _ := do(http.MethodPut, "/auto-rules/"+id+caller, rule); status != http.StatusOK {
|
|
t.Errorf("aendern: %d", status)
|
|
}
|
|
status, body = do(http.MethodGet, "/auto-rules", nil)
|
|
var list []map[string]any
|
|
_ = json.Unmarshal(body, &list)
|
|
if status != http.StatusOK || len(list) != 1 || list[0]["priority"].(float64) != 5 {
|
|
t.Errorf("liste: %d %s", status, body)
|
|
}
|
|
if status, _ := do(http.MethodPut, "/auto-rules/00000000-0000-0000-0000-000000000001"+caller, rule); status != http.StatusNotFound {
|
|
t.Errorf("unbekannte regel: %d, want 404", status)
|
|
}
|
|
if status, _ := do(http.MethodDelete, "/auto-rules/"+id+caller, nil); status != http.StatusNoContent {
|
|
t.Errorf("loeschen: %d", status)
|
|
}
|
|
}
|