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")
}
}
// Clearing a box that is already clear writes nothing: a repository that
// follows the fleet default is not given a row of its own by it.
func TestCIPublicClearedWhenClearLeavesNoRow(t *testing.T) {
db := testDB(t)
if err := db.AddRepo("follower", t.TempDir(), "", Public); err != nil {
t.Fatal(err)
}
rows := func() int {
var n int
if err := db.db.QueryRow(`SELECT count(*) FROM repo_category_caps WHERE repo = 'follower'`).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
before := rows()
changes, err := db.PatchRepo("follower", RepoPatch{CIPublic: flag(false)}, "dmestas", "")
if err != nil || len(changes) != 0 {
t.Errorf("FAIL: clearing a clear box recorded %+v, %v", changes, err)
}
if rows() != before {
t.Errorf("FAIL: clearing a clear box wrote a category row (%d -> %d)", before, rows())
}
if !t.Failed() {
t.Log("ok")
}
}