e2e/accounts_test.go
240 lines · 8410 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 // The edit is a real commit: authored with the verified email, and it
133 // displays as unsigned — the honest outcome for a server-side commit.
134 logOut, _, code := inst.ssh(t, aliceKey, "", "repo", "log", "alice/site", "--limit", "1", "--json")
135 if code != 0 {
136 t.Fatal("repo log failed")
137 }
138 var logEnv struct {
139 Data []struct {
140 Subject string `json:"subject"`
141 AuthorEmail string `json:"author_email"`
142 Signature struct {
143 State string `json:"state"`
144 } `json:"signature"`
145 } `json:"data"`
146 }
147 if err := json.Unmarshal([]byte(logOut), &logEnv); err != nil || len(logEnv.Data) == 0 {
148 t.Fatalf("log JSON: %v\n%s", err, logOut)
149 }
150 tip := logEnv.Data[0]
151 if tip.Subject != "web edit" || tip.AuthorEmail != "alice@example.test" || tip.Signature.State != "unsigned" {
152 t.Fatalf("web edit commit wrong: %+v", tip)
153 }
154 if status, body = browserGet(t, browser, inst.base()+"/alice/site/raw/main/notes.txt"); !strings.Contains(body, "edited from the web") {
155 t.Fatalf("edited content not served: %d %q", status, body)
156 }
157
158 // A require-signed repo refuses web edits instead of violating itself.
159 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "settings", "require-signed", "alice/site", "on"); code != 0 {
160 t.Fatal("require-signed failed")
161 }
162 _, body = browserPost(t, browser, inst.base()+"/alice/site/edit/main/notes.txt",
163 url.Values{"content": {"x"}, "message": {"x"}})
164 if !strings.Contains(body, "requires signed commits") {
165 t.Fatalf("require-signed web edit not refused:\n%s", body)
166 }
167
168 // Issue participation through the web.
169 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/site", "--title", "'from ssh'"); code != 0 {
170 t.Fatal("issue create failed")
171 }
172 status, _ = browserPost(t, browser, inst.base()+"/alice/site/issues/1/comment",
173 url.Values{"body": {"web comment"}})
174 if status != 200 {
175 t.Fatalf("web comment: %d", status)
176 }
177 showOut, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/site", "1")
178 if !strings.Contains(showOut, "web comment") {
179 t.Fatalf("web comment missing over ssh:\n%s", showOut)
180 }
181
182 // Cross-origin POSTs are refused.
183 req, _ := http.NewRequest("POST", inst.base()+"/alice/site/issues/1/comment",
184 strings.NewReader("body=evil"))
185 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
186 req.Header.Set("Origin", "https://evil.example")
187 resp, err := browser.Do(req)
188 if err != nil {
189 t.Fatal(err)
190 }
191 resp.Body.Close()
192 if resp.StatusCode != 403 {
193 t.Fatalf("cross-origin POST: %d, want 403", resp.StatusCode)
194 }
195
196 // Logout kills the session.
197 if status, _ = browserPost(t, browser, inst.base()+"/logout", url.Values{}); status != 200 {
198 t.Fatalf("logout: %d", status)
199 }
200 if status, _ = browserGet(t, browser, inst.base()+"/alice/webborn"); status != 404 {
201 t.Fatalf("session survived logout: %d", status)
202 }
203}
204
205// TestViewOnlyHasNoLoginOnTheWire is the M8 negative: in view_only mode the
206// login route does not exist and web login over ssh is refused.
207func TestViewOnlyHasNoLoginOnTheWire(t *testing.T) {
208 inst := startInstance(t) // default: view_only
209 aliceKey := inst.newKey(t, "alice")
210 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
211
212 if status, _ := inst.get(t, "/login"); status != 404 {
213 t.Fatalf("view_only /login = %d, want 404", status)
214 }
215 _, errOut, code := inst.ssh(t, aliceKey, "", "web", "login")
216 if code != 4 || !strings.Contains(errOut, "view-only") {
217 t.Fatalf("web login in view_only: exit %d, %s", code, errOut)
218 }
219}
220
221// TestTitleIsNotAHostname pins the split between the instance's display name
222// and its hostname: the login page prints a command to paste into a terminal,
223// so it must name the host even when the operator has set a display title.
224func TestTitleIsNotAHostname(t *testing.T) {
225 inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\ntitle = \"GitBay\"\n")
226
227 status, body := inst.get(t, "/login")
228 if status != 200 {
229 t.Fatalf("/login = %d", status)
230 }
231 if strings.Contains(body, "ssh git@GitBay") {
232 t.Fatal("login page tells you to ssh to the display title")
233 }
234 if !strings.Contains(body, "ssh git@gitbay.test web login") {
235 t.Fatalf("login page does not name the host:\n%s", body)
236 }
237 if !strings.Contains(body, "GitBay") {
238 t.Fatal("login page dropped the display title entirely")
239 }
240}