Passed
Push — main ( 0c2c25...2b69c2 )
by Acho
02:33
created

e_StatusWithMaxTransactionExceeded   A

Complexity

Conditions 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nop 1
dl 0
loc 27
rs 9.55
c 0
b 0
f 0
1
package ynote
2
3
import (
4
	"context"
5
	"net/http"
6
	"testing"
7
8
	"github.com/NdoleStudio/ynote-go/internal/helpers"
9
	"github.com/NdoleStudio/ynote-go/internal/stubs"
10
	"github.com/stretchr/testify/assert"
11
)
12
13
func TestRefundService_Refund(t *testing.T) {
14
	// Setup
15
	t.Parallel()
16
17
	// Arrange
18
	requests := make([]http.Request, 0)
19
	responses := [][]byte{stubs.TokenResponse(), stubs.RefundResponse()}
20
	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests)
21
	client := New(
22
		WithTokenURL(server.URL),
23
		WithAPIURL(server.URL),
24
		WithClientID(testClientID),
25
		WithClientSecret(testClientSecret),
26
	)
27
28
	payload := &RefundParams{
29
		ChannelUserMsisdn:         "699999999",
30
		Pin:                       "0000",
31
		Webhook:                   "https://example.com/webhook",
32
		Amount:                    "100",
33
		FinalCustomerPhone:        "699999999",
34
		FinalCustomerName:         "",
35
		RefundMethod:              "OrangeMoney",
36
		FeesIncluded:              false,
37
		FinalCustomerNameAccuracy: "0",
38
	}
39
40
	// Act
41
	transaction, response, err := client.Refund.Refund(context.Background(), payload)
42
43
	// Assert
44
	assert.Nil(t, err)
45
46
	assert.GreaterOrEqual(t, len(requests), 2)
47
	request := requests[len(requests)-1]
48
49
	assert.Equal(t, "/prod/refund", request.URL.Path)
50
	assert.Equal(t, "Bearer 19077204-9d0a-31fa-85cf-xxxxxxxxxx", request.Header.Get("Authorization"))
51
52
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
53
	assert.Equal(t, "993764f9-6b1f-41bd-a7ca-97b8b2167ed7", transaction.MessageID)
54
55
	// Teardown
56
	server.Close()
57
}
58
59
func TestRefundService_RefundWithInvalidClient(t *testing.T) {
60
	// Setup
61
	t.Parallel()
62
63
	// Arrange
64
	requests := make([]http.Request, 0)
65
	responses := [][]byte{stubs.TokenResponse(), stubs.RefundInvalidClientResponse()}
66
	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusBadRequest}, responses, &requests)
67
	client := New(
68
		WithTokenURL(server.URL),
69
		WithAPIURL(server.URL),
70
		WithClientID(testClientID),
71
		WithClientSecret(testClientSecret),
72
	)
73
74
	payload := &RefundParams{
75
		ChannelUserMsisdn:         "699999999",
76
		Pin:                       "0000",
77
		Webhook:                   "https://api.nyangapay.com/v1/y-note",
78
		Amount:                    "100",
79
		FinalCustomerPhone:        "699999999",
80
		FinalCustomerName:         "",
81
		RefundMethod:              "OrangeMoney",
82
		FeesIncluded:              false,
83
		FinalCustomerNameAccuracy: "0",
84
	}
85
86
	// Act
87
	transaction, response, err := client.Refund.Refund(context.Background(), payload)
88
89
	// Assert
90
	assert.Nil(t, transaction)
91
	assert.NotNil(t, err)
92
93
	assert.Equal(t, http.StatusBadRequest, response.HTTPResponse.StatusCode)
94
95
	// Teardown
96
	server.Close()
97
}
98
99
func TestRefundService_Status(t *testing.T) {
100
	// Setup
101
	t.Parallel()
102
103
	// Arrange
104
	requests := make([]http.Request, 0)
105
	responses := [][]byte{stubs.TokenResponse(), stubs.RefundStatusResponse()}
106
	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests)
107
	client := New(
108
		WithTokenURL(server.URL),
109
		WithAPIURL(server.URL),
110
		WithClientID(testClientID),
111
		WithClientSecret(testClientSecret),
112
	)
113
114
	// Act
115
	transaction, response, err := client.Refund.Status(context.Background(), "")
116
117
	// Assert
118
	assert.Nil(t, err)
119
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
120
	assert.Equal(t, "CI24120168FBF65A909F588B4480", transaction.Result.Data.PayToken)
121
	assert.True(t, transaction.IsSuccessful())
122
	assert.False(t, transaction.IsFailed())
123
	assert.False(t, transaction.IsPending())
124
125
	// Teardown
126
	server.Close()
127
}
128
129
func TestRefundService_StatusWithFailure(t *testing.T) {
130
	// Setup
131
	t.Parallel()
132
133
	// Arrange
134
	requests := make([]http.Request, 0)
135
	responses := [][]byte{stubs.TokenResponse(), stubs.RefundStatusResponseWithFailure()}
136
	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests)
137
	client := New(
138
		WithTokenURL(server.URL),
139
		WithAPIURL(server.URL),
140
		WithClientID(testClientID),
141
		WithClientSecret(testClientSecret),
142
	)
143
144
	// Act
145
	transaction, response, err := client.Refund.Status(context.Background(), "")
146
147
	// Assert
148
	assert.Nil(t, err)
149
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
150
	assert.Nil(t, transaction.Result)
151
	assert.True(t, transaction.IsFailed())
152
	assert.False(t, transaction.IsSuccessful())
153
	assert.False(t, transaction.IsPending())
154
155
	// Teardown
156
	server.Close()
157
}
158
159
func TestRefundService_StatusWithError(t *testing.T) {
160
	// Setup
161
	t.Parallel()
162
163
	// Arrange
164
	requests := make([]http.Request, 0)
165
	responses := [][]byte{stubs.TokenResponse(), []byte("Transactions not found")}
166
	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests)
167
	client := New(
168
		WithTokenURL(server.URL),
169
		WithAPIURL(server.URL),
170
		WithClientID(testClientID),
171
		WithClientSecret(testClientSecret),
172
	)
173
174
	// Act
175
	_, response, err := client.Refund.Status(context.Background(), "ddd")
176
177
	// Assert
178
	assert.NotNil(t, err)
179
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
180
181
	// Teardown
182
	server.Close()
183
}
184
185
func TestRefundService_StatusWithMaxTransactionExceeded(t *testing.T) {
186
	// Setup
187
	t.Parallel()
188
189
	// Arrange
190
	requests := make([]http.Request, 0)
191
	responses := [][]byte{stubs.TokenResponse(), stubs.RefundStatusWithMaxRetryExceeded()}
192
	server := helpers.MakeRequestCapturingTestServer([]int{http.StatusOK, http.StatusOK}, responses, &requests)
193
	client := New(
194
		WithTokenURL(server.URL),
195
		WithAPIURL(server.URL),
196
		WithClientID(testClientID),
197
		WithClientSecret(testClientSecret),
198
	)
199
200
	// Act
201
	transaction, response, err := client.Refund.Status(context.Background(), "")
202
203
	// Assert
204
	assert.Nil(t, err)
205
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
206
	assert.True(t, transaction.IsFailed())
207
	assert.False(t, transaction.IsSuccessful())
208
	assert.False(t, transaction.IsPending())
209
210
	// Teardown
211
	server.Close()
212
}
213