-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpay_test.go
157 lines (135 loc) · 6 KB
/
pay_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package handcash
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHTTPPay for mocking requests
type mockHTTPPay struct{}
// Do is a mock http request
func (m *mockHTTPPay) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Beta
if req.URL.String() == environments[EnvironmentBeta].APIURL+endpointGetPayRequest {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"transactionId":"05d7df52a1c58cabada16709469e6940342cb13e8cfa3c7e1438d7ea84765787","note":"Thanks dude!","type":"send","time":1608226019,"satoshiFees":127,"satoshiAmount":5372,"fiatExchangeRate":186.15198556884275,"fiatCurrencyCode":"USD","participants":[{"type":"user","alias":"[email protected]","displayName":"MrZ","profilePictureUrl":"https://www.gravatar.com/avatar/372bc0ab9b8a8930d4a86b2c5b11f11e?d=identicon","responseNote":""}],"attachments":[{"value":{"some":"data"},"format":"json"}],"appAction":"like","rawTransactionHex":"01000000018598fbea559e4a59772361994f800adb63bab592e276de7ebd5805ecc639b3b8010000006a47304402200fc98489e2bbba5cb7f8cea970c0037585d42618ef60d172179307b4446854a802206be468ffd31f97c6e01a6549be50241d42633e32ba4e06ff4b2565ec897232a2412103c1fbc71737d3820890535112ac99b2471d6bacbd8a7e7825c65863a67b1d0c7effffffff03000000000000000012006a0f7b22736f6d65223a2264617461227dfc140000000000001976a914b7ce7a4c1350f1cb9dcaecca10d48f064be9197f88ac57020000000000001976a9145233794b8bdf2fd7f809b11da081189d2e79000c88ac00000000"}`)))
}
// Default is valid
return resp, nil
}
func TestClient_Pay(t *testing.T) {
t.Parallel()
t.Run("missing auth token", func(t *testing.T) {
client := newTestClient(&mockHTTPPay{}, EnvironmentBeta)
assert.NotNil(t, client)
payment, err := client.Pay(context.Background(), "", nil)
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("missing payment parameters", func(t *testing.T) {
client := newTestClient(&mockHTTPPay{}, EnvironmentBeta)
assert.NotNil(t, client)
payment, err := client.Pay(context.Background(), "000000", nil)
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("missing payment receivers", func(t *testing.T) {
client := newTestClient(&mockHTTPPay{}, EnvironmentBeta)
assert.NotNil(t, client)
payParams := &PayParameters{
AppAction: AppActionLike,
Description: "Test description",
}
payment, err := client.Pay(context.Background(), "000000", payParams)
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("invalid auth token", func(t *testing.T) {
client := newTestClient(&mockHTTPPay{}, EnvironmentBeta)
assert.NotNil(t, client)
payParams := &PayParameters{
AppAction: AppActionLike,
Description: "Test description",
Receivers: []*Payment{{
Amount: 0.01,
CurrencyCode: CurrencyUSD,
To: "[email protected]",
}},
}
payment, err := client.Pay(context.Background(), "0", payParams)
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("bad request", func(t *testing.T) {
client := newTestClient(&mockHTTPBadRequest{}, EnvironmentBeta)
assert.NotNil(t, client)
payParams := &PayParameters{
AppAction: AppActionLike,
Description: "Test description",
Receivers: []*Payment{{
Amount: 0.01,
CurrencyCode: CurrencyUSD,
To: "[email protected]",
}},
}
payment, err := client.Pay(context.Background(), "000000", payParams)
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("invalid payment data", func(t *testing.T) {
client := newTestClient(&mockHTTPInvalidPaymentData{}, EnvironmentBeta)
assert.NotNil(t, client)
payParams := &PayParameters{
AppAction: AppActionLike,
Description: "Test description",
Receivers: []*Payment{{
Amount: 0.01,
CurrencyCode: CurrencyUSD,
To: "[email protected]",
}},
}
payment, err := client.Pay(context.Background(), "000000", payParams)
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("valid payment", func(t *testing.T) {
client := newTestClient(&mockHTTPPay{}, EnvironmentBeta)
assert.NotNil(t, client)
payParams := &PayParameters{
AppAction: AppActionLike,
Description: "Test description",
Receivers: []*Payment{{
Amount: 0.01,
CurrencyCode: CurrencyUSD,
To: "[email protected]",
}},
}
payment, err := client.Pay(context.Background(), "000000", payParams)
assert.NoError(t, err)
assert.NotNil(t, payment)
assert.Equal(t, "05d7df52a1c58cabada16709469e6940342cb13e8cfa3c7e1438d7ea84765787", payment.TransactionID)
assert.Equal(t, "01000000018598fbea559e4a59772361994f800adb63bab592e276de7ebd5805ecc639b3b8010000006a47304402200fc98489e2bbba5cb7f8cea970c0037585d42618ef60d172179307b4446854a802206be468ffd31f97c6e01a6549be50241d42633e32ba4e06ff4b2565ec897232a2412103c1fbc71737d3820890535112ac99b2471d6bacbd8a7e7825c65863a67b1d0c7effffffff03000000000000000012006a0f7b22736f6d65223a2264617461227dfc140000000000001976a914b7ce7a4c1350f1cb9dcaecca10d48f064be9197f88ac57020000000000001976a9145233794b8bdf2fd7f809b11da081189d2e79000c88ac00000000", payment.RawTransactionHex)
assert.Equal(t, uint64(1608226019), payment.Time)
assert.Equal(t, PaymentSend, payment.Type)
assert.Equal(t, AppActionLike, payment.AppAction)
assert.Equal(t, CurrencyUSD, payment.FiatCurrencyCode)
assert.Equal(t, 186.15198556884275, payment.FiatExchangeRate)
assert.Equal(t, "Thanks dude!", payment.Note)
assert.Equal(t, uint64(5372), payment.SatoshiAmount)
assert.Equal(t, uint64(127), payment.SatoshiFees)
assert.Equal(t, 1, len(payment.Participants))
assert.Equal(t, ParticipantUser, payment.Participants[0].Type)
assert.Equal(t, "", payment.Participants[0].ResponseNote)
assert.Equal(t, "[email protected]", payment.Participants[0].Alias)
assert.Equal(t, "MrZ", payment.Participants[0].DisplayName)
assert.Equal(t, "https://www.gravatar.com/avatar/372bc0ab9b8a8930d4a86b2c5b11f11e?d=identicon", payment.Participants[0].ProfilePictureURL)
})
}