gitcat
1package server
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "strconv"
8 "strings"
9 "testing"
10)
11
12// getAs 以指定会话(可为空)请求页面。
13func getAs(srv *Server, path, session string) *httptest.ResponseRecorder {
14 r := httptest.NewRequest(http.MethodGet, path, nil)
15 if session != "" {
16 r.AddCookie(&http.Cookie{Name: SessionCookieName, Value: session})
17 }
18 w := httptest.NewRecorder()
19 srv.Handler().ServeHTTP(w, r)
20 return w
21}
22
23// postFormAs 以指定会话提交表单。
24func postFormAs(srv *Server, path, session string, form url.Values) *httptest.ResponseRecorder {
25 r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(form.Encode()))
26 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
27 r.AddCookie(&http.Cookie{Name: SessionCookieName, Value: session})
28 w := httptest.NewRecorder()
29 srv.Handler().ServeHTTP(w, r)
30 return w
31}
32
33// TestRepoSettingsSavesLicense 覆盖协议设置的完整链路:大小写归一化、
34// 非法值拒绝、以及清空选择。
35func TestRepoSettingsSavesLicense(t *testing.T) {
36 srv, st, _, _ := newTestServer(t)
37 makeRepo(t, srv, st, "licproj", nil)
38
39 sess := sessionCookie(t, login(t, srv, "alice", testPass))
40 csrf := csrfOf(t, srv, "/licproj/settings", sess)
41 save := func(lic string) *httptest.ResponseRecorder {
42 return postFormAs(srv, "/licproj/settings", sess, url.Values{
43 "csrf": {csrf},
44 "description": {"带协议的项目"},
45 "license": {lic},
46 })
47 }
48
49 // 大小写不敏感,落库的是标准 SPDX 标识。
50 if w := save("mit"); w.Code != http.StatusFound {
51 t.Fatalf("保存协议返回 %d,应为 302:%s", w.Code, w.Body.String())
52 }
53 repo, err := st.RepoByName("licproj")
54 if err != nil {
55 t.Fatalf("读取仓库失败: %v", err)
56 }
57 if repo.License != "MIT" {
58 t.Fatalf("协议未按 SPDX 标识保存: %q", repo.License)
59 }
60
61 // 未收录的协议必须被拒绝,且不能破坏已有值。
62 if w := save("Totally-Not-A-License"); w.Code != http.StatusBadRequest {
63 t.Errorf("非法协议返回 %d,应为 400", w.Code)
64 }
65 if repo, _ = st.RepoByName("licproj"); repo.License != "MIT" {
66 t.Errorf("非法协议覆盖了原有选择: %q", repo.License)
67 }
68
69 // 空值表示"未选择"。
70 if w := save(""); w.Code != http.StatusFound {
71 t.Fatalf("清空协议返回 %d,应为 302", w.Code)
72 }
73 if repo, _ = st.RepoByName("licproj"); repo.License != "" {
74 t.Errorf("协议未被清空: %q", repo.License)
75 }
76}
77
78// TestRepoLicenseRequiresManage 普通成员不能通过设置页改协议。
79func TestRepoLicenseRequiresManage(t *testing.T) {
80 srv, st, _, _ := newTestServer(t)
81 makeRepo(t, srv, st, "strict", nil)
82
83 bobSess := sessionCookie(t, login(t, srv, "bob", testPass))
84 csrf := csrfOf(t, srv, "/settings", bobSess)
85 w := postFormAs(srv, "/strict/settings", bobSess, url.Values{
86 "csrf": {csrf},
87 "license": {"MIT"},
88 })
89 if w.Code != http.StatusForbidden {
90 t.Fatalf("非创建者修改设置返回 %d,应为 403", w.Code)
91 }
92 if repo, _ := st.RepoByName("strict"); repo.License != "" {
93 t.Errorf("越权请求竟然修改了协议: %q", repo.License)
94 }
95}
96
97// TestRepoHomeShowsLicense 验证项目主页的协议徽章与仓库内 LICENSE 文件入口。
98func TestRepoHomeShowsLicense(t *testing.T) {
99 srv, st, _, _ := newTestServer(t)
100 dir := makeRepo(t, srv, st, "licensed", nil)
101 commitFiles(t, dir, "init", map[string]string{
102 "LICENSE": "MIT License\n",
103 "README.md": "# licensed\n",
104 })
105
106 repo, err := st.RepoByName("licensed")
107 if err != nil {
108 t.Fatalf("读取仓库失败: %v", err)
109 }
110 repo.License = "MIT"
111 if err := st.UpdateRepo(repo); err != nil {
112 t.Fatalf("写入协议失败: %v", err)
113 }
114
115 body := getAs(srv, "/licensed", "").Body.String()
116 for _, want := range []string{"/licenses/view/MIT", "MIT License", "/blob/main/LICENSE"} {
117 if !strings.Contains(body, want) {
118 t.Errorf("项目主页缺少 %q", want)
119 }
120 }
121}
122
123// TestLicensePages 覆盖协议目录与详情页,以及未收录 ID 的 404。
124func TestLicensePages(t *testing.T) {
125 srv, _, _, _ := newTestServer(t)
126
127 if w := getAs(srv, "/licenses", ""); w.Code != http.StatusOK ||
128 !strings.Contains(w.Body.String(), "MIT License") {
129 t.Fatalf("协议目录页异常: code=%d", w.Code)
130 }
131 // URL 里的大小写不影响查找,页面展示标准名称。
132 if w := getAs(srv, "/licenses/view/apache-2.0", ""); w.Code != http.StatusOK ||
133 !strings.Contains(w.Body.String(), "Apache License 2.0") {
134 t.Fatalf("协议详情页异常: code=%d", w.Code)
135 }
136 if w := getAs(srv, "/licenses/view/no-such-license", ""); w.Code != http.StatusNotFound {
137 t.Fatalf("未知协议返回 %d,应为 404", w.Code)
138 }
139}
140
141// TestAdminRepoSaveLicense 后台修改协议与项目设置共用同一个字段。
142func TestAdminRepoSaveLicense(t *testing.T) {
143 srv, st, _, _ := newTestServer(t)
144 makeRepo(t, srv, st, "adm", nil)
145 repo, err := st.RepoByName("adm")
146 if err != nil {
147 t.Fatalf("读取仓库失败: %v", err)
148 }
149
150 sess := sessionCookie(t, login(t, srv, "alice", testPass))
151 path := "/admin/repos/" + strconv.FormatInt(repo.ID, 10)
152 csrf := csrfOf(t, srv, path, sess)
153
154 w := postFormAs(srv, path, sess, url.Values{
155 "csrf": {csrf},
156 "description": {"后台改的"},
157 "license": {"apache-2.0"},
158 })
159 if w.Code != http.StatusFound {
160 t.Fatalf("后台保存返回 %d:%s", w.Code, w.Body.String())
161 }
162 got, _ := st.RepoByID(repo.ID)
163 if got.License != "Apache-2.0" {
164 t.Errorf("后台保存的协议不正确: %q", got.License)
165 }
166 if got.Description != "后台改的" {
167 t.Errorf("同一次保存里的描述丢了: %q", got.Description)
168 }
169
170 // 非法值:不写库,保留原值。
171 bad := postFormAs(srv, path, sess, url.Values{
172 "csrf": {csrf},
173 "license": {"nope"},
174 })
175 if bad.Code != http.StatusFound {
176 t.Fatalf("非法协议应回跳并提示,返回 %d", bad.Code)
177 }
178 if got, _ := st.RepoByID(repo.ID); got.License != "Apache-2.0" {
179 t.Errorf("非法协议覆盖了后台的选择: %q", got.License)
180 }
181}