e2e/accounts_test.go
281 lines · 10636 bytes
1package e2e
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "net/http"
8 "net/http/cookiejar"
9 "net/url"
10 "os"
11 "path/filepath"
12 "strings"
13 "testing"
14)
15
16// browser is an HTTP client with a cookie jar, standing in for a logged-in
17// user's browser.
18func newBrowser(t *testing.T) *http.Client {
19 t.Helper()
20 jar, err := cookiejar.New(nil)
21 if err != nil {
22 t.Fatal(err)
23 }
24 return &http.Client{Jar: jar}
25}
26
27func (i *instance) base() string { return fmt.Sprintf("http://127.0.0.1:%d", i.httpPort) }
28
29func browserGet(t *testing.T, c *http.Client, url string) (int, string) {
30 t.Helper()
31 resp, err := c.Get(url)
32 if err != nil {
33 t.Fatal(err)
34 }
35 defer resp.Body.Close()
36 body, _ := io.ReadAll(resp.Body)
37 return resp.StatusCode, string(body)
38}
39
40func browserPost(t *testing.T, c *http.Client, u string, form url.Values) (int, string) {
41 t.Helper()
42 resp, err := c.PostForm(u, form)
43 if err != nil {
44 t.Fatal(err)
45 }
46 defer resp.Body.Close()
47 body, _ := io.ReadAll(resp.Body)
48 return resp.StatusCode, string(body)
49}
50
51func TestWebAccounts(t *testing.T) {
52 inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
53
54 aliceKey := inst.newKey(t, "alice")
55 inst.admin(t, "admin", "user", "create", "alice",
56 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
57
58 // A repo with one file to edit.
59 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/site"); code != 0 {
60 t.Fatalf("repo create: %s", errOut)
61 }
62 work := t.TempDir()
63 env := inst.gitEnv(aliceKey)
64 mustGit(t, work, env, "clone", inst.sshURL("alice/site"), "w")
65 dir := filepath.Join(work, "w")
66 os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("original\n"), 0o644)
67 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
68 mustGit(t, dir, env, "add", ".")
69 mustGit(t, dir, env, "commit", "-q", "-m", "base")
70 mustGit(t, dir, env, "push", "-q", "origin", "main")
71
72 // SSH-minted login URL.
73 out, errOut, code := inst.ssh(t, aliceKey, "", "web", "login", "--json")
74 if code != 0 {
75 t.Fatalf("web login: %s", errOut)
76 }
77 var env2 struct {
78 Data struct {
79 URL string `json:"url"`
80 } `json:"data"`
81 }
82 if err := json.Unmarshal([]byte(out), &env2); err != nil {
83 t.Fatalf("web login JSON: %v\n%s", err, out)
84 }
85 // The URL carries the configured site host; rewrite to the test port.
86 loginPath := env2.Data.URL[strings.Index(env2.Data.URL, "/login"):]
87
88 browser := newBrowser(t)
89 status, body := browserGet(t, browser, inst.base()+loginPath)
90 // The rail's footer carries the signed-in account now.
91 if status != 200 || !strings.Contains(body, ">Dashboard</h1>") ||
92 !strings.Contains(body, `class="railuser" href="/alice"`) {
93 t.Fatalf("login redirect landed wrong: %d\n%s", status, body)
94 }
95
96 // The token is single-use.
97 fresh := newBrowser(t)
98 _, body = browserGet(t, fresh, inst.base()+loginPath)
99 if !strings.Contains(body, "invalid, expired, or already used") {
100 t.Fatalf("token reuse not refused:\n%s", body)
101 }
102
103 // Create a repo through the web.
104 status, _ = browserPost(t, browser, inst.base()+"/new",
105 url.Values{"name": {"webborn"}, "visibility": {"private"}})
106 if status != 200 {
107 t.Fatalf("web repo create: %d", status)
108 }
109 if out, _, code := inst.ssh(t, aliceKey, "", "repo", "show", "alice/webborn"); code != 0 {
110 t.Fatalf("web-created repo missing over ssh: %s", out)
111 }
112
113 // Logged-in viewer sees their private repo; anonymous still gets 404.
114 if status, _ = browserGet(t, browser, inst.base()+"/alice/webborn"); status != 200 {
115 t.Fatalf("owner blocked from private repo page: %d", status)
116 }
117 if status, _ := inst.get(t, "/alice/webborn"); status != 404 {
118 t.Fatalf("anonymous sees private repo: %d", status)
119 }
120
121 // File edit: form loads with current content, POST commits.
122 status, body = browserGet(t, browser, inst.base()+"/alice/site/edit/main/notes.txt")
123 if status != 200 || !strings.Contains(body, "original") {
124 t.Fatalf("edit form: %d\n%s", status, body)
125 }
126 status, _ = browserPost(t, browser, inst.base()+"/alice/site/edit/main/notes.txt",
127 url.Values{"content": {"edited from the web\n"}, "message": {"web edit"}})
128 if status != 200 {
129 t.Fatalf("edit submit: %d", status)
130 }
131
132 // A branch that does not exist is a 404; a path that does not exist
133 // on a real branch is a new-file form that says so.
134 if status, _ := browserGet(t, browser, inst.base()+"/alice/site/edit/nope/notes.txt"); status != 404 {
135 t.Fatalf("edit form on a missing branch: %d", status)
136 }
137 status, body = browserGet(t, browser, inst.base()+"/alice/site/edit/main/new.txt")
138 if status != 200 || !strings.Contains(body, "does not exist on main; committing creates it") || !strings.Contains(body, "<textarea") {
139 t.Fatalf("edit form for a new file: %d\n%s", status, body)
140 }
141
142 // The edit is a real commit: authored with the verified email, and it
143 // displays as unsigned — the honest outcome for a server-side commit.
144 logOut, _, code := inst.ssh(t, aliceKey, "", "repo", "log", "alice/site", "--limit", "1", "--json")
145 if code != 0 {
146 t.Fatal("repo log failed")
147 }
148 var logEnv struct {
149 Data []struct {
150 Subject string `json:"subject"`
151 AuthorEmail string `json:"author_email"`
152 Signature struct {
153 State string `json:"state"`
154 } `json:"signature"`
155 } `json:"data"`
156 }
157 if err := json.Unmarshal([]byte(logOut), &logEnv); err != nil || len(logEnv.Data) == 0 {
158 t.Fatalf("log JSON: %v\n%s", err, logOut)
159 }
160 tip := logEnv.Data[0]
161 if tip.Subject != "web edit" || tip.AuthorEmail != "alice@example.test" || tip.Signature.State != "unsigned" {
162 t.Fatalf("web edit commit wrong: %+v", tip)
163 }
164 if status, body = browserGet(t, browser, inst.base()+"/alice/site/raw/main/notes.txt"); !strings.Contains(body, "edited from the web") {
165 t.Fatalf("edited content not served: %d %q", status, body)
166 }
167
168 // Editing is a command, so it works from the CLI too — the web is one
169 // rendering of it. This is the capability that used to be web-only.
170 if _, errOut, code := inst.ssh(t, aliceKey, "edited from ssh\n",
171 "repo", "commit-file", "alice/site", "notes.txt",
172 "--ref", "main", "--message", "'ssh edit'", "--file", "-"); code != 0 {
173 t.Fatalf("repo commit-file: %s", errOut)
174 }
175 if status, body = browserGet(t, browser, inst.base()+"/alice/site/raw/main/notes.txt"); !strings.Contains(body, "edited from ssh") {
176 t.Fatalf("ssh edit not served: %d %q", status, body)
177 }
178 // A path cannot climb out of the repository.
179 if _, _, code := inst.ssh(t, aliceKey, "x", "repo", "commit-file", "alice/site",
180 "../../etc/passwd", "--ref", "main", "--file", "-"); code == 0 {
181 t.Error("commit-file escaped the repository")
182 }
183 // A stranger with no write access cannot commit.
184 strangerKey := inst.newKey(t, "mallory")
185 inst.admin(t, "admin", "user", "create", "mallory",
186 "--key", strangerKey+".pub", "--email", "mallory@example.test", "--verified")
187 if _, _, code := inst.ssh(t, strangerKey, "x", "repo", "commit-file", "alice/site",
188 "notes.txt", "--ref", "main", "--file", "-"); code == 0 {
189 t.Error("a stranger committed to a repository they cannot write")
190 }
191
192 // A require-signed repo refuses web edits instead of violating itself.
193 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "settings", "require-signed", "alice/site", "on"); code != 0 {
194 t.Fatal("require-signed failed")
195 }
196 _, body = browserPost(t, browser, inst.base()+"/alice/site/edit/main/notes.txt",
197 url.Values{"content": {"x"}, "message": {"x"}})
198 if !strings.Contains(body, "requires signed commits") {
199 t.Fatalf("require-signed web edit not refused:\n%s", body)
200 }
201
202 // With signed commits required the editor cannot succeed, so the GET
203 // form says so instead of offering a textarea.
204 status, body = browserGet(t, browser, inst.base()+"/alice/site/edit/main/notes.txt")
205 if status != 200 || !strings.Contains(body, "requires signed commits") || strings.Contains(body, "<textarea") {
206 t.Fatalf("edit page under require-signed: %d\n%s", status, body)
207 }
208
209 // Issue participation through the web.
210 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/site", "--title", "'from ssh'"); code != 0 {
211 t.Fatal("issue create failed")
212 }
213 status, _ = browserPost(t, browser, inst.base()+"/alice/site/issues/1/comment",
214 url.Values{"body": {"web comment"}})
215 if status != 200 {
216 t.Fatalf("web comment: %d", status)
217 }
218 showOut, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/site", "1")
219 if !strings.Contains(showOut, "web comment") {
220 t.Fatalf("web comment missing over ssh:\n%s", showOut)
221 }
222
223 // Cross-origin POSTs are refused.
224 req, _ := http.NewRequest("POST", inst.base()+"/alice/site/issues/1/comment",
225 strings.NewReader("body=evil"))
226 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
227 req.Header.Set("Origin", "https://evil.example")
228 resp, err := browser.Do(req)
229 if err != nil {
230 t.Fatal(err)
231 }
232 resp.Body.Close()
233 if resp.StatusCode != 403 {
234 t.Fatalf("cross-origin POST: %d, want 403", resp.StatusCode)
235 }
236
237 // Logout kills the session.
238 if status, _ = browserPost(t, browser, inst.base()+"/logout", url.Values{}); status != 200 {
239 t.Fatalf("logout: %d", status)
240 }
241 if status, _ = browserGet(t, browser, inst.base()+"/alice/webborn"); status != 404 {
242 t.Fatalf("session survived logout: %d", status)
243 }
244}
245
246// TestViewOnlyHasNoLoginOnTheWire is the M8 negative: in view_only mode the
247// login route does not exist and web login over ssh is refused.
248func TestViewOnlyHasNoLoginOnTheWire(t *testing.T) {
249 inst := startInstance(t) // default: view_only
250 aliceKey := inst.newKey(t, "alice")
251 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
252
253 if status, _ := inst.get(t, "/login"); status != 404 {
254 t.Fatalf("view_only /login = %d, want 404", status)
255 }
256 _, errOut, code := inst.ssh(t, aliceKey, "", "web", "login")
257 if code != 4 || !strings.Contains(errOut, "view-only") {
258 t.Fatalf("web login in view_only: exit %d, %s", code, errOut)
259 }
260}
261
262// TestTitleIsNotAHostname pins the split between the instance's display name
263// and its hostname: the login page prints a command to paste into a terminal,
264// so it must name the host even when the operator has set a display title.
265func TestTitleIsNotAHostname(t *testing.T) {
266 inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\ntitle = \"GitBay\"\n")
267
268 status, body := inst.get(t, "/login")
269 if status != 200 {
270 t.Fatalf("/login = %d", status)
271 }
272 if strings.Contains(body, "ssh git@GitBay") {
273 t.Fatal("login page tells you to ssh to the display title")
274 }
275 if !strings.Contains(body, "ssh git@gitbay.test web login") {
276 t.Fatalf("login page does not name the host:\n%s", body)
277 }
278 if !strings.Contains(body, "GitBay") {
279 t.Fatal("login page dropped the display title entirely")
280 }
281}