108 lines
3.6 KiB
Go
108 lines
3.6 KiB
Go
// account_audit_query_test.go(原 Day29):审计查询 API(system:read 门禁)+ 拦截器联动。
|
|
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"go-sms/internal/service/audit"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/middleware"
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/casbin"
|
|
"go-sms/internal/service/account"
|
|
)
|
|
|
|
// newD29Router 双账号 + 审计装配(拦截器 + 查询端点)。
|
|
func newD29Router(t *testing.T) *gin.Engine {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.AutoMigrate(&entity.AccountUser{}, &entity.AccountConfig{}, &entity.AccountBalanceLog{},
|
|
&entity.SystemAlertRule{}, &entity.SystemOperateLog{}, &entity.SystemLoginLog{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e, err := casbin.New(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range [][]string{{"admin", "*", "*"}, {"customer", "message", "write"}} {
|
|
if _, err := e.AddPolicy(p[0], p[1], p[2]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rows := []entity.AccountUser{
|
|
{Username: "d29-admin", Role: "admin", Status: 1},
|
|
{Username: "d29-cust", Role: "customer", Status: 1},
|
|
}
|
|
for i := range rows {
|
|
if err := db.Create(&rows[i]).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := e.BindRole(rows[i].ID, rows[i].Role); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
acctSvc := account.NewService(db, account.BcryptMinCost).WithEnforcer(e)
|
|
auditSvc := audit.NewService(db)
|
|
|
|
r := gin.New()
|
|
Register(r.Group("/api/v1"), Deps{
|
|
Account: acctSvc,
|
|
Warning: account.NewWarningService(db, nil),
|
|
Audit: auditSvc,
|
|
Auth: middleware.AuthJWTWithChecker(d25Secret, acctSvc.CheckEnabled),
|
|
Perm: middleware.CasbinChecker(e),
|
|
})
|
|
return r
|
|
}
|
|
|
|
// TestAPI_AuditLogsQuery admin 触发写操作留痕 → 查询可见;customer 403。
|
|
func TestAPI_AuditLogsQuery(t *testing.T) {
|
|
r := newD29Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
custTok := d25Token(t, 2, "customer")
|
|
|
|
// admin 充值(写操作 → 拦截器留痕)
|
|
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/recharge", adminTok, "application/json", []byte(`{"amount":50}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("充值 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
// customer 查询审计 → 403(无 system:read)
|
|
if w := doReq(r, http.MethodGet, "/api/v1/audit/logs", custTok); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 查审计 = %d, 期望 403", w.Code)
|
|
}
|
|
// admin 查询 → 200 且含充值动作留痕
|
|
w := doReq(r, http.MethodGet, "/api/v1/audit/logs?module=account&action=recharge:post", adminTok)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("admin 查审计 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if b := w.Body.String(); !contains(b, `"total":1`) || !contains(b, "recharge:post") || !contains(b, "admin-u") {
|
|
t.Fatalf("审计应含充值留痕: %s", b)
|
|
}
|
|
// GET 审计本身是普通 GET:不留痕(过滤条件查无)
|
|
if w := doReq(r, http.MethodGet, "/api/v1/audit/logs?action=logs:get", adminTok); w.Code != http.StatusOK || !contains(w.Body.String(), `"total":0`) {
|
|
t.Fatalf("查询自身不应留痕: %s", w.Body.String())
|
|
}
|
|
}
|
|
|
|
// TestAPI_AuditLoginsQuery 登录审计查询。
|
|
func TestAPI_AuditLoginsQuery(t *testing.T) {
|
|
r := newD29Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
// customer → 403
|
|
if w := doReq(r, http.MethodGet, "/api/v1/audit/logins", d25Token(t, 2, "customer")); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 查登录审计 = %d, 期望 403", w.Code)
|
|
}
|
|
// admin → 200
|
|
if w := doReq(r, http.MethodGet, "/api/v1/audit/logins", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("admin 查登录审计 = %d", w.Code)
|
|
}
|
|
}
|