internal/httpd/admin_test.go

v1.34.1
gitbay/internal/httpd/admin_test.go history · blame · raw

74 lines · 2140 bytes

 1package httpd
 2
 3import (
 4	"net/http"
 5	"net/http/httptest"
 6	"strconv"
 7	"strings"
 8	"testing"
 9
10	"gitbay.org/gitbay/internal/config"
11	"gitbay.org/gitbay/internal/store"
12)
13
14// The admin page renders every queue dashboard reports, push included,
15// and names a push by its device id: a token is device-identifying and
16// reaches an admin's page no more than it reaches its owner's.
17func TestAdminPageShowsThePushQueue(t *testing.T) {
18	st, err := store.Open(":memory:")
19	if err != nil {
20		t.Fatal(err)
21	}
22	defer st.Close()
23	if err := st.MigrateUp(); err != nil {
24		t.Fatal(err)
25	}
26	uid, err := st.CreateUser("root", true)
27	if err != nil {
28		t.Fatal(err)
29	}
30	if err := st.SetPushEnabled(uid, true); err != nil {
31		t.Fatal(err)
32	}
33	token := strings.Repeat("c", 64)
34	device, err := st.AddPushDevice(uid, token, "iphone")
35	if err != nil {
36		t.Fatal(err)
37	}
38	if err := st.EnqueuePush(uid, "krz/gitbay", "alice opened #1", "/krz/gitbay/issues/1"); err != nil {
39		t.Fatal(err)
40	}
41	// Only rows that are retrying or dead-lettered are listed, so fail
42	// the queued one first.
43	due, err := st.DuePush(10)
44	if err != nil || len(due) != 1 {
45		t.Fatalf("DuePush: %v %v", due, err)
46	}
47	if err := st.MarkPushFailed(due[0].ID, "403 InvalidProviderToken", nil); err != nil {
48		t.Fatal(err)
49	}
50
51	s := New(config.Default(), st)
52	rr := httptest.NewRecorder()
53	s.adminPage(rr, httptest.NewRequest("GET", "/admin", nil), store.User{ID: uid, Username: "root", IsAdmin: true})
54	if rr.Code != http.StatusOK {
55		t.Fatalf("status %d, body %s", rr.Code, rr.Body.String())
56	}
57
58	body := rr.Body.String()
59	if !strings.Contains(body, `id="push"`) {
60		t.Fatalf("no push section:\n%s", body)
61	}
62	if !strings.Contains(body, `href="#push"`) {
63		t.Fatalf("push is missing from the jump list:\n%s", body)
64	}
65	if !strings.Contains(body, "device "+strconv.FormatInt(device, 10)) {
66		t.Fatalf("the dead-lettered push is not listed by device id:\n%s", body)
67	}
68	if !strings.Contains(body, "403 InvalidProviderToken") {
69		t.Fatalf("the dead-lettered push's error is not shown:\n%s", body)
70	}
71	if strings.Contains(body, token) {
72		t.Fatalf("the page printed a device token:\n%s", body)
73	}
74}