Skip to content

Commit

Permalink
Clean up specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Nov 17, 2011
1 parent c69eb59 commit ebab395
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 56 deletions.
59 changes: 26 additions & 33 deletions spec/fake_braintree/customer_spec.rb
@@ -1,32 +1,23 @@
require 'spec_helper'

describe "Braintree::Customer.create" do
let(:expiration_month) { "04" }
let(:expiration_year) { "2016" }
let(:expiration_date) { [expiration_month, expiration_year].join("/") }
after { FakeBraintree.verify_all_cards = false }

def create_customer_with_credit_card(options)
Braintree::Customer.create(:credit_card => options)
end

it "successfully creates a customer" do
result = create_customer_with_credit_card(:number => TEST_CC_NUMBER,
:expiration_date => expiration_date)
result = Braintree::Customer.create(:credit_card => { :number => TEST_CC_NUMBER,
:expiration_date => expiration_date })
result.should be_success
end

it "creates a customer using an expiration month and year" do
result = create_customer_with_credit_card(:number => TEST_CC_NUMBER,
:expiration_month => expiration_month,
:expiration_year => expiration_year)
result = Braintree::Customer.create(:credit_card => { :number => TEST_CC_NUMBER,
:expiration_month => expiration_month,
:expiration_year => expiration_year })
result.should be_success
end

it "records the billing address" do
result = create_customer_with_credit_card(
:number => TEST_CC_NUMBER,
:expiration_date => expiration_date,
result = create_customer(
:billing_address => {
:street_address => "1 E Main St",
:extended_address => "Suite 3",
Expand All @@ -45,39 +36,41 @@ def create_customer_with_credit_card(options)

context "when passed :verify_card => true" do
it "accepts valid cards" do
create_customer_with_credit_card(
:number => TEST_CC_NUMBER,
:expiration_date => expiration_date,
:options => { :verify_card => true }
).should be_success
create_customer(:options => { :verify_card => true }).should be_success
end

it "rejects invalid cards" do
create_customer_with_credit_card(
:number => '123456',
:expiration_date => expiration_date,
:options => { :verify_card => true }
).should_not be_success
create_customer_with_invalid_card(:options => { :verify_card => true }).should_not be_success
end
end

context "when FakeBraintree.verify_all_cards == true" do
before { FakeBraintree.verify_all_cards! }

it "accepts valid cards" do
create_customer_with_credit_card(
:number => TEST_CC_NUMBER,
:expiration_date => expiration_date
).should be_success
create_customer.should be_success
end

it "rejects invalid cards" do
create_customer_with_credit_card(
:number => '123456',
:expiration_date => expiration_date
).should_not be_success
create_customer_with_invalid_card.should_not be_success
end
end

def create_customer(options = {})
options[:number] ||= TEST_CC_NUMBER
options[:expiration_date] ||= expiration_date
Braintree::Customer.create(:credit_card => options)
end

def create_customer_with_invalid_card(options = {})
options[:number] = '123456'
options[:expiration_date] ||= expiration_date
create_customer(options)
end

let(:expiration_month) { "04" }
let(:expiration_year) { "2016" }
let(:expiration_date) { [expiration_month, expiration_year].join("/") }
end

describe "Braintree::Customer.find" do
Expand Down
46 changes: 23 additions & 23 deletions spec/fake_braintree_spec.rb
@@ -1,12 +1,6 @@
require 'spec_helper'

describe FakeBraintree, ".credit_card_from_token" do
let(:cc_number_2) { %w(4111 1111 1111 2222).join }
let(:expiration_date) { "04/2016" }
let(:expiration_date_2) { "05/2019" }
let(:token) { braintree_credit_card_token(TEST_CC_NUMBER, expiration_date) }
let(:token_2) { braintree_credit_card_token(cc_number_2, expiration_date_2) }

it "looks up the credit card based on a CC token" do
credit_card = FakeBraintree.credit_card_from_token(token)
credit_card["last_4"].should == TEST_CC_NUMBER[-4,4]
Expand All @@ -18,26 +12,28 @@
credit_card["expiration_year"].should == "2019"
credit_card["expiration_month"].should == "05"
end

let(:cc_number_2) { %w(4111 1111 1111 2222).join }
let(:expiration_date) { "04/2016" }
let(:expiration_date_2) { "05/2019" }
let(:token) { braintree_credit_card_token(TEST_CC_NUMBER, expiration_date) }
let(:token_2) { braintree_credit_card_token(cc_number_2, expiration_date_2) }
end

describe FakeBraintree, ".decline_all_cards!" do
let(:expiration_date) { "04/2016" }
let(:token) { braintree_credit_card_token(TEST_CC_NUMBER, expiration_date) }
let(:amount) { 10.00 }

before do
FakeBraintree.decline_all_cards!
end
before { FakeBraintree.decline_all_cards! }

it "declines all cards" do
result = Braintree::CreditCard.sale(token, amount: amount)
result.should_not be_success
create_sale.should_not be_success
end

it "stops declining cards after clear! is called" do
FakeBraintree.clear!
result = Braintree::CreditCard.sale(token, amount: amount)
result.should be_success
create_sale.should be_success
end

def create_sale
Braintree::CreditCard.sale(cc_token, amount: 10.00)
end
end

Expand All @@ -47,7 +43,7 @@
end
end

describe "configuration variables" do
describe Braintree::Configuration do
subject { Braintree::Configuration }

it "sets the environment to development" do
Expand All @@ -72,7 +68,7 @@

describe FakeBraintree, ".clear_log!" do
it "clears the log file" do
%w(one two).each { |string| Braintree::Configuration.logger.info(string) }
write_to_log
subject.clear_log!
File.read(FakeBraintree.log_file_path).should == ""
end
Expand All @@ -81,9 +77,17 @@
FakeBraintree.expects(:clear_log!)
FakeBraintree.clear!
end

def write_to_log
Braintree::Configuration.logger.info('foo bar baz')
end
end

describe FakeBraintree, "VALID_CREDIT_CARDS" do
it "includes only credit cards that are valid in the Braintree sandbox" do
FakeBraintree::VALID_CREDIT_CARDS.sort.should == valid_credit_cards.sort
end

let(:valid_credit_cards) do
%w(4111111111111111 4005519200000004
4009348888881881 4012000033330026
Expand All @@ -94,8 +98,4 @@
3530111333300000
)
end

it "includes only credit cards that are valid in production" do
FakeBraintree::VALID_CREDIT_CARDS.sort.should == valid_credit_cards.sort
end
end

0 comments on commit ebab395

Please sign in to comment.