stripe-ruby-mock 2.3.0 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e17b14c466a95e28e23f56859b0f20ce0fc0e6f2
4
- data.tar.gz: 14b2121916a871814374262884df72a0501c32d9
3
+ metadata.gz: c7c2b8715ff3b25f019be501ef40f6d246448d39
4
+ data.tar.gz: bbb68394eeb61334c49ce3f86d37ea44b513e516
5
5
  SHA512:
6
- metadata.gz: 1cf3ee166d1a5e9cac2b1c61bf50822568986b84fdaff85166739137d936bfe34f0cdf08ddc25ee6781412cb8a84dd55b4b4beae2a440fecf4966f6c042a7316
7
- data.tar.gz: 8237f3fbfbc338f16e64ac7671acf8715254fd35fd6ab992e55c34c68dd79bc15c4526831694e954df9327428c55cca9d32394d1d3b7fa89e2e7d4846e69f5e1
6
+ metadata.gz: 14dc5c186dc169b310d38e5639ee801863d3f856d36c7f9522c62608cd8d8667c8a69b7ddc6c80023537cb6b65866904fa2f5660c5f59e6c8290f5f3d918d4d3
7
+ data.tar.gz: 1c8f4d5fcb31c4e8f1716e379b2aed29538a59d43ec2bc96e52751bae4c21a14e5265a6003fca7b12768c61061ad200d122a930461b04b1863d6c8c61cab2d30
data/.travis.yml CHANGED
@@ -4,6 +4,7 @@ rvm:
4
4
  - 2.0.0
5
5
  - 2.1.6
6
6
  before_install:
7
+ - rvm 1.9.3 do gem install mime-types -v 2.6.2
7
8
  - gem install bundler
8
9
  before_script:
9
10
  - "sudo touch /var/log/stripe-mock-server.log"
data/Gemfile CHANGED
@@ -1,5 +1,10 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ platforms :ruby_19 do
4
+ gem 'mime-types', '~> 2.6'
5
+ gem 'rest-client', '~> 1.8'
6
+ end
7
+
3
8
  group :test do
4
9
  gem 'rake'
5
10
  end
data/README.md CHANGED
@@ -12,7 +12,7 @@ This gem has unexpectedly grown in popularity and I've gotten pretty busy, so I'
12
12
 
13
13
  In your gemfile:
14
14
 
15
- gem 'stripe-ruby-mock', '~> 2.3.0', :require => 'stripe_mock'
15
+ gem 'stripe-ruby-mock', '~> 2.3.1', :require => 'stripe_mock'
16
16
 
17
17
  ## Features
18
18
 
@@ -444,14 +444,17 @@ module StripeMock
444
444
 
445
445
  def self.mock_plan(params={})
446
446
  {
447
- interval: "month",
448
- name: "The Basic Plan",
449
- amount: 2300,
450
- currency: "usd",
451
447
  id: "2",
452
448
  object: "plan",
453
- livemode: false,
449
+ amount: 2300,
450
+ created: 1466698898,
451
+ currency: "usd",
452
+ interval: "month",
454
453
  interval_count: 1,
454
+ livemode: false,
455
+ metadata: {},
456
+ name: "The Basic Plan",
457
+ statement_descriptor: nil,
455
458
  trial_period_days: nil
456
459
  }.merge(params)
457
460
  end
@@ -13,6 +13,11 @@ module StripeMock
13
13
  end
14
14
 
15
15
  def new_charge(route, method_url, params, headers)
16
+ if params[:idempotency_key] && charges.any?
17
+ original_charge = charges.values.find { |c| c[:idempotency_key] == params[:idempotency_key]}
18
+ return charges[original_charge[:id]] if original_charge
19
+ end
20
+
16
21
  id = new_id('ch')
17
22
 
18
23
  if params[:source]
@@ -12,12 +12,14 @@ module StripeMock
12
12
  klass.add_handler 'post /v1/customers/(.*)/subscriptions', :create_customer_subscription
13
13
  klass.add_handler 'get /v1/customers/(.*)/subscriptions/(.*)', :retrieve_customer_subscription
14
14
  klass.add_handler 'get /v1/customers/(.*)/subscriptions', :retrieve_customer_subscriptions
15
+ klass.add_handler 'post /v1/customers/(.*)subscriptions/(.*)', :update_subscription
16
+ klass.add_handler 'delete /v1/customers/(.*)/subscriptions/(.*)', :cancel_subscription
15
17
  end
16
18
 
17
19
  def retrieve_customer_subscription(route, method_url, params, headers)
18
20
  route =~ method_url
19
21
 
20
- customer = :customer, $1, customers[$1]
22
+ customer = assert_existence :customer, $1, customers[$1]
21
23
  subscription = get_customer_subscription(customer, $2)
22
24
 
23
25
  assert_existence :subscription, $2, subscription
@@ -53,14 +55,16 @@ module StripeMock
53
55
  if params[:coupon]
54
56
  coupon_id = params[:coupon]
55
57
 
56
- raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400) unless coupons[coupon_id]
57
-
58
- # FIXME assert_existence returns 404 error code but Stripe returns 400
58
+ # assert_existence returns 404 error code but Stripe returns 400
59
59
  # coupon = assert_existence :coupon, coupon_id, coupons[coupon_id]
60
60
 
61
- coupon = Data.mock_coupon({ id: coupon_id })
61
+ coupon = coupons[coupon_id]
62
62
 
63
- subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
63
+ if coupon
64
+ subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
65
+ else
66
+ raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400)
67
+ end
64
68
  end
65
69
 
66
70
  subscriptions[subscription[:id]] = subscription
@@ -93,14 +97,16 @@ module StripeMock
93
97
  if params[:coupon]
94
98
  coupon_id = params[:coupon]
95
99
 
96
- raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400) unless coupons[coupon_id]
97
-
98
- # FIXME assert_existence returns 404 error code but Stripe returns 400
100
+ # assert_existence returns 404 error code but Stripe returns 400
99
101
  # coupon = assert_existence :coupon, coupon_id, coupons[coupon_id]
100
102
 
101
- coupon = Data.mock_coupon({ id: coupon_id })
103
+ coupon = coupons[coupon_id]
102
104
 
103
- subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
105
+ if coupon
106
+ subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
107
+ else
108
+ raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400)
109
+ end
104
110
  end
105
111
 
106
112
  subscriptions[subscription[:id]] = subscription
@@ -125,7 +131,10 @@ module StripeMock
125
131
 
126
132
  def update_subscription(route, method_url, params, headers)
127
133
  route =~ method_url
128
- subscription = assert_existence :subscription, $1, subscriptions[$1]
134
+
135
+ subscription_id = $2 ? $2 : $1
136
+ subscription = assert_existence :subscription, subscription_id, subscriptions[subscription_id]
137
+ verify_active_status(subscription)
129
138
 
130
139
  customer_id = subscription[:customer]
131
140
  customer = assert_existence :customer, customer_id, customers[customer_id]
@@ -137,19 +146,24 @@ module StripeMock
137
146
  end
138
147
 
139
148
  # expand the plan for addition to the customer object
140
- plan_name = params[:plan] if params[:plan] && params[:plan] != {}
141
- plan_name ||= subscription[:plan][:id]
149
+ plan_name =
150
+ params[:plan].is_a?(String) ? params[:plan] : subscription[:plan][:id]
151
+
142
152
  plan = plans[plan_name]
143
153
 
144
154
  if params[:coupon]
145
155
  coupon_id = params[:coupon]
146
- raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400) unless coupons[coupon_id]
147
156
 
148
- # FIXME assert_existence returns 404 error code but Stripe returns 400
157
+ # assert_existence returns 404 error code but Stripe returns 400
149
158
  # coupon = assert_existence :coupon, coupon_id, coupons[coupon_id]
150
159
 
151
- coupon = Data.mock_coupon({ id: coupon_id })
152
- subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
160
+ coupon = coupons[coupon_id]
161
+
162
+ if coupon
163
+ subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
164
+ else
165
+ raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400)
166
+ end
153
167
  end
154
168
 
155
169
  assert_existence :plan, plan_name, plan
@@ -161,6 +175,7 @@ module StripeMock
161
175
  subscription[:canceled_at] = nil
162
176
  end
163
177
 
178
+ params[:current_period_start] = subscription[:current_period_start]
164
179
  subscription.merge!(custom_subscription_params(plan, customer, params))
165
180
 
166
181
  # delete the old subscription, replace with the new subscription
@@ -173,7 +188,8 @@ module StripeMock
173
188
  def cancel_subscription(route, method_url, params, headers)
174
189
  route =~ method_url
175
190
 
176
- subscription = assert_existence :subscription, $1, subscriptions[$1]
191
+ subscription_id = $2 ? $2 : $1
192
+ subscription = assert_existence :subscription, subscription_id, subscriptions[subscription_id]
177
193
 
178
194
  customer_id = subscription[:customer]
179
195
  customer = assert_existence :customer, customer_id, customers[customer_id]
@@ -183,7 +199,7 @@ module StripeMock
183
199
  if cancelled_at_period_end
184
200
  cancel_params[:cancel_at_period_end] = true
185
201
  else
186
- cancel_params[:status] = "canceled"
202
+ cancel_params[:status] = 'canceled'
187
203
  cancel_params[:cancel_at_period_end] = false
188
204
  cancel_params[:ended_at] = Time.now.utc.to_i
189
205
  end
@@ -206,6 +222,14 @@ module StripeMock
206
222
  end
207
223
  end
208
224
 
225
+ def verify_active_status(subscription)
226
+ id, status = subscription.values_at(:id, :status)
227
+
228
+ if status == 'canceled'
229
+ message = "No such subscription: #{id}"
230
+ raise Stripe::InvalidRequestError.new(message, 'subscription', 404)
231
+ end
232
+ end
209
233
  end
210
234
  end
211
235
  end
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "2.3.0"
3
+ VERSION = "2.3.1"
4
4
  end
@@ -375,4 +375,31 @@ shared_examples 'Charge API' do
375
375
  end
376
376
  end
377
377
 
378
+ describe "idempotency" do
379
+ let(:idempotent_charge_params) {{
380
+ amount: 777,
381
+ currency: 'USD',
382
+ card: stripe_helper.generate_card_token,
383
+ capture: true,
384
+ idempotency_key: 'onceisenough'
385
+ }}
386
+
387
+ it "returns the original charge if the same idempotency_key is passed in" do
388
+ charge1 = Stripe::Charge.create(idempotent_charge_params)
389
+ charge2 = Stripe::Charge.create(idempotent_charge_params)
390
+
391
+ expect(charge1).to eq(charge2)
392
+ end
393
+
394
+ it "returns different charges if different idempotency_keys are used for each charge" do
395
+ idempotent_charge_params2 = idempotent_charge_params.clone
396
+ idempotent_charge_params2[:idempotency_key] = 'thisoneisdifferent'
397
+
398
+ charge1 = Stripe::Charge.create(idempotent_charge_params)
399
+ charge2 = Stripe::Charge.create(idempotent_charge_params2)
400
+
401
+ expect(charge1).not_to eq(charge2)
402
+ end
403
+ end
404
+
378
405
  end
@@ -9,6 +9,10 @@ shared_examples 'Plan API' do
9
9
  :amount => 9900,
10
10
  :currency => 'USD',
11
11
  :interval => 1,
12
+ :metadata => {
13
+ :description => "desc text",
14
+ :info => "info text"
15
+ },
12
16
  :trial_period_days => 30
13
17
  )
14
18
 
@@ -18,6 +22,10 @@ shared_examples 'Plan API' do
18
22
 
19
23
  expect(plan.currency).to eq('USD')
20
24
  expect(plan.interval).to eq(1)
25
+
26
+ expect(plan.metadata.description).to eq('desc text')
27
+ expect(plan.metadata.info).to eq('info text')
28
+
21
29
  expect(plan.trial_period_days).to eq(30)
22
30
  end
23
31
 
@@ -288,6 +288,19 @@ shared_examples 'Customer Subscriptions' do
288
288
  end
289
289
 
290
290
  context "updating a subscription" do
291
+ it 'raises invalid request exception when subscription is cancelled' do
292
+ stripe_helper.create_plan(id: 'the truth')
293
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: 'the truth')
294
+
295
+ subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
296
+ subscription.delete
297
+
298
+ expect { subscription.save }.to raise_error { |e|
299
+ expect(e).to be_a(Stripe::InvalidRequestError)
300
+ expect(e.http_status).to eq(404)
301
+ expect(e.message).to eq("No such subscription: #{subscription.id}")
302
+ }
303
+ end
291
304
 
292
305
  it "updates a stripe customer's existing subscription" do
293
306
  silver = stripe_helper.create_plan(id: 'silver')
@@ -17,11 +17,11 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
19
 
20
- gem.add_dependency 'stripe', ['>= 1.31.0', '<= 1.43']
21
- gem.add_dependency 'multi_json', '>= 1.0.0'
20
+ gem.add_dependency 'stripe', '~> 1.31'
21
+ gem.add_dependency 'multi_json', '~> 1.0'
22
22
  gem.add_dependency 'dante', '>= 0.2.0'
23
23
 
24
24
  gem.add_development_dependency 'rspec', '~> 3.1.0'
25
25
  gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
26
- gem.add_development_dependency 'thin'
26
+ gem.add_development_dependency 'thin', '~> 1.6.4'
27
27
  end
metadata CHANGED
@@ -1,49 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe-ruby-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-24 00:00:00.000000000 Z
11
+ date: 2016-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: stripe
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: 1.31.0
20
- - - <=
17
+ - - ~>
21
18
  - !ruby/object:Gem::Version
22
- version: '1.43'
19
+ version: '1.31'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - '>='
28
- - !ruby/object:Gem::Version
29
- version: 1.31.0
30
- - - <=
24
+ - - ~>
31
25
  - !ruby/object:Gem::Version
32
- version: '1.43'
26
+ version: '1.31'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: multi_json
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
- - - '>='
31
+ - - ~>
38
32
  - !ruby/object:Gem::Version
39
- version: 1.0.0
33
+ version: '1.0'
40
34
  type: :runtime
41
35
  prerelease: false
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
- - - '>='
38
+ - - ~>
45
39
  - !ruby/object:Gem::Version
46
- version: 1.0.0
40
+ version: '1.0'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: dante
49
43
  requirement: !ruby/object:Gem::Requirement
@@ -90,16 +84,16 @@ dependencies:
90
84
  name: thin
91
85
  requirement: !ruby/object:Gem::Requirement
92
86
  requirements:
93
- - - '>='
87
+ - - ~>
94
88
  - !ruby/object:Gem::Version
95
- version: '0'
89
+ version: 1.6.4
96
90
  type: :development
97
91
  prerelease: false
98
92
  version_requirements: !ruby/object:Gem::Requirement
99
93
  requirements:
100
- - - '>='
94
+ - - ~>
101
95
  - !ruby/object:Gem::Version
102
- version: '0'
96
+ version: 1.6.4
103
97
  description: A drop-in library to test stripe without hitting their servers
104
98
  email: gilbertbgarza@gmail.com
105
99
  executables: