e2e/snippet_test.go

v1.30.0
gitbay/e2e/snippet_test.go history · blame · raw

142 lines · 5998 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 := startInstanceWith(t, "[limits]\nmax_snippets_per_user = 3\n")
 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	// The per-account cap: three snippets exist, a fourth is refused.
 65	if msg := fails(aliceKey, "x\n", 2, "snippet", "create", "c.txt"); !strings.Contains(msg, "snippet limit reached") {
 66		t.Fatalf("cap refusal: %s", msg)
 67	}
 68
 69	// Refusals on create: empty, not text, over the limit, bad name.
 70	fails(aliceKey, "", 2, "snippet", "create", "x.txt")
 71	fails(aliceKey, "\xff\xfe\n", 2, "snippet", "create", "x.bin")
 72	fails(aliceKey, strings.Repeat("x", 1<<20+1), 2, "snippet", "create", "big.txt")
 73	fails(aliceKey, "x\n", 2, "snippet", "create", "../x")
 74	fails(aliceKey, "x\n", 2, "snippet", "create", "x.txt", "--visibility", "secret")
 75
 76	// Visibility from the other side. Private is not-found, never denied.
 77	fails(bobKey, "", 3, "snippet", "show", private)
 78	fails(bobKey, "", 3, "snippet", "file", "get", private, "b.txt")
 79	must(bobKey, "", "snippet", "show", unlisted)
 80	out = must(bobKey, "", "snippet", "list", "alice", "--json")
 81	if !strings.Contains(out, public) || strings.Contains(out, unlisted) || strings.Contains(out, private) {
 82		t.Fatalf("bob's view of alice's list: %s", out)
 83	}
 84	out = must(aliceKey, "", "snippet", "list", "--json")
 85	for _, id := range []string{public, unlisted, private} {
 86		if !strings.Contains(out, id) {
 87			t.Fatalf("alice's own list lacks %s: %s", id, out)
 88		}
 89	}
 90	fails(bobKey, "", 3, "snippet", "list", "nobody")
 91
 92	// Paging: two pages of one, the second reached by cursor.
 93	out = must(aliceKey, "", "snippet", "list", "--limit", "1", "--json")
 94	var page struct {
 95		Data struct {
 96			Items []struct {
 97				ID string `json:"id"`
 98			} `json:"items"`
 99			Next string `json:"next"`
100		} `json:"data"`
101	}
102	json.Unmarshal([]byte(out), &page)
103	if len(page.Data.Items) != 1 || page.Data.Items[0].ID != private || page.Data.Next == "" {
104		t.Fatalf("first page: %s", out)
105	}
106	out = must(aliceKey, "", "snippet", "list", "--limit", "1", "--cursor", page.Data.Next, "--json")
107	if !strings.Contains(out, public) {
108		t.Fatalf("second page: %s", out)
109	}
110
111	// Files: set adds, set replaces, remove drops, the last one stays.
112	must(aliceKey, "notes\n", "snippet", "file", "set", unlisted, "notes.txt")
113	must(aliceKey, "changed\n", "snippet", "file", "set", unlisted, "build.log")
114	if got := must(aliceKey, "", "snippet", "file", "get", unlisted, "build.log"); got != "changed\n" {
115		t.Fatalf("after replace: %q", got)
116	}
117	must(aliceKey, "", "snippet", "file", "remove", unlisted, "notes.txt")
118	fails(aliceKey, "", 3, "snippet", "file", "remove", unlisted, "notes.txt")
119	if msg := fails(aliceKey, "", 2, "snippet", "file", "remove", unlisted, "build.log"); !strings.Contains(msg, "at least one file") {
120		t.Fatalf("last file removal: %s", msg)
121	}
122
123	// Only the owner writes: denied on a readable one, not-found on a private one.
124	fails(bobKey, "x\n", 4, "snippet", "file", "set", unlisted, "x.txt")
125	fails(bobKey, "", 4, "snippet", "edit", unlisted, "--description", "mine")
126	fails(bobKey, "", 4, "snippet", "delete", unlisted)
127	fails(bobKey, "", 3, "snippet", "delete", private)
128
129	// Edit moves visibility and the listing follows.
130	fails(aliceKey, "", 2, "snippet", "edit", unlisted)
131	must(aliceKey, "", "snippet", "edit", unlisted, "--visibility", "public", "--description", "shared")
132	out = must(bobKey, "", "snippet", "list", "alice", "--json")
133	if !strings.Contains(out, unlisted) || !strings.Contains(out, `"description":"shared"`) {
134		t.Fatalf("list after edit: %s", out)
135	}
136
137	// Delete, then gone; deleting the user takes the rest.
138	must(aliceKey, "", "snippet", "delete", unlisted)
139	fails(aliceKey, "", 3, "snippet", "show", unlisted)
140	inst.admin(t, "admin", "user", "delete", "alice", "--yes")
141	fails(bobKey, "", 3, "snippet", "show", public)
142}