e2e/reposhowstate_test.go

v1.19.0
gitbay/e2e/reposhowstate_test.go history · blame · raw

74 lines · 3455 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	// unwatch is the way back to the default from either state, and
36	// mute is its own state, not what unwatch means (#180).
37	inst.ssh(t, bobKey, "", "repo", "unwatch", "alice/app")
38	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "alice/app", "--json"); strings.Contains(out, `"watch"`) {
39		t.Errorf("unwatch did not clear the state:\n%s", out)
40	}
41	inst.ssh(t, bobKey, "", "repo", "mute", "alice/app")
42	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "alice/app", "--json"); !strings.Contains(out, `"watch":"muted"`) {
43		t.Errorf("mute not reported:\n%s", out)
44	}
45	inst.ssh(t, bobKey, "", "repo", "unwatch", "alice/app")
46	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "alice/app", "--json"); strings.Contains(out, `"watch"`) {
47		t.Errorf("unwatch did not clear a mute:\n%s", out)
48	}
49	inst.ssh(t, bobKey, "", "repo", "watch", "alice/app")
50	// It is the caller's state: alice sees her own, not bob's.
51	if out, _, _ := inst.ssh(t, aliceKey, "", "repo", "show", "alice/app", "--json"); strings.Contains(out, `"bookmarked"`) {
52		t.Errorf("alice sees bob's bookmark:\n%s", out)
53	}
54
55	// Plain output carries the same.
56	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "alice/app"); !strings.Contains(out, "watch: watching") ||
57		!strings.Contains(out, "bookmarked") {
58		t.Errorf("plain output lacks the state:\n%s", out)
59	}
60
61	// A fork names its parent to anyone who can read the parent.
62	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "fork", "alice/app"); code != 0 {
63		t.Fatalf("fork: %s", errOut)
64	}
65	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "bob/app", "--json"); !strings.Contains(out, `"fork_of":"alice/app"`) {
66		t.Fatalf("fork_of missing:\n%s", out)
67	}
68	// Once the parent is private and unreadable, the fork stops naming
69	// it: a private repository is never confirmed to exist.
70	inst.ssh(t, aliceKey, "", "repo", "settings", "visibility", "alice/app", "private")
71	if out, _, _ := inst.ssh(t, bobKey, "", "repo", "show", "bob/app", "--json"); strings.Contains(out, "alice/app") {
72		t.Errorf("a fork named a parent the caller cannot read:\n%s", out)
73	}
74}