2026-08-09 21:51:20 +08:00
|
|
|
|
package web
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
|
"net/url"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-08-10 23:10:28 +08:00
|
|
|
|
|
|
|
|
|
|
"cmautobuy/admin/model"
|
2026-08-09 21:51:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func listPostContext(t *testing.T, path string, values url.Values) (*gin.Context, *httptest.ResponseRecorder) {
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
|
context, _ := gin.CreateTestContext(recorder)
|
|
|
|
|
|
request := httptest.NewRequest(http.MethodPost, path, strings.NewReader(values.Encode()))
|
|
|
|
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
context.Request = request
|
|
|
|
|
|
return context, recorder
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func assertRedirectQuery(t *testing.T, recorder *httptest.ResponseRecorder, wantPath string, want map[string]string) {
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
location, err := url.Parse(recorder.Header().Get("Location"))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if location.Path != wantPath {
|
|
|
|
|
|
t.Fatalf("跳转路径=%s,期望 %s", location.Path, wantPath)
|
|
|
|
|
|
}
|
|
|
|
|
|
for key, value := range want {
|
|
|
|
|
|
if got := location.Query().Get(key); got != value {
|
|
|
|
|
|
t.Fatalf("%s=%q,期望 %q;Location=%s", key, got, value, location.String())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Test列表写操作跳转保留筛选和页码(t *testing.T) {
|
2026-08-09 22:40:58 +08:00
|
|
|
|
t.Run("蝦皮", func(t *testing.T) {
|
|
|
|
|
|
context, recorder := listPostContext(t, "/shopee/save", url.Values{
|
|
|
|
|
|
"list_goods_id": {"1001"}, "status": {"no_link"}, "page": {"2"},
|
|
|
|
|
|
})
|
|
|
|
|
|
(&Handler{}).shopeeRedirect(context, "完成")
|
|
|
|
|
|
assertRedirectQuery(t, recorder, "/shopee", map[string]string{
|
|
|
|
|
|
"goods_id": "1001", "status": "no_link", "page": "2", "msg": "完成",
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
t.Run("顺运宝", func(t *testing.T) {
|
|
|
|
|
|
context, recorder := listPostContext(t, "/syb/associate-pdd", url.Values{
|
|
|
|
|
|
"order_no": {"SYB-1"}, "stage": {"pdd_missing"}, "page": {"3"},
|
|
|
|
|
|
})
|
|
|
|
|
|
(&Handler{}).sybRedirect(context, "完成")
|
|
|
|
|
|
assertRedirectQuery(t, recorder, "/syb", map[string]string{
|
|
|
|
|
|
"order_no": "SYB-1", "stage": "pdd_missing", "page": "3", "msg": "完成",
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
2026-08-09 21:51:20 +08:00
|
|
|
|
t.Run("PDD", func(t *testing.T) {
|
|
|
|
|
|
context, recorder := listPostContext(t, "/pdd/delete", url.Values{
|
|
|
|
|
|
"q": {"737"}, "status": {"pending"}, "page": {"3"},
|
|
|
|
|
|
})
|
|
|
|
|
|
(&Handler{}).pddRedirect(context, "完成")
|
|
|
|
|
|
assertRedirectQuery(t, recorder, "/pdd", map[string]string{
|
|
|
|
|
|
"q": "737", "status": "pending", "page": "3", "msg": "完成",
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
t.Run("任务", func(t *testing.T) {
|
|
|
|
|
|
context, recorder := listPostContext(t, "/tasks/delete", url.Values{
|
2026-08-10 23:10:28 +08:00
|
|
|
|
"q": {"TASK"}, "type": {"collect"}, "status": {"pending"}, "creator": {"buyer-a"}, "page": {"2"},
|
2026-08-09 21:51:20 +08:00
|
|
|
|
})
|
2026-08-10 23:10:28 +08:00
|
|
|
|
context.Set(currentUserKey, &model.User{Role: model.RoleAdmin})
|
2026-08-09 21:51:20 +08:00
|
|
|
|
(&Handler{}).taskRedirect(context, "完成")
|
|
|
|
|
|
assertRedirectQuery(t, recorder, "/tasks", map[string]string{
|
2026-08-10 23:10:28 +08:00
|
|
|
|
"q": "TASK", "type": "collect", "status": "pending", "creator": "buyer-a", "page": "2", "msg": "完成",
|
2026-08-09 21:51:20 +08:00
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
t.Run("客户端", func(t *testing.T) {
|
|
|
|
|
|
context, recorder := listPostContext(t, "/clients/assign", url.Values{
|
|
|
|
|
|
"name": {"仓库"}, "page": {"4"},
|
|
|
|
|
|
})
|
|
|
|
|
|
redirectClients(context, "完成", "")
|
|
|
|
|
|
assertRedirectQuery(t, recorder, "/clients", map[string]string{
|
|
|
|
|
|
"name": "仓库", "page": "4", "msg": "完成",
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
t.Run("用户", func(t *testing.T) {
|
|
|
|
|
|
context, recorder := listPostContext(t, "/users/status", url.Values{
|
|
|
|
|
|
"q": {"buyer"}, "list_status": {"active"}, "page": {"5"},
|
|
|
|
|
|
})
|
|
|
|
|
|
redirectUsers(context, "完成", "")
|
|
|
|
|
|
assertRedirectQuery(t, recorder, "/users", map[string]string{
|
|
|
|
|
|
"q": "buyer", "status": "active", "page": "5", "msg": "完成",
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|