64 lines
2.5 KiB
Go
64 lines
2.5 KiB
Go
package agentapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// R-1151 — the claim asks the agent to keep the package until the words come back, and reads the agent's answer;
|
|
// the confirm carries the two words. An agent older than 0.160.0 answers without confirm_required.
|
|
func TestEscrowClaimAndConfirm_R1151(t *testing.T) {
|
|
var claimBody map[string]any
|
|
var confirmBody map[string][]string
|
|
newAgent := true
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("POST /escrow/ceremony/claim", func(w http.ResponseWriter, r *http.Request) {
|
|
claimBody = nil
|
|
_ = json.NewDecoder(r.Body).Decode(&claimBody)
|
|
if newAgent {
|
|
_, _ = w.Write([]byte(`{"ok":true,"data":{"recovery_code":"a-b-c","confirm_required":true,"verify_positions":[1,3]}}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"ok":true,"data":{"recovery_code":"a-b-c"}}`))
|
|
})
|
|
mux.HandleFunc("POST /escrow/ceremony/confirm", func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewDecoder(r.Body).Decode(&confirmBody)
|
|
if confirmBody["words"][0] != "a" {
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
_, _ = w.Write([]byte(`{"ok":false,"error":"the words do not match the recovery code"}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"ok":true,"data":{"uploaded":true}}`))
|
|
})
|
|
s := httptest.NewTLSServer(mux)
|
|
defer s.Close()
|
|
c := clientFor(t, s, strings.TrimPrefix(s.URL, "https://"))
|
|
|
|
claim, status, err := c.EscrowCeremonyClaim(context.Background())
|
|
if err != nil || status != http.StatusOK || claim.Code != "a-b-c" || !claim.ConfirmRequired || len(claim.Positions) != 2 || claim.Positions[1] != 3 {
|
|
t.Fatalf("claim: %+v %d %v", claim, status, err)
|
|
}
|
|
if claimBody["defer_upload"] != true {
|
|
t.Fatalf("the claim did not ask the agent to wait for the words: body %v", claimBody)
|
|
}
|
|
if st, err := c.EscrowCeremonyConfirm(context.Background(), []string{"a", "c"}); err != nil || st != http.StatusOK {
|
|
t.Fatalf("confirm: %d %v", st, err)
|
|
}
|
|
if strings.Join(confirmBody["words"], ",") != "a,c" {
|
|
t.Fatalf("confirm sent %v", confirmBody)
|
|
}
|
|
if st, err := c.EscrowCeremonyConfirm(context.Background(), []string{"x", "c"}); err == nil || st != http.StatusUnprocessableEntity {
|
|
t.Fatalf("a wrong word must come back as a 422 error: %d %v", st, err)
|
|
}
|
|
|
|
newAgent = false // agent 0.159.2: no confirm request
|
|
claim, _, err = c.EscrowCeremonyClaim(context.Background())
|
|
if err != nil || claim.ConfirmRequired || claim.Code != "a-b-c" {
|
|
t.Fatalf("claim on an old agent: %+v %v", claim, err)
|
|
}
|
|
}
|