package api_test import ( "context" "net/http" "strings" "testing" "git.zb-server.de/ZB-Server/uptimesebi/internal/store" "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" ) func TestStatusPageHome(t *testing.T) { e := newEnv(t) admin := e.browser(t) admin.signup("a@"+uniq("home")+".test", uniq("home-")) if err := tenant.New(e.dbPool).Tx(context.Background(), tenant.Global(), func(q *store.Queries) error { return q.MakeAdmin(context.Background(), admin.userIDSelf()) }); err != nil { t.Fatal(err) } t.Cleanup(func() { admin.do("PUT", "/api/v1/admin/statuspage-home", map[string]any{"url": ""}) }) pages := e.statuspageClient(t, nil) st, body := admin.do("PUT", "/api/v1/admin/statuspage-home", map[string]any{"url": "javascript:alert(1)"}) expect(t, "script link", st, body, http.StatusUnprocessableEntity, "invalid_legal_url") if st, body := admin.do("PUT", "/api/v1/admin/statuspage-home", map[string]any{"url": " https://example.test/ "}); st != http.StatusNoContent { t.Fatalf("set home = %d %v", st, body) } if _, body := admin.do("GET", "/api/v1/admin/statuspage-home", nil); body["url"] != "https://example.test/" { t.Errorf("home = %v", body) } index, err := pages.Pages(context.Background()) if err != nil || index.HomeURL != "https://example.test/" { t.Errorf("page index home = %q %v", index.HomeURL, err) } if _, _, html := admin.page("/admin"); !strings.Contains(html, `value="https://example.test/"`) { t.Error("the admin page does not show the home page") } owner := e.browser(t) owner.signup("o@"+uniq("home")+".test", uniq("home-")) if st, _ := owner.do("PUT", "/api/v1/admin/statuspage-home", map[string]any{"url": "https://evil.test/"}); st != http.StatusForbidden { t.Errorf("non-admin sets the home page = %d, want 403", st) } }