Skip to content
quarry Artifact [dc20324a1f]
Login

Artifact [dc20324a1f]

Artifact dc20324a1f42e756deace635ccbd569fadc1a706ee35e32a4d1f8e07048108a2:


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)
	}
}