package server import ( "net/http" "net/http/httptest" "net/url" "path/filepath" "strings" "testing" "gitcat/internal/gitx" "gitcat/internal/store" ) // stubGitAvailable 临时替换 git 探测结果,模拟「服务器没装 git」。 func stubGitAvailable(t *testing.T, ok bool) { t.Helper() old := gitAvailable gitAvailable = func() bool { return ok } t.Cleanup(func() { gitAvailable = old }) } // stubGitCapability 临时替换一键安装能力探测,模拟 root / 非 root 环境。 func stubGitCapability(t *testing.T, can bool, reason string) { t.Helper() old := gitInstallCapability gitInstallCapability = func() gitx.InstallCapability { return gitx.InstallCapability{CanInstall: can, Reason: reason} } t.Cleanup(func() { gitInstallCapability = old }) } // newFreshServer 起一个尚未完成安装的测试服务。 func newFreshServer(t *testing.T) (*Server, *store.Store) { t.Helper() dir := t.TempDir() st, err := store.Open(filepath.Join(dir, "gitcat.db")) if err != nil { t.Fatalf("打开数据库失败: %v", err) } t.Cleanup(func() { st.Close() }) srv, err := New(Config{Addr: ":0", DataDir: dir, Version: "1.0.0-test"}, st) if err != nil { t.Fatalf("构造服务失败: %v", err) } return srv, st } func postJSON(srv *Server, path string) *httptest.ResponseRecorder { r := httptest.NewRequest(http.MethodPost, path, nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) return w } func postForm(srv *Server, path string, form url.Values) *httptest.ResponseRecorder { r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(form.Encode())) r.Header.Set("Content-Type", "application/x-www-form-urlencoded") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) return w } // TestInstallWizardWelcome:/install 归位到欢迎页,欢迎页含标题与开始按钮。 func TestInstallWizardWelcome(t *testing.T) { srv, _ := newFreshServer(t) r := httptest.NewRequest(http.MethodGet, "/install", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/welcome" { t.Fatalf("/install 应重定向到 /install/welcome,实际 %d → %s", w.Code, w.Header().Get("Location")) } page := getPage(srv, "/install/welcome") if !strings.Contains(page, "欢迎安装 Gitcat") || !strings.Contains(page, "简单几步") { t.Error("欢迎页缺少标题/副标题") } if !strings.Contains(page, "立即开始!") { t.Error("欢迎页缺少开始按钮") } if !strings.Contains(page, "环境检测") { t.Error("欢迎页应显示步骤指示器") } // 未知步骤由 mux 返回 404(步骤路由全部精确注册) r = httptest.NewRequest(http.MethodGet, "/install/bogus", nil) w = httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusNotFound { t.Fatalf("未知步骤应 404,实际 %d", w.Code) } } // TestInstallWizardGitStep:git 已装则自动跳过;未装时显示提醒与按钮。 func TestInstallWizardGitStep(t *testing.T) { srv, _ := newFreshServer(t) // 已装:跳过 stubGitAvailable(t, true) r := httptest.NewRequest(http.MethodGet, "/install/git", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/site" { t.Fatalf("git 已装时应跳过本步,实际 %d → %s", w.Code, w.Header().Get("Location")) } // 未装 + 可一键安装 stubGitAvailable(t, false) stubGitCapability(t, true, "") page := getPage(srv, "/install/git") if !strings.Contains(page, "服务器暂未安装 git") || !strings.Contains(page, "一键安装 Git") { t.Error("git 步骤缺少提醒/一键安装按钮") } if !strings.Contains(page, `class="btn btn-primary btn-lg btn-block wizard-next disabled"`) { t.Error("git 未就绪时「下一步」应禁用") } // 未装 + 非 root:显示换 root 提醒,不显示按钮 stubGitCapability(t, false, "gitcat 当前以非 root 用户运行,无法自动安装系统软件。请以 root 身份重新运行 gitcat 后刷新本页") page = getPage(srv, "/install/git") if !strings.Contains(page, "以 root 身份重新运行 gitcat") { t.Error("非 root 环境应提醒以 root 身份运行") } if strings.Contains(page, `id="git-install"`) { t.Error("无安装能力时不应渲染一键安装按钮") } } // TestInstallWizardSiteStep:站点名称分步保存,校验失败回显表单。 func TestInstallWizardSiteStep(t *testing.T) { srv, st := newFreshServer(t) page := getPage(srv, "/install/site") if !strings.Contains(page, "站点信息") || !strings.Contains(page, "创意在线,协作无限") { t.Error("站点步骤缺少表单/彩色标语") } // 空名称拒绝 w := postForm(srv, "/install/site", url.Values{"site_name": {" "}}) if w.Code != http.StatusBadRequest { t.Fatalf("空站点名应 400,实际 %d", w.Code) } // 超长拒绝 w = postForm(srv, "/install/site", url.Values{"site_name": {strings.Repeat("长", 61)}}) if w.Code != http.StatusBadRequest { t.Fatalf("超长站点名应 400,实际 %d", w.Code) } // 合法:落库并进入下一步 w = postForm(srv, "/install/site", url.Values{"site_name": {"测试站"}}) if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/admin" { t.Fatalf("合法站点名应 302 到 /install/admin,实际 %d → %s", w.Code, w.Header().Get("Location")) } if got := st.GetSetting("site_name"); got != "测试站" { t.Errorf("站点名称未落库,得到 %q", got) } // 重新进入 site 步时回显 if page := getPage(srv, "/install/site"); !strings.Contains(page, `value="测试站"`) { t.Error("重新进入站点步骤应回显已保存名称") } } // TestInstallWizardAdminStep:缺 git 拒绝;成功创建管理员、写 installed、登录并跳完成页。 func TestInstallWizardAdminStep(t *testing.T) { srv, st := newFreshServer(t) page := getPage(srv, "/install/admin") if !strings.Contains(page, "管理员账号") || !strings.Contains(page, "AI加持,效率up+") { t.Error("管理员步骤缺少表单/彩色标语") } form := url.Values{ "username": {"admin"}, "password": {"secret123"}, "password2": {"secret123"}, } // 缺 git 时拒绝(绕过前端直接提交) stubGitAvailable(t, false) stubGitCapability(t, true, "") w := postForm(srv, "/install/admin", form) if w.Code != http.StatusBadRequest { t.Fatalf("缺 git 提交应 400,实际 %d", w.Code) } if _, err := st.UserByUsername("admin"); err == nil { t.Error("缺 git 时不应创建管理员账号") } // git 就绪:安装成功 stubGitAvailable(t, true) w = postForm(srv, "/install/admin", form) if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/done" { t.Fatalf("安装成功应 302 到 /install/done,实际 %d → %s", w.Code, w.Header().Get("Location")) } if !st.Installed() { t.Error("安装成功应写入 installed 标记") } if u, err := st.UserByUsername("admin"); err != nil || !u.IsAdmin { t.Error("安装成功应创建管理员账号") } if st.GetSetting("site_name") == "" { // admin 步不应覆盖 site 步的结果,也不负责写站点名 t.Log("site_name 为空(未经过 site 步的正常情况)") } // 完成页可访问,且包含「安装完成」与「立即体验」 page = getPage(srv, "/install/done") if !strings.Contains(page, "安装完成!") || !strings.Contains(page, "立即体验") { t.Error("完成页缺少文案/按钮") } // 已安装后再访问其它步骤 → 回首页 for _, path := range []string{"/install", "/install/welcome", "/install/git", "/install/site", "/install/admin"} { r := httptest.NewRequest(http.MethodGet, path, nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusFound || w.Header().Get("Location") != "/" { t.Errorf("已安装后访问 %s 应回首页,实际 %d → %s", path, w.Code, w.Header().Get("Location")) } } } // TestInstallWizardDoneGuard:未安装时不能直接访问完成页(防跳步)。 func TestInstallWizardDoneGuard(t *testing.T) { srv, _ := newFreshServer(t) r := httptest.NewRequest(http.MethodGet, "/install/done", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/welcome" { t.Fatalf("未安装访问 done 应归位欢迎页,实际 %d → %s", w.Code, w.Header().Get("Location")) } } // TestInstallSubmitBlockedWithoutGit:缺 git 时即使绕过前端禁用直接 // POST 表单,服务端也必须拒绝完成安装。 func TestInstallSubmitBlockedWithoutGit(t *testing.T) { srv, st := newFreshServer(t) stubGitAvailable(t, false) stubGitCapability(t, true, "") form := url.Values{ "username": {"admin"}, "password": {"secret123"}, "password2": {"secret123"}, } w := postForm(srv, "/install/admin", form) if w.Code != http.StatusBadRequest { t.Fatalf("缺 git 提交安装应返回 400,实际 %d", w.Code) } if !strings.Contains(w.Body.String(), "git") { t.Error("错误提示应说明需要先完成 git 安装") } if _, err := st.UserByUsername("admin"); err == nil { t.Error("缺 git 时不应创建管理员账号") } if st.Installed() { t.Error("缺 git 时不应写入 installed 标记") } } // TestInstallGitEndpoint:git 已存在时接口幂等返回成功与版本号; // 未装 git 时接口返回失败 JSON 并保留原因(CI 环境不真正装包)。 func TestInstallGitEndpoint(t *testing.T) { srv, st := newFreshServer(t) // git 就绪:EnsureInstalled 直接短路成功,不会触发真实安装 w := postJSON(srv, "/install/git") if w.Code != http.StatusOK { t.Fatalf("git 就绪时应返回 200,实际 %d:%s", w.Code, w.Body.String()) } if !strings.Contains(w.Body.String(), `"ok":true`) { t.Errorf("响应缺少 ok:true:%s", w.Body.String()) } if !strings.Contains(w.Body.String(), "git version") { t.Errorf("响应应包含 git 版本号:%s", w.Body.String()) } if st.Installed() { t.Error("一键装 git 不应代替用户完成站点安装") } // 已完成安装的站点必须拒绝再调用 st.SetSetting("installed", "1") w = postJSON(srv, "/install/git") if w.Code != http.StatusForbidden { t.Fatalf("已安装站点调用应返回 403,实际 %d", w.Code) } } // TestInstallGitForbiddenWithoutCapability:非 root 且无免密 sudo 的 // 环境(如 www 用户)下,接口直接拒绝并返回原因,不会白跑安装命令。 func TestInstallGitForbiddenWithoutCapability(t *testing.T) { srv, _ := newFreshServer(t) stubGitAvailable(t, false) stubGitCapability(t, false, "gitcat 当前以非 root 用户运行,无法自动安装系统软件。请以 root 身份重新运行 gitcat 后刷新本页") w := postJSON(srv, "/install/git") if w.Code != http.StatusForbidden { t.Fatalf("无安装能力时应返回 403,实际 %d", w.Code) } if !strings.Contains(w.Body.String(), "以 root 身份") { t.Errorf("403 响应应包含换 root 运行的引导:%s", w.Body.String()) } } // TestInstallPageRedirectsAfterInstall:完成安装后安装入口不再可达。 func TestInstallPageRedirectsAfterInstall(t *testing.T) { srv, _, _, _ := newTestServer(t) // newTestServer 写入了 installed=1 r := httptest.NewRequest(http.MethodGet, "/install", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusFound { t.Fatalf("已安装站点访问 /install 应重定向,实际 %d", w.Code) } }