e2e/reposhowstate_test.go

387b381242a77e37af3a58b7301f6365b176603b
gitbay/e2e/reposhowstate_test.go history · blame · raw

59 lines · 2591 bytes

 1package e2e
 2
 3import (
 4	"strings"
 5	"testing"
 6)
 7
 8// repo show carries the caller's own watch and bookmark state and, when
 9// the caller can read it, what the repository was forked from — so a
10// client draws the real state without a second read per screen (#178).
11func TestRepoShowCarriesViewerState(t *testing.T) {
12	inst := startInstance(t)
13	aliceKey := inst.newKey(t, "alice")
14	bobKey := inst.newKey(t, "bob")
15	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
16	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
17	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
18		t.Fatalf("repo create: %s", errOut)
19	}
20
21	// Nothing yet: the fields are absent, not false or empty strings.
22	out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "alice/app", "--json")
23	for _, k := range []string{`"watch"`, `"bookmarked"`, `"fork_of"`} {
24		if strings.Contains(out, k) {
25			t.Errorf("%s present with no state:\n%s", k, out)
26		}
27	}
28
29	inst.ssh(t, bobKey, "", "repo", "watch", "alice/app")
30	inst.ssh(t, bobKey, "", "repo", "bookmark", "alice/app")
31	out, _, _ = inst.ssh(t, bobKey, "", "repo", "show", "alice/app", "--json")
32	if !strings.Contains(out, `"watch":"watching"`) || !strings.Contains(out, `"bookmarked":true`) {
33		t.Fatalf("bob's state not reported:\n%s", out)
34	}
35	// It is the caller's state: alice sees her own, not bob's.
36	if out, _, _ := inst.ssh(t, aliceKey, "", "repo", "show", "alice/app", "--json"); strings.Contains(out, `"bookmarked"`) {
37		t.Errorf("alice sees bob's bookmark:\n%s", out)
38	}
39
40	// Plain output carries the same.
41	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "alice/app"); !strings.Contains(out, "watch: watching") ||
42		!strings.Contains(out, "bookmarked") {
43		t.Errorf("plain output lacks the state:\n%s", out)
44	}
45
46	// A fork names its parent to anyone who can read the parent.
47	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "fork", "alice/app"); code != 0 {
48		t.Fatalf("fork: %s", errOut)
49	}
50	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "bob/app", "--json"); !strings.Contains(out, `"fork_of":"alice/app"`) {
51		t.Fatalf("fork_of missing:\n%s", out)
52	}
53	// Once the parent is private and unreadable, the fork stops naming
54	// it: a private repository is never confirmed to exist.
55	inst.ssh(t, aliceKey, "", "repo", "settings", "visibility", "alice/app", "private")
56	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "bob/app", "--json"); strings.Contains(out, "alice/app") {
57		t.Errorf("a fork named a parent the caller cannot read:\n%s", out)
58	}
59}