mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-20 09:14:16 +00:00
96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package authgate
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveHoldDID_UserDefaultBeatsFallback(t *testing.T) {
|
|
d := newTestDB(t)
|
|
seedUser(t, d, "did:plc:alice", "alice.test", "did:plc:hold999")
|
|
|
|
r := holdResolver{db: d, defaultHoldDID: "did:plc:fallback"}
|
|
got, err := r.resolveHoldDID(context.Background(), "did:plc:alice")
|
|
if err != nil {
|
|
t.Fatalf("resolveHoldDID: %v", err)
|
|
}
|
|
if got != "did:plc:hold999" {
|
|
t.Errorf("got %q, want did:plc:hold999 (DB cache must beat fallback)", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveHoldDID_FallbackUsedWhenNullCache(t *testing.T) {
|
|
d := newTestDB(t)
|
|
// User row exists but default_hold_did is NULL.
|
|
seedUser(t, d, "did:plc:alice", "alice.test", "")
|
|
|
|
r := holdResolver{db: d, defaultHoldDID: "did:plc:fallback"}
|
|
got, err := r.resolveHoldDID(context.Background(), "did:plc:alice")
|
|
if err != nil {
|
|
t.Fatalf("resolveHoldDID: %v", err)
|
|
}
|
|
if got != "did:plc:fallback" {
|
|
t.Errorf("got %q, want did:plc:fallback", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveHoldDID_FallbackUsedWhenNoUserRow(t *testing.T) {
|
|
d := newTestDB(t)
|
|
// No user row at all — sql.ErrNoRows path.
|
|
r := holdResolver{db: d, defaultHoldDID: "did:plc:fallback"}
|
|
got, err := r.resolveHoldDID(context.Background(), "did:plc:nobody")
|
|
if err != nil {
|
|
t.Fatalf("resolveHoldDID: %v", err)
|
|
}
|
|
if got != "did:plc:fallback" {
|
|
t.Errorf("got %q, want did:plc:fallback (ErrNoRows must fall through)", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveHoldDID_BothEmptyReturnsEmpty(t *testing.T) {
|
|
d := newTestDB(t)
|
|
seedUser(t, d, "did:plc:alice", "alice.test", "")
|
|
|
|
r := holdResolver{db: d, defaultHoldDID: ""}
|
|
got, err := r.resolveHoldDID(context.Background(), "did:plc:alice")
|
|
if err != nil {
|
|
t.Fatalf("resolveHoldDID: %v", err)
|
|
}
|
|
if got != "" {
|
|
t.Errorf("got %q, want empty (graceful-degrade contract)", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveHoldDID_PassesDIDPLCThrough(t *testing.T) {
|
|
// did:plc input short-circuits atproto.ResolveHoldDID's network call
|
|
// via its IsDID check. This is the no-network contract that lets the
|
|
// rest of the test suite stay hermetic.
|
|
d := newTestDB(t)
|
|
seedUser(t, d, "did:plc:alice", "alice.test", "did:plc:hold-abc-123")
|
|
|
|
r := holdResolver{db: d}
|
|
got, err := r.resolveHoldDID(context.Background(), "did:plc:alice")
|
|
if err != nil {
|
|
t.Fatalf("resolveHoldDID: %v", err)
|
|
}
|
|
if got != "did:plc:hold-abc-123" {
|
|
t.Errorf("got %q, want did:plc:hold-abc-123", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveHoldDID_DBErrorWrapped(t *testing.T) {
|
|
d := newTestDB(t)
|
|
// Closing the DB before the query forces a real driver error.
|
|
_ = d.Close()
|
|
|
|
r := holdResolver{db: d, defaultHoldDID: "did:plc:fallback"}
|
|
_, err := r.resolveHoldDID(context.Background(), "did:plc:alice")
|
|
if err == nil {
|
|
t.Fatal("expected error from closed DB")
|
|
}
|
|
if !strings.Contains(err.Error(), "look up default hold") {
|
|
t.Errorf("error %q should mention 'look up default hold'", err)
|
|
}
|
|
}
|