am_credit_card 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,354 @@
1
+ require 'test_helper'
2
+
3
+ class CreditCardTest < Test::Unit::TestCase
4
+ def setup
5
+ CreditCard.require_verification_value = false
6
+ @visa = credit_card("4779139500118580", :type => "visa")
7
+ @solo = credit_card("676700000000000000", :type => "solo", :issue_number => '01')
8
+ end
9
+
10
+ def teardown
11
+ CreditCard.require_verification_value = false
12
+ end
13
+
14
+ def test_constructor_should_properly_assign_values
15
+ c = credit_card
16
+
17
+ assert_equal "4242424242424242", c.number
18
+ assert_equal 9, c.month
19
+ assert_equal Time.now.year + 1, c.year
20
+ assert_equal "Longbob Longsen", c.name
21
+ assert_equal "visa", c.type
22
+ assert_valid c
23
+ end
24
+
25
+ def test_new_credit_card_should_not_be_valid
26
+ c = CreditCard.new
27
+
28
+ assert_not_valid c
29
+ assert_false c.errors.empty?
30
+ end
31
+
32
+ def test_should_be_a_valid_visa_card
33
+ assert_valid @visa
34
+ assert @visa.errors.empty?
35
+ end
36
+
37
+ def test_should_be_a_valid_solo_card
38
+ assert_valid @solo
39
+ assert @solo.errors.empty?
40
+ end
41
+
42
+ def test_cards_with_empty_names_should_not_be_valid
43
+ @visa.first_name = ''
44
+ @visa.last_name = ''
45
+
46
+ assert_not_valid @visa
47
+ assert_false @visa.errors.empty?
48
+ end
49
+
50
+ def test_should_be_able_to_access_errors_indifferently
51
+ @visa.first_name = ''
52
+
53
+ assert_not_valid @visa
54
+ assert @visa.errors.on(:first_name)
55
+ assert @visa.errors.on("first_name")
56
+ end
57
+
58
+ def test_should_be_able_to_liberate_a_bogus_card
59
+ c = credit_card('', :type => 'bogus')
60
+ assert_valid c
61
+
62
+ c.type = 'visa'
63
+ assert_not_valid c
64
+ end
65
+
66
+ def test_should_be_able_to_identify_invalid_card_numbers
67
+ @visa.number = nil
68
+ assert_not_valid @visa
69
+
70
+ @visa.number = "11112222333344ff"
71
+ assert_not_valid @visa
72
+ assert_false @visa.errors.on(:type)
73
+ assert @visa.errors.on(:number)
74
+
75
+ @visa.number = "111122223333444"
76
+ assert_not_valid @visa
77
+ assert_false @visa.errors.on(:type)
78
+ assert @visa.errors.on(:number)
79
+
80
+ @visa.number = "11112222333344444"
81
+ assert_not_valid @visa
82
+ assert_false @visa.errors.on(:type)
83
+ assert @visa.errors.on(:number)
84
+ end
85
+
86
+ def test_should_have_errors_with_invalid_card_type_for_otherwise_correct_number
87
+ @visa.type = 'master'
88
+
89
+ assert_not_valid @visa
90
+ assert_not_equal @visa.errors.on(:number), @visa.errors.on(:type)
91
+ end
92
+
93
+ def test_should_be_invalid_when_type_cannot_be_detected
94
+ @visa.number = nil
95
+ @visa.type = nil
96
+
97
+ assert_not_valid @visa
98
+ assert !@visa.errors.on(:type)
99
+ assert @visa.errors.on(:number)
100
+ assert_equal 'is required', @visa.errors.on(:number)
101
+ end
102
+
103
+ def test_should_be_a_valid_card_number
104
+ @visa.number = "4242424242424242"
105
+
106
+ assert_valid @visa
107
+ end
108
+
109
+ def test_should_require_a_valid_card_month
110
+ @visa.month = Time.now.utc.month
111
+ @visa.year = Time.now.utc.year
112
+
113
+ assert_valid @visa
114
+ end
115
+
116
+ def test_should_not_be_valid_with_empty_month
117
+ @visa.month = ''
118
+
119
+ assert_not_valid @visa
120
+ assert_equal 'is required', @visa.errors.on('month')
121
+ end
122
+
123
+ def test_should_not_be_valid_for_edge_month_cases
124
+ @visa.month = 13
125
+ @visa.year = Time.now.year
126
+ assert_not_valid @visa
127
+ assert @visa.errors.on('month')
128
+
129
+ @visa.month = 0
130
+ @visa.year = Time.now.year
131
+ assert_not_valid @visa
132
+ assert @visa.errors.on('month')
133
+ end
134
+
135
+ def test_should_be_invalid_with_empty_year
136
+ @visa.year = ''
137
+ assert_not_valid @visa
138
+ assert_equal 'is required', @visa.errors.on('year')
139
+ end
140
+
141
+ def test_should_not_be_valid_for_edge_year_cases
142
+ @visa.year = Time.now.year - 1
143
+ assert_not_valid @visa
144
+ assert @visa.errors.on('year')
145
+
146
+ @visa.year = Time.now.year + 21
147
+ assert_not_valid @visa
148
+ assert @visa.errors.on('year')
149
+ end
150
+
151
+ def test_should_be_a_valid_future_year
152
+ @visa.year = Time.now.year + 1
153
+ assert_valid @visa
154
+ end
155
+
156
+ def test_expired_card_should_have_one_error_on_year
157
+ @visa.year = Time.now.year - 1
158
+ assert_not_valid @visa
159
+ assert_equal 1, @visa.errors['year'].length
160
+ assert /expired/ =~ @visa.errors['year'].first
161
+ end
162
+
163
+ def test_should_be_valid_with_start_month_and_year_as_string
164
+ @solo.start_month = '2'
165
+ @solo.start_year = '2007'
166
+ assert_valid @solo
167
+ end
168
+
169
+ def test_should_identify_wrong_cardtype
170
+ c = credit_card(:type => 'master')
171
+ assert_not_valid c
172
+ end
173
+
174
+ def test_should_display_number
175
+ assert_equal 'XXXX-XXXX-XXXX-1234', CreditCard.new(:number => '1111222233331234').display_number
176
+ assert_equal 'XXXX-XXXX-XXXX-1234', CreditCard.new(:number => '111222233331234').display_number
177
+ assert_equal 'XXXX-XXXX-XXXX-1234', CreditCard.new(:number => '1112223331234').display_number
178
+
179
+ assert_equal 'XXXX-XXXX-XXXX-', CreditCard.new(:number => nil).display_number
180
+ assert_equal 'XXXX-XXXX-XXXX-', CreditCard.new(:number => '').display_number
181
+ assert_equal 'XXXX-XXXX-XXXX-123', CreditCard.new(:number => '123').display_number
182
+ assert_equal 'XXXX-XXXX-XXXX-1234', CreditCard.new(:number => '1234').display_number
183
+ assert_equal 'XXXX-XXXX-XXXX-1234', CreditCard.new(:number => '01234').display_number
184
+ end
185
+
186
+ def test_should_correctly_identify_card_type
187
+ assert_equal 'visa', CreditCard.type?('4242424242424242')
188
+ assert_equal 'american_express', CreditCard.type?('341111111111111')
189
+ assert_nil CreditCard.type?('')
190
+ end
191
+
192
+ def test_should_be_able_to_require_a_verification_value
193
+ CreditCard.require_verification_value = true
194
+ assert CreditCard.requires_verification_value?
195
+ end
196
+
197
+ def test_should_not_be_valid_when_requiring_a_verification_value
198
+ CreditCard.require_verification_value = true
199
+ card = credit_card('4242424242424242', :verification_value => nil)
200
+ assert_not_valid card
201
+
202
+ card.verification_value = '123'
203
+ assert_valid card
204
+ end
205
+
206
+ def test_should_require_valid_start_date_for_solo_or_switch
207
+ @solo.start_month = nil
208
+ @solo.start_year = nil
209
+ @solo.issue_number = nil
210
+
211
+ assert_not_valid @solo
212
+ assert @solo.errors.on('start_month')
213
+ assert @solo.errors.on('start_year')
214
+ assert @solo.errors.on('issue_number')
215
+
216
+ @solo.start_month = 2
217
+ @solo.start_year = 2007
218
+ assert_valid @solo
219
+ end
220
+
221
+ def test_should_require_a_valid_issue_number_for_solo_or_switch
222
+ @solo.start_month = nil
223
+ @solo.start_year = 2005
224
+ @solo.issue_number = nil
225
+
226
+ assert_not_valid @solo
227
+ assert @solo.errors.on('start_month')
228
+ assert @solo.errors.on('issue_number')
229
+
230
+ @solo.issue_number = 3
231
+ assert_valid @solo
232
+ end
233
+
234
+ def test_should_return_last_four_digits_of_card_number
235
+ ccn = CreditCard.new(:number => "4779139500118580")
236
+ assert_equal "8580", ccn.last_digits
237
+ end
238
+
239
+ def test_bogus_last_digits
240
+ ccn = CreditCard.new(:number => "1")
241
+ assert_equal "1", ccn.last_digits
242
+ end
243
+
244
+ def test_should_be_true_when_credit_card_has_a_first_name
245
+ c = CreditCard.new
246
+ assert_false c.first_name?
247
+
248
+ c = CreditCard.new(:first_name => 'James')
249
+ assert c.first_name?
250
+ end
251
+
252
+ def test_should_be_true_when_credit_card_has_a_last_name
253
+ c = CreditCard.new
254
+ assert_false c.last_name?
255
+
256
+ c = CreditCard.new(:last_name => 'Herdman')
257
+ assert c.last_name?
258
+ end
259
+
260
+ def test_should_test_for_a_full_name
261
+ c = CreditCard.new
262
+ assert_false c.name?
263
+
264
+ c = CreditCard.new(:first_name => 'James', :last_name => 'Herdman')
265
+ assert c.name?
266
+ end
267
+
268
+ def test_should_handle_full_name_when_first_or_last_is_missing
269
+ c = CreditCard.new(:first_name => 'James')
270
+ assert c.name?
271
+ assert_equal "James", c.name
272
+
273
+ c = CreditCard.new(:last_name => 'Herdman')
274
+ assert c.name?
275
+ assert_equal "Herdman", c.name
276
+ end
277
+
278
+ def test_should_assign_a_full_name
279
+ c = CreditCard.new :name => "James Herdman"
280
+ assert_equal "James", c.first_name
281
+ assert_equal "Herdman", c.last_name
282
+
283
+ c = CreditCard.new :name => "Rocket J. Squirrel"
284
+ assert_equal "Rocket J.", c.first_name
285
+ assert_equal "Squirrel", c.last_name
286
+
287
+ c = CreditCard.new :name => "Twiggy"
288
+ assert_equal "", c.first_name
289
+ assert_equal "Twiggy", c.last_name
290
+ end
291
+
292
+ # The following is a regression for a bug that raised an exception when
293
+ # a new credit card was validated
294
+ def test_validate_new_card
295
+ credit_card = CreditCard.new
296
+
297
+ assert_nothing_raised do
298
+ credit_card.validate
299
+ end
300
+ end
301
+
302
+ # The following is a regression for a bug where the keys of the
303
+ # credit card card_companies hash were not duped when detecting the type
304
+ def test_create_and_validate_credit_card_from_type
305
+ credit_card = CreditCard.new(:type => CreditCard.type?('4242424242424242'))
306
+ assert_nothing_raised do
307
+ credit_card.valid?
308
+ end
309
+ end
310
+
311
+ def test_autodetection_of_credit_card_type
312
+ credit_card = CreditCard.new(:number => '4242424242424242')
313
+ credit_card.valid?
314
+ assert_equal 'visa', credit_card.type
315
+ end
316
+
317
+ def test_card_type_should_not_be_autodetected_when_provided
318
+ credit_card = CreditCard.new(:number => '4242424242424242', :type => 'master')
319
+ credit_card.valid?
320
+ assert_equal 'master', credit_card.type
321
+ end
322
+
323
+ def test_detecting_bogus_card
324
+ credit_card = CreditCard.new(:number => '1')
325
+ credit_card.valid?
326
+ assert_equal 'bogus', credit_card.type
327
+ end
328
+
329
+ def test_validating_bogus_card
330
+ credit_card = credit_card('1', :type => nil)
331
+ assert credit_card.valid?
332
+ end
333
+
334
+ def test_mask_number
335
+ assert_equal 'XXXX-XXXX-XXXX-5100', CreditCard.mask('5105105105105100')
336
+ end
337
+
338
+ def test_strip_non_digit_characters
339
+ card = credit_card('4242-4242 %%%%%%4242......4242')
340
+ assert card.valid?
341
+ assert_equal "4242424242424242", card.number
342
+ end
343
+
344
+ def test_before_validate_handles_blank_number
345
+ card = credit_card(nil)
346
+ assert !card.valid?
347
+ assert_equal "", card.number
348
+ end
349
+
350
+ def test_type_is_aliased_as_brand
351
+ assert_equal @visa.type, @visa.brand
352
+ assert_equal @solo.type, @solo.brand
353
+ end
354
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class ExpiryDateTest < Test::Unit::TestCase
4
+ def test_should_be_expired
5
+ last_month = 2.months.ago
6
+ date = CreditCard::ExpiryDate.new(last_month.month, last_month.year)
7
+ assert date.expired?
8
+ end
9
+
10
+ def test_today_should_not_be_expired
11
+ today = Time.now.utc
12
+ date = CreditCard::ExpiryDate.new(today.month, today.year)
13
+ assert_false date.expired?
14
+ end
15
+
16
+ def test_dates_in_the_future_should_not_be_expired
17
+ next_month = 1.month.from_now
18
+ date = CreditCard::ExpiryDate.new(next_month.month, next_month.year)
19
+ assert_false date.expired?
20
+ end
21
+
22
+ def test_invalid_date
23
+ expiry = CreditCard::ExpiryDate.new(13, 2009)
24
+ assert_equal Time.at(0).utc, expiry.expiration
25
+ end
26
+
27
+ def test_month_and_year_coerced_to_integer
28
+ expiry = CreditCard::ExpiryDate.new("13", "2009")
29
+ assert_equal 13, expiry.month
30
+ assert_equal 2009, expiry.year
31
+ end
32
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ class Dood
4
+ include ActiveMerchant::Validateable
5
+
6
+ attr_accessor :name, :email, :country
7
+
8
+ def validate
9
+ errors.add "name", "cannot be empty" if name.blank?
10
+ errors.add "email", "cannot be empty" if email.blank?
11
+ errors.add_to_base "The country cannot be blank" if country.blank?
12
+ end
13
+
14
+ end
15
+
16
+ class ValidateableTest < Test::Unit::TestCase
17
+
18
+ def setup
19
+ @dood = Dood.new
20
+ end
21
+
22
+ def test_validation
23
+ assert ! @dood.valid?
24
+ assert ! @dood.errors.empty?
25
+ end
26
+
27
+ def test_assigns
28
+ @dood = Dood.new(:name => "tobi", :email => "tobi@neech.de", :country => 'DE')
29
+
30
+ assert_equal "tobi", @dood.name
31
+ assert_equal "tobi@neech.de", @dood.email
32
+ assert @dood.valid?
33
+ end
34
+
35
+ def test_multiple_calls
36
+ @dood.name = "tobi"
37
+ assert !@dood.valid?
38
+
39
+ @dood.email = "tobi@neech.de"
40
+ assert !@dood.valid?
41
+
42
+ @dood.country = 'DE'
43
+ assert @dood.valid?
44
+ end
45
+
46
+ def test_messages
47
+ @dood.valid?
48
+ assert_equal "cannot be empty", @dood.errors.on('name')
49
+ assert_equal "cannot be empty", @dood.errors.on('email')
50
+ assert_equal nil, @dood.errors.on('doesnt_exist')
51
+
52
+ end
53
+
54
+ def test_full_messages
55
+ @dood.valid?
56
+ assert_equal ["Email cannot be empty", "Name cannot be empty", "The country cannot be blank"], @dood.errors.full_messages.sort
57
+ end
58
+
59
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: am_credit_card
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Annesley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70105816630500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.11
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70105816630500
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &70105816629620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70105816629620
36
+ description: ActiveMerchant::Billing::CreditCard, without ActiveMerchant
37
+ email:
38
+ - paul@annesley.cc
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.md
46
+ - Rakefile
47
+ - am_credit_card.gemspec
48
+ - lib/active_merchant/billing/base.rb
49
+ - lib/active_merchant/billing/credit_card.rb
50
+ - lib/active_merchant/billing/credit_card_methods.rb
51
+ - lib/active_merchant/billing/expiry_date.rb
52
+ - lib/active_merchant/validateable.rb
53
+ - lib/am_credit_card.rb
54
+ - lib/am_credit_card_version.rb
55
+ - test/test_helper.rb
56
+ - test/unit/credit_card_methods_test.rb
57
+ - test/unit/credit_card_test.rb
58
+ - test/unit/expiry_date_test.rb
59
+ - test/unit/validateable_test.rb
60
+ homepage: https://github.com/pda/am_credit_card
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.11
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: ActiveMerchant has nice credit card validations, but also lots of dependencies
84
+ and other code. That's fine for those using the rest of ActiveMerchant, but if you're
85
+ just after it's credit card model/validations, this is for you.
86
+ test_files:
87
+ - test/test_helper.rb
88
+ - test/unit/credit_card_methods_test.rb
89
+ - test/unit/credit_card_test.rb
90
+ - test/unit/expiry_date_test.rb
91
+ - test/unit/validateable_test.rb