github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/invoice_test.go (about) 1 package rest_test 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/invoice" 10 "github.com/bitfinexcom/bitfinex-api-go/v2/rest" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestGenerateInvoice(t *testing.T) { 16 t.Run("unsupported currency", func(t *testing.T) { 17 c := rest.NewClient() 18 19 args := rest.DepositInvoiceRequest{ 20 Currency: "ETH", 21 Wallet: "exchange", 22 Amount: "0.0001", 23 } 24 25 invc, err := c.Invoice.GenerateInvoice(args) 26 require.NotNil(t, err) 27 require.Nil(t, invc) 28 }) 29 30 t.Run("amount too small", func(t *testing.T) { 31 c := rest.NewClient() 32 33 args := rest.DepositInvoiceRequest{ 34 Currency: "LNX", 35 Wallet: "exchange", 36 Amount: "0.0000001", 37 } 38 39 invc, err := c.Invoice.GenerateInvoice(args) 40 require.NotNil(t, err) 41 require.Nil(t, invc) 42 }) 43 44 t.Run("amount too large", func(t *testing.T) { 45 c := rest.NewClient() 46 47 args := rest.DepositInvoiceRequest{ 48 Currency: "LNX", 49 Wallet: "exchange", 50 Amount: "0.03", 51 } 52 53 invc, err := c.Invoice.GenerateInvoice(args) 54 require.NotNil(t, err) 55 require.Nil(t, invc) 56 }) 57 58 t.Run("response data slice too short", func(t *testing.T) { 59 handler := func(w http.ResponseWriter, r *http.Request) { 60 respMock := []interface{}{"invoicehash"} 61 payload, _ := json.Marshal(respMock) 62 _, err := w.Write(payload) 63 require.Nil(t, err) 64 } 65 66 server := httptest.NewServer(http.HandlerFunc(handler)) 67 defer server.Close() 68 69 c := rest.NewClientWithURL(server.URL) 70 71 pld := rest.DepositInvoiceRequest{ 72 Currency: "LNX", 73 Wallet: "exchange", 74 Amount: "0.0001", 75 } 76 77 invc, err := c.Invoice.GenerateInvoice(pld) 78 require.NotNil(t, err) 79 require.Nil(t, invc) 80 }) 81 82 t.Run("valid response data", func(t *testing.T) { 83 handler := func(w http.ResponseWriter, r *http.Request) { 84 assert.Equal(t, "/auth/w/deposit/invoice", r.RequestURI) 85 assert.Equal(t, "POST", r.Method) 86 respMock := []interface{}{ 87 "invoicehash", 88 "invoice", 89 nil, 90 nil, 91 "0.002", 92 } 93 payload, _ := json.Marshal(respMock) 94 _, err := w.Write(payload) 95 require.Nil(t, err) 96 } 97 98 server := httptest.NewServer(http.HandlerFunc(handler)) 99 defer server.Close() 100 101 c := rest.NewClientWithURL(server.URL) 102 103 pld := rest.DepositInvoiceRequest{ 104 Currency: "LNX", 105 Wallet: "exchange", 106 Amount: "0.002", 107 } 108 109 invc, err := c.Invoice.GenerateInvoice(pld) 110 require.Nil(t, err) 111 112 expected := &invoice.Invoice{ 113 InvoiceHash: "invoicehash", 114 Invoice: "invoice", 115 Amount: "0.002", 116 } 117 assert.Equal(t, expected, invc) 118 }) 119 }