Skip to content
quarry Artifact [7009353cc9]
Login

Artifact [7009353cc9]

Artifact 7009353cc9ac5d67b1c2d1ca5e8dd9b004f588dcc80e4d9c31c230632abc44f4:


package oltp

import "testing"

// The visibility box adds 8 to nobody for one repository, records the
// change like its neighbours, leaves the preset reading as the preset,
// and refuses a repository the public cannot read.
func TestCIPublicAddsEightToNobodyAndIsLogged(t *testing.T) {
	db := testDB(t)
	if err := db.AddRepo("proj", t.TempDir(), "", Public); err != nil {
		t.Fatal(err)
	}
	if err := db.SetVisibility("proj", "Public project"); err != nil {
		t.Fatal(err)
	}
	if db.EffectiveCaps("", "proj").HasAny("8") {
		t.Fatal("FAIL: a visitor holds 8 on a public preset by default")
	}
	changes, err := db.PatchRepo("proj", RepoPatch{CIPublic: flag(true)}, "dmestas", "")
	if err != nil {
		t.Fatal(err)
	}
	if len(changes) != 1 || changes[0].Field != "ci-public" || changes[0].Was != "false" || changes[0].Now != "true" {
		t.Errorf("FAIL: the change recorded as %+v", changes)
	}
	if !db.EffectiveCaps("", "proj").HasAny("8") {
		t.Errorf("FAIL: with the box ticked a visitor lacks 8")
	}
	if got := db.VisibilityName("proj"); got != "Public project" {
		t.Errorf("FAIL: the box turned the preset into %q", got)
	}
	if again, err := db.PatchRepo("proj", RepoPatch{CIPublic: flag(true)}, "dmestas", ""); err != nil || len(again) != 0 {
		t.Errorf("FAIL: ticking an already ticked box recorded %+v, %v", again, err)
	}
	if _, err := db.PatchRepo("proj", RepoPatch{CIPublic: flag(false)}, "dmestas", ""); err != nil {
		t.Fatal(err)
	}
	if db.EffectiveCaps("", "proj").HasAny("8") {
		t.Errorf("FAIL: with the box cleared a visitor still holds 8")
	}

	if err := db.AddRepo("members", t.TempDir(), "", Public); err != nil {
		t.Fatal(err)
	}
	if err := db.SetVisibility("members", "Members only"); err != nil {
		t.Fatal(err)
	}
	if _, err := db.PatchRepo("members", RepoPatch{CIPublic: flag(true)}, "dmestas", ""); err == nil {
		t.Errorf("FAIL: the box was taken on a repository visitors cannot read")
	}
	if got := db.VisibilityName("members"); got != "Members only" {
		t.Errorf("FAIL: the refused box left the repository %q", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}