Skip to content
quarry Artifact [0d33fa5a3c]
Login

Artifact [0d33fa5a3c]

Artifact 0d33fa5a3c49410b3f2d0e959ac1167f96021e10712754daa4ce837bf67098a2:


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")
	}
}

// Every link into a repository's admin pages follows the letter, never
// the site flag: a site admin with no role there would follow it to a
// refusal (#318). The settings link in the tab row and the clone
// count's traffic link both.
func TestTheSiteFlagDrawsNoRepositoryAdminLinks(t *testing.T) {
	render := func(d pageData) string {
		var b strings.Builder
		if err := noHooks.template(treePage).Execute(&b, d); err != nil {
			t.Fatal(err)
		}
		return b.String()
	}
	base := pageData{Repo: "alpha", Title: "alpha", SignedIn: "root", CloneCount: 3}
	site := base
	site.IsAdmin = true
	got := render(site)
	for _, link := range []string{`/alpha/settings"`, `/alpha/traffic"`} {
		if strings.Contains(got, link) {
			t.Errorf("FAIL: the site flag alone draws a link to %s", link)
		}
	}
	admin := base
	admin.RepoAdmin = true
	got = render(admin)
	for _, link := range []string{`/alpha/settings"`, `/alpha/traffic"`} {
		if !strings.Contains(got, link) {
			t.Errorf("FAIL: the repository's admin is not offered %s", link)
		}
	}
}