package api_test import ( "context" "errors" "net/http" "strings" "testing" "github.com/google/uuid" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" "git.zb-server.de/ZB-Server/uptimesebi/internal/config" ) func TestCertificateOrdersAreLimited(t *testing.T) { e := newEnvWith(t, func(c *config.API) { c.StatusPageURL = "https://status.platform.test" c.Certificates = config.Certificates{PerOrgPerDay: 3, PerOwnerPerHour: 100, PerNamePerDay: 2} }) ctx := context.Background() owner := e.browser(t) owner.signup("o@"+uniq("cert")+".test", uniq("cert-")) platform := e.statuspageClient(t, nil) page := func(domain string) (string, map[string]any) { t.Helper() slug := uniq("cert-") _, 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}) st, body := owner.do("PUT", "/api/v1/org/statuspages/"+id+"/domain", map[string]any{"domain": domain}) if st != http.StatusOK { t.Fatalf("set domain = %d %v", st, body) } return id, body["domain_record"].(map[string]any) } // An unverified domain gets no certificate, nor does a made-up name. d1 := uniq("a") + ".example.test" id1, rec1 := page(d1) if err := platform.AllowCertificate(ctx, d1); !errors.Is(err, agentapi.ErrNotFound) { t.Errorf("unverified domain: %v, want not found", err) } if err := platform.AllowCertificate(ctx, uniq("random")+".example.test"); !errors.Is(err, agentapi.ErrNotFound) { t.Errorf("unknown name: %v, want not found", err) } // The editor explains the records, with the platform's servers as the // CNAME target. _, _, html := owner.page("/statuspages/" + id1) for _, want := range []string{"Create this CNAME record", ".status.platform.test.", "Prove the domain with this TXT record", "ALIAS or ANAME", "at most 10 times a day"} { if !strings.Contains(html, want) { t.Errorf("the editor lacks %q", want) } } verifyDomain(t, owner, id1, rec1) if err := platform.AllowCertificate(ctx, strings.ToUpper(d1)+"."); err != nil { t.Fatalf("verified domain: %v", err) } // A domain whose orders keep failing stops at its own limit. _ = platform.AllowCertificate(ctx, d1) if err := platform.AllowCertificate(ctx, d1); !errors.Is(err, agentapi.ErrRateLimited) { t.Errorf("third order for one domain: %v, want rate limited", err) } // The organization's limit covers all its domains. d2 := uniq("b") + ".example.test" id2, rec2 := page(d2) verifyDomain(t, owner, id2, rec2) if err := platform.AllowCertificate(ctx, d2); err != nil { t.Fatalf("second domain: %v", err) } if err := platform.AllowCertificate(ctx, d2); !errors.Is(err, agentapi.ErrRateLimited) { t.Errorf("order beyond the organization's limit: %v, want rate limited", err) } // Another organization's server is not allowed the domain at all. other := e.browser(t) other.signup("x@"+uniq("cert")+".test", uniq("cert-")) otherOrg := uuid.MustParse(other.orgID()) if err := e.statuspageClient(t, &otherOrg).AllowCertificate(ctx, d2); !errors.Is(err, agentapi.ErrNotFound) { t.Errorf("another organization's server: %v, want not found", err) } } func TestDomainChangesAreLimited(t *testing.T) { e := newEnv(t) owner := e.browser(t) owner.signup("o@"+uniq("dom")+".test", uniq("dom-")) _, p := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": uniq("dom-"), "title": "Status"}) id := p["id"].(string) base := uniq("d") for i := range 10 { st, body := owner.do("PUT", "/api/v1/org/statuspages/"+id+"/domain", map[string]any{"domain": base + string(rune('a'+i)) + ".example.test"}) if st != http.StatusOK { t.Fatalf("change %d = %d %v", i+1, st, body) } // Saving the same domain again does not count. owner.do("PUT", "/api/v1/org/statuspages/"+id+"/domain", map[string]any{"domain": base + string(rune('a'+i)) + ".example.test"}) } st, body := owner.do("PUT", "/api/v1/org/statuspages/"+id+"/domain", map[string]any{"domain": base + "z.example.test"}) expect(t, "eleventh change", st, body, http.StatusTooManyRequests, "domain_changes_exceeded") // Removing the domain is always possible. st, body = owner.do("PUT", "/api/v1/org/statuspages/"+id+"/domain", map[string]any{"domain": ""}) expect(t, "remove domain", st, body, http.StatusOK, "") }