Skip to content

Commit

Permalink
Fix subscription examples when running specs in live mode
Browse files Browse the repository at this point in the history
* Stripe returns an instance of `Stripe::Discount` and not a basic
  `Stripe::StripeObject`

* Additionally while here update the add_coupon helper to allow it
  to add discount/coupon to other types of objects besides customers
  and ensure that we are returning an instance of `Stripe::Discount`
  with correct parameters in all cases where we would expect Stripe
  to do so.
  • Loading branch information
joshcass committed May 22, 2019
1 parent dadfe51 commit 759f50b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/stripe_mock/request_handlers/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def new_customer(route, method_url, params, headers)
coupon = coupons[ params[:coupon] ]
assert_existence :coupon, params[:coupon], coupon

add_coupon_to_customer(customers[params[:id]], coupon)
add_coupon_to_object(customers[params[:id]], coupon)
end

customers[ params[:id] ]
Expand Down Expand Up @@ -90,7 +90,7 @@ def update_customer(route, method_url, params, headers)
coupon = coupons[ params[:coupon] ]
assert_existence :coupon, params[:coupon], coupon

add_coupon_to_customer(cus, coupon)
add_coupon_to_object(cus, coupon)
end

cus
Expand Down
21 changes: 10 additions & 11 deletions lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
module StripeMock
module RequestHandlers
module Helpers
def add_coupon_to_object(object, coupon)
discount_attrs = {}.tap do |attrs|
attrs[object[:object]] = object[:id]
attrs[:coupon] = coupon
attrs[:start] = Time.now.to_i
attrs[:end] = (DateTime.now >> coupon[:duration_in_months].to_i).to_time.to_i if coupon[:duration] == 'repeating'
end

def add_coupon_to_customer(customer, coupon)
customer[:discount] = {
coupon: coupon,
customer: customer[:id],
start: Time.now.to_i,
}
customer[:discount][:end] = (DateTime.now >> coupon[:duration_in_months]).to_time.to_i if coupon[:duration].to_sym == :repeating && coupon[:duration_in_months]

customer
object[:discount] = Stripe::Discount.construct_from(discount_attrs)
object
end

end
end
end
end
8 changes: 4 additions & 4 deletions lib/stripe_mock/request_handlers/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def create_customer_subscription(route, method_url, params, headers)
coupon = coupons[coupon_id]

if coupon
subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
add_coupon_to_object(subscription, coupon)
else
raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', http_status: 400)
end
Expand Down Expand Up @@ -117,7 +117,7 @@ def create_subscription(route, method_url, params, headers)
coupon = coupons[coupon_id]

if coupon
subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
add_coupon_to_object(subscription, coupon)
else
raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', http_status: 400)
end
Expand Down Expand Up @@ -174,9 +174,9 @@ def update_subscription(route, method_url, params, headers)

coupon = coupons[coupon_id]
if coupon
subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
add_coupon_to_object(subscription, coupon)
elsif coupon_id == ""
subscription[:discount] = Stripe::Util.convert_to_stripe_object(nil, {})
subscription[:discount] = nil
else
raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', http_status: 400)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/shared_stripe_examples/subscription_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def gen_card_tk
expect(customer.subscriptions.data).to be_a(Array)
expect(customer.subscriptions.data.count).to eq(1)
expect(customer.subscriptions.data.first.discount).not_to be_nil
expect(customer.subscriptions.data.first.discount).to be_a(Stripe::StripeObject)
expect(customer.subscriptions.data.first.discount).to be_a(Stripe::Discount)
expect(customer.subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
end

Expand Down Expand Up @@ -614,7 +614,7 @@ def gen_card_tk
subscription.save

expect(subscription.discount).not_to be_nil
expect(subscription.discount).to be_an_instance_of(Stripe::StripeObject)
expect(subscription.discount).to be_a(Stripe::Discount)
expect(subscription.discount.coupon.id).to eq(coupon.id)
end

Expand Down

0 comments on commit 759f50b

Please sign in to comment.