e2e/webonly_test.go

9df917e73a67d15adecc3f45976690f6fcd4e47a
gitbay/e2e/webonly_test.go history · blame · raw

94 lines · 3380 bytes

 1package e2e
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// The three reads that were reachable only in a browser: history at a
11// ref, an archive, and the public listing. Each existed as a web route
12// whose handler went around the registry, so no other surface had them.
13func TestWebOnlyReadsAreCommands(t *testing.T) {
14	t.Parallel()
15	inst := startInstance(t)
16	aliceKey := inst.newKey(t, "alice")
17	bobKey := inst.newKey(t, "bob")
18	inst.admin(t, "admin", "user", "create", "alice",
19		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
20	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
21
22	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
23		t.Fatalf("repo create: %s", errOut)
24	}
25	work := t.TempDir()
26	env := inst.gitEnv(aliceKey)
27	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
28	dir := filepath.Join(work, "w")
29
30	os.WriteFile(filepath.Join(dir, "f.txt"), []byte("main line\n"), 0o644)
31	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
32	mustGit(t, dir, env, "add", ".")
33	mustGit(t, dir, env, "commit", "-q", "-m", "on main")
34	mustGit(t, dir, env, "push", "-q", "origin", "main")
35
36	mustGit(t, dir, env, "checkout", "-q", "-b", "side")
37	os.WriteFile(filepath.Join(dir, "f.txt"), []byte("side line\n"), 0o644)
38	mustGit(t, dir, env, "add", ".")
39	mustGit(t, dir, env, "commit", "-q", "-m", "only on side")
40	mustGit(t, dir, env, "push", "-q", "origin", "side")
41
42	// --- history at a ref ---
43	out, errOut, code := inst.ssh(t, aliceKey, "", "repo", "log", "alice/app", "--json")
44	if code != 0 {
45		t.Fatalf("repo log: %s", errOut)
46	}
47	if strings.Contains(out, "only on side") {
48		t.Error("the default branch log carried a side-branch commit")
49	}
50	out, errOut, code = inst.ssh(t, aliceKey, "", "repo", "log", "alice/app", "--ref", "side", "--json")
51	if code != 0 {
52		t.Fatalf("repo log --ref: %s", errOut)
53	}
54	if !strings.Contains(out, "only on side") {
55		t.Errorf("log at a ref missed its commit: %s", out)
56	}
57	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "log", "alice/app", "--ref", "nope"); code == 0 {
58		t.Error("an unknown ref resolved")
59	}
60
61	// --- archive ---
62	out, errOut, code = inst.ssh(t, aliceKey, "", "repo", "download", "alice/app")
63	if code != 0 {
64		t.Fatalf("repo download: %s", errOut)
65	}
66	// A gzip stream, not an error page: magic bytes 0x1f 0x8b.
67	if len(out) < 2 || out[0] != 0x1f || out[1] != 0x8b {
68		t.Errorf("repo download did not produce gzip (%d bytes)", len(out))
69	}
70	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "download", "alice/app", "--ref", "nope"); code == 0 {
71		t.Error("archived an unknown ref")
72	}
73
74	// --- the public listing ---
75	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/hidden", "--private"); code != 0 {
76		t.Fatal("private repo create")
77	}
78	out, errOut, code = inst.ssh(t, bobKey, "", "explore", "--json")
79	if code != 0 {
80		t.Fatalf("explore: %s", errOut)
81	}
82	if !strings.Contains(out, `"path":"alice/app"`) {
83		t.Errorf("explore missed a public repository: %s", out)
84	}
85	if strings.Contains(out, "alice/hidden") {
86		t.Errorf("explore leaked a private repository: %s", out)
87	}
88
89	// Paginated like the other listings.
90	out, _, code = inst.ssh(t, bobKey, "", "explore", "--limit", "1", "--json")
91	if code != 0 || !strings.Contains(out, `"items"`) {
92		t.Fatalf("explore --limit: %s", out)
93	}
94}