nutreq_test.go raw

   1  package nutreq
   2  
   3  import (
   4  	"strings"
   5  	"testing"
   6  )
   7  
   8  func TestRoundTrip(t *testing.T) {
   9  	p := New(500, "https://mint.example.com", "musiquay access")
  10  	p.Nut10 = &NUT10Option{Kind: "P2PK", Data: strings.Repeat("02", 33)}
  11  	s, err := p.EncodeA()
  12  	if err != nil {
  13  		t.Fatal(err)
  14  	}
  15  	if !strings.HasPrefix(s, Prefix) {
  16  		t.Fatalf("encoded request does not start with %q", Prefix)
  17  	}
  18  	got, err := Decode(s)
  19  	if err != nil {
  20  		t.Fatal(err)
  21  	}
  22  	if got.Amount != 500 || got.Unit != "sat" || got.Description != "musiquay access" {
  23  		t.Fatalf("fields did not survive: %+v", got)
  24  	}
  25  	if len(got.Mints) != 1 || got.Mints[0] != "https://mint.example.com" {
  26  		t.Fatalf("mint list did not survive: %v", got.Mints)
  27  	}
  28  	if got.Nut10 == nil || got.Nut10.Kind != "P2PK" || got.Nut10.Data != p.Nut10.Data {
  29  		t.Fatalf("nut10 did not survive: %+v", got.Nut10)
  30  	}
  31  }
  32  
  33  func TestAmountWithoutUnitRejected(t *testing.T) {
  34  	if _, err := (&PaymentRequest{Amount: 10}).EncodeA(); err == nil {
  35  		t.Fatal("encoded an amount without a unit")
  36  	}
  37  	// CBOR {"a": 10} with no unit.
  38  	if _, err := Decode(Prefix + "oWFhCg"); err == nil {
  39  		t.Fatal("decoded an amount without a unit")
  40  	}
  41  }
  42  
  43  func TestDecodeRejectsOtherEncodings(t *testing.T) {
  44  	for _, s := range []string{"", "cashuB...", "creqB1something"} {
  45  		if _, err := Decode(s); err == nil {
  46  			t.Fatalf("decoded %q", s)
  47  		}
  48  	}
  49  }
  50  
  51  func TestAcceptsMint(t *testing.T) {
  52  	p := New(10, "https://mint.example.com/", "")
  53  	if !p.AcceptsMint("https://mint.example.com") {
  54  		t.Fatal("trailing slash broke mint matching")
  55  	}
  56  	if p.AcceptsMint("https://other.example") {
  57  		t.Fatal("an unlisted mint was accepted")
  58  	}
  59  	empty := New(10, "", "")
  60  	if !empty.AcceptsMint("https://anything") {
  61  		t.Fatal("an empty mint list should accept any mint")
  62  	}
  63  }
  64