e2e/webwrites_test.go

v1.16.0
gitbay/e2e/webwrites_test.go history · blame · raw

72 lines · 2913 bytes

 1package e2e
 2
 3import (
 4	"encoding/json"
 5	"net/url"
 6	"strings"
 7	"testing"
 8)
 9
10// Web writes dispatch the command the CLI runs, so a rule the command
11// layer enforces holds from a browser too. Repo create used to call the
12// store directly and skip the per-account quota; issue comments skipped
13// the archived check (#93).
14func TestWebWritesGoThroughRegistry(t *testing.T) {
15	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n[limits]\nmax_repos_per_user = 1\n")
16	aliceKey := inst.newKey(t, "alice")
17	inst.admin(t, "admin", "user", "create", "alice",
18		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
19	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/first"); code != 0 {
20		t.Fatalf("repo create: %s", errOut)
21	}
22	if _, errOut, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/first", "--title", "one"); code != 0 {
23		t.Fatalf("issue create: %s", errOut)
24	}
25
26	out, errOut, code := inst.ssh(t, aliceKey, "", "web", "login", "--json")
27	if code != 0 {
28		t.Fatalf("web login: %s", errOut)
29	}
30	var env struct {
31		Data struct {
32			URL string `json:"url"`
33		} `json:"data"`
34	}
35	if err := json.Unmarshal([]byte(out), &env); err != nil {
36		t.Fatalf("web login JSON: %v\n%s", err, out)
37	}
38	browser := newBrowser(t)
39	if status, _ := browserGet(t, browser, inst.base()+env.Data.URL[strings.Index(env.Data.URL, "/login"):]); status != 200 {
40		t.Fatalf("login: %d", status)
41	}
42
43	// The quota holds on the web: the form comes back with the refusal and
44	// no repository exists.
45	status, body := browserPost(t, browser, inst.base()+"/new", url.Values{"name": {"second"}, "visibility": {"public"}})
46	if status != 200 || !strings.Contains(body, "role=\"alert\"") {
47		t.Fatalf("second repo over quota was not refused on the form: %d\n%s", status, body)
48	}
49	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "show", "alice/second"); code != 3 {
50		t.Fatalf("repo created past the quota from the web: exit %d, want 3", code)
51	}
52
53	// A form action on an issue that does not exist is the 404 page, not a
54	// redirect carrying a message (#106).
55	if status, _ := browserPost(t, browser, inst.base()+"/alice/first/issues/999/state", url.Values{"action": {"close"}}); status != 404 {
56		t.Fatalf("closing a missing issue from the web: %d, want 404", status)
57	}
58
59	// An archived repository refuses a comment from the web as it does
60	// over ssh.
61	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "archive", "alice/first"); code != 0 {
62		t.Fatalf("repo archive: %s", errOut)
63	}
64	status, _ = browserPost(t, browser, inst.base()+"/alice/first/issues/1/comment", url.Values{"body": {"late"}})
65	if status/100 != 4 {
66		t.Fatalf("comment on an archived repo from the web: %d, want 4xx", status)
67	}
68	show, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/first", "1")
69	if strings.Contains(show, "late") {
70		t.Fatalf("archived repo took a web comment:\n%s", show)
71	}
72}