package handoff import ( "amadan.net/rastrillo/rastrillo/assertion" "context" "database/sql" "testing" ) func TestReceivePendingRefusesUnknownNonce(t *testing.T) { f := fresh(t) _, nonce := f.pending(t) f.db.Exec("DELETE FROM oficina_handoff_pending") if _, err := f.s.ReceivePending(context.Background(), f.token(t, nonce), f.now); err != ErrRefused { t.Fatalf("unknown nonce: %v", err) } if count(t, f.db, "oficina_handoff_inbox") != 0 { t.Fatal("retained unsolicited receipt") } } func TestFinishAtomicRollsBackAppAndProof(t *testing.T) { f := fresh(t) secret, nonce := f.pending(t) handle, _ := f.received(t, nonce) f.db.Exec("CREATE TABLE minted (subject TEXT)") _, _, err := f.s.FinishAtomic(context.Background(), secret, handle, "", f.now, func(tx *sql.Tx, c assertion.Claims, _ string) error { if _, e := tx.Exec("INSERT INTO minted VALUES(?)", c.Subject); e != nil { t.Fatal(e) } return ErrRefused }) if err != ErrRefused || count(t, f.db, "minted") != 0 || count(t, f.db, "oficina_handoff_pending") != 1 { t.Fatal("partial commit", err) } _, _, err = f.s.FinishAtomic(context.Background(), secret, handle, "", f.now, func(tx *sql.Tx, c assertion.Claims, _ string) error { _, e := tx.Exec("INSERT INTO minted VALUES(?)", c.Subject) return e }) if err != nil || count(t, f.db, "minted") != 1 || count(t, f.db, "oficina_handoff_pending") != 0 { t.Fatal("atomic completion", err) } }