Skip to content

Commit

Permalink
Basic support for add-ons
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Nov 29, 2011
1 parent c163c4d commit 312cfac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/fake_braintree/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def subscription_hash
subscription_hash = @subscription_hash.dup
subscription_hash["id"] ||= subscription_id
subscription_hash["transactions"] = []
subscription_hash["add_ons"] = []
subscription_hash["add_ons"] = added_add_ons
subscription_hash["discounts"] = []
subscription_hash["plan_id"] = @subscription_hash["plan_id"]
subscription_hash["next_billing_date"] = braintree_formatted_date(1.month.from_now)
Expand Down Expand Up @@ -52,5 +52,13 @@ def braintree_formatted_date(date)
def subscription_id
md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6]
end

def added_add_ons
if @subscription_hash["add_ons"] && @subscription_hash["add_ons"]["add"]
@subscription_hash["add_ons"]["add"].map { |add_on| { "id" => add_on["inherited_from_id"] } }
else
[]
end
end
end
end
17 changes: 13 additions & 4 deletions spec/fake_braintree/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,29 @@

describe "Braintree::Subscription.find" do
it "can find a created subscription" do
payment_method_token = cc_token
plan_id = "abc123"
subscription_id =
create_subscription(:payment_method_token => payment_method_token, :plan_id => plan_id).subscription.id
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
create_subscription
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 }
it "returns add-ons added with the subscription" do
add_on_id = "def456"
subscription_id = create_subscription(:add_ons => { :add => [{ :inherited_from_id => add_on_id }] }).subscription.id
subscription = Braintree::Subscription.find(subscription_id)
add_ons = subscription.add_ons
add_ons.size.should == 1
add_ons.first.id.should == add_on_id
end
end

describe "Braintree::Subscription.update" do
Expand Down
6 changes: 3 additions & 3 deletions spec/support/subscription_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SubscriptionHelpers
def create_subscription
Braintree::Subscription.create(:payment_method_token => cc_token,
:plan_id => 'my_plan_id')
def create_subscription(options = {})
Braintree::Subscription.create({ :payment_method_token => cc_token,
:plan_id => 'my_plan_id' }.merge(options))
end
end

0 comments on commit 312cfac

Please sign in to comment.