122 lines
4.3 KiB
Go
122 lines
4.3 KiB
Go
package web
|
||
|
||
import (
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"net/url"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
|
||
"cmautobuy/admin/model"
|
||
"cmautobuy/admin/service"
|
||
)
|
||
|
||
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) {
|
||
t.Run("蝦皮", func(t *testing.T) {
|
||
context, recorder := listPostContext(t, "/shopee/save", url.Values{
|
||
"q": {"店铺 A"}, "search_field": {"shop_name"}, "status": {"no_link"}, "unlinked": {"1"}, "page": {"2"},
|
||
})
|
||
(&Handler{}).shopeeRedirect(context, "完成")
|
||
assertRedirectQuery(t, recorder, "/shopee", map[string]string{
|
||
"q": "店铺 A", "search_field": "shop_name", "status": "no_link", "unlinked": "1", "page": "2", "msg": "完成",
|
||
})
|
||
})
|
||
t.Run("顺运宝", func(t *testing.T) {
|
||
context, recorder := listPostContext(t, "/syb/associate-pdd", url.Values{
|
||
"order_no": {"SYB-1"}, "shop": {"测试店铺"}, "stage": {"pdd_missing"}, "page": {"3"},
|
||
})
|
||
(&Handler{}).sybRedirect(context, "完成")
|
||
assertRedirectQuery(t, recorder, "/syb", map[string]string{
|
||
"order_no": "SYB-1", "shop": "测试店铺", "stage": "pdd_missing", "page": "3", "msg": "完成",
|
||
})
|
||
})
|
||
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{
|
||
"q": {"TASK"}, "type": {"collect"}, "status": {"pending"}, "creator": {"buyer-a"}, "page": {"2"},
|
||
})
|
||
context.Set(currentUserKey, &model.User{Role: model.RoleAdmin})
|
||
(&Handler{}).taskRedirect(context, "完成")
|
||
assertRedirectQuery(t, recorder, "/tasks", map[string]string{
|
||
"q": "TASK", "type": "collect", "status": "pending", "creator": "buyer-a", "page": "2", "msg": "完成",
|
||
})
|
||
})
|
||
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": "完成",
|
||
})
|
||
})
|
||
}
|
||
|
||
func Test蝦皮分页详情和状态条保留分类搜索(t *testing.T) {
|
||
values := url.Values{
|
||
"q": {"店铺 A"}, "search_field": {"shop_name"}, "status": {"has_link"}, "unlinked": {"1"},
|
||
}
|
||
detail, err := url.Parse("/shopee/detail?" + detailValuesForShopee(values, 3))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
for key, want := range map[string]string{
|
||
"q": "店铺 A", "search_field": "shop_name", "status": "has_link", "unlinked": "1", "page": "3",
|
||
} {
|
||
if got := detail.Query().Get(key); got != want {
|
||
t.Errorf("详情参数 %s=%q,期望 %q", key, got, want)
|
||
}
|
||
}
|
||
|
||
result := &service.ShopeeListResult{Total: 7, Page: 1, TotalPages: 1, IsFiltered: true}
|
||
if got := shopeeStatusLine("", result); got != "筛选结果:7 个商品 · 第 1/1 页" {
|
||
t.Errorf("资料筛选状态条=%q", got)
|
||
}
|
||
}
|