stripe-ruby-mock 1.8.7.0 → 1.8.7.1

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.
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  In your gemfile:
9
9
 
10
- gem 'stripe-ruby-mock', '>= 1.8.7.0'
10
+ gem 'stripe-ruby-mock', '>= 1.8.7.1'
11
11
 
12
12
  ## Features
13
13
 
data/lib/stripe_mock.rb CHANGED
@@ -32,6 +32,7 @@ require 'stripe_mock/api/debug'
32
32
  require 'stripe_mock/request_handlers/charges.rb'
33
33
  require 'stripe_mock/request_handlers/cards.rb'
34
34
  require 'stripe_mock/request_handlers/customers.rb'
35
+ require 'stripe_mock/request_handlers/invoices.rb'
35
36
  require 'stripe_mock/request_handlers/invoice_items.rb'
36
37
  require 'stripe_mock/request_handlers/plans.rb'
37
38
  require 'stripe_mock/request_handlers/recipients.rb'
@@ -18,12 +18,14 @@ module StripeMock
18
18
  include StripeMock::RequestHandlers::Charges
19
19
  include StripeMock::RequestHandlers::Cards
20
20
  include StripeMock::RequestHandlers::Customers
21
+ include StripeMock::RequestHandlers::Invoices
21
22
  include StripeMock::RequestHandlers::InvoiceItems
22
23
  include StripeMock::RequestHandlers::Plans
23
24
  include StripeMock::RequestHandlers::Recipients
24
25
 
25
26
 
26
- attr_reader :charges, :customers, :plans, :error_queue, :recipients
27
+ attr_reader :charges, :customers, :invoices, :plans, :error_queue,
28
+ :recipients
27
29
  attr_reader :bank_tokens
28
30
  attr_accessor :debug, :strict
29
31
 
@@ -32,6 +34,7 @@ module StripeMock
32
34
  @recipients = {}
33
35
  @charges = {}
34
36
  @plans = {}
37
+ @invoices = {}
35
38
  @bank_tokens = {}
36
39
  @card_tokens = {}
37
40
 
@@ -102,7 +105,7 @@ module StripeMock
102
105
  def get_customer_card(customer, token)
103
106
  customer[:cards][:data].find{|cc| cc[:id] == token }
104
107
  end
105
-
108
+
106
109
  def add_card_to_customer(card, cus)
107
110
  card[:customer] = cus[:id]
108
111
 
@@ -4,6 +4,7 @@ module StripeMock
4
4
 
5
5
  def Charges.included(klass)
6
6
  klass.add_handler 'post /v1/charges', :new_charge
7
+ klass.add_handler 'get /v1/charges', :get_charges
7
8
  klass.add_handler 'get /v1/charges/(.*)', :get_charge
8
9
  klass.add_handler 'post /v1/charges/(.*)/capture', :capture_charge
9
10
  klass.add_handler 'post /v1/charges/(.*)/refund', :refund_charge
@@ -14,6 +15,19 @@ module StripeMock
14
15
  charges[id] = Data.mock_charge(params.merge :id => id)
15
16
  end
16
17
 
18
+ def get_charges(route, method_url, params, headers)
19
+ params[:offset] ||= 0
20
+ params[:count] ||= 10
21
+
22
+ clone = charges.clone
23
+
24
+ if params[:customer]
25
+ clone.delete_if { |k,v| v[:customer] != params[:customer] }
26
+ end
27
+
28
+ clone.values[params[:offset], params[:count]]
29
+ end
30
+
17
31
  def get_charge(route, method_url, params, headers)
18
32
  route =~ method_url
19
33
  assert_existance :charge, $1, charges[$1]
@@ -19,6 +19,19 @@ module StripeMock
19
19
  cards << get_card_by_token(params.delete(:card))
20
20
  params[:default_card] = cards.first[:id]
21
21
  end
22
+
23
+ if params[:plan]
24
+ plan = plans[ params[:plan] ]
25
+ assert_existance :plan, params[:plan], plan
26
+
27
+ if params[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
28
+ raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
29
+ end
30
+
31
+ sub = Data.mock_subscription id: new_id('su'), plan: plan, customer: params[:id]
32
+ params[:subscription] = sub
33
+ end
34
+
22
35
  customers[ params[:id] ] = Data.mock_customer(cards, params)
23
36
  end
24
37
 
@@ -31,6 +44,12 @@ module StripeMock
31
44
  plan = plans[ params[:plan] ]
32
45
  assert_existance :plan, params[:plan], plan
33
46
 
47
+ if params[:card]
48
+ new_card = get_card_by_token(params.delete(:card))
49
+ add_card_to_customer(new_card, customer)
50
+ customer[:default_card] = new_card[:id]
51
+ end
52
+
34
53
  # Ensure customer has card to charge if plan has no trial and is not free
35
54
  if customer[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
36
55
  raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
@@ -49,12 +68,19 @@ module StripeMock
49
68
  sub = customer[:subscription]
50
69
  assert_existance nil, nil, sub, "No active subscription for customer: #{$1}"
51
70
 
52
- customer[:subscription] = nil
53
-
54
71
  plan = plans[ sub[:plan][:id] ]
55
72
  assert_existance :plan, params[:plan], plan
56
73
 
57
- Data.mock_delete_subscription(id: sub[:id])
74
+ if params[:at_period_end] == true
75
+ status = 'active'
76
+ cancel_at_period_end = true
77
+ else
78
+ status = 'canceled'
79
+ cancel_at_period_end = false
80
+ end
81
+
82
+ sub = Data.mock_subscription id: sub[:id], plan: plan, customer: $1, status: status, cancel_at_period_end: cancel_at_period_end
83
+ customer[:subscription] = sub
58
84
  end
59
85
 
60
86
  def update_customer(route, method_url, params, headers)
@@ -0,0 +1,37 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module Invoices
4
+
5
+ def Invoices.included(klass)
6
+ klass.add_handler 'post /v1/invoices', :new_invoice
7
+ klass.add_handler 'get /v1/invoices/(.*)', :get_invoice
8
+ klass.add_handler 'get /v1/invoices', :list_invoices
9
+ end
10
+
11
+ def new_invoice(route, method_url, params, headers)
12
+ id = new_id('in')
13
+ invoices[id] = Data.mock_invoice(params.merge :id => id)
14
+ end
15
+
16
+ def list_invoices(route, method_url, params, headers)
17
+ params[:offset] ||= 0
18
+ params[:count] ||= 10
19
+
20
+ result = invoices.clone
21
+
22
+ if params[:customer]
23
+ result.delete_if { |k,v| v[:customer] != params[:customer] }
24
+ end
25
+
26
+ result.values[params[:offset], params[:count]]
27
+ end
28
+
29
+ def get_invoice(route, method_url, params, headers)
30
+ route =~ method_url
31
+ assert_existance :invoice, $1, invoices[$1]
32
+ invoices[$1] ||= Data.mock_invoice(:id => $1)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.8.7.0"
3
+ VERSION = "1.8.7.1"
4
4
  end
@@ -36,7 +36,6 @@ shared_examples 'Charge API' do
36
36
  expect(data[charge2.id][:amount]).to eq(777)
37
37
  end
38
38
 
39
-
40
39
  it "retrieves a stripe charge" do
41
40
  original = Stripe::Charge.create({
42
41
  amount: 777,
@@ -57,6 +56,32 @@ shared_examples 'Charge API' do
57
56
  }
58
57
  end
59
58
 
59
+ context "retrieving a list of charges" do
60
+ before do
61
+ @customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
62
+ @charge = Stripe::Charge.create(customer: @customer.id)
63
+ @charge2 = Stripe::Charge.create
64
+ end
65
+
66
+ it "stores charges for a customer in memory" do
67
+ expect(@customer.charges.map(&:id)).to eq([@charge.id])
68
+ end
69
+
70
+ it "stores all charges in memory" do
71
+ expect(Stripe::Charge.all.map(&:id)).to eq([@charge.id, @charge2.id])
72
+ end
73
+
74
+ it "defaults count to 10 charges" do
75
+ 11.times { Stripe::Charge.create }
76
+ expect(Stripe::Charge.all.count).to eq(10)
77
+ end
78
+
79
+ context "when passing count" do
80
+ it "gets that many charges" do
81
+ expect(Stripe::Charge.all(count: 1).count).to eq(1)
82
+ end
83
+ end
84
+ end
60
85
 
61
86
  context "With strict mode toggled off" do
62
87
 
@@ -34,6 +34,35 @@ shared_examples 'Customer API' do
34
34
  expect(customer.default_card).to be_nil
35
35
  end
36
36
 
37
+ 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')
40
+
41
+ customer = Stripe::Customer.retrieve('test_cus_plan')
42
+ expect(customer.subscription).to_not be_nil
43
+ expect(customer.subscription.plan.id).to eq('silver')
44
+ expect(customer.subscription.customer).to eq(customer.id)
45
+ end
46
+
47
+ it 'cannot create a customer with a plan that does not exist' do
48
+ expect {
49
+ customer = Stripe::Customer.create(id: 'test_cus_no_plan', card: 'tk', :plan => 'non-existant')
50
+ }.to raise_error {|e|
51
+ expect(e).to be_a(Stripe::InvalidRequestError)
52
+ expect(e.message).to eq('No such plan: non-existant')
53
+ }
54
+ end
55
+
56
+ it 'cannot create a customer with an exsting plan, but no card token' do
57
+ plan = Stripe::Plan.create(id: 'p')
58
+ expect {
59
+ customer = Stripe::Customer.create(id: 'test_cus_no_plan', :plan => 'p')
60
+ }.to raise_error {|e|
61
+ expect(e).to be_a(Stripe::InvalidRequestError)
62
+ expect(e.message).to eq('You must supply a valid card')
63
+ }
64
+ end
65
+
37
66
  it "stores a created stripe customer in memory" do
38
67
  customer = Stripe::Customer.create({
39
68
  email: 'johnny@appleseed.com',
@@ -172,15 +201,33 @@ shared_examples 'Customer API' do
172
201
  end
173
202
 
174
203
  it "cancels a stripe customer's subscription" do
175
- plan = Stripe::Plan.create(id: 'the truth')
204
+ Stripe::Plan.create(id: 'the truth')
176
205
  customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
177
206
  sub = customer.update_subscription({ :plan => 'the truth' })
178
207
 
179
208
  result = customer.cancel_subscription
180
- expect(result.deleted).to eq(true)
209
+ expect(result.status).to eq('canceled')
210
+ expect(result.cancel_at_period_end).to be_false
181
211
  expect(result.id).to eq(sub.id)
212
+
182
213
  customer = Stripe::Customer.retrieve('test_customer_sub')
183
- expect(customer.subscription).to be_nil
214
+ expect(customer.subscription).to_not be_nil
215
+ expect(customer.subscription.id).to eq(result.id)
216
+ end
217
+
218
+ it "cancels a stripe customer's subscription at period end" do
219
+ Stripe::Plan.create(id: 'the truth')
220
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
221
+ sub = customer.update_subscription({ :plan => 'the truth' })
222
+
223
+ result = customer.cancel_subscription(at_period_end: true)
224
+ expect(result.status).to eq('active')
225
+ expect(result.cancel_at_period_end).to be_true
226
+ expect(result.id).to eq(sub.id)
227
+
228
+ customer = Stripe::Customer.retrieve('test_customer_sub')
229
+ expect(customer.subscription).to_not be_nil
230
+ expect(customer.subscription.id).to eq(result.id)
184
231
  end
185
232
 
186
233
  it "cannot update to a plan that does not exist" do
@@ -203,6 +250,19 @@ shared_examples 'Customer API' do
203
250
  expect(customer.deleted).to be_true
204
251
  end
205
252
 
253
+ it "sets card" do
254
+ plan = Stripe::Plan.create(id: 'small')
255
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
256
+ customer.update_subscription(card: 'tk', :plan => 'small')
257
+
258
+ customer = Stripe::Customer.retrieve('test_customer_sub')
259
+
260
+ expect(customer.cards.count).to eq(1)
261
+ expect(customer.cards.data.length).to eq(1)
262
+ expect(customer.default_card).to_not be_nil
263
+ expect(customer.default_card).to eq customer.cards.data.first.id
264
+ end
265
+
206
266
  context "With strict mode toggled off" do
207
267
 
208
268
  before { StripeMock.toggle_strict(false) }
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Invoice API' do
4
+
5
+ context "creating a new invoice" do
6
+ it "creates a stripe invoice" do
7
+ invoice = Stripe::Invoice.create
8
+ expect(invoice.id).to match(/^test_in/)
9
+ end
10
+
11
+ it "stores a created stripe invoice in memory" do
12
+ invoice = Stripe::Invoice.create
13
+ data = test_data_source(:invoices)
14
+ expect(data[invoice.id]).to_not be_nil
15
+ expect(data[invoice.id][:id]).to eq(invoice.id)
16
+ end
17
+ end
18
+
19
+ context "retrieving an invoice" do
20
+ it "retrieves a stripe invoice" do
21
+ original = Stripe::Invoice.create
22
+ invoice = Stripe::Invoice.retrieve(original.id)
23
+ expect(invoice.id).to eq(original.id)
24
+ end
25
+ end
26
+
27
+ context "retrieving a list of invoices" do
28
+ before do
29
+ @customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
30
+ @invoice = Stripe::Invoice.create(customer: @customer.id)
31
+ @invoice2 = Stripe::Invoice.create
32
+ end
33
+
34
+ it "stores invoices for a customer in memory" do
35
+ expect(@customer.invoices.map(&:id)).to eq([@invoice.id])
36
+ end
37
+
38
+ it "stores all invoices in memory" do
39
+ expect(Stripe::Invoice.all.map(&:id)).to eq([@invoice.id, @invoice2.id])
40
+ end
41
+
42
+ it "defaults count to 10 invoices" do
43
+ 11.times { Stripe::Invoice.create }
44
+ expect(Stripe::Invoice.all.count).to eq(10)
45
+ end
46
+
47
+ context "when passing count" do
48
+ it "gets that many invoices" do
49
+ expect(Stripe::Invoice.all(count: 1).count).to eq(1)
50
+ end
51
+ end
52
+ end
53
+
54
+ end
@@ -6,6 +6,7 @@ def require_stripe_examples
6
6
  require 'shared_stripe_examples/charge_examples'
7
7
  require 'shared_stripe_examples/customer_examples'
8
8
  require 'shared_stripe_examples/error_mock_examples'
9
+ require 'shared_stripe_examples/invoice_examples'
9
10
  require 'shared_stripe_examples/invoice_item_examples'
10
11
  require 'shared_stripe_examples/plan_examples'
11
12
  require 'shared_stripe_examples/recipient_examples'
@@ -16,8 +17,8 @@ def it_behaves_like_stripe(&block)
16
17
  it_behaves_like 'Card Token Mocking', &block
17
18
  it_behaves_like 'Card API', &block
18
19
  it_behaves_like 'Charge API', &block
19
- it_behaves_like 'Charge API', &block
20
20
  it_behaves_like 'Customer API', &block
21
+ it_behaves_like 'Invoice API', &block
21
22
  it_behaves_like 'Invoice Item API', &block
22
23
  it_behaves_like 'Plan API', &block
23
24
  it_behaves_like 'Recipient API', &block
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe-ruby-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.7.0
4
+ version: 1.8.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-18 00:00:00.000000000 Z
12
+ date: 2013-11-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe
@@ -147,6 +147,7 @@ files:
147
147
  - lib/stripe_mock/request_handlers/charges.rb
148
148
  - lib/stripe_mock/request_handlers/customers.rb
149
149
  - lib/stripe_mock/request_handlers/invoice_items.rb
150
+ - lib/stripe_mock/request_handlers/invoices.rb
150
151
  - lib/stripe_mock/request_handlers/plans.rb
151
152
  - lib/stripe_mock/request_handlers/recipients.rb
152
153
  - lib/stripe_mock/server.rb
@@ -200,6 +201,7 @@ files:
200
201
  - spec/shared_stripe_examples/charge_examples.rb
201
202
  - spec/shared_stripe_examples/customer_examples.rb
202
203
  - spec/shared_stripe_examples/error_mock_examples.rb
204
+ - spec/shared_stripe_examples/invoice_examples.rb
203
205
  - spec/shared_stripe_examples/invoice_item_examples.rb
204
206
  - spec/shared_stripe_examples/plan_examples.rb
205
207
  - spec/shared_stripe_examples/recipient_examples.rb
@@ -247,6 +249,7 @@ test_files:
247
249
  - spec/shared_stripe_examples/charge_examples.rb
248
250
  - spec/shared_stripe_examples/customer_examples.rb
249
251
  - spec/shared_stripe_examples/error_mock_examples.rb
252
+ - spec/shared_stripe_examples/invoice_examples.rb
250
253
  - spec/shared_stripe_examples/invoice_item_examples.rb
251
254
  - spec/shared_stripe_examples/plan_examples.rb
252
255
  - spec/shared_stripe_examples/recipient_examples.rb