Skip to content
quarry Artifact [f05c859bc8]
Login

Artifact [f05c859bc8]

Artifact f05c859bc821cfb48608715be91d74862313463ad55740947760e458ce618f57:


package api

import (
	"path/filepath"
	"testing"

	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

// A member of a team reaches every repository under it (RFC 0018), and
// the dashboard has to say so. GrantsFor reads the grant ROWS, and
// membership is a union in EffectiveCaps rather than a row per member
// per repository — so somebody whose only access is a team saw an
// empty dashboard and the "you were invited to look around" page, on a
// forge where they already reach things.
func TestTheDashboardCountsWhatATeamReaches(t *testing.T) {
	db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	for _, who := range []string{"owen", "mia"} {
		if err := db.AddAccount(who, "a-long-enough-password", false); err != nil {
			t.Fatal(err)
		}
	}
	if err := db.AddTeam("grove", "Grove", "", "owen"); err != nil {
		t.Fatal(err)
	}
	if err := db.SetTeamMember("grove", "mia", "", false, true); err != nil {
		t.Fatal(err)
	}
	if err := db.SetTeamRepo("grove", "orchard", "owen"); err != nil {
		t.Fatal(err)
	}
	d := Deps{Teams: db, Accounts: acctAdapter{db}, Repos: testRepos(nil)}

	// mia holds no grant row anywhere; her only route is the team.
	if grants, err := d.Accounts.GrantsFor("mia"); err != nil || len(grants) != 0 {
		t.Fatalf("FAIL: the premise is wrong — mia has grants %v (err=%v)", grants, err)
	}
	_, names, err := d.myRepos(ops.WithUser(t.Context(), "mia"), 50)
	if err != nil {
		t.Fatal(err)
	}
	if len(names) != 1 || names[0] != "orchard" {
		t.Errorf("FAIL: the dashboard offers mia %v, want [orchard]", names)
	}
}

// A repository reached BOTH ways appears once. The grant row and the
// team both name it, and a listing that showed it twice would look
// like two repositories.
func TestAReachableRepositoryIsListedOnce(t *testing.T) {
	db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	if err := db.AddAccount("mia", "a-long-enough-password", false); err != nil {
		t.Fatal(err)
	}
	if err := db.AddTeam("grove", "Grove", "", "mia"); err != nil {
		t.Fatal(err)
	}
	if err := db.SetTeamRepo("grove", "orchard", "mia"); err != nil {
		t.Fatal(err)
	}
	if err := db.SetCaps("mia", "orchard", "a"); err != nil {
		t.Fatal(err)
	}
	d := Deps{Teams: db, Accounts: acctAdapter{db}, Repos: testRepos(nil)}
	_, names, err := d.myRepos(ops.WithUser(t.Context(), "mia"), 50)
	if err != nil {
		t.Fatal(err)
	}
	if len(names) != 1 {
		t.Errorf("FAIL: orchard listed %d times: %v", len(names), names)
	}
}

// A site admin's dashboard is what their grants and teams reach, like
// anybody's (#318). A fleet-scope row was read as "the fleet is yours"
// and handed an admin with no grants every repository's feed.
func TestASiteAdminsDashboardIsTheirGrantsOnly(t *testing.T) {
	db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	if err := db.AddAccount("root", "a-long-enough-password", true); err != nil {
		t.Fatal(err)
	}
	if err := db.SetCaps("root", "", "a"); err != nil {
		t.Fatal(err)
	}
	d := Deps{Teams: db, Accounts: acctAdapter{db}, Repos: testRepos(nil)}
	_, names, err := d.myRepos(ops.WithUser(t.Context(), "root"), 50)
	if err != nil {
		t.Fatal(err)
	}
	if len(names) != 0 {
		t.Errorf("FAIL: a site admin with no grants is offered %v", names)
	}
	if err := db.Grant("root", "test", oltp.Read); err != nil {
		t.Fatal(err)
	}
	if _, names, _ = d.myRepos(ops.WithUser(t.Context(), "root"), 50); len(names) != 1 || names[0] != "test" {
		t.Errorf("FAIL: a site admin granted test is offered %v", names)
	}
}

// An account reaching more repositories than a list shows keeps the
// most recently active, not the first names alphabetically: capitals and
// early letters sorted first, and a busy repository named later in the
// alphabet (quarry) fell off its owner's dashboard and feed.
func TestTheBusiestRepositoriesSurviveTheCut(t *testing.T) {
	busy, zb, _ := attachStore(t)
	quiet, _, _ := attachStore(t)
	d := Deps{Version: "v", Repos: NewRepos(map[string]*Repo{
		"zz-busy":  {Index: busy, Zit: zb, Sync: zitSync(zb, busy)},
		"Aa-quiet": {Index: quiet},
		"Bb-quiet": {Index: quiet},
	})}
	r := ops.NewRegistry()
	if err := RegisterAll(r, d); err != nil {
		t.Fatal(err)
	}
	if _, err := r.Invoke(as("dan", "3"), "forum.post", forumPostIn{Repo: "zz-busy", Title: "hello", Body: "first\n"}); err != nil {
		t.Fatal(err)
	}
	got := d.mostActive([]string{"Aa-quiet", "Bb-quiet", "zz-busy"}, 2)
	has := map[string]bool{}
	for _, n := range got {
		has[n] = true
	}
	if len(got) != 2 || !has["zz-busy"] {
		t.Fatalf("FAIL: the cut kept %v; the one active repository must survive it", got)
	}
	t.Log("ok: the cut keeps the active repository whatever its name")
}