e2e/profileweb_test.go

e9566eed86ebcd185c4b85f63d667b5e671fe787
gitbay/e2e/profileweb_test.go history · blame · raw

136 lines · 5224 bytes

  1package e2e
  2
  3import (
  4	"net/url"
  5	"strings"
  6	"testing"
  7)
  8
  9// TestProfileSettingsWeb covers profile set from the account settings page
 10// (#161): description, website and links round-trip through the form, and
 11// emptying a field actually clears it rather than being skipped. The about
 12// text is not on this form — it is a file, covered below.
 13func TestProfileSettingsWeb(t *testing.T) {
 14	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
 15	aliceKey := inst.newKey(t, "alice")
 16	inst.admin(t, "admin", "user", "create", "alice",
 17		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
 18	alice := inst.login(t, aliceKey)
 19	settingsURL := inst.base() + "/settings"
 20
 21	// The form is on the page, every input labelled.
 22	_, body := browserGet(t, alice, settingsURL)
 23	for _, want := range []string{
 24		`<label for="p-description">`, `<label for="p-website">`,
 25		`<label for="p-links">`,
 26	} {
 27		if !strings.Contains(body, want) {
 28			t.Fatalf("profile form missing %q:\n%s", want, body)
 29		}
 30	}
 31
 32	// Setting every field lands where the CLI reads it.
 33	status, _ := browserPost(t, alice, settingsURL, url.Values{
 34		"field":       {"profile"},
 35		"description": {"builds small tools"},
 36		"website":     {"https://alice.example"},
 37		"links":       {"Mastodon|https://fosstodon.example/@alice\nhttps://alice.example/now"},
 38	})
 39	if status != 200 && status != 303 {
 40		t.Fatalf("profile post: %d", status)
 41	}
 42	out, _, _ := inst.ssh(t, aliceKey, "", "profile", "show", "--json")
 43	for _, want := range []string{
 44		`"description":"builds small tools"`, `"website":"https://alice.example"`,
 45		`"label":"Mastodon"`, `"url":"https://fosstodon.example/@alice"`,
 46		`"url":"https://alice.example/now"`,
 47	} {
 48		if !strings.Contains(out, want) {
 49			t.Fatalf("profile set missing %q: %s", want, out)
 50		}
 51	}
 52
 53	// The page shows what was just saved.
 54	_, body = browserGet(t, alice, settingsURL)
 55	for _, want := range []string{
 56		"builds small tools", "https://alice.example", "Mastodon|https://fosstodon.example/@alice",
 57		"https://alice.example/now",
 58	} {
 59		if !strings.Contains(body, want) {
 60			t.Fatalf("settings page did not round-trip %q:\n%s", want, body)
 61		}
 62	}
 63
 64	// The whole form resubmits every time, the way a settings page does:
 65	// each step below carries the fields already in place and changes one.
 66	links := "Mastodon|https://fosstodon.example/@alice\nhttps://alice.example/now"
 67
 68	// Emptying the website clears it, not leaves it alone.
 69	if status, _ := browserPost(t, alice, settingsURL, url.Values{
 70		"field": {"profile"}, "description": {"builds small tools"},
 71		"website": {""}, "links": {links},
 72	}); status != 200 && status != 303 {
 73		t.Fatalf("profile post (clear website): %d", status)
 74	}
 75	out, _, _ = inst.ssh(t, aliceKey, "", "profile", "show", "--json")
 76	if strings.Contains(out, "alice.example") && strings.Contains(out, `"website"`) {
 77		t.Fatalf("website not cleared: %s", out)
 78	}
 79	if !strings.Contains(out, "builds small tools") {
 80		t.Fatalf("clearing website clobbered the description: %s", out)
 81	}
 82	if !strings.Contains(out, `"label":"Mastodon"`) {
 83		t.Fatalf("clearing website clobbered the links: %s", out)
 84	}
 85
 86	// Emptying the links field clears the whole list.
 87	if status, _ := browserPost(t, alice, settingsURL, url.Values{
 88		"field": {"profile"}, "description": {"builds small tools"}, "links": {""},
 89	}); status != 200 && status != 303 {
 90		t.Fatalf("profile post (clear links): %d", status)
 91	}
 92	out, _, _ = inst.ssh(t, aliceKey, "", "profile", "show", "--json")
 93	if strings.Contains(out, "Mastodon") || strings.Contains(out, `"links"`) {
 94		t.Fatalf("links not cleared: %s", out)
 95	}
 96}
 97
 98// The settings page does not edit the about text; it creates the
 99// repository that holds it and points at the file editor.
100func TestProfileAboutRepoFromWeb(t *testing.T) {
101	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
102	aliceKey := inst.newKey(t, "alice")
103	inst.admin(t, "admin", "user", "create", "alice",
104		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
105	alice := inst.login(t, aliceKey)
106	settingsURL := inst.base() + "/settings"
107
108	// With no repository yet, the page offers to create one.
109	_, body := browserGet(t, alice, settingsURL)
110	if !strings.Contains(body, "Create alice/.gitbay") {
111		t.Fatalf("settings page does not offer the profile repository:\n%s", body)
112	}
113
114	if status, _ := browserPost(t, alice, settingsURL, url.Values{
115		"field": {"profile-repo"},
116	}); status != 200 && status != 303 {
117		t.Fatalf("profile-repo post: %d", status)
118	}
119
120	// The repository exists with a starter file, and the page now links to
121	// the editor instead of offering to create it again.
122	out, _, code := inst.ssh(t, aliceKey, "", "profile", "show", "alice", "--json")
123	if code != 0 {
124		t.Fatalf("profile show: %d", code)
125	}
126	if !strings.Contains(out, `"about_path":"profile/README.md"`) {
127		t.Fatalf("starter about not committed: %s", out)
128	}
129	_, body = browserGet(t, alice, settingsURL)
130	if !strings.Contains(body, "/alice/.gitbay/edit/main/profile/README.md") {
131		t.Fatalf("settings page does not link to the about file:\n%s", body)
132	}
133	if strings.Contains(body, "Create alice/.gitbay") {
134		t.Error("settings page still offers to create an existing repository")
135	}
136}