package web
import (
"regexp"
"strings"
"testing"
)
// A repository's settings are reachable from the repository.
//
// The cog was gated on IsAdmin, which is the SITE admin flag —
// sessions.Allow(user, "", ops.Admin), resolved against the empty
// repository, and its own comment says it exists to draw the fleet
// link. So the people who administer a repository could not reach its
// settings from it: the page was there and nothing linked it, which is
// half of f44e0c66.
//
// RepoAdmin is the `a` letter on THIS repository, with a site admin
// still clearing it the way every other gate lets them.
func TestARepoAdminCanReachTheSettingsFromTheRepo(t *testing.T) {
cog := regexp.MustCompile(`class="cog aux1[^"]*"[^>]*href="[^"]*/settings"`)
render := func(d pageData) string {
var b strings.Builder
if err := navFragPage.ExecuteTemplate(&b, "repo-tabs", d); err != nil {
t.Fatal(err)
}
return b.String()
}
base := pageData{Repo: "alpha", SignedIn: "dan", Section: "tree"}
if got := render(base); cog.MatchString(got) {
t.Error("FAIL: a reader with no letters is offered the settings cog")
}
admin := base
admin.RepoAdmin = true
if got := render(admin); !cog.MatchString(got) {
t.Error("FAIL: a repository's own admin is not offered its settings; the page " +
"exists and nothing links it")
}
// And the site admin keeps it, which is the fallback that made the
// old gate look like it worked.
site := base
site.IsAdmin, site.RepoAdmin = true, true
if got := render(site); !cog.MatchString(got) {
t.Error("FAIL: a site admin lost the settings cog")
}
}
// The site flag and the repository letter are different questions, and
// the nav must not read one for the other.
func TestTheSiteFlagDoesNotStandInForTheRepoLetter(t *testing.T) {
src := webSource(t)
if strings.Contains(src, `{{if .IsAdmin}}<a class="cog aux1`) {
t.Error("FAIL: the repository settings cog reads IsAdmin, the SITE flag; a " +
"repository's own admin does not clear it")
}
}
// /login tells a person without an account where they stand.
//
// The page names nobody to ask (see TestAVisitorIsNotSentToAPerson);
// it says the forge is in closed alpha. The handler has to resolve
// InviteOnly for that line to be reachable at all.
func TestLoginSaysClosedAlpha(t *testing.T) {
if !strings.Contains(webSource(t), "InviteOnly: inviteOnlyForge(r, req),") {
t.Error("FAIL: GET /login does not resolve InviteOnly, so the " +
"closed-alpha line can never be drawn")
} else {
t.Log("ok: GET /login resolves InviteOnly")
}
}