104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
// audit_test.go Day 29:审计写入与查询过滤。
|
|
package audit
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/model/entity"
|
|
)
|
|
|
|
func newSvc29(t *testing.T) *Service {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.AutoMigrate(&entity.SystemOperateLog{}, &entity.SystemLoginLog{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return NewService(db)
|
|
}
|
|
|
|
// TestRecordAndQuery 写入 + 操作人/模块/IP/时间过滤 + 分页。
|
|
func TestRecordAndQuery(t *testing.T) {
|
|
s := newSvc29(t)
|
|
ctx := context.Background()
|
|
for _, e := range []*Entry{
|
|
{Operator: "admin", AccountID: 1, Module: "account", Action: "recharge:post", Target: "2", IP: "10.0.0.1"},
|
|
{Operator: "ops", AccountID: 2, Module: "account", Action: "create:post", IP: "10.0.0.2"},
|
|
{Operator: "admin", AccountID: 1, Module: "system", Action: "export:get", IP: "10.0.0.1"},
|
|
} {
|
|
if err := s.Record(ctx, e); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// 全量
|
|
logs, total, err := s.Query(ctx, &OperateQuery{Page: 1, PageSize: 10})
|
|
if err != nil || total != 3 || len(logs) != 3 {
|
|
t.Fatalf("全量查询 total=%d err=%v", total, err)
|
|
}
|
|
// 操作人
|
|
_, total, _ = s.Query(ctx, &OperateQuery{Operator: "admin"})
|
|
if total != 2 {
|
|
t.Fatalf("operator 过滤 = %d, 期望 2", total)
|
|
}
|
|
// 模块 + 动作
|
|
_, total, _ = s.Query(ctx, &OperateQuery{Module: "account", Action: "recharge:post"})
|
|
if total != 1 {
|
|
t.Fatalf("module/action 过滤 = %d", total)
|
|
}
|
|
// 账号 + IP
|
|
_, total, _ = s.Query(ctx, &OperateQuery{AccountID: 1, IP: "10.0.0.1"})
|
|
if total != 2 {
|
|
t.Fatalf("account/ip 过滤 = %d", total)
|
|
}
|
|
// 时间范围(未来窗口为空)
|
|
future := time.Now().Add(time.Hour)
|
|
_, total, _ = s.Query(ctx, &OperateQuery{Start: &future})
|
|
if total != 0 {
|
|
t.Fatalf("未来窗口应为空, got %d", total)
|
|
}
|
|
past := time.Now().Add(-time.Hour)
|
|
_, total, _ = s.Query(ctx, &OperateQuery{Start: &past})
|
|
if total != 3 {
|
|
t.Fatalf("过去窗口 = %d", total)
|
|
}
|
|
// 分页
|
|
logs, total, _ = s.Query(ctx, &OperateQuery{Page: 2, PageSize: 2})
|
|
if total != 3 || len(logs) != 1 {
|
|
t.Fatalf("分页 total=%d len=%d", total, len(logs))
|
|
}
|
|
}
|
|
|
|
// TestRecordLoginAndQuery 登录审计(成功/失败)+ 过滤。
|
|
func TestRecordLoginAndQuery(t *testing.T) {
|
|
s := newSvc29(t)
|
|
ctx := context.Background()
|
|
if err := s.RecordLogin(ctx, "admin", 1, "10.0.0.1", "ua", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.RecordLogin(ctx, "ghost", 0, "10.0.0.9", "ua", "用户名或密码错误"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
logs, total, err := s.QueryLogin(ctx, &LoginQuery{Page: 1, PageSize: 10})
|
|
if err != nil || total != 2 || len(logs) != 2 {
|
|
t.Fatalf("登录全量 = %d err=%v", total, err)
|
|
}
|
|
if logs[0].Ok != 0 || logs[0].FailReason == "" {
|
|
t.Fatalf("倒序首条应为失败记录: %+v", logs[0])
|
|
}
|
|
_, total, _ = s.QueryLogin(ctx, &LoginQuery{OnlyFailed: true})
|
|
if total != 1 {
|
|
t.Fatalf("仅失败 = %d", total)
|
|
}
|
|
_, total, _ = s.QueryLogin(ctx, &LoginQuery{Username: "admin", IP: "10.0.0.1"})
|
|
if total != 1 {
|
|
t.Fatalf("用户名+IP 过滤 = %d", total)
|
|
}
|
|
}
|