require File.dirname(__FILE__) + "/../test_helper" class ValidatorTest < Test::Unit::TestCase def test_strings_invalid_when_too_long assert EWS::Transaction::Request.new(basic_params).valid? assert EWS::Transaction::Request.new(basic_params(:authorization_num => 'a'*8)).valid? assert !EWS::Transaction::Request.new(basic_params(:authorization_num => 'a'*9)).valid? assert EWS::Transaction::Request.new(basic_params(:cardholder_name => 'a'*30)).valid? assert !EWS::Transaction::Request.new(basic_params(:cardholder_name => 'a'*31)).valid? assert EWS::Transaction::Request.new(basic_params(:cc_number => 'a'*19)).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_number => 'a'*20)).valid? assert EWS::Transaction::Request.new(basic_params(:cc_expiry => "0913")).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "09131")).valid? assert EWS::Transaction::Request.new(basic_params(:cavv => 'a'*40)).valid? assert !EWS::Transaction::Request.new(basic_params(:cavv => 'a'*41)).valid? assert EWS::Transaction::Request.new(basic_params(:client_email => 'a'*30)).valid? assert !EWS::Transaction::Request.new(basic_params(:client_email => 'a'*31)).valid? assert EWS::Transaction::Request.new(basic_params(:customer_ref => 'a'*20)).valid? assert !EWS::Transaction::Request.new(basic_params(:customer_ref => 'a'*21)).valid? assert EWS::Transaction::Request.new(basic_params(:gateway_id => 'a'*10)).valid? assert !EWS::Transaction::Request.new(basic_params(:gateway_id => 'a'*11)).valid? assert EWS::Transaction::Request.new(basic_params(:pan => 'a'*39)).valid? assert !EWS::Transaction::Request.new(basic_params(:pan => 'a'*40)).valid? assert EWS::Transaction::Request.new(basic_params(:password => 'a'*30)).valid? assert !EWS::Transaction::Request.new(basic_params(:password => 'a'*31)).valid? assert EWS::Transaction::Request.new(basic_params(:reference_3 => 'a'*30)).valid? assert !EWS::Transaction::Request.new(basic_params(:reference_3 => 'a'*31)).valid? assert EWS::Transaction::Request.new(basic_params(:reference_no => 'a'*20)).valid? assert !EWS::Transaction::Request.new(basic_params(:reference_no => 'a'*21)).valid? assert EWS::Transaction::Request.new(basic_params(:tax1_number => 'a'*20)).valid? assert !EWS::Transaction::Request.new(basic_params(:tax1_number => 'a'*21)).valid? assert EWS::Transaction::Request.new(basic_params(:tax2_number => 'a'*20)).valid? assert !EWS::Transaction::Request.new(basic_params(:tax2_number => 'a'*21)).valid? assert EWS::Transaction::Request.new(basic_params(:track1 => 'a'*79)).valid? assert !EWS::Transaction::Request.new(basic_params(:track1 => 'a'*80)).valid? assert EWS::Transaction::Request.new(basic_params(:track2 => 'a'*40)).valid? assert !EWS::Transaction::Request.new(basic_params(:track2 => 'a'*41)).valid? assert EWS::Transaction::Request.new(basic_params(:cc_verification_str1 => 'a'*40)).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_verification_str1 => 'a'*41)).valid? assert EWS::Transaction::Request.new(basic_params(:cc_verification_str2 => 'a'*4)).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_verification_str2 => 'a'*5)).valid? assert EWS::Transaction::Request.new(basic_params(:xid => 'a'*40)).valid? assert !EWS::Transaction::Request.new(basic_params(:xid => 'a'*41)).valid? assert EWS::Transaction::Request.new(basic_params(:zip_code => 'a'*10)).valid? assert !EWS::Transaction::Request.new(basic_params(:zip_code => 'a'*11)).valid? end def test_requires_gateway_and_password assert EWS::Transaction::Request.new(basic_params).valid? assert !EWS::Transaction::Request.new(basic_params(:gateway_id => nil)).valid? assert !EWS::Transaction::Request.new(basic_params(:gateway_id => "")).valid? assert !EWS::Transaction::Request.new(basic_params(:password => nil)).valid? assert !EWS::Transaction::Request.new(basic_params(:password => "")).valid? end def test_amount_validation # validation: amounts [:amount, :surcharge_amount, :tax1_amount, :tax2_amount].each do |amount_attr| assert EWS::Transaction::Request.new(basic_params(amount_attr => "10.13")).valid? assert EWS::Transaction::Request.new(basic_params(amount_attr => "10.")).valid? assert EWS::Transaction::Request.new(basic_params(amount_attr => "0.13")).valid? assert EWS::Transaction::Request.new(basic_params(amount_attr => 10.13)).valid? assert EWS::Transaction::Request.new(basic_params(amount_attr => 0.13)).valid? assert EWS::Transaction::Request.new(basic_params(amount_attr => 10)).valid? r = EWS::Transaction::Request.new(basic_params(amount_attr => "10.1s3")) assert !r.valid? assert !r.errors[amount_attr].blank? r = EWS::Transaction::Request.new(basic_params(amount_attr => "1s0.13")) assert !r.valid? assert !r.errors[amount_attr].blank? r = EWS::Transaction::Request.new(basic_params(amount_attr => "dopey")) assert !r.valid? assert !r.errors[amount_attr].blank? r = EWS::Transaction::Request.new(basic_params(amount_attr => 100000)) assert !r.valid? assert !r.errors[amount_attr].blank? r = EWS::Transaction::Request.new(basic_params(amount_attr => 99999.99)) assert r.valid? assert_nil r.errors[amount_attr] r = EWS::Transaction::Request.new(basic_params(amount_attr => -0.01)) assert !r.valid? assert !r.errors[amount_attr].blank? r = EWS::Transaction::Request.new(basic_params(amount_attr => 0.0)) assert r.valid? assert r.errors[amount_attr].blank? end end def test_card_validation # validation: card number assert EWS::Transaction::Request.new(basic_params(:cc_number => "4111111111111111")).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_number => "4111111111111112")).valid? # validation: card expiry (should be in MMYY format) assert EWS::Transaction::Request.new(basic_params(:cc_expiry => "1009")).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => nil)).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "")).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "708")).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "07089")).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "07f")).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "1308")).valid? # invalid month # ensure we don't accept dates in the past t = Time.new month = t.month year = t.year - 2000 # in the past assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => sprintf("%02d%02d", month, year-1))).valid? assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => sprintf("%02d%02d", month-1, year))).valid? # in the present or future assert EWS::Transaction::Request.new(basic_params(:cc_expiry => sprintf("%02d%02d", month, year))).valid? assert EWS::Transaction::Request.new(basic_params(:cc_expiry => sprintf("%02d%02d", month+1, year))).valid? assert EWS::Transaction::Request.new(basic_params(:cc_expiry => sprintf("%02d%02d", month, year+1))).valid? # we've chosen 1980 as our cutoff in the past, so check around this date assert EWS::Transaction::Request.new(basic_params(:cc_expiry => "1279")).valid? # Dec 2079 assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "0180")).valid? # Jan 1980 assert !EWS::Transaction::Request.new(basic_params(:cc_expiry => "0199")).valid? # Jan 1999 end end