package api_test import ( "context" "errors" "net/http" "strings" "sync" "testing" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5" "git.zb-server.de/ZB-Server/uptimesebi/internal/api" "git.zb-server.de/ZB-Server/uptimesebi/internal/config" "git.zb-server.de/ZB-Server/uptimesebi/internal/notify/notifytest" ) // fakeDNS answers CNAME lookups from a map, and TXT lookups with nothing. type fakeDNS struct { mu sync.Mutex cname map[string]string } func (d *fakeDNS) set(name, target string) { d.mu.Lock() defer d.mu.Unlock() if target == "" { delete(d.cname, name) } else { d.cname[name] = target } } func useFakeDNS(t *testing.T) *fakeDNS { d := &fakeDNS{cname: map[string]string{}} savedC, savedT := *api.LookupCNAME, *api.LookupTXT t.Cleanup(func() { *api.LookupCNAME, *api.LookupTXT = savedC, savedT }) *api.LookupCNAME = func(_ context.Context, name string) (string, error) { d.mu.Lock() defer d.mu.Unlock() if c, ok := d.cname[name]; ok { return c, nil } return "", errors.New("no such host") } *api.LookupTXT = func(context.Context, string) ([]string, error) { return nil, errors.New("no such record") } return d } func TestCustomDomainByCNAME(t *testing.T) { mail := notifytest.NewSMTP(t) e := newEnvWith(t, func(c *config.API) { c.SMTPURL, c.SignupVerify = mail.URL(), "off" c.StatusPageURL = "https://status.platform.test" }) ctx := context.Background() dns := useFakeDNS(t) platform := e.statuspageClient(t, nil) newPage := func(b *browser) uuid.UUID { slug := uniq("cn-") _, p := b.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Shop status"}) b.do("PUT", "/api/v1/org/statuspages/"+p["id"].(string), map[string]any{"slug": slug, "title": "Shop status", "published": true}) return uuid.MustParse(p["id"].(string)) } setDomain := func(b *browser, id uuid.UUID, domain string) string { t.Helper() st, body := b.do("PUT", "/api/v1/org/statuspages/"+id.String()+"/domain", map[string]any{"domain": domain}) if st != http.StatusOK { t.Fatalf("set domain = %d %v", st, body) } rec, _ := body["cname_record"].(map[string]any) if rec == nil || rec["name"] != domain { t.Fatalf("cname_record = %v", body["cname_record"]) } return rec["value"].(string) } served := func(domain string) bool { _, err := platform.Page(ctx, domain, "") return err == nil } a := e.browser(t) aEmail := "a@" + uniq("cn") + ".test" a.signup(aEmail, uniq("cn-")) pageA := newPage(a) domain := uniq("status") + ".shop.test" target := setDomain(a, pageA, domain) if !strings.HasSuffix(target, ".status.platform.test") || len(target) < 30 { t.Errorf("CNAME target %q is not unique to the page below the status host", target) } if _, _, html := a.page("/statuspages/" + pageA.String()); !strings.Contains(html, target+".") || !strings.Contains(html, "Check now") { t.Error("the editor does not show the page's CNAME record") } // A CNAME to the shared host alone proves nothing. now := time.Now() dns.set(domain, "status.platform.test.") if err := e.api.WatchDomainOf(ctx, now, pageA); err != nil { t.Fatal(err) } if served(domain) { t.Fatal("a CNAME to the shared host verified the domain") } // The page's own target verifies it, without anybody pressing a button. dns.set(domain, strings.ToUpper(target)+".") if err := e.api.WatchDomainOf(ctx, now.Add(time.Minute), pageA); err != nil { t.Fatal(err) } if !served(domain) { t.Fatal("the watch did not verify the domain by its CNAME") } if _, _, html := a.page("/statuspages/" + pageA.String()); !strings.Contains(html, "verified by its CNAME record") { t.Error("the editor does not say how the domain was verified") } // Another organization taking the domain over, while the CNAME still // points at the first page, gets nowhere. a.do("PUT", "/api/v1/org/statuspages/"+pageA.String()+"/domain", map[string]any{"domain": ""}) b := e.browser(t) b.signup("b@"+uniq("cn")+".test", uniq("cn-")) pageB := newPage(b) setDomain(b, pageB, domain) if err := e.api.WatchDomainOf(ctx, now.Add(2*time.Minute), pageB); err != nil { t.Fatal(err) } if st, _ := b.do("POST", "/api/v1/org/statuspages/"+pageB.String()+"/domain/verify", nil); st != http.StatusUnprocessableEntity || served(domain) { t.Fatalf("a record left by another page verified the domain (verify = %d)", st) } b.do("PUT", "/api/v1/org/statuspages/"+pageB.String()+"/domain", map[string]any{"domain": ""}) // Once verified, the record is checked daily. Gone, the owners are told // and, a week later, the domain is switched off. setDomain(a, pageA, domain) if st, body := a.do("POST", "/api/v1/org/statuspages/"+pageA.String()+"/domain/verify", nil); st != http.StatusNoContent { t.Fatalf("verify = %d %v", st, body) } dns.set(domain, "") day := now.Add(25 * time.Hour) if err := e.api.WatchDomainOf(ctx, day, pageA); err != nil { t.Fatal(err) } waitFor(t, 5*time.Second, "a mail that the record is gone", func() bool { return mailedTo(mail, aEmail, "is gone", domain) }) if !served(domain) { t.Error("the domain was switched off at once") } if _, _, html := a.page("/statuspages/" + pageA.String()); !strings.Contains(html, "has not been found since") { t.Error("the editor does not warn about the missing record") } if err := e.api.WatchDomainOf(ctx, day.Add(8*24*time.Hour), pageA); err != nil { t.Fatal(err) } if served(domain) { t.Error("the domain is still served a week after its record went") } waitFor(t, 5*time.Second, "a mail that the domain was switched off", func() bool { return mailedTo(mail, aEmail, "switched off", domain) }) } func TestDomainsVerifiedBeforeAreNotCheckedAgain(t *testing.T) { e := newEnvWith(t, func(c *config.API) { c.StatusPageURL = "https://status.platform.test" }) ctx := context.Background() useFakeDNS(t) owner := e.browser(t) owner.signup("o@"+uniq("old")+".test", uniq("old-")) slug := uniq("old-") _, p := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Status"}) id := p["id"].(string) owner.do("PUT", "/api/v1/org/statuspages/"+id, map[string]any{"slug": slug, "title": "Status", "published": true}) domain := uniq("old") + ".example.test" owner.do("PUT", "/api/v1/org/statuspages/"+id+"/domain", map[string]any{"domain": domain}) if err := e.asSystem(t, func(tx pgx.Tx) error { _, err := tx.Exec(ctx, `UPDATE status_pages SET domain_verified_at = now() - interval '1 year', domain_set_at = now() - interval '1 year' WHERE id = $1`, id) return err }); err != nil { t.Fatal(err) } if err := e.api.WatchDomainOf(ctx, time.Now().Add(30*24*time.Hour), uuid.MustParse(id)); err != nil { t.Fatal(err) } if _, err := e.statuspageClient(t, nil).Page(ctx, domain, ""); err != nil { t.Errorf("a domain verified before the checks was switched off: %v", err) } } func mailedTo(mail *notifytest.SMTP, to string, want ...string) bool { for _, m := range mail.Messages() { if !strings.Contains(strings.Join(m.To, ","), to) { continue } ok := true for _, w := range want { ok = ok && strings.Contains(m.Data, w) } if ok { return true } } return false }