package main
import (
"net/http"
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
// A private team's pages are not found to an outsider and its name is
// absent from the directory; to a member they are the team's pages.
func TestAPrivateTeamsPagesAreNotFoundToOutsiders(t *testing.T) {
g := newSiteRig(t)
if err := g.db.AddTeam("hush", "Hush", "Quiet work", "alice"); err != nil {
t.Fatal(err)
}
if _, err := g.db.SetTeamPrivate("hush", true); err != nil {
t.Fatal(err)
}
for _, p := range []string{"/t/hush", "/t/hush/people", "/t/hush/tickets", "/t/hush/avatar", "/api/v1/teams/hush"} {
if code, _ := g.doAs("", http.MethodGet, p, "", "", nil); code != http.StatusNotFound {
t.Errorf("FAIL: %s answered a stranger %d", p, code)
}
}
if _, body := g.doAs("", http.MethodGet, "/teams", "", "", nil); strings.Contains(body, "/t/hush") {
t.Error("FAIL: the directory names a private team to a stranger")
}
if code, body := g.doAs("alice", http.MethodGet, "/api/v1/teams/hush", doorPassword, "", nil); code != http.StatusOK || !strings.Contains(body, `"private":true`) {
t.Errorf("FAIL: a member cannot read the private team: %d %s", code, body)
}
}
// A private team's name is in no address: its repositories appear
// under their owner, and the team's path for one is no redirect to it.
// Public again, they are back under the team.
func TestAPrivateTeamsRepositoriesAppearUnderTheirOwner(t *testing.T) {
db := newTestRegistrar(t, 25).odb
if err := db.AddRepo("quiet", t.TempDir(), "", oltp.Public); err != nil {
t.Fatal(err)
}
if err := db.SetOwner("quiet", "alice"); err != nil {
t.Fatal(err)
}
if err := db.AddTeam("hush", "Hush", "", "alice"); err != nil {
t.Fatal(err)
}
if err := db.SetTeamRepo("hush", "quiet", "alice"); err != nil {
t.Fatal(err)
}
place := placeOf(db)
if ns, _ := place("quiet"); ns != "hush" {
t.Fatalf("a public team's repository appears under %q", ns)
}
if _, err := db.SetTeamPrivate("hush", true); err != nil {
t.Fatal(err)
}
if ns, ok := place("quiet"); !ok || ns != "alice" {
t.Errorf("FAIL: a private team's repository appears under %q (%v)", ns, ok)
}
if db.WasPlacedAt("quiet", "hush") {
t.Error("FAIL: the team's path is remembered as a past place, which redirects")
}
if _, err := db.SetTeamPrivate("hush", false); err != nil {
t.Fatal(err)
}
if ns, _ := place("quiet"); ns != "hush" {
t.Errorf("FAIL: public again, the repository appears under %q", ns)
}
}