package server import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" ) // TestPrivateRepoGuestRedirect 私有仓库的页面入口对游客一律 302 去登录页, // 登录后的工作室成员(哪怕不是仓库 owner,如 bob)可以正常访问。 func TestPrivateRepoGuestRedirect(t *testing.T) { srv, st, _, bob := newTestServer(t) dir := makeRepo(t, srv, st, "sec", nil) commitFiles(t, dir, "init", map[string]string{"main.go": "package main\n"}) repo, err := st.RepoByName("sec") if err != nil { t.Fatalf("读仓库失败: %v", err) } if err := st.SetRepoPrivate(repo.ID, true); err != nil { t.Fatalf("设为私有失败: %v", err) } for _, path := range []string{"/sec", "/sec/commits/main", "/sec/issues", "/sec/releases"} { r := httptest.NewRequest(http.MethodGet, path, nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusFound { t.Errorf("游客访问 %s 返回 %d,期望 302", path, w.Code) continue } loc := w.Header().Get("Location") if !strings.HasPrefix(loc, "/login?next=") { t.Errorf("游客访问 %s 应重定向到登录页,实际 %q", path, loc) } if next, err := url.QueryUnescape(strings.TrimPrefix(loc, "/login?next=")); err != nil || next != path { t.Errorf("next 参数应还原原地址 %q,实际 %q", path, next) } } // 登录的普通成员(非 owner 非管理员)可以访问:私有 = 工作室共享 sess := sessionCookie(t, login(t, srv, bob.Username, testPass)) r := httptest.NewRequest(http.MethodGet, "/sec", nil) r.AddCookie(&http.Cookie{Name: SessionCookieName, Value: sess}) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusOK { t.Errorf("已登录成员访问私有仓库返回 %d,期望 200:%s", w.Code, w.Body.String()) } } // TestPrivateRepoGitAccess 私有仓库的 clone/fetch 必须先通过 Basic 认证: // 任意登录成员可以克隆(不要求 owner),匿名请求收到 401 挑战。 func TestPrivateRepoGitAccess(t *testing.T) { srv, st, _, bob := newTestServer(t) dir := makeRepo(t, srv, st, "hidden", nil) commitFiles(t, dir, "init", map[string]string{"a.txt": "x"}) repo, _ := st.RepoByName("hidden") if err := st.SetRepoPrivate(repo.ID, true); err != nil { t.Fatalf("设为私有失败: %v", err) } target := "/hidden.git/info/refs?service=git-upload-pack" // 匿名 → 401 挑战,git 客户端会提示输入账号密码 r := httptest.NewRequest(http.MethodGet, target, nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusUnauthorized { t.Errorf("匿名 clone 私有仓库返回 %d,期望 401", w.Code) } if w.Header().Get("WWW-Authenticate") == "" { t.Error("缺少 WWW-Authenticate 挑战头") } // 错误密码 → 401 r = httptest.NewRequest(http.MethodGet, target, nil) r.SetBasicAuth(bob.Username, "wrong-pass") w = httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusUnauthorized { t.Errorf("错误密码 clone 返回 %d,期望 401", w.Code) } // 登录成员(非 owner)→ 200 r = httptest.NewRequest(http.MethodGet, target, nil) r.SetBasicAuth(bob.Username, testPass) w = httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusOK { t.Errorf("登录成员 clone 私有仓库返回 %d,期望 200", w.Code) } // 上传端点同样要拦:匿名 POST git-upload-pack → 401 r = httptest.NewRequest(http.MethodPost, "/hidden.git/git-upload-pack", strings.NewReader("0000")) w = httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusUnauthorized { t.Errorf("匿名 POST git-upload-pack 返回 %d,期望 401", w.Code) } // 公开仓库的匿名 clone 保持可用(回归:不能误伤) makeRepo(t, srv, st, "open", nil) r = httptest.NewRequest(http.MethodGet, "/open.git/info/refs?service=git-upload-pack", nil) w = httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusOK { t.Errorf("公开仓库匿名 clone 返回 %d,期望 200", w.Code) } } // TestPrivateRepoHiddenOnHome 私有仓库对游客从项目列表与全站动态里消失, // 登录成员可见并带有「私有」徽章。 func TestPrivateRepoHiddenOnHome(t *testing.T) { srv, st, _, _ := newTestServer(t) dir := makeRepo(t, srv, st, "ghost", nil) commitFiles(t, dir, "fix: 隐秘改动", map[string]string{"a.txt": "x"}) repo, _ := st.RepoByName("ghost") if err := st.SetRepoPrivate(repo.ID, true); err != nil { t.Fatalf("设为私有失败: %v", err) } // 伪造一条推送记录,验证全站动态也不泄露私有仓库名与摘要 if _, err := st.CreatePush(repo.ID, nil, "someone", "某人", "main", "a", "b", nil, nil); err != nil { t.Fatalf("造推送记录失败: %v", err) } // 公开仓库作为对照组 makeRepo(t, srv, st, "shown", nil) // 游客:看不到私有仓库,也看不到它的动态 w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil)) body := w.Body.String() if strings.Contains(body, "ghost") { t.Error("游客首页不应出现私有仓库 ghost(列表或动态)") } if !strings.Contains(body, "shown") { t.Error("游客首页应仍能看到公开仓库") } // 登录成员:可见 + 私有徽章 sess := sessionCookie(t, login(t, srv, "alice", testPass)) r := httptest.NewRequest(http.MethodGet, "/", nil) r.AddCookie(&http.Cookie{Name: SessionCookieName, Value: sess}) w = httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) body = w.Body.String() if !strings.Contains(body, "ghost") || !strings.Contains(body, "tag-private") { t.Error("登录成员首页应显示私有仓库及其徽章") } } // TestPrivateRepoAssistantGuest 私有仓库的 AI 助手对游客返回 401, // 而不是让未登录请求读到代码内容。 func TestPrivateRepoAssistantGuest(t *testing.T) { srv, st, _, _ := newTestServer(t) dir := makeRepo(t, srv, st, "vault", nil) commitFiles(t, dir, "init", map[string]string{"main.go": "package main\n"}) repo, _ := st.RepoByName("vault") if err := st.SetRepoPrivate(repo.ID, true); err != nil { t.Fatalf("设为私有失败: %v", err) } payload := `{"question":"这个项目是做什么的?"}` r := httptest.NewRequest(http.MethodPost, "/vault/assistant", strings.NewReader(payload)) r.Header.Set("Content-Type", "application/json") r.Header.Set("X-Forwarded-For", "10.9.9.9") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusUnauthorized { t.Fatalf("游客调用私有仓库助手返回 %d,期望 401:%s", w.Code, w.Body.String()) } if !strings.Contains(w.Body.String(), "私有") { t.Errorf("错误信息应说明是私有仓库:%s", w.Body.String()) } } // TestPrivateRepoSettingsToggle 项目设置里的「私有仓库」开关要能落库生效。 func TestPrivateRepoSettingsToggle(t *testing.T) { srv, st, _, _ := newTestServer(t) dir := makeRepo(t, srv, st, "toggle", nil) commitFiles(t, dir, "init", map[string]string{"a.txt": "x"}) sess := sessionCookie(t, login(t, srv, "alice", testPass)) csrf := csrfOf(t, srv, "/toggle/settings", sess) post := func(privacy string) { form := url.Values{"csrf": {csrf}, "description": {""}, "default_branch": {"main"}, "is_private": {privacy}} r := httptest.NewRequest(http.MethodPost, "/toggle/settings", strings.NewReader(form.Encode())) r.Header.Set("Content-Type", "application/x-www-form-urlencoded") r.AddCookie(&http.Cookie{Name: SessionCookieName, Value: sess}) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) if w.Code != http.StatusFound { t.Fatalf("保存设置返回 %d", w.Code) } } post("on") if repo, _ := st.RepoByName("toggle"); !repo.IsPrivate { t.Error("勾选后应保存为私有") } if !st.IsPrivateRepo("toggle") { t.Error("IsPrivateRepo 应识别出私有仓库") } post("") if repo, _ := st.RepoByName("toggle"); repo.IsPrivate { t.Error("取消勾选后应恢复公开") } } // TestPrivateRepoStoreRoundTrip 存储层:v4 迁移后的字段读写与查询过滤。 func TestPrivateRepoStoreRoundTrip(t *testing.T) { srv, st, _, _ := newTestServer(t) makeRepo(t, srv, st, "rt", nil) // 新建仓库默认公开 repo, _ := st.RepoByName("rt") if repo.IsPrivate { t.Fatal("新建仓库不应默认私有") } if st.IsPrivateRepo("rt") { t.Fatal("IsPrivateRepo 对公开仓库应返回 false") } if err := st.SetRepoPrivate(repo.ID, true); err != nil { t.Fatalf("SetRepoPrivate 失败: %v", err) } repo, _ = st.RepoByName("rt") if !repo.IsPrivate { t.Fatal("RepoByName 未读回 is_private") } cards, err := st.ListRepos("rt") if err != nil || len(cards) != 1 || !cards[0].IsPrivate { t.Fatalf("ListRepos 未带出 is_private:%v %v", cards, err) } }