Skip to content
quarry Artifact [efe84ba36e]
Login

Artifact [efe84ba36e]

Artifact efe84ba36e4e8411c9e2001d6fae503a20ca6847b256401ec0c115a8fa0ff447:


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, and nothing else.
func TestARepoAdminCanReachTheSettingsFromTheRepo(t *testing.T) {
	cog := regexp.MustCompile(`class="cog aux1[^"]*"[^>]*href="[^"]*/settings"`)
	render := func(d pageData) string {
		var b strings.Builder
		if err := noHooks.template(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")
	}
	// The site flag is not the letter (#318): a site admin who does not
	// administer this repository is not offered its settings.
	site := base
	site.IsAdmin = true
	if got := render(site); cog.MatchString(got) {
		t.Error("FAIL: a site admin with no role here is offered 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")
	}
}