-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks_test.go
201 lines (182 loc) · 8.82 KB
/
webhooks_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
package dusupay
import (
"context"
"encoding/json"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"io/ioutil"
"net/http"
"testing"
)
type WebhooksTestSuite struct {
suite.Suite
}
func (suite *WebhooksTestSuite) TestCollectionWebhookUnmarshalSuccess() {
var webhook CollectionWebhook
body, _ := LoadStubResponseData("stubs/webhooks/request/collection-success.json")
err := json.Unmarshal(body, &webhook)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), int64(226), webhook.ID)
assert.Equal(suite.T(), 0.2, webhook.RequestAmount)
assert.Equal(suite.T(), "USD", webhook.RequestCurrency)
assert.Equal(suite.T(), 737.9934, webhook.AccountAmount)
assert.Equal(suite.T(), "UGX", webhook.AccountCurrency)
assert.Equal(suite.T(), 21.4018, webhook.TransactionFee)
assert.Equal(suite.T(), 716.5916, webhook.TotalCredit)
assert.Equal(suite.T(), false, webhook.CustomerCharged)
assert.Equal(suite.T(), "mtn_ug", webhook.ProviderID)
assert.Equal(suite.T(), "76859aae-f148-48c5-9901-2e474cf19b71", webhook.MerchantReference)
assert.Equal(suite.T(), "DUSUPAY405GZM1G5JXGA71IK", webhook.InternalReference)
assert.Equal(suite.T(), "COMPLETED", webhook.TransactionStatus)
assert.Equal(suite.T(), "collection", webhook.TransactionType)
assert.Equal(suite.T(), "Transaction Completed Successfully", webhook.Message)
assert.Equal(suite.T(), "256777111786 - Optional", webhook.AccountNumber)
assert.Equal(suite.T(), "- Optional", webhook.AccountName)
assert.Equal(suite.T(), "MTN Mobile Money - Optional", webhook.InstitutionName)
}
func (suite *WebhooksTestSuite) TestCollectionWebhookBuildPayloadString() {
var webhook CollectionWebhook
body, _ := LoadStubResponseData("stubs/webhooks/request/collection-success.json")
_ = json.Unmarshal(body, &webhook)
result := webhook.BuildPayloadString("https://www.sample-url.com/callback")
assert.Equal(suite.T(), "226:DUSUPAY405GZM1G5JXGA71IK:COMPLETED:https://www.sample-url.com/callback", result)
}
func (suite *WebhooksTestSuite) TestPayoutWebhookUnmarshalSuccess() {
var webhook PayoutWebhook
body, _ := LoadStubResponseData("stubs/webhooks/request/payout-success.json")
err := json.Unmarshal(body, &webhook)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), int64(226), webhook.ID)
assert.Equal(suite.T(), 0.2, webhook.RequestAmount)
assert.Equal(suite.T(), "USD", webhook.RequestCurrency)
assert.Equal(suite.T(), 737.9934, webhook.AccountAmount)
assert.Equal(suite.T(), "UGX", webhook.AccountCurrency)
assert.Equal(suite.T(), 21.4018, webhook.TransactionFee)
assert.Equal(suite.T(), 716.5916, webhook.TotalDebit)
assert.Equal(suite.T(), "mtn_ug", webhook.ProviderID)
assert.Equal(suite.T(), "76859aae-f148-48c5-9901-2e474cf19b71", webhook.MerchantReference)
assert.Equal(suite.T(), "DUSUPAY405GZM1G5JXGA71IK", webhook.InternalReference)
assert.Equal(suite.T(), "COMPLETED", webhook.TransactionStatus)
assert.Equal(suite.T(), "payout", webhook.TransactionType)
assert.Equal(suite.T(), "Transaction Completed Successfully", webhook.Message)
assert.Equal(suite.T(), "256777111786 - Optional", webhook.AccountNumber)
assert.Equal(suite.T(), "- Optional", webhook.AccountName)
assert.Equal(suite.T(), "MTN Mobile Money - Optional", webhook.InstitutionName)
}
func (suite *WebhooksTestSuite) TestPayoutWebhookBuildPayloadString() {
var webhook PayoutWebhook
body, _ := LoadStubResponseData("stubs/webhooks/request/payout-success.json")
_ = json.Unmarshal(body, &webhook)
result := webhook.BuildPayloadString("https://www.sample-url.com/callback")
assert.Equal(suite.T(), "226:DUSUPAY405GZM1G5JXGA71IK:COMPLETED:https://www.sample-url.com/callback", result)
}
func (suite *WebhooksTestSuite) TestRefundWebhookUnmarshalSuccess() {
var webhook RefundWebhook
body, _ := LoadStubResponseData("stubs/webhooks/request/refund-success.json")
err := json.Unmarshal(body, &webhook)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), int64(65205), webhook.ID)
assert.Equal(suite.T(), float64(1054), webhook.RefundAmount)
assert.Equal(suite.T(), "UGX", webhook.RefundCurrency)
assert.Equal(suite.T(), float64(0), webhook.TransactionFee)
assert.Equal(suite.T(), float64(1054), webhook.TotalDebit)
assert.Equal(suite.T(), "international_ugx", webhook.ProviderID)
assert.Equal(suite.T(), "DUSUPAYXYXYXYXYXYXYXYXYX", webhook.CollectionReference)
assert.Equal(suite.T(), "RFD-DUSUPAYXYXYXYXYXYXYXYXYX-3486003", webhook.InternalReference)
assert.Equal(suite.T(), "COMPLETED", webhook.TransactionStatus)
assert.Equal(suite.T(), "refund", webhook.TransactionType)
assert.Equal(suite.T(), "Refund Processed Successfully", webhook.Message)
assert.Equal(suite.T(), "4860610032773134", webhook.AccountNumber)
}
func (suite *WebhooksTestSuite) TestRefundWebhookBuildPayloadString() {
var webhook RefundWebhook
body, _ := LoadStubResponseData("stubs/webhooks/request/refund-success.json")
_ = json.Unmarshal(body, &webhook)
result := webhook.BuildPayloadString("https://www.sample-url.com/callback")
assert.Equal(suite.T(), "65205:RFD-DUSUPAYXYXYXYXYXYXYXYXYX-3486003:COMPLETED:https://www.sample-url.com/callback", result)
}
func TestWebhooksTestSuite(t *testing.T) {
suite.Run(t, new(WebhooksTestSuite))
}
type WebhooksResourceTestSuite struct {
suite.Suite
cfg *Config
ctx context.Context
testable *WebhooksResource
}
func (suite *WebhooksResourceTestSuite) SetupTest() {
cfg := BuildStubConfig()
transport := BuildStubHttpTransport()
suite.cfg = cfg
suite.ctx = context.Background()
suite.testable = &WebhooksResource{NewResourceAbstract(transport, cfg)}
httpmock.Activate()
}
func (suite *WebhooksResourceTestSuite) TearDownTest() {
httpmock.DeactivateAndReset()
}
func (suite *WebhooksResourceTestSuite) TestSendCallbackSuccess() {
body, _ := LoadStubResponseData("stubs/webhooks/send-callback/success.json")
httpmock.RegisterResponder(http.MethodGet, suite.cfg.Uri+"/v1/send-callback/qwerty", httpmock.NewBytesResponder(http.StatusOK, body))
result, resp, err := suite.testable.SendCallback(suite.ctx, "qwerty")
assert.NoError(suite.T(), err)
assert.NotEmpty(suite.T(), resp)
assert.NotEmpty(suite.T(), result)
//result
assert.True(suite.T(), result.IsSuccess())
assert.Equal(suite.T(), 200, result.Code)
assert.Equal(suite.T(), "success", result.Status)
assert.Equal(suite.T(), "Callback Initiated Successfully", result.Message)
assert.Equal(suite.T(), int64(613589), result.Data.Payload.ID)
assert.Equal(suite.T(), float64(520000), result.Data.Payload.RequestAmount)
assert.Equal(suite.T(), "XAF", result.Data.Payload.RequestCurrency)
assert.Equal(suite.T(), 791.44, result.Data.Payload.AccountAmount)
assert.Equal(suite.T(), "EUR", result.Data.Payload.AccountCurrency)
assert.Equal(suite.T(), 38.7806, result.Data.Payload.TransactionFee)
assert.Equal(suite.T(), "international_eur", result.Data.Payload.ProviderID)
assert.Equal(suite.T(), "123456789", result.Data.Payload.MerchantReference)
assert.Equal(suite.T(), "DUSUPAY5FNZCVUKZ8C0KZE", result.Data.Payload.InternalReference)
assert.Equal(suite.T(), "COMPLETED", result.Data.Payload.TransactionStatus)
assert.Equal(suite.T(), "collection", result.Data.Payload.TransactionType)
assert.Equal(suite.T(), "Transaction Completed Successfully", result.Data.Payload.Message)
//response
defer resp.Body.Close()
bodyRsp, _ := ioutil.ReadAll(resp.Body)
assert.Equal(suite.T(), body, bodyRsp)
}
func (suite *WebhooksResourceTestSuite) TestSendCallbackJsonError() {
body, _ := LoadStubResponseData("stubs/errors/401.json")
httpmock.RegisterResponder(http.MethodGet, suite.cfg.Uri+"/v1/send-callback/qwerty", httpmock.NewBytesResponder(http.StatusOK, body))
result, resp, err := suite.testable.SendCallback(suite.ctx, "qwerty")
assert.Error(suite.T(), err)
assert.NotEmpty(suite.T(), resp)
assert.NotEmpty(suite.T(), result)
//result
assert.False(suite.T(), result.IsSuccess())
assert.Equal(suite.T(), 401, result.Code)
assert.Equal(suite.T(), "error", result.Status)
assert.Equal(suite.T(), "Unauthorized API access. Unknown Merchant", result.Message)
assert.Empty(suite.T(), result.Data)
//response
defer resp.Body.Close()
bodyRsp, _ := ioutil.ReadAll(resp.Body)
assert.Equal(suite.T(), body, bodyRsp)
//error
assert.Equal(suite.T(), "Unauthorized API access. Unknown Merchant", err.Error())
}
func (suite *WebhooksResourceTestSuite) TestSendCallbackNonJsonError() {
body, _ := LoadStubResponseData("stubs/errors/500.html")
httpmock.RegisterResponder(http.MethodGet, suite.cfg.Uri+"/v1/send-callback/qwerty", httpmock.NewBytesResponder(http.StatusOK, body))
result, resp, err := suite.testable.SendCallback(suite.ctx, "qwerty")
assert.Error(suite.T(), err)
assert.NotEmpty(suite.T(), resp)
assert.Empty(suite.T(), result)
//response
defer resp.Body.Close()
bodyRsp, _ := ioutil.ReadAll(resp.Body)
assert.Equal(suite.T(), body, bodyRsp)
}
func TestWebhooksResourceTestSuite(t *testing.T) {
suite.Run(t, new(WebhooksResourceTestSuite))
}