Passed
Push — main ( 331b34...0c52e1 )
by Acho
01:13
created

bills_service_test.go   A

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 177
dl 0
loc 306
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C mobilenig.TestBillsService_PayDStv 0 103 9
C mobilenig.TestBillsService_QueryDStv 0 82 8
C mobilenig.TestBillsService_CheckDStvUser 0 94 8
1
package mobilenig
2
3
import (
4
	"context"
5
	"net/http"
6
	"net/url"
7
	"testing"
8
	"time"
9
10
	"github.com/NdoleStudio/mobilenig-go/internal/helpers"
11
	"github.com/NdoleStudio/mobilenig-go/internal/stubs"
12
	"github.com/stretchr/testify/assert"
13
)
14
15
//nolint:funlen
16
func TestBillsService_CheckDStvUser(t *testing.T) {
17
	t.Parallel()
18
	t.Run("it returns the dstv user with a valid smartcard", func(t *testing.T) {
19
		// Setup
20
		t.Parallel()
21
		server := helpers.MakeTestServer(http.StatusOK, stubs.CheckDstvUserResponse())
22
23
		// Arrange
24
		baseURL, _ := url.Parse(server.URL)
25
		client := New(WithBaseURL(baseURL))
26
		date, _ := time.Parse("2006-01-02T15:04:05-07:00", "2018-11-13T00:00:00+01:00")
27
28
		// Act
29
		user, _, err := client.Bills.CheckDStvUser(context.Background(), "4131953321")
30
31
		// Assert
32
		assert.NoError(t, err)
33
34
		assert.Equal(t, "OPEN", user.Details.AccountStatus)
35
		assert.Equal(t, "ESU", user.Details.Firstname)
36
		assert.Equal(t, "INI OBONG BASSEY", user.Details.Lastname)
37
		assert.Equal(t, "SUD", user.Details.CustomerType)
38
		assert.Equal(t, 1, user.Details.InvoicePeriod)
39
		assert.Equal(t, date, user.Details.DueDate)
40
		assert.Equal(t, int64(275953782), user.Details.CustomerNumber)
41
42
		// Teardown
43
		server.Close()
44
	})
45
46
	t.Run("it constructs the request correctly", func(t *testing.T) {
47
		// Setup
48
		t.Parallel()
49
50
		environments := []Environment{LiveEnvironment, TestEnvironment}
51
		for _, environment := range environments {
52
			t.Run(environment.String(), func(t *testing.T) {
53
				// Setup
54
				t.Parallel()
55
56
				// Arrange
57
				request := new(http.Request)
58
				server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CheckDstvUserResponse(), request)
59
60
				baseURL, _ := url.Parse(server.URL)
61
				username := "test_username"
62
				apiKey := "test_api_key"
63
				smartcardNumber := "4131953321"
64
65
				client := New(WithBaseURL(baseURL), WithAPIKey(apiKey), WithUsername(username), WithEnvironment(environment))
66
67
				// Act
68
				_, _, _ = client.Bills.CheckDStvUser(context.Background(), smartcardNumber)
69
70
				// Assert
71
				assert.Equal(t, "/bills/user_check", request.URL.Path)
72
				assert.Equal(t, username, request.URL.Query().Get("username"))
73
				assert.Equal(t, apiKey, request.URL.Query().Get("api_key"))
74
				assert.Equal(t, "DSTV", request.URL.Query().Get("service"))
75
				assert.Equal(t, smartcardNumber, request.URL.Query().Get("number"))
76
77
				// Teardown
78
				server.Close()
79
			})
80
		}
81
	})
82
83
	t.Run("it returns an error when the api responds with an error", func(t *testing.T) {
84
		t.Parallel()
85
86
		environments := []Environment{LiveEnvironment, TestEnvironment}
87
		for _, environment := range environments {
88
			t.Run(environment.String(), func(t *testing.T) {
89
				// Setup
90
				t.Parallel()
91
92
				// Arrange
93
				server := helpers.MakeTestServer(http.StatusOK, stubs.ErrorResponse())
94
				baseURL, _ := url.Parse(server.URL)
95
				smartcardNumber := "4131953321"
96
97
				client := New(WithBaseURL(baseURL), WithEnvironment(environment))
98
99
				// Act
100
				_, resp, err := client.Bills.CheckDStvUser(context.Background(), smartcardNumber)
101
102
				// Assert
103
				assert.Error(t, err)
104
105
				assert.Equal(t, "ERR101", resp.Error.Code)
106
				assert.Equal(t, "Invalid username or api_key", resp.Error.Description)
107
108
				// Teardown
109
				server.Close()
110
			})
111
		}
112
	})
113
}
114
115
//nolint:funlen
116
func TestBillsService_PayDStv(t *testing.T) {
117
	t.Parallel()
118
	t.Run("it returns the dstv transaction response with a valid payment", func(t *testing.T) {
119
		t.Parallel()
120
121
		// Arrange
122
		server := helpers.MakeTestServer(http.StatusOK, stubs.PayDstvBillResponse())
123
		baseURL, _ := url.Parse(server.URL)
124
		client := New(WithBaseURL(baseURL))
125
126
		// Act
127
		transaction, _, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{})
128
129
		// Assert
130
		assert.NoError(t, err)
131
132
		assert.Equal(t, "122790223", transaction.TransactionID)
133
		assert.Equal(t, "DSTV", transaction.Details.Service)
134
		assert.Equal(t, "DStv Mobile MAXI", transaction.Details.Package)
135
		assert.Equal(t, "4131953321", transaction.Details.SmartcardNumber)
136
		assert.Equal(t, "790", transaction.Details.Price)
137
		assert.Equal(t, "SUCCESSFUL", transaction.Details.Status)
138
		assert.Equal(t, "7931", transaction.Details.Balance)
139
140
		// Teardown
141
		server.Close()
142
	})
143
144
	t.Run("it constructs the request correctly", func(t *testing.T) {
145
		t.Parallel()
146
147
		environments := []Environment{LiveEnvironment, TestEnvironment}
148
		for _, environment := range environments {
149
			t.Run(environment.String(), func(t *testing.T) {
150
				// Arrange
151
				request := new(http.Request)
152
				server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CheckDstvUserResponse(), request)
153
154
				baseURL, _ := url.Parse(server.URL)
155
				username := "test_username"
156
				apiKey := "test_api_key"
157
				smartcardNumber := "4131953321"
158
				customerNumber := "275953782"
159
				customerName := "ESU INI OBONG BASSEY"
160
				price := "790"
161
				transactionID := "122790223"
162
163
				client := New(WithBaseURL(baseURL), WithAPIKey(apiKey), WithUsername(username), WithEnvironment(environment))
164
165
				// Act
166
				_, _, _ = client.Bills.PayDStv(context.Background(), &PayDstvOptions{
167
					TransactionID:   transactionID,
168
					Price:           price,
169
					ProductCode:     DstvProductCodePremium,
170
					CustomerName:    customerName,
171
					CustomerNumber:  customerNumber,
172
					SmartcardNumber: smartcardNumber,
173
				})
174
175
				// Assert
176
				uri := "/bills/dstv"
177
				if environment == TestEnvironment {
178
					uri += "_test"
179
				}
180
181
				assert.Equal(t, uri, request.URL.Path)
182
				assert.Equal(t, username, request.URL.Query().Get("username"))
183
				assert.Equal(t, apiKey, request.URL.Query().Get("api_key"))
184
				assert.Equal(t, smartcardNumber, request.URL.Query().Get("smartno"))
185
				assert.Equal(t, string(DstvProductCodePremium), request.URL.Query().Get("product_code"))
186
				assert.Equal(t, customerName, request.URL.Query().Get("customer_name"))
187
				assert.Equal(t, customerNumber, request.URL.Query().Get("customer_number"))
188
				assert.Equal(t, price, request.URL.Query().Get("price"))
189
				assert.Equal(t, transactionID, request.URL.Query().Get("trans_id"))
190
191
				// Teardown
192
				server.Close()
193
			})
194
		}
195
	})
196
197
	t.Run("it returns an error when the api responds with an error", func(t *testing.T) {
198
		t.Parallel()
199
200
		environments := []Environment{LiveEnvironment, TestEnvironment}
201
		for _, environment := range environments {
202
			t.Run(environment.String(), func(t *testing.T) {
203
				// Arrange
204
				server := helpers.MakeTestServer(http.StatusOK, stubs.ErrorResponse())
205
				baseURL, _ := url.Parse(server.URL)
206
207
				client := New(WithBaseURL(baseURL), WithEnvironment(environment))
208
209
				// Act
210
				_, resp, err := client.Bills.PayDStv(context.Background(), &PayDstvOptions{})
211
212
				// Assert
213
				assert.Error(t, err)
214
215
				assert.Equal(t, "ERR101", resp.Error.Code)
216
				assert.Equal(t, "Invalid username or api_key", resp.Error.Description)
217
218
				server.Close()
219
			})
220
		}
221
	})
222
}
223
224
//nolint:funlen
225
func TestBillsService_QueryDStv(t *testing.T) {
226
	t.Parallel()
227
	t.Run("it returns the dstv transaction response with a valid transaction ID", func(t *testing.T) {
228
		t.Parallel()
229
230
		// Arrange
231
		server := helpers.MakeTestServer(http.StatusOK, stubs.PayDstvBillResponse())
232
		baseURL, _ := url.Parse(server.URL)
233
		client := New(WithBaseURL(baseURL))
234
235
		// Act
236
		transaction, _, err := client.Bills.QueryDStv(context.Background(), "122790223")
237
238
		// Assert
239
		assert.NoError(t, err)
240
241
		assert.Equal(t, "122790223", transaction.TransactionID)
242
		assert.Equal(t, "DSTV", transaction.Details.Service)
243
		assert.Equal(t, "DStv Mobile MAXI", transaction.Details.Package)
244
		assert.Equal(t, "4131953321", transaction.Details.SmartcardNumber)
245
		assert.Equal(t, "790", transaction.Details.Price)
246
		assert.Equal(t, "SUCCESSFUL", transaction.Details.Status)
247
		assert.Equal(t, "7931", transaction.Details.Balance)
248
249
		// Teardown
250
		server.Close()
251
	})
252
253
	t.Run("it constructs the request correctly", func(t *testing.T) {
254
		t.Parallel()
255
256
		environments := []Environment{LiveEnvironment, TestEnvironment}
257
		for _, environment := range environments {
258
			t.Run(environment.String(), func(t *testing.T) {
259
				// Arrange
260
				request := new(http.Request)
261
				server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.CheckDstvUserResponse(), request)
262
263
				baseURL, _ := url.Parse(server.URL)
264
				username := "test_username"
265
				apiKey := "test_api_key"
266
				transactionID := "122790223"
267
268
				client := New(WithBaseURL(baseURL), WithAPIKey(apiKey), WithUsername(username), WithEnvironment(environment))
269
270
				// Act
271
				_, _, _ = client.Bills.QueryDStv(context.Background(), transactionID)
272
273
				// Assert
274
				assert.Equal(t, "/bills/query", request.URL.Path)
275
				assert.Equal(t, username, request.URL.Query().Get("username"))
276
				assert.Equal(t, apiKey, request.URL.Query().Get("api_key"))
277
				assert.Equal(t, transactionID, request.URL.Query().Get("trans_id"))
278
279
				// Teardown
280
				server.Close()
281
			})
282
		}
283
	})
284
285
	t.Run("it returns an error when the api responds with an error", func(t *testing.T) {
286
		t.Parallel()
287
288
		environments := []Environment{LiveEnvironment, TestEnvironment}
289
		for _, environment := range environments {
290
			t.Run(environment.String(), func(t *testing.T) {
291
				// Arrange
292
				server := helpers.MakeTestServer(http.StatusOK, stubs.ErrorResponse())
293
				baseURL, _ := url.Parse(server.URL)
294
295
				client := New(WithBaseURL(baseURL), WithEnvironment(environment))
296
297
				// Act
298
				_, resp, err := client.Bills.QueryDStv(context.Background(), "122790223")
299
300
				// Assert
301
				assert.Error(t, err)
302
303
				assert.Equal(t, "ERR101", resp.Error.Code)
304
				assert.Equal(t, "Invalid username or api_key", resp.Error.Description)
305
306
				server.Close()
307
			})
308
		}
309
	})
310
}
311