feat: 实现 Admin 首次初始化与网页登录 (#50)
This commit is contained in:
@@ -5,8 +5,10 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cmautobuy/admin/repository"
|
||||
"cmautobuy/admin/service"
|
||||
)
|
||||
|
||||
// 五个主页面都走一次真实路由和模板渲染。
|
||||
@@ -26,9 +28,31 @@ func TestMainPagesReturnOK(t *testing.T) {
|
||||
t.Fatalf("组装路由失败: %v", err)
|
||||
}
|
||||
|
||||
// 全新库的业务页必须先去初始化,不能匿名打开。
|
||||
unauthenticated := httptest.NewRequest(http.MethodGet, "/shopee", nil)
|
||||
unauthenticatedResponse := httptest.NewRecorder()
|
||||
router.ServeHTTP(unauthenticatedResponse, unauthenticated)
|
||||
if unauthenticatedResponse.Code != http.StatusSeeOther ||
|
||||
unauthenticatedResponse.Header().Get("Location") != "/setup" {
|
||||
t.Fatalf("全新库访问业务页应跳转 /setup,实际 %d %s",
|
||||
unauthenticatedResponse.Code, unauthenticatedResponse.Header().Get("Location"))
|
||||
}
|
||||
|
||||
if err := service.SetupInitialAdmin(db, "admin", "test-password", "test-password", time.Now()); err != nil {
|
||||
t.Fatalf("准备测试管理员失败: %v", err)
|
||||
}
|
||||
token, _, _, err := service.Login(db, "admin", "test-password", time.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("准备测试登录 Session 失败: %v", err)
|
||||
}
|
||||
addAuth := func(request *http.Request) {
|
||||
request.AddCookie(&http.Cookie{Name: "cmautobuy_session", Value: token})
|
||||
}
|
||||
|
||||
for _, path := range []string{"/shopee", "/pdd", "/syb", "/tasks", "/clients"} {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
addAuth(request)
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusOK {
|
||||
@@ -39,6 +63,7 @@ func TestMainPagesReturnOK(t *testing.T) {
|
||||
}
|
||||
|
||||
sybRequest := httptest.NewRequest(http.MethodGet, "/syb", nil)
|
||||
addAuth(sybRequest)
|
||||
sybResponse := httptest.NewRecorder()
|
||||
router.ServeHTTP(sybResponse, sybRequest)
|
||||
for _, want := range []string{
|
||||
@@ -72,6 +97,7 @@ func TestMainPagesReturnOK(t *testing.T) {
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/pdd/detail?id=1", nil)
|
||||
addAuth(request)
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusOK {
|
||||
|
||||
Reference in New Issue
Block a user