e2e/deps_test.go
89 lines · 2847 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 t.Parallel()
24 inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
25 aliceKey := inst.newKey(t, "alice")
26 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub",
27 "--email", "alice@example.test", "--verified")
28 bobKey := inst.newKey(t, "bob")
29 inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub",
30 "--email", "bob@example.test", "--verified")
31
32 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
33 t.Fatalf("repo create: %s", errOut)
34 }
35
36 status := func(key string) depsStatus {
37 t.Helper()
38 out, errOut, code := inst.ssh(t, key, "", "repo", "deps", "status", "alice/app", "--json")
39 if code != 0 {
40 t.Fatalf("deps status: %s", errOut)
41 }
42 var s depsStatus
43 if err := json.Unmarshal([]byte(out), &s); err != nil {
44 t.Fatalf("parsing status %q: %v", out, err)
45 }
46 return s
47 }
48
49 if status(aliceKey).Data.Enabled {
50 t.Error("checks are on before being enabled")
51 }
52
53 // A reader can see the state but cannot change it.
54 if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "deps", "enable", "alice/app"); code == 0 {
55 t.Error("a non-admin enabled dependency checks")
56 } else if !strings.Contains(errOut, "denied") {
57 t.Errorf("unexpected refusal: %s", errOut)
58 }
59
60 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "deps", "enable", "alice/app"); code != 0 {
61 t.Fatalf("deps enable: %s", errOut)
62 }
63 got := status(aliceKey)
64 if !got.Data.Enabled {
65 t.Error("checks are off after being enabled")
66 }
67 if len(got.Data.Behind) != 0 {
68 t.Errorf("behind = %v before any sweep", got.Data.Behind)
69 }
70
71 // Enabling twice is not an error.
72 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "deps", "enable", "alice/app"); code != 0 {
73 t.Fatalf("second deps enable: %s", errOut)
74 }
75
76 // The settings page reports the same state the command does (#164).
77 alice := inst.login(t, aliceKey)
78 _, page := browserGet(t, alice, inst.base()+"/alice/app/settings")
79 if !strings.Contains(page, "Last checked") || !strings.Contains(page, "Nothing behind") {
80 t.Errorf("settings page does not report the check state:\n%s", page)
81 }
82
83 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "deps", "disable", "alice/app"); code != 0 {
84 t.Fatalf("deps disable: %s", errOut)
85 }
86 if status(aliceKey).Data.Enabled {
87 t.Error("checks are on after being disabled")
88 }
89}