e2e/runnerweb_test.go

3f4afd2210627e46fd0cea1f5831857af71638f6
gitbay/e2e/runnerweb_test.go history · blame · raw

51 lines · 2056 bytes

 1package e2e
 2
 3import (
 4	"net/url"
 5	"os"
 6	"strings"
 7	"testing"
 8)
 9
10// The settings page attaches and detaches runners through the same
11// commands the CLI uses, and lists what is attached.
12func TestRunnerSettingsWeb(t *testing.T) {
13	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
14	aliceKey := inst.newKey(t, "alice")
15	inst.admin(t, "admin", "user", "create", "alice",
16		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
17	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
18		t.Fatalf("repo create: %s", errOut)
19	}
20	runnerKey := inst.newKey(t, "laptop")
21	pub, _ := os.ReadFile(runnerKey + ".pub")
22
23	alice := inst.login(t, aliceKey)
24	settings := inst.base() + "/alice/app/settings"
25	_, body := browserGet(t, alice, settings)
26	if !strings.Contains(body, "No runners attached") {
27		t.Fatalf("empty state missing:\n%s", body)
28	}
29	if status, _ := browserPost(t, alice, settings, url.Values{"field": {"runner-add"}, "key": {string(pub)}}); status != 200 {
30		t.Fatalf("runner-add post: %d", status)
31	}
32	out, _, _ := inst.ssh(t, aliceKey, "", "repo", "runner", "list", "alice/app", "--json")
33	if !strings.Contains(out, `"fingerprint":"SHA256:`) {
34		t.Fatalf("not attached after the form: %s", out)
35	}
36	fp := out[strings.Index(out, "SHA256:"):]
37	fp = fp[:strings.Index(fp, `"`)]
38	// html/template writes + as + in text and attributes, and a
39	// fingerprint is base64, so the page shows the escaped form.
40	shown := strings.ReplaceAll(fp, "+", "+")
41	_, body = browserGet(t, alice, settings)
42	if !strings.Contains(body, shown) || !strings.Contains(body, `value="runner-remove"`) {
43		t.Fatalf("attached runner not listed:\n%s", body)
44	}
45	if status, _ := browserPost(t, alice, settings, url.Values{"field": {"runner-remove"}, "fingerprint": {fp}}); status != 200 {
46		t.Fatalf("runner-remove post: %d", status)
47	}
48	if out, _, _ = inst.ssh(t, aliceKey, "", "repo", "runner", "list", "alice/app", "--json"); strings.Contains(out, fp) {
49		t.Fatalf("still attached after remove: %s", out)
50	}
51}