Skip to content
quarry Artifact [8a974f5a7d]
Login

Artifact [8a974f5a7d]

Artifact 8a974f5a7dbd454c21543a2d8590b26872038523504eb1518b68c32eb0b4b41e:


package oltp

import "testing"

// RFC 0008 §Visibility: the presets are the two strings, the column
// follows them, the fleet default is Public until a forge says
// otherwise, and a repository's own strings win over the fleet's.
func TestVisibilityPresetsAndTheColumnAgree(t *testing.T) {
	db := testDB(t)
	if err := db.EnsureRepo("r", t.TempDir()); err != nil {
		t.Fatal(err)
	}
	if got := db.FleetVisibility(); got != "Public project" {
		t.Fatalf("fleet default %q", got)
	}
	if got := db.VisibilityName("r"); got != "Public project" {
		t.Fatalf("new repo reads %q", got)
	}
	if !db.EffectiveCaps("", "r").HasAny("2") || !db.EffectiveCaps("someone", "r").HasAny("C") {
		t.Fatal("Public project should give visitors the forum and the signed-in chat")
	}
	if err := db.SetVisibility("r", "Members only"); err != nil {
		t.Fatal(err)
	}
	if db.EffectiveCaps("", "r").HasAny("o") || !db.EffectiveCaps("someone", "r").HasAny("o") {
		t.Fatal("Members only: visitors nothing, signed-in read")
	}
	if priv, _ := db.PrivateRepos(); priv["r"] {
		t.Fatal("Members only is not private")
	}
	if err := db.SetVisibility("r", "Private"); err != nil {
		t.Fatal(err)
	}
	if priv, _ := db.PrivateRepos(); !priv["r"] {
		t.Fatal("Private did not set the column")
	}
	// The API's flag is the same thing, both ways.
	if err := db.SetRepoVisibility("r", Public); err != nil {
		t.Fatal(err)
	}
	if got := db.VisibilityName("r"); got != "Public project" {
		t.Fatalf("public via the flag reads %q, want the fleet default", got)
	}
	if err := db.SetRepoVisibility("r", Private); err != nil {
		t.Fatal(err)
	}
	if got := db.VisibilityName("r"); got != "Private" {
		t.Fatalf("private via the flag reads %q", got)
	}
	// Blanking the strings by hand is the same act.
	if err := db.SetVisibility("r", "Public project"); err != nil {
		t.Fatal(err)
	}
	must := func(err error) {
		if err != nil {
			t.Fatal(err)
		}
	}
	must(db.SetCategoryCaps("r", "nobody", ""))
	must(db.SetCategoryCaps("r", "anonymous", ""))
	if priv, _ := db.PrivateRepos(); !priv["r"] {
		t.Fatal("blank strings did not set the column")
	}
	if err := db.SetVisibility("r", "Open bar"); err == nil {
		t.Fatal("an unknown preset must not write anything")
	}
	// The fleet default moves new repositories, not ones with rows.
	must(db.SetFleetVisibility("Members only"))
	if got := db.VisibilityName("r"); got != "Private" {
		t.Fatalf("own rows lost to the fleet default: %q", got)
	}
	must(db.EnsureRepo("fresh", t.TempDir()))
	if got := db.VisibilityName("fresh"); got != "Members only" {
		t.Fatalf("new repo did not take the fleet default: %q", got)
	}
}

func TestReachOnlyByCategoryIsWhoLosesAccess(t *testing.T) {
	db := testDB(t)
	for _, n := range []string{"alice", "bob"} {
		if err := db.AddAccount(n, "pw12345678", false); err != nil {
			t.Fatal(err)
		}
	}
	if err := db.AddAccount("root", "pw12345678", true); err != nil {
		t.Fatal(err)
	}
	if err := db.Grant("alice", "r", Write); err != nil {
		t.Fatal(err)
	}
	// A site admin with no role here reads a public repository by the
	// categories like anyone, and loses it like anyone (#318).
	got, err := db.ReachOnlyByCategory("r")
	if err != nil || len(got) != 2 || got[0] != "bob" || got[1] != "root" {
		t.Fatalf("got %v, %v", got, err)
	}
}

// The participation boxes ride on the anonymous string without
// changing which preset it reads as.
func TestParticipationIsSeparateFromThePreset(t *testing.T) {
	if got := VisibilityOf("gjorzh2", "mnc"); got != "Public project" {
		t.Fatalf("tickets only reads as %q", got)
	}
	if got := VisibilityOf("", "gjorzh2m"); got != "Members only" {
		t.Fatalf("members with nothing ticked reads as %q", got)
	}
	if got := VisibilityOf("gjorzh2", "m3"); got != "Public project" {
		t.Fatalf("forum only reads as %q", got)
	}
	if got := WithParticipation("mnc3C", "nc"); got != "mnc" {
		t.Fatalf("tickets only = %q", got)
	}
	if got := WithParticipation("m", "3C"); got != "m3C" {
		t.Fatalf("forum and chat = %q", got)
	}
	if got := WithParticipation("gjorzh2mnc3C", ""); got != "gjorzh2m" {
		t.Fatalf("nothing = %q", got)
	}
}