修复(#71): 管理员订阅限速接口拒绝负数限速和非法 traffic_limit
- SpeedLimit 加 validate:"gte=0" 校验 - TrafficLimit 加 JSON 格式校验 - 新增单测覆盖负数限速和非法 JSON 场景 Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
+1
-1
@@ -149,7 +149,7 @@ type (
|
|||||||
ExpiredAt int64 `json:"expired_at"`
|
ExpiredAt int64 `json:"expired_at"`
|
||||||
Upload int64 `json:"upload"`
|
Upload int64 `json:"upload"`
|
||||||
Download int64 `json:"download"`
|
Download int64 `json:"download"`
|
||||||
SpeedLimit *int64 `json:"speed_limit,omitempty"`
|
SpeedLimit *int64 `json:"speed_limit,omitempty" validate:"omitempty,gte=0"`
|
||||||
TrafficLimit *string `json:"traffic_limit,omitempty"`
|
TrafficLimit *string `json:"traffic_limit,omitempty"`
|
||||||
}
|
}
|
||||||
GetUserLoginLogsRequest {
|
GetUserLoginLogsRequest {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/perfect-panel/server/internal/logic/admin/user"
|
"github.com/perfect-panel/server/internal/logic/admin/user"
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
"github.com/perfect-panel/server/internal/svc"
|
||||||
@@ -18,9 +21,25 @@ func UpdateUserSubscribeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context)
|
|||||||
result.ParamErrorResult(c, validateErr)
|
result.ParamErrorResult(c, validateErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if err := validateUpdateUserSubscribeTrafficLimit(&req); err != nil {
|
||||||
|
result.ParamErrorResult(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
l := user.NewUpdateUserSubscribeLogic(c.Request.Context(), svcCtx)
|
l := user.NewUpdateUserSubscribeLogic(c.Request.Context(), svcCtx)
|
||||||
err := l.UpdateUserSubscribe(&req)
|
err := l.UpdateUserSubscribe(&req)
|
||||||
result.HttpResult(c, nil, err)
|
result.HttpResult(c, nil, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateUpdateUserSubscribeTrafficLimit(req *types.UpdateUserSubscribeRequest) error {
|
||||||
|
if req.TrafficLimit == nil || *req.TrafficLimit == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var rules []types.TrafficLimit
|
||||||
|
if err := json.Unmarshal([]byte(*req.TrafficLimit), &rules); err != nil {
|
||||||
|
return errors.New("traffic_limit must be a valid JSON array")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/perfect-panel/server/internal/svc"
|
||||||
|
"github.com/perfect-panel/server/pkg/xerr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUpdateUserSubscribeHandlerRejectsInvalidLimits(t *testing.T) {
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
body string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "negative speed limit",
|
||||||
|
body: `{"user_subscribe_id":1,"subscribe_id":1,"traffic":0,"expired_at":4102444800000,"upload":0,"download":0,"speed_limit":-1}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid traffic limit json",
|
||||||
|
body: `{"user_subscribe_id":1,"subscribe_id":1,"traffic":0,"expired_at":4102444800000,"upload":0,"download":0,"traffic_limit":"not-json"}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
router := gin.New()
|
||||||
|
router.PUT("/v1/admin/user/subscribe", UpdateUserSubscribeHandler(&svc.ServiceContext{}))
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPut, "/v1/admin/user/subscribe", bytes.NewBufferString(tt.body))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
|
router.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Fatalf("expected HTTP 200, got %d", rec.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp struct {
|
||||||
|
Code uint32 `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||||
|
t.Fatalf("unmarshal response: %v", err)
|
||||||
|
}
|
||||||
|
if resp.Code != xerr.InvalidParams {
|
||||||
|
t.Fatalf("expected code %d, got %d (%s)", xerr.InvalidParams, resp.Code, resp.Msg)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,22 +39,32 @@ func (l *UpdateUserSubscribeLogic) UpdateUserSubscribe(req *types.UpdateUserSubs
|
|||||||
} else {
|
} else {
|
||||||
userSub.Status = 1
|
userSub.Status = 1
|
||||||
}
|
}
|
||||||
|
speedLimit := userSub.SpeedLimit
|
||||||
|
if req.SpeedLimit != nil {
|
||||||
|
speedLimit = *req.SpeedLimit
|
||||||
|
}
|
||||||
|
trafficLimit := userSub.TrafficLimit
|
||||||
|
if req.TrafficLimit != nil {
|
||||||
|
trafficLimit = *req.TrafficLimit
|
||||||
|
}
|
||||||
|
|
||||||
err = l.svcCtx.UserModel.UpdateSubscribe(l.ctx, &user.Subscribe{
|
err = l.svcCtx.UserModel.UpdateSubscribe(l.ctx, &user.Subscribe{
|
||||||
Id: userSub.Id,
|
Id: userSub.Id,
|
||||||
UserId: userSub.UserId,
|
UserId: userSub.UserId,
|
||||||
OrderId: userSub.OrderId,
|
OrderId: userSub.OrderId,
|
||||||
SubscribeId: req.SubscribeId,
|
SubscribeId: req.SubscribeId,
|
||||||
StartTime: userSub.StartTime,
|
StartTime: userSub.StartTime,
|
||||||
ExpireTime: time.UnixMilli(req.ExpiredAt),
|
ExpireTime: time.UnixMilli(req.ExpiredAt),
|
||||||
Traffic: req.Traffic,
|
Traffic: req.Traffic,
|
||||||
Download: req.Download,
|
Download: req.Download,
|
||||||
Upload: req.Upload,
|
Upload: req.Upload,
|
||||||
Token: userSub.Token,
|
SpeedLimit: speedLimit,
|
||||||
UUID: userSub.UUID,
|
TrafficLimit: trafficLimit,
|
||||||
Status: userSub.Status,
|
Token: userSub.Token,
|
||||||
NodeGroupId: userSub.NodeGroupId,
|
UUID: userSub.UUID,
|
||||||
GroupLocked: userSub.GroupLocked,
|
Status: userSub.Status,
|
||||||
|
NodeGroupId: userSub.NodeGroupId,
|
||||||
|
GroupLocked: userSub.GroupLocked,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3442,7 +3442,7 @@ type UpdateUserSubscribeRequest struct {
|
|||||||
ExpiredAt int64 `json:"expired_at"`
|
ExpiredAt int64 `json:"expired_at"`
|
||||||
Upload int64 `json:"upload"`
|
Upload int64 `json:"upload"`
|
||||||
Download int64 `json:"download"`
|
Download int64 `json:"download"`
|
||||||
SpeedLimit *int64 `json:"speed_limit,omitempty"`
|
SpeedLimit *int64 `json:"speed_limit,omitempty" validate:"omitempty,gte=0"`
|
||||||
TrafficLimit *string `json:"traffic_limit,omitempty"`
|
TrafficLimit *string `json:"traffic_limit,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user