Skip to content

Commit

Permalink
Merge pull request stripe-ruby-mock#638 from PlugIN73/live-tests-fixes
Browse files Browse the repository at this point in the history
Live tests fixes
  • Loading branch information
alexmamonchik authored Sep 29, 2019
2 parents 226bf2d + 081f610 commit cfc8846
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rvm:
- 2.3.4
before_install:
- rvm 2.1.10 do gem install mime-types -v 2.6.2
- gem install bundler
- gem install bundler -v '< 2'
before_script:
- "sudo touch /var/log/stripe-mock-server.log"
- "sudo chown travis /var/log/stripe-mock-server.log"
Expand All @@ -26,3 +26,4 @@ notifications:
on_success: change # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: false # default: false

2 changes: 1 addition & 1 deletion lib/stripe_mock/request_handlers/payment_intents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def non_positive_charge_amount?(params)
params[:amount] && params[:amount] < 1
end

def last_payment_error_generator(code:, message:, decline_code:)
def last_payment_error_generator(code: nil, message: nil, decline_code: nil)
{
code: code,
doc_url: "https://stripe.com/docs/error-codes/payment-intent-authentication-failure",
Expand Down
11 changes: 11 additions & 0 deletions lib/stripe_mock/request_handlers/validators/param_validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ module ParamValidators

def validate_create_plan_params(params)
params[:id] = params[:id].to_s
required_product_fields = @base_strategy.create_plan_params[:product].keys

message = "Missing required param: name."
raise Stripe::InvalidRequestError.new(message, :product) if params[:product].nil?

required_product_fields.each do |name|
message = "Missing required param: #{name}."
raise Stripe::InvalidRequestError.new(message, name) if params[:product][name].nil?
end

@base_strategy.create_plan_params.keys.each do |name|
message =
if name == :amount
"Plans require an `#{name}` parameter to be set."
elsif name == :product
"Missing required param: name."
else
"Missing required param: #{name}."
end
Expand Down
10 changes: 4 additions & 6 deletions spec/shared_stripe_examples/invoice_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
let(:customer) { Stripe::Customer.create(source: stripe_helper.generate_card_token) }
let(:coupon_amtoff) { stripe_helper.create_coupon(id: '100OFF', currency: 'usd', amount_off: 100_00, duration: 'repeating', duration_in_months: 6) }
let(:coupon_pctoff) { stripe_helper.create_coupon(id: '50%OFF', currency: 'usd', percent_off: 50, amount_off: nil, duration: 'repeating', duration_in_months: 6) }
let(:plan) { stripe_helper.create_plan(id: '50m', amount: 50_00, interval: 'month', name: '50m', currency: 'usd') }
let(:plan) { stripe_helper.create_plan(id: '50m', amount: 50_00, interval: 'month', product: { name: '50m' }, currency: 'usd') }
let(:quantity) { 3 }
let(:subscription) { Stripe::Subscription.create(plan: plan.id, customer: customer.id, quantity: quantity) }

Expand Down Expand Up @@ -248,9 +248,9 @@

[false, true].each do |with_trial|
describe "prorating a subscription with a new plan, with_trial: #{with_trial}" do
let(:new_monthly_plan) { stripe_helper.create_plan(id: '100m', amount: 100_00, interval: 'month', name: '100m', currency: 'usd') }
let(:new_yearly_plan) { stripe_helper.create_plan(id: '100y', amount: 100_00, interval: 'year', name: '100y', currency: 'usd') }
let(:plan) { stripe_helper.create_plan(id: '50m', amount: 50_00, interval: 'month', name: '50m', currency: 'usd') }
let(:new_monthly_plan) { stripe_helper.create_plan(id: '100m', amount: 100_00, interval: 'month', product: { name: '100m' }, currency: 'usd') }
let(:new_yearly_plan) { stripe_helper.create_plan(id: '100y', amount: 100_00, interval: 'year', product: { name: '100y' }, currency: 'usd') }
let(:plan) { stripe_helper.create_plan(id: '50m', amount: 50_00, interval: 'month', product: { name: '50m' }, currency: 'usd') }

it 'prorates while maintaining billing interval', live: true do
# Given
Expand All @@ -277,7 +277,6 @@
expect(upcoming.amount_due).to be_within(1).of prorated_amount_due - credit_balance
end
expect(upcoming.starting_balance).to eq -credit_balance
expect(upcoming.ending_balance).to be_nil
expect(upcoming.subscription).to eq(subscription.id)

if with_trial
Expand Down Expand Up @@ -328,7 +327,6 @@
expect(upcoming.amount_due).to be_within(1).of prorated_amount_due - credit_balance
end
expect(upcoming.starting_balance).to eq -credit_balance
expect(upcoming.ending_balance).to be_nil
expect(upcoming.subscription).to eq(subscription.id)

expect(upcoming.lines.data[0].proration).to be_truthy
Expand Down
12 changes: 5 additions & 7 deletions spec/shared_stripe_examples/plan_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
it "creates a stripe plan" do
plan = Stripe::Plan.create(
:id => 'pid_1',
:name => 'The Mock Plan',
:amount => 9900,
:currency => 'USD',
:interval => 1,
Expand All @@ -20,7 +19,7 @@
)

expect(plan.id).to eq('pid_1')
expect(plan.name).to eq('The Mock Plan')
expect(plan.product.name).to eq('A product')
expect(plan.amount).to eq(9900)

expect(plan.currency).to eq('USD')
Expand All @@ -35,7 +34,6 @@

it "creates a stripe plan without specifying ID" do
plan = Stripe::Plan.create(
:name => 'The Mock Plan',
:amount => 9900,
:currency => 'USD',
:interval => 1,
Expand All @@ -50,7 +48,6 @@
it "stores a created stripe plan in memory" do
plan = Stripe::Plan.create(
:id => 'pid_2',
:name => 'The Memory Plan',
:amount => 1100,
:currency => 'USD',
:interval => 1,
Expand All @@ -60,7 +57,6 @@
)
plan2 = Stripe::Plan.create(
:id => 'pid_3',
:name => 'The Bonk Plan',
:amount => 7777,
:currency => 'USD',
:interval => 1,
Expand Down Expand Up @@ -145,7 +141,6 @@
expect {
Stripe::Plan.create(
:id => 'pid_1',
:name => 'The Mock Plan',
:amount => 99.99,
:currency => 'USD',
:interval => 'month',
Expand All @@ -172,7 +167,10 @@
expect { subject }.to raise_error(Stripe::InvalidRequestError, message)
end

it("requires a product") { @name = :product }
it("requires a product name") {
@name = :name
params[:product].delete(@name)
}
it("requires an amount") { @name = :amount }
it("requires a currency") { @name = :currency }
it("requires an interval") { @name = :interval }
Expand Down
26 changes: 16 additions & 10 deletions spec/shared_stripe_examples/subscription_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,6 @@ def gen_card_tk
expect(sub.days_until_due).to eq 30
end

let(:subscription_header) {{
:idempotency_key => 'a_idempotency_key'
}}

it "adds a new subscription to customer with identical idempotency key" do
plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
amount: 4999, currency: 'usd')
Expand All @@ -531,6 +527,10 @@ def gen_card_tk
expect(customer.subscriptions.data).to be_empty
expect(customer.subscriptions.count).to eq(0)

subscription_header = {
:idempotency_key => "uniq_idempotency_key_#{customer.id}"
}

sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
expect(sub1).to eq(sub2)
Expand All @@ -544,8 +544,12 @@ def gen_card_tk
expect(customer.subscriptions.data).to be_empty
expect(customer.subscriptions.count).to eq(0)

subscription_header = {
:idempotency_key => "uniq_idempotency_key_#{customer.id}"
}

another_subscription_header = {
:idempotency_key => 'another_idempotency_key'
:idempotency_key => "another_uniq_idempotency_key_#{customer.id}"
}

sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
Expand Down Expand Up @@ -1046,11 +1050,13 @@ def gen_card_tk

it "doesn't require a card when trial_end is present", :live => true do
plan = stripe_helper.create_plan(
:amount => 2000,
:interval => 'month',
:name => 'Amazing Gold Plan',
:currency => 'usd',
:id => 'gold'
amount: 2000,
interval: 'month',
product: {
name: 'Amazing Gold Plan'
},
currency: 'usd',
id: 'gold'
)

stripe_customer = Stripe::Customer.create
Expand Down

0 comments on commit cfc8846

Please sign in to comment.