Skip to content
quarry Artifact [504bf20323]
Login

Artifact [504bf20323]

Artifact 504bf20323155ca63474f15691d2a00f948c5a297d57b94b09a2a5716494c5e8:


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)
	}
	got, err := db.ReachOnlyByCategory("r")
	if err != nil || len(got) != 1 || got[0] != "bob" {
		t.Fatalf("got %v, %v", got, err)
	}
}