-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_payment_test.go
120 lines (101 loc) · 4.2 KB
/
get_payment_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
package handcash
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHTTPGetPayment for mocking requests
type mockHTTPGetPayment struct{}
// Do is a mock http request
func (m *mockHTTPGetPayment) 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+endpointGetPaymentRequest {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"transactionId":"4eb7ab228ab9a23831b5b788e3f0eb5bed6dcdbb6d9d808eaba559c49afb9b0a","note":"Test description","type":"send","time":1608222315,"satoshiFees":113,"satoshiAmount":5301,"fiatExchangeRate":188.6311183023935,"fiatCurrencyCode":"USD","participants":[{"type":"user","alias":"[email protected]","displayName":"MrZ","profilePictureUrl":"https://www.gravatar.com/avatar/372bc0ab9b8a8930d4a86b2c5b11f11e?d=identicon","responseNote":""}],"attachments":[],"appAction":"like"}`)))
}
// Default is valid
return resp, nil
}
// mockHTTPInvalidPaymentData for mocking requests
type mockHTTPInvalidPaymentData struct{}
// Do is a mock http request
func (m *mockHTTPInvalidPaymentData) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"invalid":"payment"}`)))
// Default is valid
return resp, nil
}
func TestClient_GetPayment(t *testing.T) {
t.Parallel()
t.Run("missing auth token", func(t *testing.T) {
client := newTestClient(&mockHTTPGetPayment{}, EnvironmentBeta)
assert.NotNil(t, client)
payment, err := client.GetPayment(context.Background(), "", "")
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("missing transaction id", func(t *testing.T) {
client := newTestClient(&mockHTTPGetPayment{}, EnvironmentBeta)
assert.NotNil(t, client)
payment, err := client.GetPayment(context.Background(), "000000", "")
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("invalid auth token", func(t *testing.T) {
client := newTestClient(&mockHTTPGetPayment{}, EnvironmentBeta)
assert.NotNil(t, client)
payment, err := client.GetPayment(context.Background(), "0", "000000")
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("bad request", func(t *testing.T) {
client := newTestClient(&mockHTTPBadRequest{}, EnvironmentBeta)
assert.NotNil(t, client)
payment, err := client.GetPayment(context.Background(), "000000", "000000")
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)
payment, err := client.GetPayment(context.Background(), "000000", "000000")
assert.Error(t, err)
assert.Nil(t, payment)
})
t.Run("valid payment response", func(t *testing.T) {
client := newTestClient(&mockHTTPGetPayment{}, EnvironmentBeta)
assert.NotNil(t, client)
payment, err := client.GetPayment(context.Background(), "000000", "000000")
assert.NoError(t, err)
assert.NotNil(t, payment)
assert.Equal(t, "4eb7ab228ab9a23831b5b788e3f0eb5bed6dcdbb6d9d808eaba559c49afb9b0a", payment.TransactionID)
assert.Equal(t, uint64(1608222315), payment.Time)
assert.Equal(t, PaymentSend, payment.Type)
assert.Equal(t, AppActionLike, payment.AppAction)
assert.Equal(t, CurrencyUSD, payment.FiatCurrencyCode)
assert.Equal(t, 188.6311183023935, payment.FiatExchangeRate)
assert.Equal(t, "Test description", payment.Note)
assert.Equal(t, uint64(5301), payment.SatoshiAmount)
assert.Equal(t, uint64(113), 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)
})
}