package api_test
import (
"bytes"
"io"
"net/http"
"strings"
"testing"
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
)
func (b *browser) raw(path string) (int, http.Header, []byte) {
b.t.Helper()
resp, err := b.hc.Get(b.base + path)
if err != nil {
b.t.Fatalf("GET %s failed: %v", path, err)
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(resp.Body)
return resp.StatusCode, resp.Header, body
}
func TestPublicPagesForSearchEngines(t *testing.T) {
const publicURL = "https://app.example.test"
cloud := newEnvWith(t, func(c *config.API) {
c.Edition, c.SignupVerify, c.PublicURL = config.EditionCloud, "off", publicURL
})
b := cloud.browser(t)
_, _, html := b.page("/login?next=/monitors&lang=de")
for _, want := range []string{
``,
``,
``,
``,
``,
} {
if !strings.Contains(html, want) {
t.Errorf("login page lacks %s", want)
}
}
if strings.Contains(html, "noindex") {
t.Errorf("login page is noindex in the cloud edition")
}
if _, _, html := b.page("/signup"); !strings.Contains(html, ``) {
t.Errorf("signup page lacks its canonical address")
}
_, _, robots := b.raw("/robots.txt")
if !strings.Contains(string(robots), "Allow: /login\n") || !strings.Contains(string(robots), "Disallow: /\n") || !strings.Contains(string(robots), "Sitemap: "+publicURL+"/sitemap.xml") {
t.Errorf("robots.txt = %q", robots)
}
code, _, sm := b.raw("/sitemap.xml")
if code != http.StatusOK || !strings.Contains(string(sm), ""+publicURL+"/signup") || !strings.Contains(string(sm), `hreflang="de" href="`+publicURL+`/login?lang=de"`) {
t.Errorf("sitemap = %d %s", code, sm)
}
code, h, png := b.raw("/og.png?lang=en")
if code != http.StatusOK || h.Get("Content-Type") != "image/png" || !bytes.HasPrefix(png, []byte("\x89PNG")) {
t.Errorf("preview image = %d %s", code, h.Get("Content-Type"))
}
// Signed in, the console stays out of search engines.
b.signup("seo@"+uniq("seo")+".test", uniq("seo-"))
if _, _, html := b.page("/"); !strings.Contains(html, ``) || strings.Contains(html, "og:title") {
t.Errorf("console page is indexable")
}
// Self-hosted installations keep everything out.
self := newEnv(t).browser(t)
if _, _, html := self.page("/login"); !strings.Contains(html, "noindex") || strings.Contains(html, "canonical") {
t.Errorf("self-hosted login page is indexable")
}
if _, _, robots := self.raw("/robots.txt"); string(robots) != "User-agent: *\nDisallow: /\n" {
t.Errorf("self-hosted robots.txt = %q", robots)
}
if code, _, _ := self.raw("/sitemap.xml"); code != http.StatusNotFound {
t.Errorf("self-hosted sitemap = %d, want 404", code)
}
}