module RubyPsigate class CreditCardTest < Test::Unit::TestCase def setup @valid_attributes = { :number => "4111111111111111", :month => "12", :year => "2020", :verification_value => "123", :name => "Bob John" } @cc = CreditCard.new(@valid_attributes) end should "respond to number" do assert @cc.respond_to?(:number) assert @cc.respond_to?(:number=) end should "respond to month" do assert @cc.respond_to?(:month) assert @cc.respond_to?(:month=) end should "respond to year" do assert @cc.respond_to?(:year) assert @cc.respond_to?(:year=) end should "respond to verification_value" do assert @cc.respond_to?(:verification_value) assert @cc.respond_to?(:verification_value=) end should "respond to type" do assert @cc.respond_to?(:type) end should "respond_to name" do assert @cc.respond_to?(:name) assert @cc.respond_to?(:name=) end %w( number month year name ).each do |p| should "raise error if #{p} is not included" do @valid_attributes.delete(p.to_sym) assert_raises(ArgumentError) { CreditCard.new(@valid_attributes) } end end context "cc numbers" do %w( 4111111111111111 ).each do |n| should "#{n} be a valid" do @valid_attributes[:number] = n assert_nothing_raised { CreditCard.new(@valid_attributes) } end end %w( 1234567890123456 ).each do |n| should "#{n} not be valid" do @valid_attributes[:number] = n assert_raises(CreditCardNumberInvalid) { CreditCard.new(@valid_attributes) } end end end context "cc type" do %w( visa mastercard american_express ).each do |type| should "support #{type}" do String.any_instance.expects(:creditcard_type).returns(type) assert_nothing_raised { CreditCard.new(@valid_attributes) } end end %w( diners_club bankcard jcb enroute switch ).each do |type| should "not support #{type}" do String.any_instance.expects(:creditcard_type).returns(type) assert_raises(CreditCardNotSupported) { CreditCard.new(@valid_attributes) } end end end should "raise error if name is blank" do @valid_attributes[:name] = nil assert_raises(CreditCardOwnerNameMissing) { CreditCard.new(@valid_attributes) } end context "expiry date" do %w( 13 14 15 16 ).each do |month| should "raise CreditCardExpiryInvalid when the month is #{month}" do @valid_attributes[:month] = month assert_raises(CreditCardExpiryInvalid) { CreditCard.new(@valid_attributes) } end end %w( 1990 1999 2008 2009 ).each do |year| should "raise CreditCardExpiryInvalid when month is #{year}" do @valid_attributes[:year] = year assert_raises(CreditCardExpiryInvalid) { CreditCard.new(@valid_attributes) } end end should "raise CreditCardExpired when expiry is #{Time.now.month-1}/#{Time.now.year}" do @valid_attributes[:month] = ((Time.now.month)-1) @valid_attributes[:year] = Time.now.year assert_raises(CreditCardExpired) { CreditCard.new(@valid_attributes) } end %w( 1 2 3 4 5 6 7 8 9 10 11 12 ).each do |month| should "not raise CreditCardExpired when expiry is #{month}/2010 and today's date is the last day of the same month" do @valid_attributes[:month] = month @valid_attributes[:year] = 2010 last_day = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month.to_i] mock_today = Time.new(2010, month, last_day) Time.stubs(:now).returns(mock_today) assert_nothing_raised { CreditCard.new(@valid_attributes) } end end should "not raise CreditCardExpired when expiry is 2/2012 and today's date is 2/29/2010 (leap year)" do @valid_attributes[:month] = 2 @valid_attributes[:year] = 2012 mock_today = Time.new(2010, 2, 29) Time.stubs(:now).returns(mock_today) assert_nothing_raised { CreditCard.new(@valid_attributes) } end end should "return a hash" do @expectation = { :PaymentType => "CC", :CardNumber => @cc.number, :CardExpMonth => @cc.month, :CardExpYear => @cc.year[2..3], :CardIDNumber => @cc.verification_value } assert_equal @expectation, @cc.to_hash end should "return a hash for CardInfo hash (used in the account manager API)" do @expectation = { :CardHolder => "Bob John", :CardNumber => "4111111111111111", :CardExpMonth => "12", :CardExpYear => "20" } assert_equal @expectation, @cc.to_hash(:card_info) end end end