stripe-ruby-mock 1.10.1.7 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,11 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Charge API' do
4
4
 
5
+ it "requires a valid card token", :live => true do
6
+ expect {
7
+ charge = Stripe::Charge.create(
8
+ amount: 99,
9
+ currency: 'usd',
10
+ card: 'bogus_card_token'
11
+ )
12
+ }.to raise_error(Stripe::InvalidRequestError, /Invalid token id/)
13
+ end
14
+
5
15
  it "creates a stripe charge item with a card token" do
6
16
  charge = Stripe::Charge.create(
7
17
  amount: 999,
8
18
  currency: 'USD',
9
- card: 'card_token_abcde',
19
+ card: stripe_helper.generate_card_token,
10
20
  description: 'card charge'
11
21
  )
12
22
 
@@ -21,12 +31,12 @@ shared_examples 'Charge API' do
21
31
  charge = Stripe::Charge.create({
22
32
  amount: 333,
23
33
  currency: 'USD',
24
- card: 'card_token_333'
34
+ card: stripe_helper.generate_card_token
25
35
  })
26
36
  charge2 = Stripe::Charge.create({
27
37
  amount: 777,
28
38
  currency: 'USD',
29
- card: 'card_token_777'
39
+ card: stripe_helper.generate_card_token
30
40
  })
31
41
  data = test_data_source(:charges)
32
42
  expect(data[charge.id]).to_not be_nil
@@ -40,7 +50,7 @@ shared_examples 'Charge API' do
40
50
  original = Stripe::Charge.create({
41
51
  amount: 777,
42
52
  currency: 'USD',
43
- card: 'card_token_abc'
53
+ card: stripe_helper.generate_card_token
44
54
  })
45
55
  charge = Stripe::Charge.retrieve(original.id)
46
56
 
@@ -56,6 +66,24 @@ shared_examples 'Charge API' do
56
66
  }
57
67
  end
58
68
 
69
+ it "creates a unique balance transaction" do
70
+ charge1 = Stripe::Charge.create(
71
+ amount: 999,
72
+ currency: 'USD',
73
+ card: stripe_helper.generate_card_token,
74
+ description: 'card charge'
75
+ )
76
+
77
+ charge2 = Stripe::Charge.create(
78
+ amount: 999,
79
+ currency: 'USD',
80
+ card: stripe_helper.generate_card_token,
81
+ description: 'card charge'
82
+ )
83
+
84
+ expect(charge1.balance_transaction).not_to eq(charge2.balance_transaction)
85
+ end
86
+
59
87
  context "retrieving a list of charges" do
60
88
  before do
61
89
  @customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
@@ -83,50 +111,37 @@ shared_examples 'Charge API' do
83
111
  end
84
112
  end
85
113
 
86
- context "With strict mode toggled off" do
87
-
88
- before { StripeMock.toggle_strict(false) }
89
-
90
- it "retrieves a stripe charge with an id that doesn't exist" do
91
- charge = Stripe::Charge.retrieve('test_charge_x')
92
- expect(charge.id).to eq('test_charge_x')
93
- expect(charge.amount).to_not be_nil
94
- expect(charge.card).to_not be_nil
95
- end
96
- end
97
-
98
-
99
114
  describe 'captured status value' do
100
115
  it "reports captured by default" do
101
116
  charge = Stripe::Charge.create({
102
117
  amount: 777,
103
118
  currency: 'USD',
104
- card: 'card_token_abc'
119
+ card: stripe_helper.generate_card_token
105
120
  })
106
121
 
107
- expect(charge.captured).to be_true
122
+ expect(charge.captured).to eq(true)
108
123
  end
109
124
 
110
125
  it "reports captured if capture requested" do
111
126
  charge = Stripe::Charge.create({
112
127
  amount: 777,
113
128
  currency: 'USD',
114
- card: 'card_token_abc',
129
+ card: stripe_helper.generate_card_token,
115
130
  capture: true
116
131
  })
117
132
 
118
- expect(charge.captured).to be_true
133
+ expect(charge.captured).to eq(true)
119
134
  end
120
135
 
121
136
  it "reports not captured if capture: false requested" do
122
137
  charge = Stripe::Charge.create({
123
138
  amount: 777,
124
139
  currency: 'USD',
125
- card: 'card_token_abc',
140
+ card: stripe_helper.generate_card_token,
126
141
  capture: false
127
142
  })
128
143
 
129
- expect(charge.captured).to be_false
144
+ expect(charge.captured).to eq(false)
130
145
  end
131
146
  end
132
147
 
@@ -135,29 +150,30 @@ shared_examples 'Charge API' do
135
150
  charge = Stripe::Charge.create({
136
151
  amount: 777,
137
152
  currency: 'USD',
138
- card: 'card_token_abc',
153
+ card: stripe_helper.generate_card_token,
139
154
  capture: false
140
155
  })
141
156
 
142
157
  returned_charge = charge.capture
143
- expect(charge.captured).to be_true
158
+ expect(charge.captured).to eq(true)
144
159
  expect(returned_charge.id).to eq(charge.id)
145
- expect(returned_charge.captured).to be_true
160
+ expect(returned_charge.captured).to eq(true)
146
161
  end
147
- end
148
-
149
- it "refunds a stripe charge item" do
150
- charge = Stripe::Charge.create(
151
- amount: 999,
152
- currency: 'USD',
153
- card: 'card_token_abcde',
154
- description: 'card charge'
155
- )
156
162
 
157
- charge = charge.refund(amount: 999)
163
+ it "captures with specified amount" do
164
+ charge = Stripe::Charge.create({
165
+ amount: 777,
166
+ currency: 'USD',
167
+ card: stripe_helper.generate_card_token,
168
+ capture: false
169
+ })
158
170
 
159
- expect(charge.refunded).to eq(true)
160
- expect(charge.refunds.first.amount).to eq(999)
161
- expect(charge.amount_refunded).to eq(999)
171
+ returned_charge = charge.capture({ amount: 677 })
172
+ expect(charge.captured).to eq(true)
173
+ expect(returned_charge.amount_refunded).to eq(100)
174
+ expect(returned_charge.id).to eq(charge.id)
175
+ expect(returned_charge.captured).to eq(true)
176
+ end
162
177
  end
178
+
163
179
  end
@@ -89,22 +89,7 @@ shared_examples 'Coupon API' do
89
89
 
90
90
  all = Stripe::Coupon.all
91
91
  expect(all.length).to eq(2)
92
- all.map(&:id).should include('Coupon One', 'Coupon Two')
93
- all.map(&:amount_off).should include(1500, 3000)
92
+ expect(all.map &:id).to include('Coupon One', 'Coupon Two')
93
+ expect(all.map &:amount_off).to include(1500, 3000)
94
94
  end
95
-
96
-
97
- context "With strict mode toggled off" do
98
-
99
- before { StripeMock.toggle_strict(false) }
100
-
101
- it "can retrieve a stripe coupon with an id that doesn't exist" do
102
- coupon = Stripe::Coupon.retrieve('test_coupon_x')
103
-
104
- expect(coupon.id).to eq('test_coupon_x')
105
- expect(coupon.percent_off).to_not be_nil
106
- expect(coupon.valid).to be_true
107
- end
108
- end
109
-
110
95
  end
@@ -2,10 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Customer API' do
4
4
 
5
+ def gen_card_tk
6
+ stripe_helper.generate_card_token
7
+ end
8
+
5
9
  it "creates a stripe customer with a default card" do
6
10
  customer = Stripe::Customer.create({
7
11
  email: 'johnny@appleseed.com',
8
- card: 'some_card_token',
12
+ card: gen_card_tk,
9
13
  description: "a description"
10
14
  })
11
15
  expect(customer.id).to match(/^test_cus/)
@@ -35,8 +39,8 @@ shared_examples 'Customer API' do
35
39
  end
36
40
 
37
41
  it 'creates a customer with a plan' do
38
- plan = Stripe::Plan.create(id: 'silver')
39
- customer = Stripe::Customer.create(id: 'test_cus_plan', card: 'tk', :plan => 'silver')
42
+ plan = stripe_helper.create_plan(id: 'silver')
43
+ customer = Stripe::Customer.create(id: 'test_cus_plan', card: gen_card_tk, :plan => 'silver')
40
44
 
41
45
  customer = Stripe::Customer.retrieve('test_cus_plan')
42
46
  expect(customer.subscriptions.count).to eq(1)
@@ -48,14 +52,14 @@ shared_examples 'Customer API' do
48
52
  end
49
53
 
50
54
  it "creates a customer with a plan (string/symbol agnostic)" do
51
- plan = Stripe::Plan.create(id: 'string_id')
52
- customer = Stripe::Customer.create(id: 'test_cus_plan', card: 'tk', :plan => :string_id)
55
+ plan = stripe_helper.create_plan(id: 'string_id')
56
+ customer = Stripe::Customer.create(id: 'test_cus_plan', card: gen_card_tk, :plan => :string_id)
53
57
 
54
58
  customer = Stripe::Customer.retrieve('test_cus_plan')
55
59
  expect(customer.subscriptions.first.plan.id).to eq('string_id')
56
60
 
57
- plan = Stripe::Plan.create(:id => :sym_id)
58
- customer = Stripe::Customer.create(id: 'test_cus_plan', card: 'tk', :plan => 'sym_id')
61
+ plan = stripe_helper.create_plan(:id => :sym_id)
62
+ customer = Stripe::Customer.create(id: 'test_cus_plan', card: gen_card_tk, :plan => 'sym_id')
59
63
 
60
64
  customer = Stripe::Customer.retrieve('test_cus_plan')
61
65
  expect(customer.subscriptions.first.plan.id).to eq('sym_id')
@@ -64,9 +68,9 @@ shared_examples 'Customer API' do
64
68
  context "create customer" do
65
69
 
66
70
  it "with a trial when trial_end is set" do
67
- plan = Stripe::Plan.create(id: 'no_trial', amount: 999)
71
+ plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
68
72
  trial_end = Time.now.utc.to_i + 3600
69
- customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'no_trial', trial_end: trial_end)
73
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: gen_card_tk, plan: 'no_trial', trial_end: trial_end)
70
74
 
71
75
  customer = Stripe::Customer.retrieve('test_cus_trial_end')
72
76
  expect(customer.subscriptions.count).to eq(1)
@@ -80,9 +84,9 @@ shared_examples 'Customer API' do
80
84
  end
81
85
 
82
86
  it 'overrides trial period length when trial_end is set' do
83
- plan = Stripe::Plan.create(id: 'silver', amount: 999, trial_period_days: 14)
87
+ plan = stripe_helper.create_plan(id: 'silver', amount: 999, trial_period_days: 14)
84
88
  trial_end = Time.now.utc.to_i + 3600
85
- customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'silver', trial_end: trial_end)
89
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: gen_card_tk, plan: 'silver', trial_end: trial_end)
86
90
 
87
91
  customer = Stripe::Customer.retrieve('test_cus_trial_end')
88
92
  expect(customer.subscriptions.count).to eq(1)
@@ -95,8 +99,8 @@ shared_examples 'Customer API' do
95
99
  end
96
100
 
97
101
  it "returns no trial when trial_end is set to 'now'" do
98
- plan = Stripe::Plan.create(id: 'silver', amount: 999, trial_period_days: 14)
99
- customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'silver', trial_end: "now")
102
+ plan = stripe_helper.create_plan(id: 'silver', amount: 999, trial_period_days: 14)
103
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: gen_card_tk, plan: 'silver', trial_end: "now")
100
104
 
101
105
  customer = Stripe::Customer.retrieve('test_cus_trial_end')
102
106
  expect(customer.subscriptions.count).to eq(1)
@@ -110,9 +114,9 @@ shared_examples 'Customer API' do
110
114
  end
111
115
 
112
116
  it "returns an error if trial_end is set to a past time" do
113
- plan = Stripe::Plan.create(id: 'silver', amount: 999)
117
+ plan = stripe_helper.create_plan(id: 'silver', amount: 999)
114
118
  expect {
115
- Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'silver', trial_end: Time.now.utc.to_i - 3600)
119
+ Stripe::Customer.create(id: 'test_cus_trial_end', card: gen_card_tk, plan: 'silver', trial_end: Time.now.utc.to_i - 3600)
116
120
  }.to raise_error {|e|
117
121
  expect(e).to be_a(Stripe::InvalidRequestError)
118
122
  expect(e.message).to eq('Invalid timestamp: must be an integer Unix timestamp in the future')
@@ -121,7 +125,7 @@ shared_examples 'Customer API' do
121
125
 
122
126
  it "returns an error if trial_end is set without a plan" do
123
127
  expect {
124
- Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', trial_end: "now")
128
+ Stripe::Customer.create(id: 'test_cus_trial_end', card: gen_card_tk, trial_end: "now")
125
129
  }.to raise_error {|e|
126
130
  expect(e).to be_a(Stripe::InvalidRequestError)
127
131
  expect(e.message).to eq('Received unknown parameter: trial_end')
@@ -132,15 +136,15 @@ shared_examples 'Customer API' do
132
136
 
133
137
  it 'cannot create a customer with a plan that does not exist' do
134
138
  expect {
135
- customer = Stripe::Customer.create(id: 'test_cus_no_plan', card: 'tk', :plan => 'non-existant')
139
+ customer = Stripe::Customer.create(id: 'test_cus_no_plan', card: gen_card_tk, :plan => 'non-existant')
136
140
  }.to raise_error {|e|
137
141
  expect(e).to be_a(Stripe::InvalidRequestError)
138
142
  expect(e.message).to eq('No such plan: non-existant')
139
143
  }
140
144
  end
141
145
 
142
- it 'cannot create a customer with an exsting plan, but no card token' do
143
- plan = Stripe::Plan.create(id: 'p')
146
+ it 'cannot create a customer with an existing plan, but no card token' do
147
+ plan = stripe_helper.create_plan(id: 'p')
144
148
  expect {
145
149
  customer = Stripe::Customer.create(id: 'test_cus_no_plan', :plan => 'p')
146
150
  }.to raise_error {|e|
@@ -152,11 +156,11 @@ shared_examples 'Customer API' do
152
156
  it "stores a created stripe customer in memory" do
153
157
  customer = Stripe::Customer.create({
154
158
  email: 'johnny@appleseed.com',
155
- card: 'some_card_token'
159
+ card: gen_card_tk
156
160
  })
157
161
  customer2 = Stripe::Customer.create({
158
162
  email: 'bob@bobbers.com',
159
- card: 'another_card_token'
163
+ card: gen_card_tk
160
164
  })
161
165
  data = test_data_source(:customers)
162
166
  expect(data[customer.id]).to_not be_nil
@@ -169,7 +173,7 @@ shared_examples 'Customer API' do
169
173
  it "retrieves a stripe customer" do
170
174
  original = Stripe::Customer.create({
171
175
  email: 'johnny@appleseed.com',
172
- card: 'some_card_token'
176
+ card: gen_card_tk
173
177
  })
174
178
  customer = Stripe::Customer.retrieve(original.id)
175
179
 
@@ -194,7 +198,7 @@ shared_examples 'Customer API' do
194
198
 
195
199
  all = Stripe::Customer.all
196
200
  expect(all.length).to eq(2)
197
- all.map(&:email).should include('one@one.com', 'two@two.com')
201
+ expect(all.map &:email).to include('one@one.com', 'two@two.com')
198
202
  end
199
203
 
200
204
  it "updates a stripe customer" do
@@ -213,12 +217,12 @@ shared_examples 'Customer API' do
213
217
  end
214
218
 
215
219
  it "updates a stripe customer's card" do
216
- original = Stripe::Customer.create(id: 'test_customer_update', card: 'token')
220
+ original = Stripe::Customer.create(id: 'test_customer_update', card: gen_card_tk)
217
221
  card = original.cards.data.first
218
222
  expect(original.default_card).to eq(card.id)
219
223
  expect(original.cards.count).to eq(1)
220
224
 
221
- original.card = 'new_token'
225
+ original.card = gen_card_tk
222
226
  original.save
223
227
 
224
228
  new_card = original.cards.data.first
@@ -231,19 +235,6 @@ shared_examples 'Customer API' do
231
235
  it "deletes a customer" do
232
236
  customer = Stripe::Customer.create(id: 'test_customer_sub')
233
237
  customer = customer.delete
234
- expect(customer.deleted).to be_true
235
- end
236
-
237
- context "With strict mode toggled off" do
238
-
239
- before { StripeMock.toggle_strict(false) }
240
-
241
- it "retrieves a stripe customer with an id that doesn't exist" do
242
- customer = Stripe::Customer.retrieve('test_customer_x')
243
- expect(customer.id).to eq('test_customer_x')
244
- expect(customer.email).to_not be_nil
245
- expect(customer.description).to_not be_nil
246
- end
238
+ expect(customer.deleted).to eq(true)
247
239
  end
248
-
249
240
  end
@@ -49,7 +49,7 @@ shared_examples 'Stripe Error Mocking' do
49
49
  error = Stripe::AuthenticationError.new('Bad Auth', 499, 'abody', 'json abody')
50
50
  StripeMock.prepare_error(error)
51
51
 
52
- expect { Stripe::Plan.create() }.to raise_error {|e|
52
+ expect { stripe_helper.create_plan() }.to raise_error {|e|
53
53
  expect(e).to be_a(Stripe::AuthenticationError)
54
54
  expect(e.message).to eq('Bad Auth')
55
55
 
@@ -24,6 +24,22 @@ shared_examples 'Invoice API' do
24
24
  end
25
25
  end
26
26
 
27
+ context "updating an invoice" do
28
+ it "updates a stripe invoice" do
29
+ invoice = Stripe::Invoice.create(currency: "cad", statement_description: "orig-desc")
30
+ expect(invoice.currency).to eq("cad")
31
+ expect(invoice.statement_description).to eq("orig-desc")
32
+
33
+ invoice.currency = "usd"
34
+ invoice.statement_description = "new-desc"
35
+ invoice.save
36
+
37
+ invoice = Stripe::Invoice.retrieve(invoice.id)
38
+ expect(invoice.currency).to eq("usd")
39
+ expect(invoice.statement_description).to eq("new-desc")
40
+ end
41
+ end
42
+
27
43
  context "retrieving a list of invoices" do
28
44
  before do
29
45
  @customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
@@ -58,8 +74,8 @@ shared_examples 'Invoice API' do
58
74
  end
59
75
 
60
76
  it 'updates attempted and paid flags' do
61
- expect(@invoice.attempted).to be_true
62
- expect(@invoice.paid).to be_true
77
+ expect(@invoice.attempted).to eq(true)
78
+ expect(@invoice.paid).to eq(true)
63
79
  end
64
80
 
65
81
  it 'sets the charge attribute' do
@@ -70,7 +86,7 @@ shared_examples 'Invoice API' do
70
86
 
71
87
  context "retrieving upcoming invoice" do
72
88
  before do
73
- @customer = Stripe::Customer.create(email: 'johnny@appleseed.com', card: 'some_card_token')
89
+ @customer = Stripe::Customer.create(email: 'johnny@appleseed.com', card: stripe_helper.generate_card_token)
74
90
  end
75
91
 
76
92
  it 'fails without parameters' do
@@ -78,7 +94,7 @@ shared_examples 'Invoice API' do
78
94
  expect(e).to be_a(ArgumentError) }
79
95
  end
80
96
 
81
- it 'fails without a valid customer in strict mode' do
97
+ it 'fails without a valid customer' do
82
98
  expect { Stripe::Invoice.upcoming(customer: 'whatever') }.to raise_error {|e|
83
99
  expect(e).to be_a(Stripe::InvalidRequestError)
84
100
  expect(e.message).to eq('No such customer: whatever') }
@@ -99,7 +115,7 @@ shared_examples 'Invoice API' do
99
115
  end
100
116
 
101
117
  it 'works when customer has a subscription' do
102
- @plan = Stripe::Plan.create()
118
+ @plan = stripe_helper.create_plan()
103
119
  @subscription = @customer.subscriptions.create(plan: @plan.id)
104
120
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
105
121
 
@@ -113,7 +129,7 @@ shared_examples 'Invoice API' do
113
129
  end
114
130
 
115
131
  it 'sets the start and end of billing periods correctly when plan has an interval_count' do
116
- @oddplan = Stripe::Plan.create(interval: "week", interval_count: 11)
132
+ @oddplan = stripe_helper.create_plan(interval: "week", interval_count: 11)
117
133
  @subscription = @customer.subscriptions.create(plan: @oddplan.id)
118
134
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
119
135
 
@@ -124,9 +140,9 @@ shared_examples 'Invoice API' do
124
140
  end
125
141
 
126
142
  it 'chooses the most recent of multiple subscriptions' do
127
- @shortplan = Stripe::Plan.create(interval: "week") # 1 week sub
128
- @plainplan = Stripe::Plan.create() # 1 month sub
129
- @longplan = Stripe::Plan.create(interval: "year") # 1 year sub
143
+ @shortplan = stripe_helper.create_plan(id: 'a', interval: "week") # 1 week sub
144
+ @plainplan = stripe_helper.create_plan(id: 'b') # 1 month sub
145
+ @longplan = stripe_helper.create_plan(id: 'c', interval: "year") # 1 year sub
130
146
 
131
147
  @plainsub = @customer.subscriptions.create(plan: @plainplan.id)
132
148
  @shortsub = @customer.subscriptions.create(plan: @shortplan.id)
@@ -142,7 +158,7 @@ shared_examples 'Invoice API' do
142
158
  context 'calculates month and year offsets correctly' do
143
159
 
144
160
  it 'for one month plan on the 1st' do
145
- @plan = Stripe::Plan.create()
161
+ @plan = stripe_helper.create_plan()
146
162
  @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2014,1,1,12).to_i)
147
163
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
148
164
 
@@ -153,7 +169,7 @@ shared_examples 'Invoice API' do
153
169
  end
154
170
 
155
171
  it 'for one year plan on the 1st' do
156
- @plan = Stripe::Plan.create(interval: "year")
172
+ @plan = stripe_helper.create_plan(interval: "year")
157
173
  @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2012,1,1,12).to_i)
158
174
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
159
175
 
@@ -164,7 +180,7 @@ shared_examples 'Invoice API' do
164
180
  end
165
181
 
166
182
  it 'for one month plan on the 31st' do
167
- @plan = Stripe::Plan.create()
183
+ @plan = stripe_helper.create_plan()
168
184
  @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2014,1,31,12).to_i)
169
185
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
170
186
 
@@ -175,7 +191,7 @@ shared_examples 'Invoice API' do
175
191
  end
176
192
 
177
193
  it 'for one year plan on feb. 29th' do
178
- @plan = Stripe::Plan.create(interval: "year")
194
+ @plan = stripe_helper.create_plan(interval: "year")
179
195
  @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2012,2,29,12).to_i)
180
196
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
181
197
 
@@ -186,7 +202,7 @@ shared_examples 'Invoice API' do
186
202
  end
187
203
 
188
204
  it 'for two month plan on dec. 31st' do
189
- @plan = Stripe::Plan.create(interval_count: 2)
205
+ @plan = stripe_helper.create_plan(interval_count: 2)
190
206
  @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2013,12,31,12).to_i)
191
207
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
192
208
 
@@ -197,7 +213,7 @@ shared_examples 'Invoice API' do
197
213
  end
198
214
 
199
215
  it 'for three month plan on nov. 30th' do
200
- @plan = Stripe::Plan.create(interval_count: 3)
216
+ @plan = stripe_helper.create_plan(interval_count: 3)
201
217
  @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2013,11,30,12).to_i)
202
218
  @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
203
219
 
@@ -2,16 +2,68 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Invoice Item API' do
4
4
 
5
- it "creates a stripe invoice item" do
6
- invoice_item = Stripe::InvoiceItem.create({
7
- amount: 1099,
8
- customer: 1234,
9
- currency: 'USD',
10
- description: "invoice item desc"
11
- }, 'abcde')
12
-
13
- expect(invoice_item.amount).to eq(1099)
14
- expect(invoice_item.description).to eq('invoice item desc')
5
+ context "creating a new invoice item" do
6
+ it "creates a stripe invoice item" do
7
+ invoice_item = Stripe::InvoiceItem.create({
8
+ amount: 1099,
9
+ customer: 1234,
10
+ currency: 'USD',
11
+ description: "invoice item desc"
12
+ }, 'abcde')
13
+
14
+ expect(invoice_item.id).to match(/^test_ii/)
15
+ expect(invoice_item.amount).to eq(1099)
16
+ expect(invoice_item.description).to eq('invoice item desc')
17
+ end
18
+
19
+ it "stores a created stripe invoice item in memory" do
20
+ invoice_item = Stripe::InvoiceItem.create
21
+ data = test_data_source(:invoice_items)
22
+ expect(data[invoice_item.id]).to_not be_nil
23
+ expect(data[invoice_item.id][:id]).to eq(invoice_item.id)
24
+ end
25
+ end
26
+
27
+ context "retrieving an invoice item" do
28
+ it "retrieves a stripe invoice item" do
29
+ original = Stripe::InvoiceItem.create
30
+ invoice_item = Stripe::InvoiceItem.retrieve(original.id)
31
+ expect(invoice_item.id).to eq(original.id)
32
+ end
33
+ end
34
+
35
+ context "retrieving a list of invoice items" do
36
+ before do
37
+ Stripe::InvoiceItem.create({ amount: 1075 })
38
+ Stripe::InvoiceItem.create({ amount: 1540 })
39
+ end
40
+
41
+ it "retrieves all invoice items" do
42
+ all = Stripe::InvoiceItem.all
43
+ expect(all.length).to eq(2)
44
+ expect(all.map &:amount).to include(1075, 1540)
45
+ end
46
+ end
47
+
48
+ it "updates a stripe invoice_item" do
49
+ original = Stripe::InvoiceItem.create(id: 'test_invoice_item_update')
50
+ amount = original.amount
51
+
52
+ original.description = 'new desc'
53
+ original.save
54
+
55
+ expect(original.amount).to eq(amount)
56
+ expect(original.description).to eq('new desc')
57
+
58
+ invoice_item = Stripe::InvoiceItem.retrieve("test_invoice_item_update")
59
+ expect(invoice_item.amount).to eq(original.amount)
60
+ expect(invoice_item.description).to eq('new desc')
61
+ end
62
+
63
+ it "deletes a invoice_item" do
64
+ invoice_item = Stripe::InvoiceItem.create(id: 'test_invoice_item_sub')
65
+ invoice_item = invoice_item.delete
66
+ expect(invoice_item.deleted).to eq true
15
67
  end
16
68
 
17
69
  end