232 lines
8.8 KiB
Go
232 lines
8.8 KiB
Go
// account_phase2_regression_test.go(原 Day30):阶段 2 验收全量回归(A-2.1~A-2.13)。
|
|
// 以真实 HTTP 请求串联全部端点,等价 Postman 集合;证据映射见 docs/phase2-review.md。
|
|
package v1
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/glebarez/sqlite"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/middleware"
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/casbin"
|
|
jwtpkg "go-sms/internal/pkg/jwt"
|
|
"go-sms/internal/service/account"
|
|
"go-sms/internal/service/audit"
|
|
"go-sms/internal/service/auth"
|
|
)
|
|
|
|
// e2eEnv 阶段 2 全量环境。
|
|
type e2eEnv struct {
|
|
r *gin.Engine
|
|
db *gorm.DB
|
|
acct *account.Service
|
|
authSvc *auth.Service
|
|
}
|
|
|
|
func newE2E(t *testing.T) *e2eEnv {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sqlDB, e := db.DB(); e == nil {
|
|
sqlDB.SetMaxOpenConns(1)
|
|
}
|
|
if err := db.AutoMigrate(&entity.AccountUser{}, &entity.AccountConfig{}, &entity.MessageDetail{},
|
|
&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)
|
|
}
|
|
}
|
|
hash, _ := bcrypt.GenerateFromPassword([]byte("secret66"), bcrypt.MinCost)
|
|
admin := &entity.AccountUser{Username: "e2e-admin", PasswordHash: string(hash), Role: "admin", Status: 1}
|
|
cust := &entity.AccountUser{Username: "e2e-cust", PasswordHash: string(hash), Role: "customer", Status: 1}
|
|
if err := db.Create(admin).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := e.BindRole(admin.ID, "admin"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(cust).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := e.BindRole(cust.ID, "customer"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
acctSvc := account.NewService(db, account.BcryptMinCost).WithEnforcer(e)
|
|
authSvc := auth.NewService(db, jwtpkg.NewManager(d25Secret, time.Hour), nil)
|
|
env := &e2eEnv{db: db, acct: acctSvc, authSvc: authSvc}
|
|
r := gin.New()
|
|
auditSvc := audit.NewService(db)
|
|
Register(r.Group("/api/v1"), Deps{
|
|
Account: acctSvc,
|
|
Warning: account.NewWarningService(db, nil),
|
|
Audit: auditSvc,
|
|
Auth: middleware.AuthJWTWithChecker(d25Secret, acctSvc.CheckEnabled),
|
|
AuthSvc: authSvc,
|
|
Perm: middleware.CasbinChecker(e),
|
|
})
|
|
env.r = r
|
|
return env
|
|
}
|
|
|
|
// loginE2E 真实登录拿 token(A-2.7)。
|
|
func (env *e2eEnv) login(t *testing.T, username, password string) (string, int) {
|
|
t.Helper()
|
|
w := doReqBody(env.r, http.MethodPost, "/api/v1/auth/login", "", "application/json",
|
|
[]byte(`{"username":"`+username+`","password":"`+password+`"}`))
|
|
var resp struct {
|
|
Data struct {
|
|
Token string `json:"token"`
|
|
} `json:"data"`
|
|
}
|
|
tok := ""
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err == nil {
|
|
tok = resp.Data.Token
|
|
}
|
|
return tok, w.Code
|
|
}
|
|
|
|
// TestPhase2E2E_AccountSystem A-2.1/2.2/2.3/2.4/2.7。
|
|
func TestPhase2E2E_AccountSystem(t *testing.T) {
|
|
env := newE2E(t)
|
|
// A-2.7 正确密码登录返 JWT
|
|
tok, code := env.login(t, "e2e-admin", "secret66")
|
|
if code != http.StatusOK || tok == "" {
|
|
t.Fatalf("登录 = %d token空=%v", code, tok == "")
|
|
}
|
|
// A-2.1 开户 + 四协议
|
|
w := doReqBody(env.r, http.MethodPost, "/api/v1/accounts", tok, "application/json",
|
|
[]byte(`{"username":"e2e-u1","password":"secret66","role":"customer","business":3}`))
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("开户 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
for _, p := range []string{"http", "cmpp", "smpp", "web"} {
|
|
if w := doReqBody(env.r, http.MethodPut, "/api/v1/accounts/3/protocol", tok, "application/json",
|
|
[]byte(`{"business":"sms","protocol":"`+p+`"}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("协议 %s = %d body=%s", p, w.Code, w.Body.String())
|
|
}
|
|
}
|
|
// A-2.2 业务线独立生效(business=3 → sms+intl)
|
|
w = doReq(env.r, http.MethodGet, "/api/v1/accounts/3/business-status", tok)
|
|
if w.Code != http.StatusOK || !contains(w.Body.String(), `"sms":true`) || !contains(w.Body.String(), `"intl":true`) {
|
|
t.Fatalf("业务线状态 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// A-2.3 IP 白名单:非白名单 IP 拒绝
|
|
if w := doReqBody(env.r, http.MethodPut, "/api/v1/accounts/3/ip-bindings", tok, "application/json",
|
|
[]byte(`{"ips":["10.9.9.9"]}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("设置白名单 = %d", w.Code)
|
|
}
|
|
ok, err := env.acct.CheckIPAllowed(context.Background(), 3, "1.2.3.4")
|
|
if err != nil || ok {
|
|
t.Fatalf("非白名单 IP 应拒绝: ok=%v err=%v", ok, err)
|
|
}
|
|
// A-2.4 审核模式切换(0→2)
|
|
if w := doReqBody(env.r, http.MethodPut, "/api/v1/accounts/3/audit-mode", tok, "application/json",
|
|
[]byte(`{"business":"sms","mode":2}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("审核模式 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
_, cfgs, _ := env.acct.GetAccount(context.Background(), 3)
|
|
smsMode := int8(-1)
|
|
for _, c := range cfgs {
|
|
if c.Business == "sms" {
|
|
smsMode = c.AuditMode
|
|
}
|
|
}
|
|
if smsMode != 2 {
|
|
t.Fatalf("审核模式未生效: sms mode=%d cfgs=%+v", smsMode, cfgs)
|
|
}
|
|
}
|
|
|
|
// TestPhase2E2E_PermissionAndFunds A-2.5/2.6/2.8/2.9/2.10/2.11。
|
|
func TestPhase2E2E_PermissionAndFunds(t *testing.T) {
|
|
env := newE2E(t)
|
|
adminTok, _ := env.login(t, "e2e-admin", "secret66")
|
|
custTok, _ := env.login(t, "e2e-cust", "secret66")
|
|
|
|
// A-2.5 角色隔离:customer 访问审计(管理面)→ 403
|
|
if w := doReq(env.r, http.MethodGet, "/api/v1/audit/logs", custTok); w.Code != http.StatusForbidden {
|
|
t.Fatalf("角色隔离 = %d, 期望 403", w.Code)
|
|
}
|
|
// A-2.6 二级权限:无 recharge 充值 → 403;授予 → 200
|
|
if w := doReqBody(env.r, http.MethodPost, "/api/v1/accounts/2/recharge", custTok, "application/json",
|
|
[]byte(`{"amount":10}`)); w.Code != http.StatusForbidden {
|
|
t.Fatalf("无权限充值 = %d, 期望 403", w.Code)
|
|
}
|
|
if w := doReq(env.r, http.MethodPost, "/api/v1/accounts/2/permissions/recharge/grant", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("授权 = %d", w.Code)
|
|
}
|
|
if w := doReqBody(env.r, http.MethodPost, "/api/v1/accounts/2/recharge", custTok, "application/json",
|
|
[]byte(`{"amount":10}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("授权后充值 = %d", w.Code)
|
|
}
|
|
// A-2.8 admin 充值 100 → 余额 110(10+100)
|
|
if w := doReqBody(env.r, http.MethodPost, "/api/v1/accounts/2/recharge", adminTok, "application/json",
|
|
[]byte(`{"amount":100}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("admin 充值 = %d", w.Code)
|
|
}
|
|
w := doReq(env.r, http.MethodGet, "/api/v1/accounts/2/balance", custTok)
|
|
if !contains(w.Body.String(), `"balance":110`) {
|
|
t.Fatalf("余额应 110: %s", w.Body.String())
|
|
}
|
|
// A-2.9 预警配置就绪(阈值高于余额;触发链路见 day27 单测)
|
|
if w := doReqBody(env.r, http.MethodPut, "/api/v1/accounts/2/warning", custTok, "application/json",
|
|
[]byte(`{"threshold":500}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("预警配置 = %d", w.Code)
|
|
}
|
|
// A-2.10 删除限制:有发送记录不可删
|
|
if err := env.db.Create(&entity.MessageDetail{AccountID: 2, Mobile: "13800000000"}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := doReq(env.r, http.MethodDelete, "/api/v1/accounts/2", adminTok); !contains(w.Body.String(), "不可删除") {
|
|
t.Fatalf("删除守卫 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
// A-2.11 导出仅 admin
|
|
if w := doReq(env.r, http.MethodGet, "/api/v1/accounts/export", custTok); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 导出 = %d, 期望 403", w.Code)
|
|
}
|
|
if w := doReq(env.r, http.MethodGet, "/api/v1/accounts/export", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("admin 导出 = %d", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestPhase2E2E_RealnameAndAudit A-2.12/2.13 + 操作留痕。
|
|
func TestPhase2E2E_RealnameAndAudit(t *testing.T) {
|
|
env := newE2E(t)
|
|
adminTok, _ := env.login(t, "e2e-admin", "secret66")
|
|
// A-2.13 登录审计:失败+成功各留痕
|
|
_, _ = env.login(t, "e2e-cust", "wrong-pass")
|
|
_, _ = env.login(t, "e2e-cust", "secret66")
|
|
w := doReq(env.r, http.MethodGet, "/api/v1/audit/logins?username=e2e-cust", adminTok)
|
|
if w.Code != http.StatusOK || !contains(w.Body.String(), `"total":2`) {
|
|
t.Fatalf("登录审计 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
// A-2.12 实名流转:提交 → 通过 → 状态更新
|
|
custTok, _ := env.login(t, "e2e-cust", "secret66")
|
|
if w := doReqBody(env.r, http.MethodPost, "/api/v1/accounts/2/realname", custTok, "application/json",
|
|
[]byte(`{"real_name":"李四","id_card":"350203199001019999"}`)); w.Code != http.StatusOK {
|
|
t.Fatalf("实名提交 = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if w := doReqBody(env.r, http.MethodPost, "/api/v1/accounts/2/realname/review", adminTok, "application/json",
|
|
[]byte(`{"approve":true}`)); w.Code != http.StatusOK || !contains(w.Body.String(), `"status":1`) {
|
|
t.Fatalf("实名审核 = %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|