package web
import (
"bytes"
"net/http"
"net/url"
"strings"
"testing"
)
// The confirmation asks for the acknowledgement on a mirror, and says a
// repository that tracks nothing is already canonical.
func TestCanonicalPageAsksOnlyOnAMirror(t *testing.T) {
render := func(d pageData) string {
t.Helper()
var buf bytes.Buffer
if err := noHooks.template(repoCanonicalPage).ExecuteTemplate(&buf, "content", d); err != nil {
t.Fatal(err)
}
return buf.String()
}
mirror := pageData{Repo: "zit"}
mirror.Posture.Mirror.Tracking, mirror.Posture.Mirror.URL = true, "https://fossil.example.org/zit"
got := render(mirror)
if !strings.Contains(got, `data-hook="make-canonical"`) || !strings.Contains(got, `name="ack" value="fork" required`) {
t.Error("a mirror's confirmation lacks the button or the required acknowledgement")
}
native := render(pageData{Repo: "alpha"})
if strings.Contains(native, `data-hook="make-canonical"`) || !strings.Contains(native, `data-hook="already-canonical"`) {
t.Error("a repository that tracks nothing is offered a detach")
}
}
// Without the acknowledgement the POST detaches nothing and sends the
// admin back to the confirmation; a native repository's Danger zone has
// no canonical row.
func TestCanonicalPostNeedsTheAcknowledgement(t *testing.T) {
plane := &memCaps{stored: map[string]string{"dan": "a"}}
srv := peopleServer(t, plane)
resp := postAsDan(t, srv, "/r/alpha/settings/canonical", url.Values{})
if resp.StatusCode != http.StatusSeeOther || !strings.HasSuffix(resp.Header.Get("Location"), "/r/alpha/settings/canonical") {
t.Fatalf("status %d, location %q", resp.StatusCode, resp.Header.Get("Location"))
}
if strings.Contains(asDan(t, srv, "/r/alpha/settings"), `data-hook="canonical-row"`) {
t.Error("a native repository's Danger zone offers to make it canonical")
}
}