package web
import (
"strings"
"testing"
)
// A create control follows the letter its op requires.
//
// jva measured this signed out AND signed in as a role-less account:
// the forum's New thread, the tracker's New ticket, the notes' new
// note and the wiki's Create were all drawn unconditionally. Signed
// out each posted into a bare 303 to /login that discarded the typed
// text; signed in each answered 403, "this account does not have the
// capability this request needs" (0457b6e6).
//
// The letters: forum.post wants 3, ticket.new and ticket.intake want
// n, the technote write gate wants k, wiki.save wants kf.
func TestACreateControlFollowsItsOpsLetter(t *testing.T) {
for _, c := range []struct {
name, control string
grant func(*pageData)
}{
{"tracker New ticket", `/ticket/new`, func(d *pageData) { d.CanFile = true }},
{"forum New thread", `/forum-new`, func(d *pageData) { d.CanPost = true }},
{"notes new note", `/technote-new`, func(d *pageData) { d.CanNote = true }},
} {
var without, with pageData
without.Repo, with.Repo = "alpha", "alpha"
without.SignedIn, with.SignedIn = "jan", "jan"
c.grant(&with)
cold := renderBase(t, without)
if strings.Contains(cold, `href="/r/alpha`+c.control+`"`) {
t.Errorf("FAIL: %s is offered to an account whose op refuses it", c.name)
}
warm := renderBase(t, with)
if !strings.Contains(warm, `href="/r/alpha`+c.control+`"`) {
t.Errorf("FAIL: %s is withheld from an account that holds its letter", c.name)
}
}
}
// The wiki's Create is a form, not a link, so it is checked on its own.
func TestTheWikisCreateFormFollowsTheWikiLetters(t *testing.T) {
render := func(can bool) string {
var b strings.Builder
d := pageData{Repo: "alpha", SignedIn: "jan", CanWikiNew: can}
if err := wikiListPage.ExecuteTemplate(&b, "content", d); err != nil {
t.Fatal(err)
}
return b.String()
}
if strings.Contains(render(false), `action="/r/alpha/wiki-new"`) {
t.Error("FAIL: the wiki offers Create to an account wiki.save refuses")
}
if !strings.Contains(render(true), `action="/r/alpha/wiki-new"`) {
t.Error("FAIL: the wiki withholds Create from an account holding k or f")
}
}
// A forge with no capability plane keeps every control.
//
// The same fallback mayCap and needCI take: an ungated forge has no
// strangers, and hiding the only way in would be worse than offering
// it. Only a KNOWN capability set may take a control away.
func TestAnUngatedForgeKeepsItsCreateControls(t *testing.T) {
src := webSource(t)
for _, want := range []string{
"data.CanPost, data.CanFile = true, true",
"data.CanNote, data.CanWikiNew = true, true",
} {
if !strings.Contains(src, want) {
t.Errorf("FAIL: the layout does not default the create controls on (%s); a "+
"forge with no capability plane would hide its own way in", want)
}
}
}