Skip to content
quarry Artifact [27d38a7a09]
Login

Artifact [27d38a7a09]

Artifact 27d38a7a09b68bae6041e7150ee173b23d6c7376b824949b0a4f54326cb94d20:


package oltp

import (
	"path/filepath"
	"testing"
)

// Every repository gets an address, namespace/handle, and the handle of
// one made before namespaced names is its name (RFC 0025): nothing that
// holds a name changes meaning. Two repositories may not share an
// address.
func TestEveryRepositoryHasItsNameAsItsHandle(t *testing.T) {
	path := filepath.Join(t.TempDir(), "oltp.db")
	d, err := Open(path)
	if err != nil {
		t.Fatal(err)
	}
	if err := d.AddRepo("tools", t.TempDir(), "", Public); err != nil {
		t.Fatal(err)
	}
	if err := d.AddImportingRepo("mirror", filepath.Join(t.TempDir(), "m"), "", Public, "https://example.com/m"); err != nil {
		t.Fatal(err)
	}
	// A row from before the column: no handle yet.
	if _, err := d.w.Exec(`UPDATE repo SET handle = '' WHERE name = 'tools'`); err != nil {
		t.Fatal(err)
	}
	d.Close()
	d, err = Open(path)
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { d.Close() })
	for _, name := range []string{"tools", "mirror"} {
		var handle string
		if err := d.db.QueryRow(`SELECT handle FROM repo WHERE name = ?`, name).Scan(&handle); err != nil || handle != name {
			t.Errorf("FAIL: %s has handle %q (%v), want its name", name, handle, err)
		}
	}
	// One address, one repository.
	if _, err := d.w.Exec(`UPDATE repo SET handle = 'tools' WHERE name = 'mirror'`); err == nil {
		t.Error("FAIL: two repositories took the same address")
	}
}

// The forge's namespace is reserved in the pool accounts and teams share.
func TestTheForgeNamespaceIsReserved(t *testing.T) {
	d, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { d.Close() })
	for _, name := range []string{ForgeNamespace, "ZeitForge"} {
		if err := d.AddAccount(name, "a-long-enough-password", false); err == nil {
			t.Errorf("FAIL: an account took %q", name)
		}
	}
}