Passed
Push — main ( 0c52e1...45ba15 )
by Acho
57s
created

mobilenig.TestBillsService_CheckDStvUser   D

Complexity

Conditions 11

Size

Total Lines 117
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 66
nop 1
dl 0
loc 117
rs 4.8926
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like mobilenig.TestBillsService_CheckDStvUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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