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 18, 2011
1 parent 45b5e62 commit 0a647db
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 89 deletions.
9 changes: 8 additions & 1 deletion lib/fake_braintree/customer.rb
Expand Up @@ -81,7 +81,14 @@ def verify_credit_card?(customer_hash)
end

def has_invalid_credit_card?(customer_hash)
! FakeBraintree::VALID_CREDIT_CARDS.include?(@customer_hash["credit_card"]["number"])
has_credit_card_number? &&
! FakeBraintree::VALID_CREDIT_CARDS.include?(@customer_hash["credit_card"]["number"])
end

def has_credit_card_number?
@customer_hash.key?("credit_card") &&
@customer_hash["credit_card"].is_a?(Hash) &&
@customer_hash["credit_card"].key?("number")
end
end
end
59 changes: 19 additions & 40 deletions spec/fake_braintree/customer_spec.rb
Expand Up @@ -5,7 +5,7 @@

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

Expand All @@ -16,8 +16,8 @@

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

Expand All @@ -38,55 +38,34 @@
billing_address.street_address.should == "1 E Main St"
billing_address.postal_code.should == "60622"
end
end

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

it "rejects invalid cards" do
create_customer_with_invalid_card(:options => { :verify_card => true }).should_not be_success
end
describe "Braintree::Customer.create", "when passed :verify_card => true" do
it "accepts valid cards" do
create_customer(:options => { :verify_card => true }).should be_success
end

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

it "accepts valid cards" do
create_customer.should be_success
end

it "rejects invalid cards" do
create_customer_with_invalid_card.should_not be_success
end
it "rejects invalid cards" do
create_customer_with_invalid_card(:options => { :verify_card => true }).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
describe "Braintree::Customer.create", "when FakeBraintree.verify_all_cards == true" do
before { FakeBraintree.verify_all_cards! }

def create_customer_with_invalid_card(options = {})
options[:number] = '123456'
options[:expiration_date] ||= expiration_date
create_customer(options)
it "accepts valid cards" do
create_customer.should be_success
end

let(:expiration_month) { "04" }
let(:expiration_year) { "2016" }
let(:expiration_date) { [expiration_month, expiration_year].join("/") }
it "rejects invalid cards" do
create_customer_with_invalid_card.should_not be_success
end
end

describe "Braintree::Customer.find" do
let(:expiration_date) { "04/2016" }

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

it "successfully finds a customer" do
result = Braintree::Customer.create(:first_name => "Bob", :last_name => "Smith")
result = Braintree::Customer.create(:first_name => "Bob",
:last_name => "Smith")

Braintree::Customer.find(result.customer.id).first_name.should == "Bob"
end
Expand Down
92 changes: 44 additions & 48 deletions spec/fake_braintree/subscription_spec.rb
@@ -1,62 +1,58 @@
require 'spec_helper'

describe FakeBraintree::SinatraApp do
context "Braintree::Subscription.create" do
let(:plan_id) { 'plan-id-from-braintree-control-panel' }
let(:cc_number) { %w(4111 1111 1111 9876).join }
let(:expiration_date) { "04/2016" }
let(:payment_method_token) { braintree_credit_card_token(cc_number, expiration_date) }
let(:payment_method_token_2) { braintree_credit_card_token(cc_number.sub('1', '5'), expiration_date) }

it "successfully creates a subscription" do
result = Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id)
result.should be_success
end
describe "Braintree::Subscription.create" do
let(:plan_id) { 'plan-id-from-braintree-control-panel' }
let(:expiration_date) { "04/2016" }

it "successfully creates a subscription" do
result = Braintree::Subscription.create(:payment_method_token => cc_token,
:plan_id => plan_id)
result.should be_success
end

it "assigns a Braintree-esque ID to the subscription" do
result = Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id)
it "assigns a Braintree-esque ID to the subscription" do
result = Braintree::Subscription.create(:payment_method_token => cc_token,
:plan_id => plan_id)

result.subscription.id.should =~ /^[a-z0-9]{6}$/
end
result.subscription.id.should =~ /^[a-z0-9]{6}$/
end

it "assigns unique IDs to each subscription" do
first_result = Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id)
second_result = Braintree::Subscription.create(:payment_method_token => payment_method_token_2,
:plan_id => plan_id)
it "assigns unique IDs to each subscription" do
cc_token_1 = cc_token
cc_token_2 = braintree_credit_card_token(TEST_CC_NUMBER.sub('1', '5'), expiration_date)
first_result = Braintree::Subscription.create(:payment_method_token => cc_token_1,
:plan_id => plan_id)
second_result = Braintree::Subscription.create(:payment_method_token => cc_token_2,
:plan_id => plan_id)

first_result.subscription.id.should_not == second_result.subscription.id
end
first_result.subscription.id.should_not == second_result.subscription.id
end

it "sets the next billing date to 1 month from now in UTC" do
Timecop.freeze do
result = Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id)
it "sets the next billing date to 1 month from now in UTC" do
Timecop.freeze do
result = Braintree::Subscription.create(:payment_method_token => cc_token,
:plan_id => plan_id)

result.subscription.next_billing_date.to_i.should == 1.month.from_now.utc.to_i
end
result.subscription.next_billing_date.to_i.should == 1.month.from_now.utc.to_i
end
end
end

context "Braintree::Subscription.find" do
let(:cc_number) { %w(4111 1111 1111 9876).join }
let(:expiration_date) { "04/2016" }
let(:payment_method_token) { braintree_credit_card_token(cc_number, expiration_date) }
let(:plan_id) { 'my-plan-id' }
let(:subscription_result) { Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id) }

it "can find a created subscription" do
subscription = Braintree::Subscription.find(subscription_result.subscription.id)
subscription.should_not be_nil
subscription.payment_method_token.should == payment_method_token
subscription.plan_id.should == plan_id
end
describe "Braintree::Subscription.find" do

it "raises a Braintree:NotFoundError when it cannot find a subscription" do
expect { Braintree::Subscription.find('abc123') }.to raise_error(Braintree::NotFoundError, /abc123/)
end
it "can find a created subscription" do
subscription = Braintree::Subscription.find(subscription_id)
subscription.should_not be_nil
subscription.payment_method_token.should == payment_method_token
subscription.plan_id.should == plan_id
end

it "raises a Braintree:NotFoundError when it cannot find a subscription" do
expect { Braintree::Subscription.find('abc123') }.to raise_error(Braintree::NotFoundError, /abc123/)
end

let(:payment_method_token) { cc_token }
let(:plan_id) { 'my-plan-id' }
let(:subscription_id) { Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id).subscription.id }
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -17,6 +17,7 @@
config.mock_with :mocha

config.include BraintreeHelpers
config.include CustomerHelpers

config.before { FakeBraintree.clear! }
end
Expand Down
12 changes: 12 additions & 0 deletions spec/support/customer_helpers.rb
@@ -0,0 +1,12 @@
module CustomerHelpers
def create_customer(credit_card_options = {})
credit_card_options[:number] ||= TEST_CC_NUMBER
credit_card_options[:expiration_date] ||= '04/2016'
Braintree::Customer.create(:credit_card => credit_card_options)
end

def create_customer_with_invalid_card(credit_card_options = {})
credit_card_options[:number] = '123456'
create_customer(credit_card_options)
end
end

0 comments on commit 0a647db

Please sign in to comment.