mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-04 20:41:51 +08:00
feat: search approval history by content and date
This commit is contained in:
1 parent
e33eedeb17
commit
2f2b78cc78
9 files changed
+255
-11
No files matched your search
+29
-1
@@ -10,6 +10,8 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
"github.com/Autumn-27/artex/guard"
|
||||
@@ -321,7 +323,33 @@ func (s *Server) interceptHistory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func interceptFilterParams(q url.Values) (db.InterceptApprovalFilter, error) {
|
||||
filter := db.InterceptApprovalFilter{Status: q.Get("status"), DecisionSource: q.Get("decision_source")}
|
||||
filter := db.InterceptApprovalFilter{
|
||||
Status: q.Get("status"),
|
||||
DecisionSource: q.Get("decision_source"),
|
||||
Search: strings.TrimSpace(q.Get("q")),
|
||||
CreatedFrom: q.Get("from"),
|
||||
CreatedTo: q.Get("to"),
|
||||
}
|
||||
if utf8.RuneCountInString(filter.Search) > 200 {
|
||||
return filter, fmt.Errorf("q 不能超过 200 个字符")
|
||||
}
|
||||
if strings.HasPrefix(filter.Search, "#") {
|
||||
id, err := strconv.ParseInt(strings.TrimPrefix(filter.Search, "#"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return filter, fmt.Errorf("审批编号必须为 # 后接正整数")
|
||||
}
|
||||
}
|
||||
for name, value := range map[string]string{"from": filter.CreatedFrom, "to": filter.CreatedTo} {
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if _, err := time.Parse("2006-01-02", value); err != nil {
|
||||
return filter, fmt.Errorf("%s 必须是 YYYY-MM-DD 日期", name)
|
||||
}
|
||||
}
|
||||
if filter.CreatedFrom != "" && filter.CreatedTo != "" && filter.CreatedFrom > filter.CreatedTo {
|
||||
return filter, fmt.Errorf("from 不能晚于 to")
|
||||
}
|
||||
switch filter.Status {
|
||||
case "", "pending", "allowed", "denied", "timeout":
|
||||
default:
|
||||
|
||||
@@ -4,12 +4,43 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
"github.com/Autumn-27/artex/intercept"
|
||||
)
|
||||
|
||||
func TestInterceptSearchFilterParams(t *testing.T) {
|
||||
filter, err := interceptFilterParams(url.Values{"q": []string{" https://example.test/a?x=1 "}})
|
||||
if err != nil || filter.Search != "https://example.test/a?x=1" {
|
||||
t.Fatalf("search=%q err=%v", filter.Search, err)
|
||||
}
|
||||
if _, err := interceptFilterParams(url.Values{"q": []string{"#0"}}); err == nil {
|
||||
t.Fatal("zero approval id accepted")
|
||||
}
|
||||
if _, err := interceptFilterParams(url.Values{"q": []string{"#abc"}}); err == nil {
|
||||
t.Fatal("non-numeric approval id accepted")
|
||||
}
|
||||
if _, err := interceptFilterParams(url.Values{"q": []string{strings.Repeat("x", 201)}}); err == nil {
|
||||
t.Fatal("overlong search accepted")
|
||||
}
|
||||
filter, err = interceptFilterParams(url.Values{"from": []string{"2026-10-01"}, "to": []string{"2026-10-12"}})
|
||||
if err != nil || filter.CreatedFrom != "2026-10-01" || filter.CreatedTo != "2026-10-12" {
|
||||
t.Fatalf("date filter=%+v err=%v", filter, err)
|
||||
}
|
||||
for _, values := range []url.Values{
|
||||
{"from": []string{"2026-10-1"}},
|
||||
{"to": []string{"not-a-date"}},
|
||||
{"from": []string{"2026-10-13"}, "to": []string{"2026-10-12"}},
|
||||
} {
|
||||
if _, err := interceptFilterParams(values); err == nil {
|
||||
t.Fatalf("invalid date filter accepted: %v", values)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterceptFilterHTTP(t *testing.T) {
|
||||
dsn, _, err := db.DSN()
|
||||
if err != nil {
|
||||
@@ -54,7 +85,14 @@ func TestInterceptFilterHTTP(t *testing.T) {
|
||||
if rec := do(base+"?status=denied&decision_source=model", false); rec.Code != 401 {
|
||||
t.Fatalf("unprotected list: %d", rec.Code)
|
||||
}
|
||||
for _, query := range []string{"?status=invalid", "?decision_source=invalid", "?page=1&status=denied%27+OR+1%3D1--", "?size=20&decision_source=MODEL"} {
|
||||
for _, query := range []string{
|
||||
"?status=invalid",
|
||||
"?decision_source=invalid",
|
||||
"?page=1&status=denied%27+OR+1%3D1--",
|
||||
"?size=20&decision_source=MODEL",
|
||||
"?q=%23abc",
|
||||
"?from=2026-10-13&to=2026-10-12",
|
||||
} {
|
||||
if rec := do(base+query, true); rec.Code != 400 {
|
||||
t.Fatalf("invalid filter %s: %d %s", query, rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user