Passed
Push — main ( 53658a...2d22a4 )
by Acho
01:49
created

smobilpay.TestTopupService_Verify   A

Complexity

Conditions 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nop 1
dl 0
loc 31
rs 9.4
c 0
b 0
f 0
1
package smobilpay
2
3
import (
4
	"context"
5
	"net/http"
6
	"testing"
7
8
	"github.com/NdoleStudio/smobilpay-go/internal/helpers"
9
	"github.com/NdoleStudio/smobilpay-go/internal/stubs"
10
	"github.com/stretchr/testify/assert"
11
)
12
13
func TestTopupService_GetPackages(t *testing.T) {
14
	// Setup
15
	t.Parallel()
16
17
	// Arrange
18
	server := helpers.MakeTestServer(http.StatusOK, stubs.TopupGetPackagesOk())
19
	accessToken := "6B352110-4716-11ED-963F-0800200C9A66"
20
	client := New(
21
		WithBaseURL(server.URL),
22
		WithAccessToken(accessToken),
23
		WithAccessSecret("1B875FB0-4717-11ED-963F-0800200C9A66"),
24
	)
25
	nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995"
26
27
	// Act
28
	topupPackages, response, err := client.Topup.GetPackages(
29
		context.Background(),
30
		"1234",
31
		WithRequestNonce(nonce),
32
	)
33
34
	// Assert
35
	assert.Nil(t, err)
36
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
37
	assert.Equal(t, 2, len(topupPackages))
38
39
	// Teardown
40
	server.Close()
41
}
42
43
func TestTopupService_Quote(t *testing.T) {
44
	// Setup
45
	t.Parallel()
46
47
	// Arrange
48
	server := helpers.MakeTestServer(http.StatusOK, stubs.TopupQuoteOk())
49
	accessToken := "6B352110-4716-11ED-963F-0800200C9A66"
50
	client := New(
51
		WithBaseURL(server.URL),
52
		WithAccessToken(accessToken),
53
		WithAccessSecret("1B875FB0-4717-11ED-963F-0800200C9A66"),
54
	)
55
	nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995"
56
	payItemID := "S-112-951-CMORANGE-20062-CM_ORANGE_VTU_CUSTOM-1"
57
58
	// Act
59
	quote, response, err := client.Topup.Quote(
60
		context.Background(),
61
		&QuoteParams{
62
			PayItemID: payItemID,
63
			Amount:    "100",
64
		},
65
		WithRequestNonce(nonce),
66
	)
67
68
	// Assert
69
	assert.Nil(t, err)
70
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
71
	assert.Equal(t, payItemID, quote.PayItemID)
72
	assert.Equal(t, 100, quote.PriceSystemCurrency)
73
74
	// Teardown
75
	server.Close()
76
}
77
78
func TestTopupService_Collect(t *testing.T) {
79
	// Setup
80
	t.Parallel()
81
82
	// Arrange
83
	server := helpers.MakeTestServer(http.StatusOK, stubs.TopupCollectOk())
84
	accessToken := "6B352110-4716-11ED-963F-0800200C9A66"
85
	client := New(
86
		WithBaseURL(server.URL),
87
		WithAccessToken(accessToken),
88
		WithAccessSecret("1B875FB0-4717-11ED-963F-0800200C9A66"),
89
	)
90
	nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995"
91
	params := &CollectParams{
92
		QuoteID:               "15380e55-6227-4a25-8e1f-23c8735ce242",
93
		CustomerPhoneNumber:   "697777777",
94
		CustomerEmailAddress:  "[email protected]",
95
		ServiceNumber:         "697777777",
96
		ExternalTransactionID: "999992624813740205",
97
	}
98
99
	// Act
100
	transaction, response, err := client.Topup.Collect(
101
		context.Background(),
102
		params,
103
		WithRequestNonce(nonce),
104
	)
105
106
	// Assert
107
	assert.Nil(t, err)
108
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
109
	assert.Equal(t, params.ExternalTransactionID, *transaction.ExternalTransactionID)
110
	assert.False(t, transaction.IsFailed())
111
	assert.Equal(t, "SUCCESS", transaction.Status)
112
113
	// Teardown
114
	server.Close()
115
}
116
117
func TestTopupService_Verify(t *testing.T) {
118
	// Setup
119
	t.Parallel()
120
121
	// Arrange
122
	server := helpers.MakeTestServer(http.StatusOK, stubs.TopupVerifyOk())
123
	accessToken := "6B352110-4716-11ED-963F-0800200C9A66"
124
	client := New(
125
		WithBaseURL(server.URL),
126
		WithAccessToken(accessToken),
127
		WithAccessSecret("1B875FB0-4717-11ED-963F-0800200C9A66"),
128
	)
129
	nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995"
130
	paymentTransactionNumber := "99999166542651400095315364801168"
131
132
	// Act
133
	transaction, response, err := client.Topup.Verify(
134
		context.Background(),
135
		paymentTransactionNumber,
136
		WithRequestNonce(nonce),
137
	)
138
139
	// Assert
140
	assert.Nil(t, err)
141
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
142
	assert.Equal(t, paymentTransactionNumber, transaction.PaymentTransactionNumber)
143
	assert.False(t, transaction.IsFailed())
144
	assert.Equal(t, "SUCCESS", transaction.Status)
145
146
	// Teardown
147
	server.Close()
148
}
149
150
func TestTopupService_VerifyEmpty(t *testing.T) {
151
	// Setup
152
	t.Parallel()
153
154
	// Arrange
155
	server := helpers.MakeTestServer(http.StatusOK, stubs.TopupVerifyEmpty())
156
	accessToken := "6B352110-4716-11ED-963F-0800200C9A66"
157
	client := New(
158
		WithBaseURL(server.URL),
159
		WithAccessToken(accessToken),
160
		WithAccessSecret("1B875FB0-4717-11ED-963F-0800200C9A66"),
161
	)
162
	nonce := "95cdf110-4614-4d95-b6c2-f14fe01c4995"
163
	paymentTransactionNumber := "99999166542651400095315364801168"
164
165
	// Act
166
	_, _, err := client.Topup.Verify(
167
		context.Background(),
168
		paymentTransactionNumber,
169
		WithRequestNonce(nonce),
170
	)
171
172
	// Assert
173
	assert.NotNil(t, err)
174
175
	// Teardown
176
	server.Close()
177
}
178