e2e/deps_test.go

70dc0648f931f6f7112c6b71b0be485eeae4077f
gitbay/e2e/deps_test.go history · blame · raw

81 lines · 2459 bytes

 1package e2e
 2
 3import (
 4	"encoding/json"
 5	"strings"
 6	"testing"
 7)
 8
 9type depsStatus struct {
10	Data struct {
11		Enabled     bool  `json:"enabled"`
12		IssueNumber int64 `json:"issue_number"`
13		Behind      []struct {
14			Name string `json:"name"`
15		} `json:"behind"`
16	} `json:"data"`
17}
18
19// Dependency checking is opt-in per repository, and only its administrators
20// can turn it on: the check tells a public registry what the repository
21// depends on.
22func TestDepsEnableDisable(t *testing.T) {
23	inst := startInstance(t)
24	aliceKey := inst.newKey(t, "alice")
25	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub",
26		"--email", "alice@example.test", "--verified")
27	bobKey := inst.newKey(t, "bob")
28	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub",
29		"--email", "bob@example.test", "--verified")
30
31	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
32		t.Fatalf("repo create: %s", errOut)
33	}
34
35	status := func(key string) depsStatus {
36		t.Helper()
37		out, errOut, code := inst.ssh(t, key, "", "repo", "deps", "status", "alice/app", "--json")
38		if code != 0 {
39			t.Fatalf("deps status: %s", errOut)
40		}
41		var s depsStatus
42		if err := json.Unmarshal([]byte(out), &s); err != nil {
43			t.Fatalf("parsing status %q: %v", out, err)
44		}
45		return s
46	}
47
48	if status(aliceKey).Data.Enabled {
49		t.Error("checks are on before being enabled")
50	}
51
52	// A reader can see the state but cannot change it.
53	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "deps", "enable", "alice/app"); code == 0 {
54		t.Error("a non-admin enabled dependency checks")
55	} else if !strings.Contains(errOut, "denied") {
56		t.Errorf("unexpected refusal: %s", errOut)
57	}
58
59	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "deps", "enable", "alice/app"); code != 0 {
60		t.Fatalf("deps enable: %s", errOut)
61	}
62	got := status(aliceKey)
63	if !got.Data.Enabled {
64		t.Error("checks are off after being enabled")
65	}
66	if len(got.Data.Behind) != 0 {
67		t.Errorf("behind = %v before any sweep", got.Data.Behind)
68	}
69
70	// Enabling twice is not an error.
71	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "deps", "enable", "alice/app"); code != 0 {
72		t.Fatalf("second deps enable: %s", errOut)
73	}
74
75	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "deps", "disable", "alice/app"); code != 0 {
76		t.Fatalf("deps disable: %s", errOut)
77	}
78	if status(aliceKey).Data.Enabled {
79		t.Error("checks are on after being disabled")
80	}
81}