package server import ( "net/http" "net/http/httptest" "net/url" "strconv" "strings" "testing" ) // getAs 以指定会话(可为空)请求页面。 func getAs(srv *Server, path, session string) *httptest.ResponseRecorder { r := httptest.NewRequest(http.MethodGet, path, nil) if session != "" { r.AddCookie(&http.Cookie{Name: SessionCookieName, Value: session}) } w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) return w } // postFormAs 以指定会话提交表单。 func postFormAs(srv *Server, path, session 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") r.AddCookie(&http.Cookie{Name: SessionCookieName, Value: session}) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) return w } // TestRepoSettingsSavesLicense 覆盖协议设置的完整链路:大小写归一化、 // 非法值拒绝、以及清空选择。 func TestRepoSettingsSavesLicense(t *testing.T) { srv, st, _, _ := newTestServer(t) makeRepo(t, srv, st, "licproj", nil) sess := sessionCookie(t, login(t, srv, "alice", testPass)) csrf := csrfOf(t, srv, "/licproj/settings", sess) save := func(lic string) *httptest.ResponseRecorder { return postFormAs(srv, "/licproj/settings", sess, url.Values{ "csrf": {csrf}, "description": {"带协议的项目"}, "license": {lic}, }) } // 大小写不敏感,落库的是标准 SPDX 标识。 if w := save("mit"); w.Code != http.StatusFound { t.Fatalf("保存协议返回 %d,应为 302:%s", w.Code, w.Body.String()) } repo, err := st.RepoByName("licproj") if err != nil { t.Fatalf("读取仓库失败: %v", err) } if repo.License != "MIT" { t.Fatalf("协议未按 SPDX 标识保存: %q", repo.License) } // 未收录的协议必须被拒绝,且不能破坏已有值。 if w := save("Totally-Not-A-License"); w.Code != http.StatusBadRequest { t.Errorf("非法协议返回 %d,应为 400", w.Code) } if repo, _ = st.RepoByName("licproj"); repo.License != "MIT" { t.Errorf("非法协议覆盖了原有选择: %q", repo.License) } // 空值表示"未选择"。 if w := save(""); w.Code != http.StatusFound { t.Fatalf("清空协议返回 %d,应为 302", w.Code) } if repo, _ = st.RepoByName("licproj"); repo.License != "" { t.Errorf("协议未被清空: %q", repo.License) } } // TestRepoLicenseRequiresManage 普通成员不能通过设置页改协议。 func TestRepoLicenseRequiresManage(t *testing.T) { srv, st, _, _ := newTestServer(t) makeRepo(t, srv, st, "strict", nil) bobSess := sessionCookie(t, login(t, srv, "bob", testPass)) csrf := csrfOf(t, srv, "/settings", bobSess) w := postFormAs(srv, "/strict/settings", bobSess, url.Values{ "csrf": {csrf}, "license": {"MIT"}, }) if w.Code != http.StatusForbidden { t.Fatalf("非创建者修改设置返回 %d,应为 403", w.Code) } if repo, _ := st.RepoByName("strict"); repo.License != "" { t.Errorf("越权请求竟然修改了协议: %q", repo.License) } } // TestRepoHomeShowsLicense 验证项目主页的协议徽章与仓库内 LICENSE 文件入口。 func TestRepoHomeShowsLicense(t *testing.T) { srv, st, _, _ := newTestServer(t) dir := makeRepo(t, srv, st, "licensed", nil) commitFiles(t, dir, "init", map[string]string{ "LICENSE": "MIT License\n", "README.md": "# licensed\n", }) repo, err := st.RepoByName("licensed") if err != nil { t.Fatalf("读取仓库失败: %v", err) } repo.License = "MIT" if err := st.UpdateRepo(repo); err != nil { t.Fatalf("写入协议失败: %v", err) } body := getAs(srv, "/licensed", "").Body.String() for _, want := range []string{"/licenses/view/MIT", "MIT License", "/blob/main/LICENSE"} { if !strings.Contains(body, want) { t.Errorf("项目主页缺少 %q", want) } } } // TestLicensePages 覆盖协议目录与详情页,以及未收录 ID 的 404。 func TestLicensePages(t *testing.T) { srv, _, _, _ := newTestServer(t) if w := getAs(srv, "/licenses", ""); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "MIT License") { t.Fatalf("协议目录页异常: code=%d", w.Code) } // URL 里的大小写不影响查找,页面展示标准名称。 if w := getAs(srv, "/licenses/view/apache-2.0", ""); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Apache License 2.0") { t.Fatalf("协议详情页异常: code=%d", w.Code) } if w := getAs(srv, "/licenses/view/no-such-license", ""); w.Code != http.StatusNotFound { t.Fatalf("未知协议返回 %d,应为 404", w.Code) } } // TestAdminRepoSaveLicense 后台修改协议与项目设置共用同一个字段。 func TestAdminRepoSaveLicense(t *testing.T) { srv, st, _, _ := newTestServer(t) makeRepo(t, srv, st, "adm", nil) repo, err := st.RepoByName("adm") if err != nil { t.Fatalf("读取仓库失败: %v", err) } sess := sessionCookie(t, login(t, srv, "alice", testPass)) path := "/admin/repos/" + strconv.FormatInt(repo.ID, 10) csrf := csrfOf(t, srv, path, sess) w := postFormAs(srv, path, sess, url.Values{ "csrf": {csrf}, "description": {"后台改的"}, "license": {"apache-2.0"}, }) if w.Code != http.StatusFound { t.Fatalf("后台保存返回 %d:%s", w.Code, w.Body.String()) } got, _ := st.RepoByID(repo.ID) if got.License != "Apache-2.0" { t.Errorf("后台保存的协议不正确: %q", got.License) } if got.Description != "后台改的" { t.Errorf("同一次保存里的描述丢了: %q", got.Description) } // 非法值:不写库,保留原值。 bad := postFormAs(srv, path, sess, url.Values{ "csrf": {csrf}, "license": {"nope"}, }) if bad.Code != http.StatusFound { t.Fatalf("非法协议应回跳并提示,返回 %d", bad.Code) } if got, _ := st.RepoByID(repo.ID); got.License != "Apache-2.0" { t.Errorf("非法协议覆盖了后台的选择: %q", got.License) } }