require 'spec_helper' describe CreditCardValidator do context "Credit card has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: true end end subject { klass.new } it { should allow_value("378282246310005").for(:cc_number) } # American Express it { should allow_value("371449635398431").for(:cc_number) } # American Express it { should allow_value("30569309025904").for(:cc_number) } # Diners Club it { should allow_value("38520000023237").for(:cc_number) } # Diners Club it { should allow_value("6011111111111117").for(:cc_number) } # Discover it { should allow_value("6011000990139424").for(:cc_number) } # Discover it { should allow_value("3530111333300000").for(:cc_number) } # JCB it { should allow_value("3566002020360505").for(:cc_number) } # JCB it { should allow_value("5555555555554444").for(:cc_number) } # Master Card it { should allow_value("5200828282828210").for(:cc_number) } # Master Card Debit it { should allow_value("5105105105105100").for(:cc_number) } # Master Card Prepaid it { should allow_value("4242424242424242").for(:cc_number) } # Visa it { should allow_value("4012888888881881").for(:cc_number) } # Visa it { should allow_value("4012888888881881").for(:cc_number) } # Visa Debit it { should allow_value("4000056655665556").for(:cc_number) } # Visa Debit it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("400005665566").for(:cc_number) } it { should_not allow_value("40000566556655566").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end context "Credit card with :american_express option has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: { american_express: true } end end subject { klass.new } it { should allow_value("378282246310005").for(:cc_number) } it { should allow_value("371449635398431").for(:cc_number) } it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("37828224631000").for(:cc_number) } it { should_not allow_value("3714496353984315").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end context "Credit card with :american_express option has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: { american_express: true } end end subject { klass.new } it { should allow_value("378282246310005").for(:cc_number) } it { should allow_value("371449635398431").for(:cc_number) } it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("30569309025904").for(:cc_number) } it { should_not allow_value("38520000023237").for(:cc_number) } it { should_not allow_value("6011111111111117").for(:cc_number) } it { should_not allow_value("6011000990139424").for(:cc_number) } it { should_not allow_value("3530111333300000").for(:cc_number) } it { should_not allow_value("3566002020360505").for(:cc_number) } it { should_not allow_value("5555555555554444").for(:cc_number) } it { should_not allow_value("5200828282828210").for(:cc_number) } it { should_not allow_value("5105105105105100").for(:cc_number) } it { should_not allow_value("4242424242424242").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4000056655665556").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end context "Credit card with :diners_club option has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: { diners_club: true } end end subject { klass.new } it { should allow_value("30569309025904").for(:cc_number) } it { should allow_value("38520000023237").for(:cc_number) } it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("378282246310005").for(:cc_number) } it { should_not allow_value("371449635398431").for(:cc_number) } it { should_not allow_value("6011111111111117").for(:cc_number) } it { should_not allow_value("6011000990139424").for(:cc_number) } it { should_not allow_value("3530111333300000").for(:cc_number) } it { should_not allow_value("3566002020360505").for(:cc_number) } it { should_not allow_value("5555555555554444").for(:cc_number) } it { should_not allow_value("5200828282828210").for(:cc_number) } it { should_not allow_value("5105105105105100").for(:cc_number) } it { should_not allow_value("4242424242424242").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4000056655665556").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end context "Credit card with :discover option has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: { discover: true } end end subject { klass.new } it { should allow_value("6011111111111117").for(:cc_number) } it { should allow_value("6011000990139424").for(:cc_number) } it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("378282246310005").for(:cc_number) } it { should_not allow_value("371449635398431").for(:cc_number) } it { should_not allow_value("30569309025904").for(:cc_number) } it { should_not allow_value("38520000023237").for(:cc_number) } it { should_not allow_value("3530111333300000").for(:cc_number) } it { should_not allow_value("3566002020360505").for(:cc_number) } it { should_not allow_value("5555555555554444").for(:cc_number) } it { should_not allow_value("5200828282828210").for(:cc_number) } it { should_not allow_value("5105105105105100").for(:cc_number) } it { should_not allow_value("4242424242424242").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4000056655665556").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end context "Credit card with :jbc option has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: { jbc: true } end end subject { klass.new } it { should allow_value("3530111333300000").for(:cc_number) } it { should allow_value("3566002020360505").for(:cc_number) } it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("378282246310005").for(:cc_number) } it { should_not allow_value("371449635398431").for(:cc_number) } it { should_not allow_value("30569309025904").for(:cc_number) } it { should_not allow_value("38520000023237").for(:cc_number) } it { should_not allow_value("6011111111111117").for(:cc_number) } it { should_not allow_value("6011000990139424").for(:cc_number) } it { should_not allow_value("5555555555554444").for(:cc_number) } it { should_not allow_value("5200828282828210").for(:cc_number) } it { should_not allow_value("5105105105105100").for(:cc_number) } it { should_not allow_value("4242424242424242").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4000056655665556").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end context "Credit card with :master_card option has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: { master_card: true } end end subject { klass.new } it { should allow_value("5555555555554444").for(:cc_number) } it { should allow_value("5200828282828210").for(:cc_number) } it { should allow_value("5105105105105100").for(:cc_number) } it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("378282246310005").for(:cc_number) } it { should_not allow_value("371449635398431").for(:cc_number) } it { should_not allow_value("30569309025904").for(:cc_number) } it { should_not allow_value("38520000023237").for(:cc_number) } it { should_not allow_value("6011111111111117").for(:cc_number) } it { should_not allow_value("6011000990139424").for(:cc_number) } it { should_not allow_value("3530111333300000").for(:cc_number) } it { should_not allow_value("3566002020360505").for(:cc_number) } it { should_not allow_value("4242424242424242").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4012888888881881").for(:cc_number) } it { should_not allow_value("4000056655665556").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end context "Credit card with :visa option has a valid value" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :cc_number, :name validates :cc_number, credit_card: { visa: true } end end subject { klass.new } it { should allow_value("4242424242424242").for(:cc_number) } it { should allow_value("4012888888881881").for(:cc_number) } it { should allow_value("4012888888881881").for(:cc_number) } it { should allow_value("4000056655665556").for(:cc_number) } it { should_not allow_value('').for(:cc_number) } it { should_not allow_value(' ').for(:cc_number) } it { should_not allow_value(nil).for(:cc_number) } it { should_not allow_value("#").for(:cc_number) } it { should_not allow_value("9").for(:cc_number) } it { should_not allow_value("a").for(:cc_number) } it { should_not allow_value("378282246310005").for(:cc_number) } it { should_not allow_value("371449635398431").for(:cc_number) } it { should_not allow_value("30569309025904").for(:cc_number) } it { should_not allow_value("38520000023237").for(:cc_number) } it { should_not allow_value("6011111111111117").for(:cc_number) } it { should_not allow_value("6011000990139424").for(:cc_number) } it { should_not allow_value("3530111333300000").for(:cc_number) } it { should_not allow_value("3566002020360505").for(:cc_number) } it { should_not allow_value("5555555555554444").for(:cc_number) } it { should_not allow_value("5200828282828210").for(:cc_number) } it { should_not allow_value("5105105105105100").for(:cc_number) } it { should_not allow_value("! \#$%\`|").for(:cc_number) } it { should_not allow_value("<>@[]\`|").for(:cc_number) } it { should ensure_valid_credit_card_format_of(:cc_number) } it { should_not ensure_valid_credit_card_format_of(:name) } end end