package main
import (
"errors"
"os"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// Who owns a repository an account creates: the account; a person's
// agent's operator; the site admin answerable for a forge agent. A
// forge agent nobody answers for is refused rather than leaving a
// repository with no owner.
func TestARepositorysOwnerIsWhoAnswersForItsCreator(t *testing.T) {
rg := newTestRegistrar(t, 25)
for _, a := range []struct {
name string
admin bool
}{{"dan", true}, {"eve", true}, {"alice", false}, {"bot", false}} {
if err := rg.odb.AddAccount(a.name, "pw-long-enough-1", a.admin); err != nil {
t.Fatal(err)
}
}
if err := rg.odb.SetOperator("bot", "alice"); err != nil {
t.Fatal(err)
}
for agent, admin := range map[string]string{"zg": "dan", "orphan": "eve"} {
if err := rg.odb.NewForgeAgent(agent, admin); err != nil {
t.Fatal(err)
}
}
// A disabled admin answers for nobody, which leaves orphan unowned.
if err := rg.odb.SetDisabled("eve", true); err != nil {
t.Fatal(err)
}
for author, want := range map[string]string{"alice": "alice", "bot": "alice", "zg": "dan"} {
if got, err := rg.Owner(author); err != nil || got != want {
t.Errorf("FAIL: %s's repository is owned by %q (%v), want %q", author, got, err, want)
}
}
if got, err := rg.Owner("orphan"); !errors.Is(err, ops.ErrForbidden) {
t.Errorf("FAIL: an agent nobody answers for: owner %q, err %v", got, err)
}
}
// A clone whose create was withdrawn while it copied (its row is gone)
// is removed when the copy finishes, not served and not mirrored.
func TestAWithdrawnImportIsNotServed(t *testing.T) {
rg, up, _, _, _ := followRig(t)
store := filepath.Join(t.TempDir(), "withdrawn")
spec := api.RepoSpec{Name: "withdrawn", Path: store, Clone: up, Git: true, Mirror: true}
if err := rg.odb.SetMirror("withdrawn", "https://example.org/withdrawn.git", oltp.MinMirrorInterval); err != nil {
t.Fatal(err)
}
rg.importClone(spec, spec)
if _, err := os.Stat(store); !errors.Is(err, os.ErrNotExist) {
t.Errorf("FAIL: the withdrawn import's store is still on disk: %v", err)
}
if _, served := rg.deps.Repos.Get("withdrawn"); served {
t.Errorf("FAIL: the withdrawn import is served")
}
if _, found, _ := rg.odb.MirrorFor("withdrawn"); found {
t.Errorf("FAIL: the withdrawn import is still mirrored")
}
}