e2e/snippet_test.go
137 lines · 5728 bytes
1package e2e
2
3import (
4 "encoding/json"
5 "regexp"
6 "strings"
7 "testing"
8)
9
10// Snippets over SSH: create from stdin, read back, list by visibility,
11// edit files and metadata, and the not-found rule for private ones.
12func TestSnippets(t *testing.T) {
13 inst := startInstance(t)
14 aliceKey := inst.newKey(t, "alice")
15 bobKey := inst.newKey(t, "bob")
16 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
17 inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub", "--email", "bob@example.test", "--verified")
18 must := func(key, stdin string, args ...string) string {
19 t.Helper()
20 out, errOut, code := inst.ssh(t, key, stdin, args...)
21 if code != 0 {
22 t.Fatalf("%v: exit %d %s", args, code, errOut)
23 }
24 return out
25 }
26 fails := func(key, stdin string, want int, args ...string) string {
27 t.Helper()
28 _, errOut, code := inst.ssh(t, key, stdin, args...)
29 if code != want {
30 t.Fatalf("%v: exit %d, want %d: %s", args, code, want, errOut)
31 }
32 return errOut
33 }
34 idOf := func(out string) string {
35 t.Helper()
36 var env struct {
37 Data struct {
38 ID string `json:"id"`
39 URL string `json:"url"`
40 } `json:"data"`
41 }
42 if err := json.Unmarshal([]byte(out), &env); err != nil || !regexp.MustCompile(`^[0-9a-f]{12}$`).MatchString(env.Data.ID) {
43 t.Fatalf("create output: %s", out)
44 }
45 if !strings.HasSuffix(env.Data.URL, "/alice/-/snippets/"+env.Data.ID) {
46 t.Fatalf("url: %s", env.Data.URL)
47 }
48 return env.Data.ID
49 }
50
51 // Create with the default visibility, read back byte for byte.
52 body := "line one\nline two\n"
53 unlisted := idOf(must(aliceKey, body, "snippet", "create", "build.log", "--description", "'a log'", "--json"))
54 if got := must(aliceKey, "", "snippet", "file", "get", unlisted, "build.log"); got != body {
55 t.Fatalf("file get: %q", got)
56 }
57 out := must(aliceKey, "", "snippet", "show", unlisted, "--json")
58 if !strings.Contains(out, `"visibility":"unlisted"`) || !strings.Contains(out, `"content":"line one\nline two\n"`) {
59 t.Fatalf("show: %s", out)
60 }
61 public := idOf(must(aliceKey, "pub\n", "snippet", "create", "a.txt", "--visibility", "public", "--json"))
62 private := idOf(must(aliceKey, "sec\n", "snippet", "create", "b.txt", "--visibility", "private", "--json"))
63
64 // Refusals on create: empty, not text, over the limit, bad name.
65 fails(aliceKey, "", 2, "snippet", "create", "x.txt")
66 fails(aliceKey, "\xff\xfe\n", 2, "snippet", "create", "x.bin")
67 fails(aliceKey, strings.Repeat("x", 1<<20+1), 2, "snippet", "create", "big.txt")
68 fails(aliceKey, "x\n", 2, "snippet", "create", "../x")
69 fails(aliceKey, "x\n", 2, "snippet", "create", "x.txt", "--visibility", "secret")
70
71 // Visibility from the other side. Private is not-found, never denied.
72 fails(bobKey, "", 3, "snippet", "show", private)
73 fails(bobKey, "", 3, "snippet", "file", "get", private, "b.txt")
74 must(bobKey, "", "snippet", "show", unlisted)
75 out = must(bobKey, "", "snippet", "list", "alice", "--json")
76 if !strings.Contains(out, public) || strings.Contains(out, unlisted) || strings.Contains(out, private) {
77 t.Fatalf("bob's view of alice's list: %s", out)
78 }
79 out = must(aliceKey, "", "snippet", "list", "--json")
80 for _, id := range []string{public, unlisted, private} {
81 if !strings.Contains(out, id) {
82 t.Fatalf("alice's own list lacks %s: %s", id, out)
83 }
84 }
85 fails(bobKey, "", 3, "snippet", "list", "nobody")
86
87 // Paging: two pages of one, the second reached by cursor.
88 out = must(aliceKey, "", "snippet", "list", "--limit", "1", "--json")
89 var page struct {
90 Data struct {
91 Items []struct {
92 ID string `json:"id"`
93 } `json:"items"`
94 Next string `json:"next"`
95 } `json:"data"`
96 }
97 json.Unmarshal([]byte(out), &page)
98 if len(page.Data.Items) != 1 || page.Data.Items[0].ID != private || page.Data.Next == "" {
99 t.Fatalf("first page: %s", out)
100 }
101 out = must(aliceKey, "", "snippet", "list", "--limit", "1", "--cursor", page.Data.Next, "--json")
102 if !strings.Contains(out, public) {
103 t.Fatalf("second page: %s", out)
104 }
105
106 // Files: set adds, set replaces, remove drops, the last one stays.
107 must(aliceKey, "notes\n", "snippet", "file", "set", unlisted, "notes.txt")
108 must(aliceKey, "changed\n", "snippet", "file", "set", unlisted, "build.log")
109 if got := must(aliceKey, "", "snippet", "file", "get", unlisted, "build.log"); got != "changed\n" {
110 t.Fatalf("after replace: %q", got)
111 }
112 must(aliceKey, "", "snippet", "file", "remove", unlisted, "notes.txt")
113 fails(aliceKey, "", 3, "snippet", "file", "remove", unlisted, "notes.txt")
114 if msg := fails(aliceKey, "", 2, "snippet", "file", "remove", unlisted, "build.log"); !strings.Contains(msg, "at least one file") {
115 t.Fatalf("last file removal: %s", msg)
116 }
117
118 // Only the owner writes: denied on a readable one, not-found on a private one.
119 fails(bobKey, "x\n", 4, "snippet", "file", "set", unlisted, "x.txt")
120 fails(bobKey, "", 4, "snippet", "edit", unlisted, "--description", "mine")
121 fails(bobKey, "", 4, "snippet", "delete", unlisted)
122 fails(bobKey, "", 3, "snippet", "delete", private)
123
124 // Edit moves visibility and the listing follows.
125 fails(aliceKey, "", 2, "snippet", "edit", unlisted)
126 must(aliceKey, "", "snippet", "edit", unlisted, "--visibility", "public", "--description", "shared")
127 out = must(bobKey, "", "snippet", "list", "alice", "--json")
128 if !strings.Contains(out, unlisted) || !strings.Contains(out, `"description":"shared"`) {
129 t.Fatalf("list after edit: %s", out)
130 }
131
132 // Delete, then gone; deleting the user takes the rest.
133 must(aliceKey, "", "snippet", "delete", unlisted)
134 fails(aliceKey, "", 3, "snippet", "show", unlisted)
135 inst.admin(t, "admin", "user", "delete", "alice", "--yes")
136 fails(bobKey, "", 3, "snippet", "show", public)
137}