internal/httpd/webux_test.go

174 lines · 5231 bytes

2 symbols in this file
  1package httpd
  2
  3import (
  4	"net/http"
  5	"net/http/httptest"
  6	"net/url"
  7	"strings"
  8	"testing"
  9
 10	"gitbay.org/gitbay/internal/config"
 11	"gitbay.org/gitbay/internal/store"
 12)
 13
 14func TestWebUXPickersStatebarAndRailPins(t *testing.T) {
 15	st, err := store.Open(":memory:")
 16	if err != nil {
 17		t.Fatal(err)
 18	}
 19	defer st.Close()
 20	if err := st.MigrateUp(); err != nil {
 21		t.Fatal(err)
 22	}
 23	uid, err := st.CreateUser("alice", false)
 24	if err != nil {
 25		t.Fatal(err)
 26	}
 27	repoID, err := st.CreateRepo("user", uid, "app", "public")
 28	if err != nil {
 29		t.Fatal(err)
 30	}
 31	repo, err := st.RepoByID(repoID)
 32	if err != nil {
 33		t.Fatal(err)
 34	}
 35	if err := st.PinRepo(uid, repoID); err != nil {
 36		t.Fatal(err)
 37	}
 38	if err := st.SetLabel(repo, "bug", "d73a4a"); err != nil {
 39		t.Fatal(err)
 40	}
 41	if _, err := st.CreateIssue(repoID, uid, "first bug", "body", "md"); err != nil {
 42		t.Fatal(err)
 43	}
 44
 45	var cfg config.Config
 46	cfg.Server.Root = t.TempDir()
 47	cfg.Server.SiteURL = "https://forge.test"
 48	cfg.Web.Mode = "accounts"
 49	srv := New(cfg, st, nil)
 50	cookie := sessionCookieFor(t, srv, st, uid)
 51	h := srv.Handler()
 52
 53	// Issues list carries quick statebar and rail pinned-repo switcher.
 54	req := httptest.NewRequest("GET", "/alice/app/issues", nil)
 55	req.AddCookie(cookie)
 56	rec := httptest.NewRecorder()
 57	h.ServeHTTP(rec, req)
 58	if rec.Code != http.StatusOK {
 59		t.Fatalf("GET /alice/app/issues: %d", rec.Code)
 60	}
 61	body := rec.Body.String()
 62	for _, want := range []string{`class="statebar"`, `class="railpins"`, `href="/alice/app"`} {
 63		if !strings.Contains(body, want) {
 64			t.Errorf("issues list missing %s", want)
 65		}
 66	}
 67
 68	// Issue detail carries popover label & assignee pickers, datalists, and Cmd+K palette.
 69	req = httptest.NewRequest("GET", "/alice/app/issues/1", nil)
 70	req.AddCookie(cookie)
 71	rec = httptest.NewRecorder()
 72	h.ServeHTTP(rec, req)
 73	if rec.Code != http.StatusOK {
 74		t.Fatalf("GET /alice/app/issues/1: %d", rec.Code)
 75	}
 76	body = rec.Body.String()
 77	for _, want := range []string{`id="repo-labels"`, `id="repo-people"`, `class="popover"`, `class="picker-btn"`, `id="cmdk"`, `/static/app.js`} {
 78		if !strings.Contains(body, want) {
 79			t.Errorf("issue page missing %s", want)
 80		}
 81	}
 82
 83	// /static/app.js is served with JavaScript content-type.
 84	req = httptest.NewRequest("GET", "/static/app.js", nil)
 85	rec = httptest.NewRecorder()
 86	h.ServeHTTP(rec, req)
 87	if rec.Code != http.StatusOK || !strings.Contains(rec.Header().Get("Content-Type"), "javascript") {
 88		t.Fatalf("GET /static/app.js: %d %q", rec.Code, rec.Header().Get("Content-Type"))
 89	}
 90
 91	// Clicking a picker button applies the label.
 92	form := url.Values{"add": {"bug"}}
 93	req = httptest.NewRequest("POST", "https://forge.test/alice/app/issues/1/label", strings.NewReader(form.Encode()))
 94	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
 95	req.Header.Set("Origin", "https://forge.test")
 96	req.AddCookie(cookie)
 97	rec = httptest.NewRecorder()
 98	h.ServeHTTP(rec, req)
 99	if rec.Code != http.StatusSeeOther {
100		t.Fatalf("POST label: %d", rec.Code)
101	}
102	iss, err := st.IssueByNumber(repoID, 1)
103	if err != nil || len(iss.Labels) != 1 || iss.Labels[0] != "bug" {
104		t.Fatalf("labels after picker click: %+v err=%v", iss.Labels, err)
105	}
106}
107
108func TestAccountAndAdminUX(t *testing.T) {
109	st, err := store.Open(":memory:")
110	if err != nil {
111		t.Fatal(err)
112	}
113	defer st.Close()
114	if err := st.MigrateUp(); err != nil {
115		t.Fatal(err)
116	}
117	uid, err := st.CreateUser("admin", true)
118	if err != nil {
119		t.Fatal(err)
120	}
121	if _, err := st.CreateUser("bob", false); err != nil {
122		t.Fatal(err)
123	}
124
125	var cfg config.Config
126	cfg.Server.Root = t.TempDir()
127	cfg.Server.SiteURL = "https://forge.test"
128	cfg.Web.Mode = "accounts"
129	srv := New(cfg, st, nil)
130	cookie := sessionCookieFor(t, srv, st, uid)
131	h := srv.Handler()
132
133	// /settings renders setcard sections, autosave forms, and popover confirmations.
134	req := httptest.NewRequest("GET", "/settings", nil)
135	req.AddCookie(cookie)
136	rec := httptest.NewRecorder()
137	h.ServeHTTP(rec, req)
138	if rec.Code != http.StatusOK {
139		t.Fatalf("GET /settings: %d", rec.Code)
140	}
141	for _, want := range []string{`class="setcard"`, `data-autosave="1"`, `data-live-theme="1"`, `data-scrollspy="1"`, `class="popover"`} {
142		if !strings.Contains(rec.Body.String(), want) {
143			t.Errorf("/settings missing %s", want)
144		}
145	}
146
147	// /admin renders unified tabs, overview tiles, and in-place refresh trigger.
148	req = httptest.NewRequest("GET", "/admin", nil)
149	req.AddCookie(cookie)
150	rec = httptest.NewRecorder()
151	h.ServeHTTP(rec, req)
152	if rec.Code != http.StatusOK {
153		t.Fatalf("GET /admin: %d", rec.Code)
154	}
155	for _, want := range []string{`admintabs`, `data-refresh="/admin"`, `id="admin-queues"`, `class="tiles"`} {
156		if !strings.Contains(rec.Body.String(), want) {
157			t.Errorf("/admin missing %s", want)
158		}
159	}
160
161	// /admin/users renders live filter input and popover confirm controls.
162	req = httptest.NewRequest("GET", "/admin/users", nil)
163	req.AddCookie(cookie)
164	rec = httptest.NewRecorder()
165	h.ServeHTTP(rec, req)
166	if rec.Code != http.StatusOK {
167		t.Fatalf("GET /admin/users: %d", rec.Code)
168	}
169	for _, want := range []string{`admintabs`, `data-filter-table="accounts-table"`, `class="popover"`, `bob`} {
170		if !strings.Contains(rec.Body.String(), want) {
171			t.Errorf("/admin/users missing %s", want)
172		}
173	}
174}