Skip to content
quarry Artifact [f36f7fe556]
Login

Artifact [f36f7fe556]

Artifact f36f7fe55648e572153547ddec50a244bde6a62bf8e3a25bc7f5a48328031942:


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