krz/gitbay
A CLI-first git forge.
clone: git clone https://gitbay.org/krz/gitbay.git
main: e2e/accounts_test.go · raw
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 if status != 200 || !strings.Contains(body, "logged in as alice") {
91 t.Fatalf("login redirect landed wrong: %d\n%s", status, body)
92 }
93
94 // The token is single-use.
95 fresh := newBrowser(t)
96 _, body = browserGet(t, fresh, inst.base()+loginPath)
97 if !strings.Contains(body, "invalid, expired, or already used") {
98 t.Fatalf("token reuse not refused:\n%s", body)
99 }
100
101 // Create a repo through the web.
102 status, _ = browserPost(t, browser, inst.base()+"/new",
103 url.Values{"name": {"webborn"}, "visibility": {"private"}})
104 if status != 200 {
105 t.Fatalf("web repo create: %d", status)
106 }
107 if out, _, code := inst.ssh(t, aliceKey, "", "repo", "show", "alice/webborn"); code != 0 {
108 t.Fatalf("web-created repo missing over ssh: %s", out)
109 }
110
111 // Logged-in viewer sees their private repo; anonymous still gets 404.
112 if status, _ = browserGet(t, browser, inst.base()+"/alice/webborn"); status != 200 {
113 t.Fatalf("owner blocked from private repo page: %d", status)
114 }
115 if status, _ := inst.get(t, "/alice/webborn"); status != 404 {
116 t.Fatalf("anonymous sees private repo: %d", status)
117 }
118
119 // File edit: form loads with current content, POST commits.
120 status, body = browserGet(t, browser, inst.base()+"/alice/site/edit/main/notes.txt")
121 if status != 200 || !strings.Contains(body, "original") {
122 t.Fatalf("edit form: %d\n%s", status, body)
123 }
124 status, _ = browserPost(t, browser, inst.base()+"/alice/site/edit/main/notes.txt",
125 url.Values{"content": {"edited from the web\n"}, "message": {"web edit"}})
126 if status != 200 {
127 t.Fatalf("edit submit: %d", status)
128 }
129
130 // The edit is a real commit: authored with the verified email, and it
131 // displays as unsigned — the honest outcome for a server-side commit.
132 logOut, _, code := inst.ssh(t, aliceKey, "", "repo", "log", "alice/site", "--limit", "1", "--json")
133 if code != 0 {
134 t.Fatal("repo log failed")
135 }
136 var logEnv struct {
137 Data []struct {
138 Subject string `json:"subject"`
139 AuthorEmail string `json:"author_email"`
140 Signature struct {
141 State string `json:"state"`
142 } `json:"signature"`
143 } `json:"data"`
144 }
145 if err := json.Unmarshal([]byte(logOut), &logEnv); err != nil || len(logEnv.Data) == 0 {
146 t.Fatalf("log JSON: %v\n%s", err, logOut)
147 }
148 tip := logEnv.Data[0]
149 if tip.Subject != "web edit" || tip.AuthorEmail != "alice@example.test" || tip.Signature.State != "unsigned" {
150 t.Fatalf("web edit commit wrong: %+v", tip)
151 }
152 if status, body = browserGet(t, browser, inst.base()+"/alice/site/raw/main/notes.txt"); !strings.Contains(body, "edited from the web") {
153 t.Fatalf("edited content not served: %d %q", status, body)
154 }
155
156 // A require-signed repo refuses web edits instead of violating itself.
157 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "settings", "require-signed", "alice/site", "on"); code != 0 {
158 t.Fatal("require-signed failed")
159 }
160 _, body = browserPost(t, browser, inst.base()+"/alice/site/edit/main/notes.txt",
161 url.Values{"content": {"x"}, "message": {"x"}})
162 if !strings.Contains(body, "requires signed commits") {
163 t.Fatalf("require-signed web edit not refused:\n%s", body)
164 }
165
166 // Issue participation through the web.
167 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/site", "--title", "'from ssh'"); code != 0 {
168 t.Fatal("issue create failed")
169 }
170 status, _ = browserPost(t, browser, inst.base()+"/alice/site/issues/1/comment",
171 url.Values{"body": {"web comment"}})
172 if status != 200 {
173 t.Fatalf("web comment: %d", status)
174 }
175 showOut, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/site", "1")
176 if !strings.Contains(showOut, "web comment") {
177 t.Fatalf("web comment missing over ssh:\n%s", showOut)
178 }
179
180 // Cross-origin POSTs are refused.
181 req, _ := http.NewRequest("POST", inst.base()+"/alice/site/issues/1/comment",
182 strings.NewReader("body=evil"))
183 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
184 req.Header.Set("Origin", "https://evil.example")
185 resp, err := browser.Do(req)
186 if err != nil {
187 t.Fatal(err)
188 }
189 resp.Body.Close()
190 if resp.StatusCode != 403 {
191 t.Fatalf("cross-origin POST: %d, want 403", resp.StatusCode)
192 }
193
194 // Logout kills the session.
195 if status, _ = browserPost(t, browser, inst.base()+"/logout", url.Values{}); status != 200 {
196 t.Fatalf("logout: %d", status)
197 }
198 if status, _ = browserGet(t, browser, inst.base()+"/alice/webborn"); status != 404 {
199 t.Fatalf("session survived logout: %d", status)
200 }
201}
202
203// TestViewOnlyHasNoLoginOnTheWire is the M8 negative: in view_only mode the
204// login route does not exist and web login over ssh is refused.
205func TestViewOnlyHasNoLoginOnTheWire(t *testing.T) {
206 inst := startInstance(t) // default: view_only
207 aliceKey := inst.newKey(t, "alice")
208 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
209
210 if status, _ := inst.get(t, "/login"); status != 404 {
211 t.Fatalf("view_only /login = %d, want 404", status)
212 }
213 _, errOut, code := inst.ssh(t, aliceKey, "", "web", "login")
214 if code != 4 || !strings.Contains(errOut, "view-only") {
215 t.Fatalf("web login in view_only: exit %d, %s", code, errOut)
216 }
217}