Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798package main
import ( "database/sql" "strings" "testing"
_ "github.com/mattn/go-sqlite3")
const schema = `CREATE TABLE domain_claims ( id INTEGER PRIMARY KEY AUTOINCREMENT, did TEXT NOT NULL, domain TEXT NOT NULL UNIQUE, deleted TEXT);CREATE UNIQUE INDEX idx_domain_claims_active_did ON domain_claims(did) WHERE deleted IS NULL;`
func claimDB(t *testing.T, seed string) *sql.DB { t.Helper() db, err := sql.Open("sqlite3", ":memory:") if err != nil { t.Fatal(err) } t.Cleanup(func() { db.Close() }) if _, err := db.Exec(schema + seed); err != nil { t.Fatal(err) } return db}
func runClaim(t *testing.T, db *sql.DB, did, handle string) { t.Helper() var b strings.Builder writeClaimSQL(&b, did, handle) b.WriteString("INSERT OR IGNORE INTO domain_claims (did, domain, deleted) VALUES ('" + did + "', '" + handle + "', NULL);") if _, err := db.Exec(b.String()); err != nil { t.Fatalf("claim SQL: %v", err) }}
func activeOwner(t *testing.T, db *sql.DB, domain string) string { t.Helper() var owner string err := db.QueryRow("SELECT did FROM domain_claims WHERE domain = ? AND deleted IS NULL", domain).Scan(&owner) if err == sql.ErrNoRows { return "" } if err != nil { t.Fatal(err) } return owner}
func TestClaimSQLFreesAnExpiredDomainAndKeepsTheAccountsTombstones(t *testing.T) { db := claimDB(t, ` INSERT INTO domain_claims (did, domain, deleted) VALUES ('did:plc:z', 'bob.tngl.sh', strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '-40 days')), ('did:plc:y', 'bob.tngl.io', strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '-40 days')), ('did:plc:y', 'blog.tngl.io', strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '-2 days'));`) runClaim(t, db, "did:plc:y", "bob.tngl.sh") if owner := activeOwner(t, db, "bob.tngl.sh"); owner != "did:plc:y" { t.Fatalf("bob.tngl.sh owner = %q, want did:plc:y", owner) } var tombstones int if err := db.QueryRow("SELECT COUNT(*) FROM domain_claims WHERE did = 'did:plc:y' AND deleted IS NOT NULL").Scan(&tombstones); err != nil { t.Fatal(err) } if tombstones != 2 { t.Fatalf("did:plc:y tombstones = %d, want 2", tombstones) }}
func TestClaimSQLLeavesAnAccountWithAnActiveClaimAlone(t *testing.T) { db := claimDB(t, ` INSERT INTO domain_claims (did, domain, deleted) VALUES ('did:plc:y', 'site.tngl.io', NULL), ('did:plc:y', 'bob.tngl.sh', strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '-2 days'));`) runClaim(t, db, "did:plc:y", "bob.tngl.sh") if owner := activeOwner(t, db, "site.tngl.io"); owner != "did:plc:y" { t.Fatalf("site.tngl.io owner = %q, want did:plc:y", owner) } if owner := activeOwner(t, db, "bob.tngl.sh"); owner != "" { t.Fatalf("bob.tngl.sh owner = %q, want it left released", owner) }}
func TestClaimSQLKeepsAnothersDomainInCooldown(t *testing.T) { db := claimDB(t, ` INSERT INTO domain_claims (did, domain, deleted) VALUES ('did:plc:z', 'bob.tngl.sh', strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '-2 days'));`) runClaim(t, db, "did:plc:y", "bob.tngl.sh") if owner := activeOwner(t, db, "bob.tngl.sh"); owner != "" { t.Fatalf("bob.tngl.sh owner = %q, want it still released", owner) }}