Files
go-sms-V2/internal/api/http/v1/account_recharge_permission_test.go
T

129 lines
5.6 KiB
Go

// account_recharge_permission_test.go(原 Day26)验收:充值二级权限门禁 / 批量 CSV / 余额越权。
// 复用 newD25Router(admin=1 通配,customer=2;Register 已含 Day 26 路由)。
package v1
import (
"bytes"
"net/http"
"strings"
"testing"
"github.com/gin-gonic/gin"
"net/http/httptest"
)
// doReqBody 带请求体的请求(POST JSON/CSV 原文)。
func doReqBody(r *gin.Engine, method, path, token, contentType string, body []byte) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
req := httptest.NewRequest(method, path, bytes.NewReader(body))
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
r.ServeHTTP(w, req)
return w
}
// TestAPI_RechargePermission Day 26 验收主线:
// customer 充值 → 403 ISC:403;admin → 200 余额正确;授予 recharge 后即时放行,撤销收回。
func TestAPI_RechargePermission(t *testing.T) {
r, _ := newD25Router(t)
adminTok := d25Token(t, 1, "admin")
custTok := d25Token(t, 2, "customer")
body := []byte(`{"amount":1000,"remark":"首充"}`)
// 未认证 → 401
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/1/recharge", "", "application/json", body); w.Code != http.StatusUnauthorized {
t.Fatalf("未认证充值 = %d, 期望 401", w.Code)
}
// customer → 403 ISC:403(验收锚点:无 recharge 二级权限被拦截)
w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/recharge", custTok, "application/json", body)
if w.Code != http.StatusForbidden || !strings.Contains(w.Body.String(), "ISC:403") {
t.Fatalf("customer 充值 = %d body=%s, 期望 403 ISC:403", w.Code, w.Body.String())
}
// admin → 200,余额正确累加
w = doReqBody(r, http.MethodPost, "/api/v1/accounts/2/recharge", adminTok, "application/json", body)
if w.Code != http.StatusOK {
t.Fatalf("admin 充值 = %d body=%s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), `"balance":1000`) {
t.Fatalf("充值响应应含 balance=1000: %s", w.Body.String())
}
// 非法金额 → 400
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/recharge", adminTok, "application/json", []byte(`{"amount":-1}`)); w.Code != http.StatusBadRequest {
t.Fatalf("负数金额 = %d, 期望 400", w.Code)
}
// 授予 customer recharge → 即时放行
if w := doReq(r, http.MethodPost, "/api/v1/accounts/2/permissions/recharge/grant", adminTok); w.Code != http.StatusOK {
t.Fatalf("授权 = %d body=%s", w.Code, w.Body.String())
}
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/recharge", custTok, "application/json", []byte(`{"amount":500}`)); w.Code != http.StatusOK {
t.Fatalf("授权后 customer 充值 = %d body=%s, 期望 200", w.Code, w.Body.String())
}
// 撤销 → 403
if w := doReq(r, http.MethodPost, "/api/v1/accounts/2/permissions/recharge/revoke", adminTok); w.Code != http.StatusOK {
t.Fatalf("撤销 = %d", w.Code)
}
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/2/recharge", custTok, "application/json", []byte(`{"amount":1}`)); w.Code != http.StatusForbidden {
t.Fatalf("撤销后充值 = %d, 期望 403", w.Code)
}
}
// TestAPI_RechargeBatch CSV 批量:multipart/原文皆可;行级结果;空内容 400。
func TestAPI_RechargeBatch(t *testing.T) {
r, _ := newD25Router(t)
adminTok := d25Token(t, 1, "admin")
custTok := d25Token(t, 2, "customer")
csvData := []byte("username,amount,remark\nd25-cust,300,批量\nghost,100,不存在\n")
// customer → 403
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/recharge/batch", custTok, "text/csv", csvData); w.Code != http.StatusForbidden {
t.Fatalf("customer 批量充值 = %d, 期望 403", w.Code)
}
// admin 原文上传 → 2 行:1 成功 1 失败(行级定位)
w := doReqBody(r, http.MethodPost, "/api/v1/accounts/recharge/batch", adminTok, "text/csv", csvData)
if w.Code != http.StatusOK {
t.Fatalf("admin 批量充值 = %d body=%s", w.Code, w.Body.String())
}
b := w.Body.String()
for _, want := range []string{`"total":2`, `"success":1`, `"failed":1`, "ghost", "不存在", `"line":2`} {
if !strings.Contains(b, want) {
t.Fatalf("批量响应缺 %q: %s", want, b)
}
}
// 余额落账校验(300)
if w := doReq(r, http.MethodGet, "/api/v1/accounts/2/balance", adminTok); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `"balance":300`) {
t.Fatalf("批量后余额查询 = %d body=%s, 期望 balance=300", w.Code, w.Body.String())
}
// 空内容 → 400
if w := doReqBody(r, http.MethodPost, "/api/v1/accounts/recharge/batch", adminTok, "text/csv", []byte(" ")); w.Code != http.StatusBadRequest {
t.Fatalf("空 CSV = %d, 期望 400", w.Code)
}
}
// TestAPI_BalanceAccessControl 余额查询:本人可查,越权 403,admin 全查。
func TestAPI_BalanceAccessControl(t *testing.T) {
r, _ := newD25Router(t)
adminTok := d25Token(t, 1, "admin")
custTok := d25Token(t, 2, "customer")
// customer 查本人 → 200
if w := doReq(r, http.MethodGet, "/api/v1/accounts/2/balance", custTok); w.Code != http.StatusOK {
t.Fatalf("本人余额 = %d, 期望 200", w.Code)
}
// customer 查 admin → 403
if w := doReq(r, http.MethodGet, "/api/v1/accounts/1/balance", custTok); w.Code != http.StatusForbidden {
t.Fatalf("越权查他人余额 = %d, 期望 403", w.Code)
}
// admin 查任意 → 200
if w := doReq(r, http.MethodGet, "/api/v1/accounts/2/balance", adminTok); w.Code != http.StatusOK {
t.Fatalf("admin 查余额 = %d, 期望 200", w.Code)
}
// 不存在账号 → 404(admin)
if w := doReq(r, http.MethodGet, "/api/v1/accounts/999/balance", adminTok); w.Code != http.StatusNotFound {
t.Fatalf("不存在账号余额 = %d, 期望 404", w.Code)
}
}