146 lines
4.9 KiB
Go
146 lines
4.9 KiB
Go
// account_day25_test.go Day 25 验收:普通账号访问 /accounts/export 返回 403,
|
|
// admin 可正常导出;二级权限授予后即时放行、撤销后收回。
|
|
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
const d25Secret = "d25-api-secret"
|
|
|
|
// newD25Router admin(1)+customer(2)双账号 + Casbin 门禁路由。
|
|
func newD25Router(t *testing.T) (*gin.Engine, *casbin.Enforcer) {
|
|
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{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e, err := casbin.New(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range [][]string{
|
|
{"admin", "*", "*"},
|
|
{"ops", "account", "write"},
|
|
{"customer", "message", "write"},
|
|
} {
|
|
if _, err := e.AddPolicy(p[0], p[1], p[2]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rows := []entity.AccountUser{
|
|
{Username: "d25-admin", Role: "admin", Status: 1},
|
|
{Username: "d25-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)
|
|
|
|
r := gin.New()
|
|
Register(r.Group("/api/v1"), Deps{
|
|
Account: acctSvc,
|
|
Warning: account.NewWarningService(db, nil), // Day 27:预警配置端点
|
|
Auth: middleware.AuthJWTWithChecker(d25Secret, acctSvc.CheckEnabled),
|
|
Perm: middleware.CasbinChecker(e),
|
|
})
|
|
return r, e
|
|
}
|
|
|
|
func d25Token(t *testing.T, id int64, role string) string {
|
|
t.Helper()
|
|
tok, err := middleware.SignAccessToken(d25Secret, id, role+"-u", role, time.Minute)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return tok
|
|
}
|
|
|
|
// TestAPI_ExportPermission Day 25 验收主线。
|
|
func TestAPI_ExportPermission(t *testing.T) {
|
|
r, _ := newD25Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
custTok := d25Token(t, 2, "customer")
|
|
|
|
// 未认证 → 401
|
|
if w := doReq(r, http.MethodGet, "/api/v1/accounts/export", ""); w.Code != http.StatusUnauthorized {
|
|
t.Fatalf("未认证导出 = %d, 期望 401", w.Code)
|
|
}
|
|
// 普通账号(customer)→ 403 ISC:403
|
|
w := doReq(r, http.MethodGet, "/api/v1/accounts/export", custTok)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("普通账号导出 = %d, 期望 403, body=%s", w.Code, w.Body.String())
|
|
}
|
|
if !contains(w.Body.String(), "ISC:403") {
|
|
t.Fatalf("403 响应应含 ISC:403: %s", w.Body.String())
|
|
}
|
|
// admin → 200 + CSV 内容正确
|
|
w = doReq(r, http.MethodGet, "/api/v1/accounts/export", adminTok)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("admin 导出 = %d, body=%s", w.Code, w.Body.String())
|
|
}
|
|
if ct := w.Header().Get("Content-Type"); ct != "text/csv; charset=utf-8" {
|
|
t.Fatalf("Content-Type = %q", ct)
|
|
}
|
|
body := w.Body.String()
|
|
if !contains(body, "d25-admin") || !contains(body, "d25-cust") {
|
|
t.Fatalf("CSV 缺少账号行: %s", body)
|
|
}
|
|
}
|
|
|
|
// TestAPI_GrantRevokeExport 二级权限授予 → 即时放行;撤销 → 收回;非法标记 400。
|
|
func TestAPI_GrantRevokeExport(t *testing.T) {
|
|
r, _ := newD25Router(t)
|
|
adminTok := d25Token(t, 1, "admin")
|
|
custTok := d25Token(t, 2, "customer")
|
|
|
|
// admin 授予 customer export 权限(account:write 门禁下 admin 通配)
|
|
if w := doReq(r, http.MethodPost, "/api/v1/accounts/2/permissions/export/grant", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("授权 = %d, body=%s", w.Code, w.Body.String())
|
|
}
|
|
// 权限列表可见
|
|
w := doReq(r, http.MethodGet, "/api/v1/accounts/2/permissions", adminTok)
|
|
if w.Code != http.StatusOK || !contains(w.Body.String(), "account:export") {
|
|
t.Fatalf("权限列表 = %d, body=%s", w.Code, w.Body.String())
|
|
}
|
|
// customer 导出即时放行
|
|
if w := doReq(r, http.MethodGet, "/api/v1/accounts/export", custTok); w.Code != http.StatusOK {
|
|
t.Fatalf("授权后导出 = %d, 期望 200", w.Code)
|
|
}
|
|
// 撤销 → 403
|
|
if w := doReq(r, http.MethodPost, "/api/v1/accounts/2/permissions/export/revoke", adminTok); w.Code != http.StatusOK {
|
|
t.Fatalf("撤销 = %d, body=%s", w.Code, w.Body.String())
|
|
}
|
|
if w := doReq(r, http.MethodGet, "/api/v1/accounts/export", custTok); w.Code != http.StatusForbidden {
|
|
t.Fatalf("撤销后导出 = %d, 期望 403", w.Code)
|
|
}
|
|
// 非法二级权限标记 → 400
|
|
if w := doReq(r, http.MethodPost, "/api/v1/accounts/2/permissions/hackall/grant", adminTok); w.Code != http.StatusBadRequest {
|
|
t.Fatalf("非法标记 = %d, 期望 400", w.Code)
|
|
}
|
|
// customer 无 account:write → 不能给别人授权(403)
|
|
if w := doReq(r, http.MethodPost, "/api/v1/accounts/2/permissions/export/grant", custTok); w.Code != http.StatusForbidden {
|
|
t.Fatalf("customer 越权授权 = %d, 期望 403", w.Code)
|
|
}
|
|
}
|