Phase 13, part 4: proving a team's domain

Before Supermarket crawls a team's website, reads its Search Console data
or publishes to it, the team proves the domain is theirs: a DNS TXT
record, a file on the site, or owning its Search Console property. One
team holds a domain at a time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 12:21:42 -07:00
co-authored by Claude Opus 5.5
parent d5fb1898e8
commit 47cc8b2b10
11 changed files with 444 additions and 8 deletions
+72
View File
@@ -1,7 +1,11 @@
package web
import (
"context"
"errors"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
@@ -248,3 +252,71 @@ func newTeamOwnerCookie(t *testing.T, s *Server, team int64) (*users.User, int64
t.Fatal("no owner")
return nil, 0, nil
}
func TestProvingTheDomain(t *testing.T) {
s, h := newMultiEdition(t)
a, _, ac := newTeam(t, s, "Acme", "olu@acme.example")
// Before the proof, what touches the site waits.
rec := as(h, ac, http.MethodPost, "/seo/audit?site=0", url.Values{})
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/settings/domain?msg=prove-first" {
t.Errorf("audit before the proof: %d %s", rec.Code, rec.Header().Get("Location"))
}
body := as(h, ac, http.MethodGet, "/settings/domain", nil).Body.String()
team, _ := users.GetTeam(s.platform(), a.ID)
if team.VerifyToken == "" || !strings.Contains(body, team.VerifyToken) || !strings.Contains(body, "acme.example") {
t.Fatalf("domain page: %s", body)
}
// DNS: wrong, then right.
records := []string{"v=spf1 -all"}
lookupTXT = func(_ context.Context, name string) ([]string, error) {
if name != "acme.example" {
return nil, errors.New("nxdomain")
}
return records, nil
}
defer func() { lookupTXT = net.DefaultResolver.LookupTXT }()
if rec := as(h, ac, http.MethodPost, "/settings/domain/check/dns", url.Values{}); rec.Code != http.StatusUnprocessableEntity {
t.Errorf("no record: %d", rec.Code)
}
records = append(records, team.VerifyToken)
if rec := as(h, ac, http.MethodPost, "/settings/domain/check/dns", url.Values{}); rec.Code != http.StatusSeeOther {
t.Fatalf("record: %d %s", rec.Code, rec.Body.String())
}
if tm, _ := users.GetTeam(s.platform(), a.ID); !tm.Proved() || tm.VerifiedBy != "dns" {
t.Errorf("not proved: %+v", tm)
}
if rec := as(h, ac, http.MethodPost, "/seo/audit?site=0", url.Values{}); rec.Header().Get("Location") == "/settings/domain?msg=prove-first" {
t.Error("still waiting after the proof")
}
// Another team on the same domain can't prove it while Acme holds it,
// even with the file in place.
other, _ := users.CreateTeam(s.platform(), "Impostor", "acme.example", time.Now())
s.Teams.Create(other.ID)
imp := &users.User{Email: "eve@evil.example", Status: users.Active}
users.Create(s.platform(), imp, time.Now())
users.Join(s.platform(), other.ID, imp.ID, users.Owner, nil, time.Now())
tok, _, _ := users.StartSessionIn(s.platform(), imp.ID, other.ID, "", "", time.Now())
ic := &http.Cookie{Name: sessionCookie, Value: tok}
otherTok, _ := users.VerifyToken(s.platform(), other.ID)
site := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == verifyPath {
w.Write([]byte(otherTok + "\n"))
return
}
http.NotFound(w, r)
}))
defer site.Close()
fileBase = func(string) string { return site.URL }
defer func() { fileBase = func(host string) string { return "https://" + host } }()
rec = as(h, ic, http.MethodPost, "/settings/domain/check/file", url.Values{})
if rec.Code != http.StatusConflict || !strings.Contains(rec.Body.String(), "Another team") {
t.Errorf("second team: %d %s", rec.Code, rec.Body.String())
}
// Released, the file proves it.
users.ReleaseDomain(s.platform(), a.ID)
if rec := as(h, ic, http.MethodPost, "/settings/domain/check/file", url.Values{}); rec.Code != http.StatusSeeOther {
t.Errorf("file: %d %s", rec.Code, rec.Body.String())
}
}