gitcat
1package server
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "gitcat/internal/gitx"
12 "gitcat/internal/store"
13)
14
15// stubGitAvailable 临时替换 git 探测结果,模拟「服务器没装 git」。
16func stubGitAvailable(t *testing.T, ok bool) {
17 t.Helper()
18 old := gitAvailable
19 gitAvailable = func() bool { return ok }
20 t.Cleanup(func() { gitAvailable = old })
21}
22
23// stubGitCapability 临时替换一键安装能力探测,模拟 root / 非 root 环境。
24func stubGitCapability(t *testing.T, can bool, reason string) {
25 t.Helper()
26 old := gitInstallCapability
27 gitInstallCapability = func() gitx.InstallCapability {
28 return gitx.InstallCapability{CanInstall: can, Reason: reason}
29 }
30 t.Cleanup(func() { gitInstallCapability = old })
31}
32
33// newFreshServer 起一个尚未完成安装的测试服务。
34func newFreshServer(t *testing.T) (*Server, *store.Store) {
35 t.Helper()
36 dir := t.TempDir()
37 st, err := store.Open(filepath.Join(dir, "gitcat.db"))
38 if err != nil {
39 t.Fatalf("打开数据库失败: %v", err)
40 }
41 t.Cleanup(func() { st.Close() })
42 srv, err := New(Config{Addr: ":0", DataDir: dir, Version: "1.0.0-test"}, st)
43 if err != nil {
44 t.Fatalf("构造服务失败: %v", err)
45 }
46 return srv, st
47}
48
49func postJSON(srv *Server, path string) *httptest.ResponseRecorder {
50 r := httptest.NewRequest(http.MethodPost, path, nil)
51 w := httptest.NewRecorder()
52 srv.Handler().ServeHTTP(w, r)
53 return w
54}
55
56func postForm(srv *Server, path string, form url.Values) *httptest.ResponseRecorder {
57 r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(form.Encode()))
58 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
59 w := httptest.NewRecorder()
60 srv.Handler().ServeHTTP(w, r)
61 return w
62}
63
64// TestInstallWizardWelcome:/install 归位到欢迎页,欢迎页含标题与开始按钮。
65func TestInstallWizardWelcome(t *testing.T) {
66 srv, _ := newFreshServer(t)
67
68 r := httptest.NewRequest(http.MethodGet, "/install", nil)
69 w := httptest.NewRecorder()
70 srv.Handler().ServeHTTP(w, r)
71 if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/welcome" {
72 t.Fatalf("/install 应重定向到 /install/welcome,实际 %d → %s", w.Code, w.Header().Get("Location"))
73 }
74
75 page := getPage(srv, "/install/welcome")
76 if !strings.Contains(page, "欢迎安装 Gitcat") || !strings.Contains(page, "简单几步") {
77 t.Error("欢迎页缺少标题/副标题")
78 }
79 if !strings.Contains(page, "立即开始!") {
80 t.Error("欢迎页缺少开始按钮")
81 }
82 if !strings.Contains(page, "环境检测") {
83 t.Error("欢迎页应显示步骤指示器")
84 }
85
86 // 未知步骤由 mux 返回 404(步骤路由全部精确注册)
87 r = httptest.NewRequest(http.MethodGet, "/install/bogus", nil)
88 w = httptest.NewRecorder()
89 srv.Handler().ServeHTTP(w, r)
90 if w.Code != http.StatusNotFound {
91 t.Fatalf("未知步骤应 404,实际 %d", w.Code)
92 }
93}
94
95// TestInstallWizardGitStep:git 已装则自动跳过;未装时显示提醒与按钮。
96func TestInstallWizardGitStep(t *testing.T) {
97 srv, _ := newFreshServer(t)
98
99 // 已装:跳过
100 stubGitAvailable(t, true)
101 r := httptest.NewRequest(http.MethodGet, "/install/git", nil)
102 w := httptest.NewRecorder()
103 srv.Handler().ServeHTTP(w, r)
104 if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/site" {
105 t.Fatalf("git 已装时应跳过本步,实际 %d → %s", w.Code, w.Header().Get("Location"))
106 }
107
108 // 未装 + 可一键安装
109 stubGitAvailable(t, false)
110 stubGitCapability(t, true, "")
111 page := getPage(srv, "/install/git")
112 if !strings.Contains(page, "服务器暂未安装 git") || !strings.Contains(page, "一键安装 Git") {
113 t.Error("git 步骤缺少提醒/一键安装按钮")
114 }
115 if !strings.Contains(page, `class="btn btn-primary btn-lg btn-block wizard-next disabled"`) {
116 t.Error("git 未就绪时「下一步」应禁用")
117 }
118
119 // 未装 + 非 root:显示换 root 提醒,不显示按钮
120 stubGitCapability(t, false, "gitcat 当前以非 root 用户运行,无法自动安装系统软件。请以 root 身份重新运行 gitcat 后刷新本页")
121 page = getPage(srv, "/install/git")
122 if !strings.Contains(page, "以 root 身份重新运行 gitcat") {
123 t.Error("非 root 环境应提醒以 root 身份运行")
124 }
125 if strings.Contains(page, `id="git-install"`) {
126 t.Error("无安装能力时不应渲染一键安装按钮")
127 }
128}
129
130// TestInstallWizardSiteStep:站点名称分步保存,校验失败回显表单。
131func TestInstallWizardSiteStep(t *testing.T) {
132 srv, st := newFreshServer(t)
133
134 page := getPage(srv, "/install/site")
135 if !strings.Contains(page, "站点信息") || !strings.Contains(page, "创意在线,协作无限") {
136 t.Error("站点步骤缺少表单/彩色标语")
137 }
138
139 // 空名称拒绝
140 w := postForm(srv, "/install/site", url.Values{"site_name": {" "}})
141 if w.Code != http.StatusBadRequest {
142 t.Fatalf("空站点名应 400,实际 %d", w.Code)
143 }
144 // 超长拒绝
145 w = postForm(srv, "/install/site", url.Values{"site_name": {strings.Repeat("长", 61)}})
146 if w.Code != http.StatusBadRequest {
147 t.Fatalf("超长站点名应 400,实际 %d", w.Code)
148 }
149 // 合法:落库并进入下一步
150 w = postForm(srv, "/install/site", url.Values{"site_name": {"测试站"}})
151 if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/admin" {
152 t.Fatalf("合法站点名应 302 到 /install/admin,实际 %d → %s", w.Code, w.Header().Get("Location"))
153 }
154 if got := st.GetSetting("site_name"); got != "测试站" {
155 t.Errorf("站点名称未落库,得到 %q", got)
156 }
157 // 重新进入 site 步时回显
158 if page := getPage(srv, "/install/site"); !strings.Contains(page, `value="测试站"`) {
159 t.Error("重新进入站点步骤应回显已保存名称")
160 }
161}
162
163// TestInstallWizardAdminStep:缺 git 拒绝;成功创建管理员、写 installed、登录并跳完成页。
164func TestInstallWizardAdminStep(t *testing.T) {
165 srv, st := newFreshServer(t)
166
167 page := getPage(srv, "/install/admin")
168 if !strings.Contains(page, "管理员账号") || !strings.Contains(page, "AI加持,效率up+") {
169 t.Error("管理员步骤缺少表单/彩色标语")
170 }
171
172 form := url.Values{
173 "username": {"admin"}, "password": {"secret123"}, "password2": {"secret123"},
174 }
175
176 // 缺 git 时拒绝(绕过前端直接提交)
177 stubGitAvailable(t, false)
178 stubGitCapability(t, true, "")
179 w := postForm(srv, "/install/admin", form)
180 if w.Code != http.StatusBadRequest {
181 t.Fatalf("缺 git 提交应 400,实际 %d", w.Code)
182 }
183 if _, err := st.UserByUsername("admin"); err == nil {
184 t.Error("缺 git 时不应创建管理员账号")
185 }
186
187 // git 就绪:安装成功
188 stubGitAvailable(t, true)
189 w = postForm(srv, "/install/admin", form)
190 if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/done" {
191 t.Fatalf("安装成功应 302 到 /install/done,实际 %d → %s", w.Code, w.Header().Get("Location"))
192 }
193 if !st.Installed() {
194 t.Error("安装成功应写入 installed 标记")
195 }
196 if u, err := st.UserByUsername("admin"); err != nil || !u.IsAdmin {
197 t.Error("安装成功应创建管理员账号")
198 }
199 if st.GetSetting("site_name") == "" {
200 // admin 步不应覆盖 site 步的结果,也不负责写站点名
201 t.Log("site_name 为空(未经过 site 步的正常情况)")
202 }
203
204 // 完成页可访问,且包含「安装完成」与「立即体验」
205 page = getPage(srv, "/install/done")
206 if !strings.Contains(page, "安装完成!") || !strings.Contains(page, "立即体验") {
207 t.Error("完成页缺少文案/按钮")
208 }
209
210 // 已安装后再访问其它步骤 → 回首页
211 for _, path := range []string{"/install", "/install/welcome", "/install/git", "/install/site", "/install/admin"} {
212 r := httptest.NewRequest(http.MethodGet, path, nil)
213 w := httptest.NewRecorder()
214 srv.Handler().ServeHTTP(w, r)
215 if w.Code != http.StatusFound || w.Header().Get("Location") != "/" {
216 t.Errorf("已安装后访问 %s 应回首页,实际 %d → %s", path, w.Code, w.Header().Get("Location"))
217 }
218 }
219}
220
221// TestInstallWizardDoneGuard:未安装时不能直接访问完成页(防跳步)。
222func TestInstallWizardDoneGuard(t *testing.T) {
223 srv, _ := newFreshServer(t)
224 r := httptest.NewRequest(http.MethodGet, "/install/done", nil)
225 w := httptest.NewRecorder()
226 srv.Handler().ServeHTTP(w, r)
227 if w.Code != http.StatusFound || w.Header().Get("Location") != "/install/welcome" {
228 t.Fatalf("未安装访问 done 应归位欢迎页,实际 %d → %s", w.Code, w.Header().Get("Location"))
229 }
230}
231
232// TestInstallSubmitBlockedWithoutGit:缺 git 时即使绕过前端禁用直接
233// POST 表单,服务端也必须拒绝完成安装。
234func TestInstallSubmitBlockedWithoutGit(t *testing.T) {
235 srv, st := newFreshServer(t)
236 stubGitAvailable(t, false)
237 stubGitCapability(t, true, "")
238
239 form := url.Values{
240 "username": {"admin"}, "password": {"secret123"}, "password2": {"secret123"},
241 }
242 w := postForm(srv, "/install/admin", form)
243
244 if w.Code != http.StatusBadRequest {
245 t.Fatalf("缺 git 提交安装应返回 400,实际 %d", w.Code)
246 }
247 if !strings.Contains(w.Body.String(), "git") {
248 t.Error("错误提示应说明需要先完成 git 安装")
249 }
250 if _, err := st.UserByUsername("admin"); err == nil {
251 t.Error("缺 git 时不应创建管理员账号")
252 }
253 if st.Installed() {
254 t.Error("缺 git 时不应写入 installed 标记")
255 }
256}
257
258// TestInstallGitEndpoint:git 已存在时接口幂等返回成功与版本号;
259// 未装 git 时接口返回失败 JSON 并保留原因(CI 环境不真正装包)。
260func TestInstallGitEndpoint(t *testing.T) {
261 srv, st := newFreshServer(t)
262
263 // git 就绪:EnsureInstalled 直接短路成功,不会触发真实安装
264 w := postJSON(srv, "/install/git")
265 if w.Code != http.StatusOK {
266 t.Fatalf("git 就绪时应返回 200,实际 %d:%s", w.Code, w.Body.String())
267 }
268 if !strings.Contains(w.Body.String(), `"ok":true`) {
269 t.Errorf("响应缺少 ok:true:%s", w.Body.String())
270 }
271 if !strings.Contains(w.Body.String(), "git version") {
272 t.Errorf("响应应包含 git 版本号:%s", w.Body.String())
273 }
274 if st.Installed() {
275 t.Error("一键装 git 不应代替用户完成站点安装")
276 }
277
278 // 已完成安装的站点必须拒绝再调用
279 st.SetSetting("installed", "1")
280 w = postJSON(srv, "/install/git")
281 if w.Code != http.StatusForbidden {
282 t.Fatalf("已安装站点调用应返回 403,实际 %d", w.Code)
283 }
284}
285
286// TestInstallGitForbiddenWithoutCapability:非 root 且无免密 sudo 的
287// 环境(如 www 用户)下,接口直接拒绝并返回原因,不会白跑安装命令。
288func TestInstallGitForbiddenWithoutCapability(t *testing.T) {
289 srv, _ := newFreshServer(t)
290 stubGitAvailable(t, false)
291 stubGitCapability(t, false, "gitcat 当前以非 root 用户运行,无法自动安装系统软件。请以 root 身份重新运行 gitcat 后刷新本页")
292
293 w := postJSON(srv, "/install/git")
294 if w.Code != http.StatusForbidden {
295 t.Fatalf("无安装能力时应返回 403,实际 %d", w.Code)
296 }
297 if !strings.Contains(w.Body.String(), "以 root 身份") {
298 t.Errorf("403 响应应包含换 root 运行的引导:%s", w.Body.String())
299 }
300}
301
302// TestInstallPageRedirectsAfterInstall:完成安装后安装入口不再可达。
303func TestInstallPageRedirectsAfterInstall(t *testing.T) {
304 srv, _, _, _ := newTestServer(t) // newTestServer 写入了 installed=1
305 r := httptest.NewRequest(http.MethodGet, "/install", nil)
306 w := httptest.NewRecorder()
307 srv.Handler().ServeHTTP(w, r)
308 if w.Code != http.StatusFound {
309 t.Fatalf("已安装站点访问 /install 应重定向,实际 %d", w.Code)
310 }
311}