-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcreditcard_test.go
483 lines (376 loc) · 14.3 KB
/
creditcard_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package creditcard
import (
"strconv"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestFourDigits(t *testing.T) {
Convey("Should be able to retrieve the last four digits of my card", t, func() {
Convey("When I have four or more digits", func() {
Convey("Using LastFour method", func() {
card := Card{Number: "4012888888881881", Cvv: "111"}
lastFour, err := card.LastFour()
So(err, ShouldBeNil)
So(lastFour, ShouldEqual, "1881")
})
Convey("Using LastFourDigits method", func() {
card := Card{Number: "4012888888881881", Cvv: "111"}
lastFour, err := card.LastFourDigits()
So(err, ShouldBeNil)
So(lastFour, ShouldEqual, "1881")
})
})
Convey("When I don't have four or more digits", func() {
card := Card{Number: "123", Cvv: "111"}
lastFour, err := card.LastFour()
So(err, ShouldNotBeNil)
So(lastFour, ShouldEqual, "")
})
})
}
func TestWipe(t *testing.T) {
Convey("Should be able to wipe our credit card", t, func() {
card := Card{Number: "4012888888881881", Cvv: "111", Month: "02", Year: "2015"}
card.Wipe()
So(card.Number, ShouldEqual, "0000000000000000")
So(card.Cvv, ShouldEqual, "0000")
So(card.Month, ShouldEqual, "01")
So(card.Year, ShouldEqual, "1970")
})
}
func TestValidation(t *testing.T) {
month := strconv.Itoa(int(time.Now().UTC().Month()))
year := strconv.Itoa(time.Now().UTC().Year())
Convey("Expiration should matter", t, func() {
Convey("When the expiration year is less than the current year", func() {
card := Card{Number: "4012888888881881", Cvv: "111", Month: "02", Year: "2001"}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
Convey("When the month is non-sensical", func() {
nextYear, _, _ := (time.Now().UTC()).AddDate(1, 0, 0).Date()
examples := []int{0, 13}
for _, month := range examples {
card := Card{Number: "4012888888881881", Cvv: "111", Month: strconv.Itoa(int(month)), Year: strconv.Itoa(nextYear)}
err := card.Validate(true)
So(err, ShouldNotBeNil)
}
})
Convey("When the expiration month and year is less than the current date", func() {
year1, month1, _ := (time.Now().UTC()).AddDate(0, -1, 0).Date()
card := Card{Number: "4012888888881881", Cvv: "111", Month: strconv.Itoa(int(month1)), Year: strconv.Itoa(year1)}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
Convey("When the expiration year is invalid", func() {
Convey("with two letters", func() {
card := Card{Number: "4012888888881881", Cvv: "111", Month: "1", Year: "ab"}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
Convey("with more than two letters", func() {
card := Card{Number: "4012888888881881", Cvv: "111", Month: "1", Year: "abc"}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
})
Convey("When the expiration month is invalid", func() {
card := Card{Number: "4012888888881881", Cvv: "111", Month: "abc", Year: year}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
Convey("and should work when we use only two numbers", func() {
card := Card{Number: "4012888888881881", Cvv: "111", Month: month, Year: year[2:]}
err := card.Validate(true)
So(err, ShouldBeNil)
})
})
Convey("CVV validation", t, func() {
Convey("Should work with three characters", func() {
card := Card{Number: "4012888888881881", Cvv: "111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldBeNil)
})
Convey("Should work with four characters", func() {
card := Card{Number: "4012888888881881", Cvv: "1111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldBeNil)
})
Convey("Should give us an error if CVV is invalid", func() {
Convey("Too many numbers", func() {
card := Card{Number: "4012888888881881", Cvv: "11111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
Convey("Too little numbers", func() {
card := Card{Number: "4012888888881881", Cvv: "11", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
})
})
Convey("Credit card number should validate to numbers only", t, func() {
card := Card{Number: "abcdefg", Cvv: "1111", Month: month, Year: year}
_, err := card.MethodValidate()
So(err, ShouldNotBeNil)
})
Convey("Credit card number length should matter", t, func() {
Convey("Should give us an error if the length is less than 13 characters", func() {
card := Card{Number: "1234", Cvv: "1111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
Convey("Should not give us an error if the number is greater than or equal to 13 characters", func() {
card := Card{Number: "4012888888881881", Cvv: "1111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldBeNil)
})
})
Convey("Test cards", t, func() {
numbers := []string{"4242424242424242", "4012888888881881", "4000056655665556", "5555555555554444", "5200828282828210", "5105105105105100", "378282246310005", "371449635398431", "6011111111111117", "6011000990139424", "30569309025904", "38520000023237", "3530111333300000", "3566002020360505"}
Convey("should pass through", func() {
for _, num := range numbers {
Convey(num, func() {
card := Card{Number: num, Cvv: "1111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldBeNil)
})
}
})
Convey("should not pass through", func() {
for _, num := range numbers {
Convey(num, func() {
card := Card{Number: num, Cvv: "1111", Month: month, Year: year}
err := card.Validate()
So(err, ShouldNotBeNil)
})
}
})
})
Convey("Should be able to validate a number with the Luhn algorithm", t, func() {
Convey("With a valid card", func() {
Convey("Test Card", func() {
card := Card{Number: "4242424242424242", Cvv: "1111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldBeNil)
})
Convey("Real Card", func() {
card := Card{Number: "4556974850403706", Cvv: "1111", Month: month, Year: year}
err := card.Validate()
So(err, ShouldBeNil)
})
})
Convey("With an invalid card", func() {
card := Card{Number: "42483272242424242", Cvv: "1111", Month: month, Year: year}
err := card.Validate(true)
So(err, ShouldNotBeNil)
})
Convey("Not enough numbers", func() {
card := Card{Number: "424832", Cvv: "1111", Month: month, Year: year}
err := card.ValidateNumber()
So(err, ShouldBeFalse)
})
})
}
func TestMethod(t *testing.T) {
month := strconv.Itoa(int(time.Now().UTC().Month()))
year := strconv.Itoa(time.Now().UTC().Year())
Convey("Card method should validate even when there's less than 13 characters", t, func() {
Convey("Should work for American Express", func() {
card := Card{Number: "3782822463", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "amex")
So(card.Company.Long, ShouldEqual, "American Express")
})
Convey("Should work for Aura", func() {
card := Card{Number: "5078608877345328", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "aura")
So(card.Company.Long, ShouldEqual, "Aura")
})
Convey("Should work for Bankcard", func() {
Convey("5610", func() {
card := Card{Number: "56101111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "bankcard")
So(card.Company.Long, ShouldEqual, "Bankcard")
})
Convey("560221 - 560225", func() {
card := Card{Number: "56022111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "bankcard")
So(card.Company.Long, ShouldEqual, "Bankcard")
})
})
Convey("Should work for Cabal", func() {
card := Card{Number: "6035227716427021", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "cabal")
So(card.Company.Long, ShouldEqual, "Cabal")
})
Convey("Should work for Diners club", func() {
Convey("Carte blanche", func() {
card := Card{Number: "300221111111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "diners club carte blanche")
So(card.Company.Long, ShouldEqual, "Diners Club Carte Blanche")
})
Convey("Club enRoute", func() {
card := Card{Number: "20142111111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "diners club enroute")
So(card.Company.Long, ShouldEqual, "Diners Club enRoute")
})
Convey("Club international", func() {
card := Card{Number: "3002111111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "diners club international")
So(card.Company.Long, ShouldEqual, "Diners Club International")
})
})
Convey("Should work for China UnionPay", func() {
Convey("62", func() {
card := Card{Number: "62111111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "china unionpay")
So(card.Company.Long, ShouldEqual, "China UnionPay")
})
Convey("81", func() {
card := Card{Number: "810111111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "china unionpay")
So(card.Company.Long, ShouldEqual, "China UnionPay")
})
})
Convey("Should work for Dankort", func() {
card := Card{Number: "501955555", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "dankort")
So(card.Company.Long, ShouldEqual, "Dankort")
})
Convey("Should work for Diners Club", func() {
card := Card{Number: "30569309025", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "diners club international")
So(card.Company.Long, ShouldEqual, "Diners Club International")
})
Convey("Should work for Discover", func() {
card := Card{Number: "60111111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "discover")
So(card.Company.Long, ShouldEqual, "Discover")
})
Convey("Should work for Hipercard", func() {
card := Card{Number: "6062826786276634", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "hipercard")
So(card.Company.Long, ShouldEqual, "Hipercard")
})
Convey("Should work for InterPayment", func() {
card := Card{Number: "6360111331111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "interpayment")
So(card.Company.Long, ShouldEqual, "InterPayment")
})
Convey("Should work for InstaPayment", func() {
card := Card{Number: "6370111331111111", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "instapayment")
So(card.Company.Long, ShouldEqual, "InstaPayment")
})
Convey("Should work for JCB", func() {
card := Card{Number: "353011133", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "jcb")
So(card.Company.Long, ShouldEqual, "JCB")
})
Convey("Should work for Maestro", func() {
card := Card{Number: "501855555", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "maestro")
So(card.Company.Long, ShouldEqual, "Maestro")
})
Convey("Should work for MasterCard", func() {
Convey("51 - 55", func() {
card := Card{Number: "5425233430109903", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "mastercard")
So(card.Company.Long, ShouldEqual, "MasterCard")
})
Convey("2 Series", func() {
card := Card{Number: "2223000048410010", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "mastercard")
So(card.Company.Long, ShouldEqual, "MasterCard")
})
})
Convey("Should work for Naranja", func() {
card := Card{Number: "5895626746595650", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "naranja")
So(card.Company.Long, ShouldEqual, "Naranja")
})
Convey("Should work for Visa Electron", func() {
card := Card{Number: "4026424242", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "visa electron")
So(card.Company.Long, ShouldEqual, "Visa Electron")
})
Convey("Should work for Visa", func() {
card := Card{Number: "4242424242", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "visa")
So(card.Company.Long, ShouldEqual, "Visa")
})
Convey("Should work for Elo", func() {
card := Card{Number: "6362970000457013", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldBeNil)
So(card.Company.Short, ShouldEqual, "elo")
So(card.Company.Long, ShouldEqual, "Elo")
})
Convey("Should fail to recognize an unknown number", func() {
card := Card{Number: "1112424242", Cvv: "1111", Month: month, Year: year}
err := card.Method()
So(err, ShouldNotBeNil)
So(card.Company.Short, ShouldEqual, "")
So(card.Company.Long, ShouldEqual, "")
})
})
}
func TestCard_ValidateExpiration(t *testing.T) {
Convey("should get an error if card year is current year and card month is before current month", t, func() {
defer resetMocks()
timeNowCaller = func() time.Time {
return time.Date(2001, 3, 1, 1, 1, 1, 1, time.UTC)
}
card := Card{Number: "4012888888881881", Cvv: "111", Month: "02", Year: "2001"}
err := card.ValidateExpiration()
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Credit card has expired")
})
}