-
Notifications
You must be signed in to change notification settings - Fork 2
/
cicil_test.go
210 lines (173 loc) · 7.58 KB
/
cicil_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package cicil
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
)
const (
BaseURL = "https://sandbox-api.cicil.dev/v1"
MerchantID = "YOUR_MERCHANT_ID"
MerchantSecret = "YOUR_MERCHANT_SERCRET"
APIKey = "YOUR_API_KEY"
DefaultTimeout = 20 * time.Second
)
func TestCicilCreation(t *testing.T){
t.Run("Test Cicil Creation", func(t *testing.T) {
//construct cicil
//and add required parameters
cic := New(BaseURL,APIKey, MerchantID, MerchantSecret,DefaultTimeout)
if cic == nil {
t.Error("Cannot Call Cicil Constructor")
}
})
}
func TestCicil_GetCheckoutURL(t *testing.T) {
orderData := []byte(`{"transaction": {"total_amount": 13119000,"transaction_id": "ORD10111808","item_list": [{"item_id":"SKU101112","type": "product","name":"Notebook CICIL C12","price":12999000,"category":"laptop","url":"https://www.tokocicil.com/product/sku101112","quantity":1,"seller_id":"tokocicil-official"},{"item_id":"SKU131415","type": "product","name":"Sticker Aja","price":60000,"category":"accessories","url":"https://www.tokocicil.com/product/sku131415","quantity":2},{"item_id":"insurance","type": "fee","name":"Insurance Fee","price":5000,"quantity":1},{"item_id":"shipment_cost","type": "shipment_cost","name":"Shipping Fee","price":45000,"quantity":1},{"item_id":"CICIL1212","type": "discount","name":"Promo Harbolnas CICIL","price":-50000,"quantity":1}]},"buyer": {"fullname": "John Doe","email": "[email protected]","phone": "085322984060","address": "Jl. Sd Inpres RT.003/RW.006 No.174A 13950 Cakung, Pulogebang","city": "Jakarta Timur","district": "JK","postal_code": "11630","company": "Cicil","country": "ID"},"shipment": {"shipment_provider": "Flat rate","shipping_price": 40000,"shipping_tax": 0,"name": "John Doe","address": "Jl. Sd Inpres RT.003/RW.006 No.174A 13950 Cakung, Pulogebang","city": "Jakarta Timur","district": "Jakarta Timur","postal_code": "11630","phone": "085322984060","company": "Cicil","country": "ID"},"push_url": "https://api.tokocicil.com/update","redirect_url": "https://toko.cicil.dev","back_url": "https://toko.cicil.dev"}`)
t.Run("Test Cicil Get Order URL", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{ "message": "", "po_number": "PO210122-143655", "status": "success", "url": "https://sandbox-staging.cicil.dev/po/UE8yMTAxMjItMTQzNjU1" }`))
}))
//close server
defer ts.Close()
//construct cicil
amm := New(ts.URL, APIKey, MerchantID, MerchantSecret,DefaultTimeout)
var order CheckoutRequest
err := json.Unmarshal(orderData, &order)
if err != nil {
t.Error("Cannot Unmarshal Order JSON data")
}
_, errGetOrderURL := amm.GetCheckoutURL(order)
if errGetOrderURL != nil {
t.Errorf("GetOrderURL() returned an error: %s", errGetOrderURL.Error())
}
})
t.Run("Test Negative Case Cicil Get Order URL", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message":"invalid parameter on order request"}`))
}))
//close server
defer ts.Close()
//construct cicil
amm := New(ts.URL, APIKey, MerchantID, MerchantSecret,DefaultTimeout)
var order CheckoutRequest
err := json.Unmarshal(orderData, &order)
if err != nil {
t.Error("Cannot Unmarshal Order JSON data")
}
_, errGetOrderURL := amm.GetCheckoutURL(order)
if errGetOrderURL != nil {
assert.Error(t, errGetOrderURL)
}
})
}
func TestCicil_SetCancelOrder(t *testing.T) {
orderData := []byte(`{"po_number": "PO181112-123456","reason": "out of stock","cancelled_by": "tokocicil","total_amount":13119000,"transaction_id":"ORD10111808"}`)
t.Run("Test Cicil Get Order URL", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"success","message":""}`))
}))
//close server
defer ts.Close()
//construct cicil
cic := New(ts.URL, APIKey, MerchantID, MerchantSecret,DefaultTimeout)
var cancelOrder CancelOrderRequest
err := json.Unmarshal(orderData, &cancelOrder)
if err != nil {
t.Error("Cannot Unmarshal Order JSON data")
}
_, errCancelOrder := cic.SetCancelOrder(cancelOrder)
if errCancelOrder != nil {
t.Errorf("SetCancelOrder() returned an error: %s", errCancelOrder.Error())
}
})
t.Run("Test Negative Case Cicil Get Order URL", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message":"invalid parameter on order request"}`))
}))
//close server
defer ts.Close()
//construct cicil
amm := New(ts.URL, APIKey, MerchantID, MerchantSecret,DefaultTimeout)
var cancelOrder CancelOrderRequest
err := json.Unmarshal(orderData, &cancelOrder)
if err != nil {
t.Error("Cannot Unmarshal Order JSON data")
}
_, errCancelOrder := amm.SetCancelOrder(cancelOrder)
if errCancelOrder != nil {
assert.Error(t, errCancelOrder)
}
})
}
func TestCicil_UpdateStatus(t *testing.T) {
orderData := []byte(`{"po_number": "PO181112-123456","po_status": "delivered","transaction_id":"ORD10111808"}`)
t.Run("Test Cicil Update Status", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"success","message":""}`))
}))
//close server
defer ts.Close()
//construct cicil
amm := New(ts.URL, APIKey, MerchantID, MerchantSecret,DefaultTimeout)
var updateStatus UpdateStatusRequest
err := json.Unmarshal(orderData, &updateStatus)
if err != nil {
t.Error("Cannot Unmarshal Order JSON data")
}
_, errUpdateStatus := amm.UpdateStatus(updateStatus)
if errUpdateStatus != nil {
t.Errorf("GetOrderURL() returned an error: %s", errUpdateStatus.Error())
}
})
t.Run("Test Negative Case Cicil Update Status", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
}
w.Header().Set("Content-Type", "application-json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message":"invalid parameter on order request"}`))
}))
//close server
defer ts.Close()
//construct cicil
amm := New(ts.URL, APIKey, MerchantID, MerchantSecret,DefaultTimeout)
var updateStatus UpdateStatusRequest
err := json.Unmarshal(orderData, &updateStatus)
if err != nil {
t.Error("Cannot Unmarshal Order JSON data")
}
_, errUpdateStatus := amm.UpdateStatus(updateStatus)
if errUpdateStatus != nil {
assert.Error(t, errUpdateStatus)
}
})
}