automation 重构期间的配套修改: - browser:订单抓取/密码登录/二维码刷新/stealth 等适配新流程,删 search.go - db:cookies/orders_ext/extras/sessions/users/password/repository 等调整 - xianyu:mtop client、protocol(sign/decrypt/msgpack/cookies)、qrlogin、ws、ua - server:admin/analytics/auth/cookie/helpers/item/item_publish_batch handlers - frontend:App/Dashboard/ItemList/Rules/Sidebar/api/types,删 Keywords/TestPage - auth、notify、webui、go.mod 等 本次会话补充: - cmd/server、cmd/init-admin 改为 // Package main 开头的标准包文档 - .gitignore 加 internal/webui/static/assets/(hash 化构建产物不入库) - internal/webui/static/index.html 重新构建 全量 go build/vet/test + race + 前端 test/build 通过。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestProtectedRouteGroupsRequireAuthentication(t *testing.T) {
|
|
srv, _, cleanup := newTestServer(t)
|
|
defer cleanup()
|
|
h := srv.Router()
|
|
routes := []struct {
|
|
method string
|
|
path string
|
|
}{
|
|
{http.MethodGet, "/cookies"},
|
|
{http.MethodGet, "/api/orders"},
|
|
{http.MethodGet, "/analytics/orders"},
|
|
{http.MethodGet, "/cards"},
|
|
{http.MethodGet, "/items"},
|
|
{http.MethodGet, "/keywords/acc1"},
|
|
{http.MethodGet, "/default-replies/acc1"},
|
|
{http.MethodGet, "/notification-channels"},
|
|
{http.MethodGet, "/system-settings"},
|
|
{http.MethodGet, "/ai-reply-settings"},
|
|
{http.MethodGet, "/user-settings"},
|
|
{http.MethodGet, "/admin/stats"},
|
|
}
|
|
for _, route := range routes {
|
|
t.Run(route.method+" "+route.path, func(t *testing.T) {
|
|
req := httptest.NewRequest(route.method, route.path, nil)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCookiePreferenceEndpoints(t *testing.T) {
|
|
srv, _, cleanup := newTestServer(t)
|
|
defer cleanup()
|
|
h := srv.Router()
|
|
cookie := loginHelper(t, h)
|
|
|
|
requests := []struct {
|
|
path string
|
|
body string
|
|
}{
|
|
{"/cookies/acc1/auto-confirm", `{"auto_confirm":true}`},
|
|
{"/cookies/acc1/remark", `{"remark":"primary"}`},
|
|
{"/cookies/acc1/pause-duration", `{"pause_duration":30}`},
|
|
}
|
|
for _, tc := range requests {
|
|
req := httptest.NewRequest(http.MethodPut, tc.path, strings.NewReader(tc.body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.AddCookie(cookie)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("PUT %s status=%d body=%s", tc.path, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
for _, path := range []string{"/cookies/acc1/auto-confirm", "/cookies/acc1/pause-duration", "/cookie/acc1/details"} {
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
req.AddCookie(cookie)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GET %s status=%d body=%s", path, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodPut, "/cookies/acc1/pause-duration", strings.NewReader(`{"pause_duration":-1}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.AddCookie(cookie)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("negative pause status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|