新增 filter_rules、admin.content_safety.read、列表/详情/试匹配 API 与 admin-h5「安全」页;禁审核写/UGC/真支付。 Co-authored-by: Cursor <cursoragent@cursor.com>
113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package integration_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func TestContentSafetyFilterRules(t *testing.T) {
|
|
r, pool := setupAPIPool(t)
|
|
ctx := context.Background()
|
|
tok := adminLogin(t, r, "admin", "change-me")
|
|
|
|
_, code := doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/content-safety/filter-rules", nil, "")
|
|
if code != http.StatusUnauthorized {
|
|
t.Fatalf("expected 401, got %d", code)
|
|
}
|
|
|
|
limitedRoleID := uuid.New()
|
|
_, err := pool.Exec(ctx, `
|
|
INSERT INTO admin_roles(id, name, system) VALUES ($1,$2,false)`,
|
|
limitedRoleID, "cs_lim_"+limitedRoleID.String()[:8])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = pool.Exec(ctx, `
|
|
INSERT INTO admin_role_permissions(role_id, code) VALUES ($1,'admin.users.read')`, limitedRoleID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("limited-pass"), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
limUser := fmt.Sprintf("cslim_%d", time.Now().UnixNano())
|
|
_, err = pool.Exec(ctx, `
|
|
INSERT INTO admin_accounts(username, password_hash, role_id) VALUES ($1,$2,$3)`,
|
|
limUser, string(hash), limitedRoleID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_, _ = pool.Exec(ctx, `DELETE FROM admin_accounts WHERE username=$1`, limUser)
|
|
_, _ = pool.Exec(ctx, `DELETE FROM admin_roles WHERE id=$1`, limitedRoleID)
|
|
})
|
|
limTok := adminLogin(t, r, limUser, "limited-pass")
|
|
_, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/content-safety/filter-rules", nil, limTok)
|
|
if code != http.StatusForbidden {
|
|
t.Fatalf("expected 403, got %d", code)
|
|
}
|
|
|
|
start := time.Now()
|
|
env, code := doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/content-safety/filter-rules", nil, tok)
|
|
if code != 200 || env.Code != 0 {
|
|
t.Fatalf("list http=%d msg=%s", code, env.Message)
|
|
}
|
|
if time.Since(start) > 500*time.Millisecond {
|
|
t.Fatalf("list too slow %v", time.Since(start))
|
|
}
|
|
var list struct {
|
|
Items []struct {
|
|
ID string `json:"id"`
|
|
Code string `json:"code"`
|
|
System bool `json:"system"`
|
|
} `json:"items"`
|
|
}
|
|
_ = json.Unmarshal(env.Data, &list)
|
|
if len(list.Items) < 1 {
|
|
t.Fatal("expected seeded filter rules")
|
|
}
|
|
var firstID string
|
|
for _, it := range list.Items {
|
|
if it.System {
|
|
firstID = it.ID
|
|
break
|
|
}
|
|
}
|
|
if firstID == "" {
|
|
firstID = list.Items[0].ID
|
|
}
|
|
|
|
env, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/content-safety/filter-rules/"+firstID, nil, tok)
|
|
if code != 200 {
|
|
t.Fatalf("get %d", code)
|
|
}
|
|
|
|
_, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/content-safety/filter-rules/"+fakeUUID(), nil, tok)
|
|
if code != http.StatusNotFound {
|
|
t.Fatalf("expected 404, got %d", code)
|
|
}
|
|
|
|
env, code = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/content-safety/evaluate",
|
|
map[string]string{"text": "真的不想活了怎么办"}, tok)
|
|
if code != 200 {
|
|
t.Fatalf("evaluate %d msg=%s", code, env.Message)
|
|
}
|
|
var ev struct {
|
|
Matches []struct {
|
|
Code string `json:"code"`
|
|
} `json:"matches"`
|
|
}
|
|
_ = json.Unmarshal(env.Data, &ev)
|
|
if len(ev.Matches) < 1 {
|
|
t.Fatalf("expected match, got %#v", ev)
|
|
}
|
|
}
|