stripe-ruby-mock 1.10.1.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/README.md +70 -3
  4. data/Rakefile +1 -1
  5. data/lib/stripe_mock/api/client.rb +1 -0
  6. data/lib/stripe_mock/api/instance.rb +1 -0
  7. data/lib/stripe_mock/api/live.rb +15 -0
  8. data/lib/stripe_mock/api/server.rb +24 -21
  9. data/lib/stripe_mock/api/test_helpers.rb +24 -0
  10. data/lib/stripe_mock/client.rb +4 -8
  11. data/lib/stripe_mock/data.rb +54 -30
  12. data/lib/stripe_mock/instance.rb +15 -5
  13. data/lib/stripe_mock/request_handlers/cards.rb +29 -18
  14. data/lib/stripe_mock/request_handlers/charges.rb +34 -6
  15. data/lib/stripe_mock/request_handlers/coupons.rb +1 -3
  16. data/lib/stripe_mock/request_handlers/customers.rb +3 -9
  17. data/lib/stripe_mock/request_handlers/events.rb +1 -3
  18. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +16 -9
  19. data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
  20. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +9 -2
  21. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +3 -1
  22. data/lib/stripe_mock/request_handlers/invoice_items.rb +32 -2
  23. data/lib/stripe_mock/request_handlers/invoices.rb +7 -3
  24. data/lib/stripe_mock/request_handlers/plans.rb +2 -5
  25. data/lib/stripe_mock/request_handlers/recipients.rb +26 -4
  26. data/lib/stripe_mock/request_handlers/subscriptions.rb +26 -33
  27. data/lib/stripe_mock/request_handlers/tokens.rb +24 -4
  28. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +18 -0
  29. data/lib/stripe_mock/server.rb +4 -5
  30. data/lib/stripe_mock/test_strategies/base.rb +27 -0
  31. data/lib/stripe_mock/test_strategies/live.rb +22 -0
  32. data/lib/stripe_mock/test_strategies/mock.rb +19 -0
  33. data/lib/stripe_mock/util.rb +5 -0
  34. data/lib/stripe_mock/version.rb +1 -1
  35. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +3 -2
  36. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +16 -9
  37. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +3 -2
  38. data/lib/stripe_mock/webhook_fixtures/customer.card.created.json +1 -0
  39. data/lib/stripe_mock/webhook_fixtures/customer.card.deleted.json +1 -0
  40. data/lib/stripe_mock/webhook_fixtures/customer.card.updated.json +1 -0
  41. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  42. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +2 -1
  43. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  44. data/lib/stripe_mock.rb +9 -1
  45. data/spec/fixtures/create_refund.yml +126 -0
  46. data/spec/instance_spec.rb +4 -2
  47. data/spec/integration_examples/charge_token_examples.rb +49 -0
  48. data/spec/integration_examples/customer_card_examples.rb +42 -0
  49. data/spec/integration_examples/prepare_error_examples.rb +18 -0
  50. data/spec/readme_spec.rb +2 -1
  51. data/spec/server_spec.rb +12 -3
  52. data/spec/shared_stripe_examples/card_examples.rb +108 -3
  53. data/spec/shared_stripe_examples/card_token_examples.rb +26 -0
  54. data/spec/shared_stripe_examples/charge_examples.rb +55 -39
  55. data/spec/shared_stripe_examples/coupon_examples.rb +2 -17
  56. data/spec/shared_stripe_examples/customer_examples.rb +30 -39
  57. data/spec/shared_stripe_examples/error_mock_examples.rb +1 -1
  58. data/spec/shared_stripe_examples/invoice_examples.rb +31 -15
  59. data/spec/shared_stripe_examples/invoice_item_examples.rb +62 -10
  60. data/spec/shared_stripe_examples/plan_examples.rb +29 -18
  61. data/spec/shared_stripe_examples/recipient_examples.rb +55 -5
  62. data/spec/shared_stripe_examples/refund_examples.rb +90 -0
  63. data/spec/shared_stripe_examples/subscription_examples.rb +159 -82
  64. data/spec/shared_stripe_examples/validation_examples.rb +19 -0
  65. data/spec/spec_helper.rb +32 -1
  66. data/spec/stripe_mock_spec.rb +70 -0
  67. data/spec/support/stripe_examples.rb +7 -14
  68. data/spec/util_spec.rb +8 -0
  69. data/stripe-ruby-mock.gemspec +2 -2
  70. metadata +38 -34
  71. data/lib/stripe_mock/api/strict.rb +0 -11
@@ -2,20 +2,26 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Customer Subscriptions' do
4
4
 
5
+ def gen_card_tk
6
+ stripe_helper.generate_card_token
7
+ end
8
+
5
9
  context "creating a new subscription" do
6
- it "adds a new subscription to customer with none" do
7
- plan = Stripe::Plan.create(id: 'silver', name: 'Silver Plan', amount: 4999)
8
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
10
+ it "adds a new subscription to customer with none", :live => true do
11
+ plan = stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999)
12
+ customer = Stripe::Customer.create(card: gen_card_tk)
9
13
 
10
14
  expect(customer.subscriptions.data).to be_empty
11
15
  expect(customer.subscriptions.count).to eq(0)
12
16
 
13
- sub = customer.subscriptions.create({ :plan => 'silver' })
17
+ sub = customer.subscriptions.create({ :plan => 'silver', :metadata => { :foo => "bar", :example => "yes" } })
14
18
 
15
19
  expect(sub.object).to eq('subscription')
16
- expect(sub.plan).to eq('silver')
20
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
21
+ expect(sub.metadata.foo).to eq( "bar" )
22
+ expect(sub.metadata.example).to eq( "yes" )
17
23
 
18
- customer = Stripe::Customer.retrieve('test_customer_sub')
24
+ customer = Stripe::Customer.retrieve(customer.id)
19
25
  expect(customer.subscriptions.data).to_not be_empty
20
26
  expect(customer.subscriptions.count).to eq(1)
21
27
  expect(customer.subscriptions.data.length).to eq(1)
@@ -23,6 +29,8 @@ shared_examples 'Customer Subscriptions' do
23
29
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
24
30
  expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
25
31
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
32
+ expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
33
+ expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
26
34
 
27
35
  end
28
36
 
@@ -35,7 +43,7 @@ shared_examples 'Customer Subscriptions' do
35
43
  :id => 'silver',
36
44
  :statement_description => "testPlan"
37
45
  )
38
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
46
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: gen_card_tk)
39
47
 
40
48
  subscription = customer.subscriptions.create({
41
49
  :plan => "silver", quantity: 2, application_fee_percent: 10})
@@ -44,33 +52,33 @@ shared_examples 'Customer Subscriptions' do
44
52
  end
45
53
 
46
54
  it "adds additional subscription to customer with existing subscription" do
47
- silver = Stripe::Plan.create(id: 'silver')
48
- gold = Stripe::Plan.create(id: 'gold')
49
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: 'gold')
55
+ silver = stripe_helper.create_plan(id: 'silver')
56
+ gold = stripe_helper.create_plan(id: 'gold')
57
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: gen_card_tk, plan: 'gold')
50
58
 
51
59
  sub = customer.subscriptions.create({ :plan => 'silver' })
52
60
 
53
61
  expect(sub.object).to eq('subscription')
54
- expect(sub.plan).to eq('silver')
62
+ expect(sub.plan.to_hash).to eq(silver.to_hash)
55
63
 
56
64
  customer = Stripe::Customer.retrieve('test_customer_sub')
57
65
  expect(customer.subscriptions.data).to_not be_empty
58
66
  expect(customer.subscriptions.count).to eq(2)
59
67
  expect(customer.subscriptions.data.length).to eq(2)
60
68
 
61
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold.to_hash)
62
- expect(customer.subscriptions.data.first.customer).to eq(customer.id)
63
-
64
- expect(customer.subscriptions.data.last.id).to eq(sub.id)
65
- expect(customer.subscriptions.data.last.plan.to_hash).to eq(silver.to_hash)
69
+ expect(customer.subscriptions.data.last.plan.to_hash).to eq(gold.to_hash)
66
70
  expect(customer.subscriptions.data.last.customer).to eq(customer.id)
71
+
72
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
73
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(silver.to_hash)
74
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
67
75
  end
68
76
 
69
77
  it "subscribes a cardless customer when specifing a card token" do
70
- plan = Stripe::Plan.create(id: 'enterprise', amount: 499)
78
+ plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
71
79
  customer = Stripe::Customer.create(id: 'cardless')
72
80
 
73
- sub = customer.subscriptions.create({ :plan => 'enterprise', :card => 'card_token' })
81
+ sub = customer.subscriptions.create(plan: 'enterprise', card: gen_card_tk)
74
82
  customer = Stripe::Customer.retrieve('cardless')
75
83
 
76
84
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
@@ -96,7 +104,7 @@ shared_examples 'Customer Subscriptions' do
96
104
  end
97
105
 
98
106
  it "throws an error when subscribing a customer with no card" do
99
- plan = Stripe::Plan.create(id: 'enterprise', amount: 499)
107
+ plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
100
108
  customer = Stripe::Customer.create(id: 'cardless')
101
109
 
102
110
  expect { customer.subscriptions.create({ :plan => 'enterprise' }) }.to raise_error {|e|
@@ -110,13 +118,13 @@ shared_examples 'Customer Subscriptions' do
110
118
  end
111
119
 
112
120
  it "subscribes a customer with no card to a plan with a free trial" do
113
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
121
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
114
122
  customer = Stripe::Customer.create(id: 'cardless')
115
123
 
116
124
  sub = customer.subscriptions.create({ :plan => 'trial' })
117
125
 
118
126
  expect(sub.object).to eq('subscription')
119
- expect(sub.plan).to eq('trial')
127
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
120
128
  expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
121
129
 
122
130
  customer = Stripe::Customer.retrieve('cardless')
@@ -130,13 +138,13 @@ shared_examples 'Customer Subscriptions' do
130
138
  end
131
139
 
132
140
  it "subscribes a customer with no card to a free plan" do
133
- plan = Stripe::Plan.create(id: 'free_tier', amount: 0)
141
+ plan = stripe_helper.create_plan(id: 'free_tier', amount: 0)
134
142
  customer = Stripe::Customer.create(id: 'cardless')
135
143
 
136
144
  sub = customer.subscriptions.create({ :plan => 'free_tier' })
137
145
 
138
146
  expect(sub.object).to eq('subscription')
139
- expect(sub.plan).to eq('free_tier')
147
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
140
148
 
141
149
  customer = Stripe::Customer.retrieve('cardless')
142
150
  expect(customer.subscriptions.data).to_not be_empty
@@ -149,33 +157,33 @@ shared_examples 'Customer Subscriptions' do
149
157
  end
150
158
 
151
159
  it "overrides trial length when trial end is set" do
152
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
160
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
153
161
  customer = Stripe::Customer.create(id: 'short_trial')
154
162
  trial_end = Time.now.utc.to_i + 3600
155
163
 
156
164
  sub = customer.subscriptions.create({ plan: 'trial', trial_end: trial_end })
157
165
 
158
166
  expect(sub.object).to eq('subscription')
159
- expect(sub.plan).to eq('trial')
167
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
160
168
  expect(sub.current_period_end).to eq(trial_end)
161
169
  expect(sub.trial_end).to eq(trial_end)
162
170
  end
163
171
 
164
172
  it "returns without a trial when trial_end is set to 'now'" do
165
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
166
- customer = Stripe::Customer.create(id: 'no_trial', card: 'tk')
173
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
174
+ customer = Stripe::Customer.create(id: 'no_trial', card: gen_card_tk)
167
175
 
168
176
  sub = customer.subscriptions.create({ plan: 'trial', trial_end: "now" })
169
177
 
170
178
  expect(sub.object).to eq('subscription')
171
- expect(sub.plan).to eq('trial')
179
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
172
180
  expect(sub.status).to eq('active')
173
181
  expect(sub.trial_start).to be_nil
174
182
  expect(sub.trial_end).to be_nil
175
183
  end
176
184
 
177
185
  it "raises error when trial_end is not an integer or 'now'" do
178
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
186
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
179
187
  customer = Stripe::Customer.create(id: 'cus_trial')
180
188
 
181
189
  expect { customer.subscriptions.create({ plan: 'trial', trial_end: "gazebo" }) }.to raise_error {|e|
@@ -186,7 +194,7 @@ shared_examples 'Customer Subscriptions' do
186
194
  end
187
195
 
188
196
  it "raises error when trial_end is set to a time in the past" do
189
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
197
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
190
198
  customer = Stripe::Customer.create(id: 'past_trial')
191
199
  trial_end = Time.now.utc.to_i - 3600
192
200
 
@@ -198,7 +206,7 @@ shared_examples 'Customer Subscriptions' do
198
206
  end
199
207
 
200
208
  it "raises error when trial_end is set to a time more than five years in the future" do
201
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
209
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
202
210
  customer = Stripe::Customer.create(id: 'long_trial')
203
211
  trial_end = Time.now.utc.to_i + 31557600*5 + 3600 # 5 years + 1 hour
204
212
 
@@ -213,16 +221,23 @@ shared_examples 'Customer Subscriptions' do
213
221
  context "updating a subscription" do
214
222
 
215
223
  it "updates a stripe customer's existing subscription" do
216
- silver = Stripe::Plan.create(id: 'silver')
217
- gold = Stripe::Plan.create(id: 'gold')
218
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: 'silver')
224
+ silver = stripe_helper.create_plan(id: 'silver')
225
+ gold = stripe_helper.create_plan(id: 'gold')
226
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: gen_card_tk, plan: 'silver')
219
227
 
220
228
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
221
229
  sub.plan = 'gold'
222
- sub.save
230
+ sub.quantity = 5
231
+ sub.metadata.foo = "bar"
232
+ sub.metadata.example = "yes"
233
+
234
+ expect(sub.save).to be_truthy
223
235
 
224
236
  expect(sub.object).to eq('subscription')
225
- expect(sub.plan).to eq('gold')
237
+ expect(sub.plan.to_hash).to eq(gold.to_hash)
238
+ expect(sub.quantity).to eq(5)
239
+ expect(sub.metadata.foo).to eq( "bar" )
240
+ expect(sub.metadata.example).to eq( "yes" )
226
241
 
227
242
  customer = Stripe::Customer.retrieve('test_customer_sub')
228
243
  expect(customer.subscriptions.data).to_not be_empty
@@ -235,7 +250,7 @@ shared_examples 'Customer Subscriptions' do
235
250
  end
236
251
 
237
252
  it "throws an error when plan does not exist" do
238
- free = Stripe::Plan.create(id: 'free', amount: 0)
253
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
239
254
  customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
240
255
 
241
256
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
@@ -254,7 +269,7 @@ shared_examples 'Customer Subscriptions' do
254
269
  end
255
270
 
256
271
  it "throws an error when subscription does not exist" do
257
- free = Stripe::Plan.create(id: 'free', amount: 0)
272
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
258
273
  customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
259
274
 
260
275
  expect { customer.subscriptions.retrieve("gazebo") }.to raise_error {|e|
@@ -270,8 +285,8 @@ shared_examples 'Customer Subscriptions' do
270
285
  end
271
286
 
272
287
  it "throws an error when updating a customer with no card" do
273
- free = Stripe::Plan.create(id: 'free', amount: 0)
274
- paid = Stripe::Plan.create(id: 'enterprise', amount: 499)
288
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
289
+ paid = stripe_helper.create_plan(id: 'enterprise', amount: 499)
275
290
  customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
276
291
 
277
292
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
@@ -290,8 +305,8 @@ shared_examples 'Customer Subscriptions' do
290
305
  end
291
306
 
292
307
  it "updates a customer with no card to a plan with a free trial" do
293
- free = Stripe::Plan.create(id: 'free', amount: 0)
294
- trial = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
308
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
309
+ trial = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
295
310
  customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
296
311
 
297
312
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
@@ -299,7 +314,7 @@ shared_examples 'Customer Subscriptions' do
299
314
  sub.save
300
315
 
301
316
  expect(sub.object).to eq('subscription')
302
- expect(sub.plan).to eq('trial')
317
+ expect(sub.plan.to_hash).to eq(trial.to_hash)
303
318
 
304
319
  customer = Stripe::Customer.retrieve('cardless')
305
320
  expect(customer.subscriptions.data).to_not be_empty
@@ -312,8 +327,8 @@ shared_examples 'Customer Subscriptions' do
312
327
  end
313
328
 
314
329
  it "updates a customer with no card to a free plan" do
315
- free = Stripe::Plan.create(id: 'free', amount: 0)
316
- gratis = Stripe::Plan.create(id: 'gratis', amount: 0)
330
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
331
+ gratis = stripe_helper.create_plan(id: 'gratis', amount: 0)
317
332
  customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
318
333
 
319
334
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
@@ -321,7 +336,7 @@ shared_examples 'Customer Subscriptions' do
321
336
  sub.save
322
337
 
323
338
  expect(sub.object).to eq('subscription')
324
- expect(sub.plan).to eq('gratis')
339
+ expect(sub.plan.to_hash).to eq(gratis.to_hash)
325
340
 
326
341
  customer = Stripe::Customer.retrieve('cardless')
327
342
  expect(customer.subscriptions.data).to_not be_empty
@@ -334,13 +349,13 @@ shared_examples 'Customer Subscriptions' do
334
349
  end
335
350
 
336
351
  it "sets a card when updating a customer's subscription" do
337
- free = Stripe::Plan.create(id: 'free', amount: 0)
338
- paid = Stripe::Plan.create(id: 'paid', amount: 499)
352
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
353
+ paid = stripe_helper.create_plan(id: 'paid', amount: 499)
339
354
  customer = Stripe::Customer.create(id: 'test_customer_sub', plan: 'free')
340
355
 
341
356
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
342
357
  sub.plan = 'paid'
343
- sub.card = 'tk'
358
+ sub.card = gen_card_tk
344
359
  sub.save
345
360
 
346
361
  customer = Stripe::Customer.retrieve('test_customer_sub')
@@ -352,7 +367,7 @@ shared_examples 'Customer Subscriptions' do
352
367
  end
353
368
 
354
369
  it "overrides trial length when trial end is set" do
355
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
370
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
356
371
  customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
357
372
 
358
373
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
@@ -367,7 +382,7 @@ shared_examples 'Customer Subscriptions' do
367
382
  end
368
383
 
369
384
  it "returns without a trial when trial_end is set to 'now'" do
370
- plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
385
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
371
386
  customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
372
387
 
373
388
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
@@ -376,15 +391,15 @@ shared_examples 'Customer Subscriptions' do
376
391
  sub.save
377
392
 
378
393
  expect(sub.object).to eq('subscription')
379
- expect(sub.plan).to eq('trial')
394
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
380
395
  expect(sub.status).to eq('active')
381
396
  expect(sub.trial_start).to be_nil
382
397
  expect(sub.trial_end).to be_nil
383
398
  end
384
399
 
385
400
  it "changes an active subscription to a trial when trial_end is set" do
386
- plan = Stripe::Plan.create(id: 'no_trial', amount: 999)
387
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', card: 'tk')
401
+ plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
402
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', card: gen_card_tk)
388
403
 
389
404
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
390
405
 
@@ -393,7 +408,7 @@ shared_examples 'Customer Subscriptions' do
393
408
  sub.save
394
409
 
395
410
  expect(sub.object).to eq('subscription')
396
- expect(sub.plan).to eq('no_trial')
411
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
397
412
  expect(sub.status).to eq('trialing')
398
413
  expect(sub.trial_end).to eq(trial_end)
399
414
  expect(sub.current_period_end).to eq(trial_end)
@@ -401,8 +416,8 @@ shared_examples 'Customer Subscriptions' do
401
416
 
402
417
 
403
418
  it "raises error when trial_end is not an integer or 'now'" do
404
- plan = Stripe::Plan.create(id: 'no_trial', amount: 999)
405
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', card: 'tk')
419
+ plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
420
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', card: gen_card_tk)
406
421
 
407
422
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
408
423
  sub.trial_end = "gazebo"
@@ -417,15 +432,33 @@ shared_examples 'Customer Subscriptions' do
417
432
 
418
433
  context "cancelling a subscription" do
419
434
 
420
- it "cancels a stripe customer's subscription" do
421
- truth = Stripe::Plan.create(id: 'the truth')
422
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "the truth")
435
+ it "cancels a stripe customer's subscription", :live => true do
436
+ truth = stripe_helper.create_plan(id: 'the truth')
437
+ customer = Stripe::Customer.create(card: gen_card_tk, plan: "the truth")
423
438
 
424
439
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
425
- result = sub.delete()
440
+ result = sub.delete
426
441
 
427
442
  expect(result.status).to eq('canceled')
428
- expect(result.cancel_at_period_end).to be_false
443
+ expect(result.cancel_at_period_end).to eq false
444
+ expect(result.canceled_at).to_not be_nil
445
+ expect(result.id).to eq(sub.id)
446
+
447
+ customer = Stripe::Customer.retrieve(customer.id)
448
+ expect(customer.subscriptions.data).to be_empty
449
+ expect(customer.subscriptions.count).to eq(0)
450
+ expect(customer.subscriptions.data.length).to eq(0)
451
+ end
452
+
453
+ it "cancels a stripe customer's subscription at period end" do
454
+ truth = stripe_helper.create_plan(id: 'the_truth')
455
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: gen_card_tk, plan: "the_truth")
456
+
457
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
458
+ result = sub.delete(at_period_end: true)
459
+
460
+ expect(result.status).to eq('active')
461
+ expect(result.cancel_at_period_end).to eq(true)
429
462
  expect(result.id).to eq(sub.id)
430
463
 
431
464
  customer = Stripe::Customer.retrieve('test_customer_sub')
@@ -433,22 +466,21 @@ shared_examples 'Customer Subscriptions' do
433
466
  expect(customer.subscriptions.count).to eq(1)
434
467
  expect(customer.subscriptions.data.length).to eq(1)
435
468
 
436
- expect(customer.subscriptions.data.first.status).to eq('canceled')
437
- expect(customer.subscriptions.data.first.cancel_at_period_end).to be_false
438
- expect(customer.subscriptions.data.first.ended_at).to_not be_nil
469
+ expect(customer.subscriptions.data.first.status).to eq('active')
470
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
471
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
439
472
  expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
440
473
  end
441
474
 
442
- it "cancels a stripe customer's subscription at period end" do
443
- truth = Stripe::Plan.create(id: 'the_truth')
444
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "the_truth")
475
+ it "resumes an at period end cancelled subscription" do
476
+ truth = stripe_helper.create_plan(id: 'the_truth')
477
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: gen_card_tk, plan: "the_truth")
445
478
 
446
479
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
447
480
  result = sub.delete(at_period_end: true)
448
481
 
449
- expect(result.status).to eq('active')
450
- expect(result.cancel_at_period_end).to be_true
451
- expect(result.id).to eq(sub.id)
482
+ sub.plan = 'the_truth'
483
+ sub.save
452
484
 
453
485
  customer = Stripe::Customer.retrieve('test_customer_sub')
454
486
  expect(customer.subscriptions.data).to_not be_empty
@@ -456,15 +488,15 @@ shared_examples 'Customer Subscriptions' do
456
488
  expect(customer.subscriptions.data.length).to eq(1)
457
489
 
458
490
  expect(customer.subscriptions.data.first.status).to eq('active')
459
- expect(customer.subscriptions.data.first.cancel_at_period_end).to be_true
491
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
460
492
  expect(customer.subscriptions.data.first.ended_at).to be_nil
461
- expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
493
+ expect(customer.subscriptions.data.first.canceled_at).to be_nil
462
494
  end
463
495
  end
464
496
 
465
497
  it "doesn't change status of subscription when cancelling at period end" do
466
- trial = Stripe::Plan.create(id: 'trial', trial_period_days: 14)
467
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "trial")
498
+ trial = stripe_helper.create_plan(id: 'trial', trial_period_days: 14)
499
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: gen_card_tk, plan: "trial")
468
500
 
469
501
  sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
470
502
  result = sub.delete(at_period_end: true)
@@ -476,12 +508,27 @@ shared_examples 'Customer Subscriptions' do
476
508
  expect(customer.subscriptions.data.first.status).to eq('trialing')
477
509
  end
478
510
 
511
+ it "doesn't require a card when trial_end is present", :live => true do
512
+ plan = stripe_helper.create_plan(
513
+ :amount => 2000,
514
+ :interval => 'month',
515
+ :name => 'Amazing Gold Plan',
516
+ :currency => 'usd',
517
+ :id => 'gold'
518
+ )
519
+
520
+ options = {plan: plan.id, trial_end: (Date.today + 30).to_time.to_i}
521
+
522
+ stripe_customer = Stripe::Customer.create
523
+ stripe_customer.subscriptions.create options
524
+ end
525
+
479
526
  context "retrieve multiple subscriptions" do
480
527
 
481
528
  it "retrieves a list of multiple subscriptions" do
482
- free = Stripe::Plan.create(id: 'free', amount: 0)
483
- paid = Stripe::Plan.create(id: 'paid', amount: 499)
484
- customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk', plan: "free")
529
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
530
+ paid = stripe_helper.create_plan(id: 'paid', amount: 499)
531
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: gen_card_tk, plan: "free")
485
532
  customer.subscriptions.create({ :plan => 'paid' })
486
533
 
487
534
  customer = Stripe::Customer.retrieve('test_customer_sub')
@@ -492,11 +539,11 @@ shared_examples 'Customer Subscriptions' do
492
539
  expect(list.count).to eq(2)
493
540
  expect(list.data.length).to eq(2)
494
541
 
495
- expect(list.data.first.object).to eq("subscription")
496
- expect(list.data.first.plan.to_hash).to eq(free.to_hash)
497
-
498
542
  expect(list.data.last.object).to eq("subscription")
499
- expect(list.data.last.plan.to_hash).to eq(paid.to_hash)
543
+ expect(list.data.last.plan.to_hash).to eq(free.to_hash)
544
+
545
+ expect(list.data.first.object).to eq("subscription")
546
+ expect(list.data.first.plan.to_hash).to eq(paid.to_hash)
500
547
  end
501
548
 
502
549
  it "retrieves an empty list if there's no subscriptions" do
@@ -511,4 +558,34 @@ shared_examples 'Customer Subscriptions' do
511
558
  end
512
559
  end
513
560
 
561
+ describe "metadata" do
562
+
563
+ it "creates a stripe customer and subscribes them to a plan with meta data", :live => true do
564
+
565
+ stripe_helper.create_plan(
566
+ :amount => 500,
567
+ :interval => 'month',
568
+ :name => 'Sample Plan',
569
+ :currency => 'usd',
570
+ :id => 'Sample5',
571
+ :statement_description => "Plan Statement"
572
+ )
573
+
574
+ customer = Stripe::Customer.create({
575
+ email: 'johnny@appleseed.com',
576
+ card: gen_card_tk
577
+ })
578
+
579
+ subscription = customer.subscriptions.create(:plan => "Sample5")
580
+ subscription.metadata['foo'] = 'bar'
581
+
582
+ expect(subscription.save).to be_a Stripe::Subscription
583
+
584
+ customer = Stripe::Customer.retrieve(customer.id)
585
+ expect(customer.email).to eq('johnny@appleseed.com')
586
+ expect(customer.subscriptions.first.plan.id).to eq('Sample5')
587
+ expect(customer.subscriptions.first.metadata['foo']).to eq('bar')
588
+ end
589
+ end
590
+
514
591
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Server Validation', :live => true do
4
+
5
+ def credit_card_valid?(account_number)
6
+ digits = account_number.scan(/./).map(&:to_i)
7
+ check = digits.pop
8
+
9
+ sum = digits.reverse.each_slice(2).map do |x, y|
10
+ [(x * 2).divmod(10), y || 0]
11
+ end.flatten.inject(:+)
12
+
13
+ (10 - sum % 10) == check
14
+ end
15
+
16
+ it "runs a luhn check for charges" do
17
+ Stripe::Charge.new :with => "4242424242424241"
18
+ end
19
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'set'
2
2
 
3
- gem 'rspec', '~> 2.4'
3
+ gem 'rspec', '~> 3.1'
4
4
  require 'rspec'
5
5
  require 'stripe'
6
6
  require 'stripe_mock'
@@ -9,3 +9,34 @@ require 'stripe_mock/server'
9
9
  # Requires supporting ruby files with custom matchers and macros, etc,
10
10
  # in spec/support/ and its subdirectories.
11
11
  Dir["./spec/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |c|
14
+ tags = c.filter_manager.inclusions.rules
15
+
16
+ if tags.include?(:live) || tags.include?(:oauth)
17
+ puts "Running **live** tests against Stripe..."
18
+ StripeMock.set_default_test_helper_strategy(:live)
19
+
20
+ if tags.include?(:oauth)
21
+ oauth_token = ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN']
22
+ if oauth_token.nil? || oauth_token == ''
23
+ raise "Please set your STRIPE_TEST_OAUTH_ACCESS_TOKEN environment variable."
24
+ end
25
+ c.filter_run_excluding :mock_server => true, :live => true
26
+ else
27
+ c.filter_run_excluding :mock_server => true, :oauth => true
28
+ end
29
+
30
+ api_key = ENV['STRIPE_TEST_SECRET_KEY']
31
+ if api_key.nil? || api_key == ''
32
+ raise "Please set your STRIPE_TEST_SECRET_KEY environment variable."
33
+ end
34
+
35
+ c.before(:each) do
36
+ StripeMock.stub(:start).and_return(nil)
37
+ StripeMock.stub(:stop).and_return(nil)
38
+ Stripe.api_key = api_key
39
+ end
40
+ c.after(:each) { sleep 1 }
41
+ end
42
+ end
@@ -37,4 +37,74 @@ describe StripeMock do
37
37
  }
38
38
  end
39
39
 
40
+ describe "Live Testing" do
41
+ after { StripeMock.instance_variable_set(:@state, 'ready') }
42
+
43
+ it "sets the default test strategy" do
44
+ StripeMock.toggle_live(true)
45
+ expect(StripeMock.create_test_helper).to be_a StripeMock::TestStrategies::Live
46
+
47
+ StripeMock.toggle_live(false)
48
+ expect(StripeMock.create_test_helper).to be_a StripeMock::TestStrategies::Mock
49
+ end
50
+
51
+ it "does not start when live" do
52
+ expect(StripeMock.state).to eq 'ready'
53
+ StripeMock.toggle_live(true)
54
+ expect(StripeMock.state).to eq 'live'
55
+ expect(StripeMock.start).to eq false
56
+ expect(StripeMock.start_client).to eq false
57
+ end
58
+
59
+ it "can be undone" do
60
+ StripeMock.toggle_live(true)
61
+ StripeMock.toggle_live(false)
62
+ expect(StripeMock.state).to eq 'ready'
63
+ expect(StripeMock.start).to_not eq false
64
+ StripeMock.stop
65
+ end
66
+
67
+ it "cannot be toggled when already started" do
68
+ StripeMock.start
69
+ expect { StripeMock.toggle_live(true) }.to raise_error
70
+ StripeMock.stop
71
+
72
+ StripeMock.instance_variable_set(:@state, 'remote')
73
+ expect { StripeMock.toggle_live(true) }.to raise_error
74
+ end
75
+ end
76
+
77
+ describe "Test Helper Strategies" do
78
+ before { StripeMock.instance_variable_set("@__test_strat", nil) }
79
+
80
+ it "uses mock by default" do
81
+ helper = StripeMock.create_test_helper
82
+ expect(helper).to be_a StripeMock::TestStrategies::Mock
83
+ end
84
+
85
+ it "can specify which strategy to use" do
86
+ helper = StripeMock.create_test_helper(:live)
87
+ expect(helper).to be_a StripeMock::TestStrategies::Live
88
+
89
+ helper = StripeMock.create_test_helper(:mock)
90
+ expect(helper).to be_a StripeMock::TestStrategies::Mock
91
+ end
92
+
93
+ it "throws an error on an unknown strategy" do
94
+ expect { StripeMock.create_test_helper(:lol) }.to raise_error
95
+ end
96
+
97
+ it "can configure the default strategy" do
98
+ StripeMock.set_default_test_helper_strategy(:live)
99
+ helper = StripeMock.create_test_helper
100
+ expect(helper).to be_a StripeMock::TestStrategies::Live
101
+ end
102
+
103
+ it "can overrige a set default strategy" do
104
+ StripeMock.set_default_test_helper_strategy(:live)
105
+ helper = StripeMock.create_test_helper(:mock)
106
+ expect(helper).to be_a StripeMock::TestStrategies::Mock
107
+ end
108
+ end
109
+
40
110
  end