-
Notifications
You must be signed in to change notification settings - Fork 0
/
coingate_test.go
183 lines (136 loc) · 3.51 KB
/
coingate_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package coingate_test
import (
"fmt"
"github.com/chekalskiy/go-coingate"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
"time"
)
const (
AppID = 1015
APIKey = "WV5FGZRuoCstBiHayYXbp3"
APISecret = "ni6FmIUTxBSR8PK2JeDA5NVjpzc4tqZO"
)
var Coingate *coingate.Coingate
var BetweenTests struct {
Order coingate.Order
}
func init() {
Coingate = coingate.New(AppID, APIKey, APISecret, true)
}
func TestCoingate_Ping(t *testing.T) {
res := Coingate.Ping()
if res != true {
t.Fatal("Ping doesn't responeded")
}
}
func TestCoingate_CreateOrder(t *testing.T) {
price := "10.01"
o, err := Coingate.CreateOrder(coingate.CreateOrderRequest{
Price: price,
Currency: "USD",
ReceiveCurrency: "BTC",
})
if err != nil {
t.Fatal("Error on create order")
}
if o.ID <= 0 {
t.Fatal("Wrong response from server")
}
if o.Price != price {
t.Fatal("Wrong price in created order")
}
if len(o.PaymentURL) == 0 {
t.Fatal("Empty payment URL")
}
BetweenTests.Order = o
}
func TestCoingate_GetOrder(t *testing.T) {
o, err := Coingate.GetOrder(BetweenTests.Order.ID)
if err != nil {
t.Fatal("Error on getting order")
}
if !reflect.DeepEqual(BetweenTests.Order, o) {
t.Fatal("Order doesn't match created")
}
}
func TestCoingate_ListOrders(t *testing.T) {
o, err := Coingate.ListOrders(coingate.ListOrdersRequest{})
if err != nil {
t.Fatal("Error on getting orders")
}
if !reflect.DeepEqual(BetweenTests.Order, o.Orders[0]) {
t.Fatal("Order is not found in list")
}
o, err = Coingate.ListOrders(coingate.ListOrdersRequest{
Page: -10,
PerPage: 0,
Sort: "bad_sort",
})
if err != nil {
t.Fatal("Error on getting orders")
}
if !reflect.DeepEqual(BetweenTests.Order, o.Orders[0]) {
t.Fatal("Order is not found in list")
}
}
func TestCoingate_ProcessCallback(t *testing.T) {
testTime := time.Now()
testParams := fmt.Sprintf(`id=12345&order_id=ORDER-1415020039&status=paid&price=1050.99¤cy=USD&receive_currency=EUR&receive_amount=926.73&btc_amount=4.849315&created_at=%s`, url.QueryEscape(testTime.Format("2006-01-02T15:04:05-07:00")))
r, err := http.NewRequest("POST", "", strings.NewReader(testParams))
if err != nil {
t.Fatal("Request creating error", err)
}
r.Header = http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
}
cd, err := Coingate.ProcessCallback(r)
if err != nil {
t.Fatal("Error processing callback", err)
}
if cd.ID != 12345 {
t.Fatal("Wrong callback object ID")
}
if cd.CreatedAt.Format(time.RFC3339) != testTime.Format(time.RFC3339) {
t.Fatal("Wrong callback object time")
}
// ---------
r, err = http.NewRequest("POST", "", nil)
if err != nil {
t.Fatal("Request creating error", err)
}
cd, err = Coingate.ProcessCallback(r)
if err == nil {
t.Fatal("Should be an error")
}
// ---------
r, err = http.NewRequest("POST", "", strings.NewReader("bad=1"))
if err != nil {
t.Fatal("Request creating error", err)
}
r.Header = http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
}
cd, _ = Coingate.ProcessCallback(r)
if cd.ID > 0 {
t.Fatal("Request creating error", err)
}
}
func TestCoingate_WithErrors(t *testing.T) {
_, err := Coingate.CreateOrder(coingate.CreateOrderRequest{})
if err == nil {
t.Fatal("Should be an error")
}
_, err = Coingate.GetOrder(0)
if err == nil {
t.Fatal("Should be an error")
}
BadCoingate := coingate.New(0, "", "", true)
_, err = BadCoingate.ListOrders(coingate.ListOrdersRequest{})
if err == nil {
t.Fatal("Should be an error")
}
}