stripe-ruby-mock 1.8.3.14 → 1.8.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -231,6 +231,16 @@ If you always want debug to be on (it's quite verbose), you should put this in a
231
231
  * Throw useful errors that emulate Stripe's
232
232
  * For example: "You must supply either a card or a customer id" for `Stripe::Charge`
233
233
 
234
+ ## Developing stripe-ruby-mock
235
+
236
+ To run the tests:
237
+
238
+ $ rspec
239
+
240
+ Patches are welcome and greatly appreciated! If you're contributing to fix a problem,
241
+ be sure to write tests that illustrate the problem being fixed.
242
+ This will help ensure that the problem remains fixed in future updates.
243
+
234
244
  ## Copyright
235
245
 
236
246
  Copyright (c) 2013 Gilbert
@@ -2,36 +2,28 @@ module StripeMock
2
2
  module Data
3
3
 
4
4
  def self.test_customer(params)
5
+ cus_id = params[:id] || "test_cus_default"
6
+ card_id = params.delete(:card_id)
7
+ cards = []
8
+ cards << test_card(id: card_id, customer: cus_id) if card_id
5
9
  {
6
10
  email: 'stripe_mock@example.com',
7
11
  description: 'an auto-generated stripe customer data mock',
8
12
  object: "customer",
9
13
  created: 1372126710,
10
- id: "cus_24vuDSooAnB087",
14
+ id: cus_id,
11
15
  livemode: false,
12
- active_card: {
13
- object: "card",
14
- last4: "4242",
15
- type: "Visa",
16
- exp_month: 12,
17
- exp_year: 2013,
18
- fingerprint: "wXWJT135mEK107G8",
19
- country: "US",
20
- name: "Vetty Vet",
21
- address_line1: nil,
22
- address_line2: nil,
23
- address_city: nil,
24
- address_state: nil,
25
- address_zip: nil,
26
- address_country: nil,
27
- cvc_check: "pass",
28
- address_line1_check: nil,
29
- address_zip_check: nil
30
- },
31
16
  delinquent: false,
32
17
  subscription: nil,
33
18
  discount: nil,
34
- account_balance: 0
19
+ account_balance: 0,
20
+ cards: {
21
+ object: "list",
22
+ count: cards.count,
23
+ url: "/v1/customers/#{cus_id}/cards",
24
+ data: cards
25
+ },
26
+ default_card: card_id
35
27
  }.merge(params)
36
28
  end
37
29
 
@@ -87,13 +79,25 @@ module StripeMock
87
79
 
88
80
  def self.test_card(params={})
89
81
  {
90
- :type => "Visa",
91
- :last4 => "4242",
92
- :exp_month => 11,
93
- :country => "US",
94
- :exp_year => 2012,
95
- :id => "cc_test_card",
96
- :object => "card"
82
+ id: "test_cc_default",
83
+ object: "card",
84
+ last4: "4242",
85
+ type: "Visa",
86
+ exp_month: 4,
87
+ exp_year: 2013,
88
+ fingerprint: "wXWJT135mEK107G8",
89
+ customer: "test_cus_default",
90
+ country: "US",
91
+ name: "Johnny App",
92
+ address_line1: nil,
93
+ address_line2: nil,
94
+ address_city: nil,
95
+ address_state: nil,
96
+ address_zip: nil,
97
+ address_country: nil,
98
+ cvc_check: nil,
99
+ address_line1_check: nil,
100
+ address_zip_check: nil
97
101
  }.merge(params)
98
102
  end
99
103
 
@@ -47,16 +47,16 @@ module StripeMock
47
47
  end
48
48
 
49
49
  method_url = "#{method} #{url}"
50
- handler = Instance.handler_for_method_url(method_url)
51
- mock_error = @error_queue.error_for_handler_name(handler[:name])
52
-
53
- if handler && mock_error
54
- @error_queue.dequeue
55
- raise mock_error
56
- elsif handler
57
- res = self.send(handler[:name], handler[:route], method_url, params, headers)
58
- puts "[StripeMock res] #{res}" if @debug == true
59
- [res, api_key]
50
+
51
+ if handler = Instance.handler_for_method_url(method_url)
52
+ if mock_error = @error_queue.error_for_handler_name(handler[:name])
53
+ @error_queue.dequeue
54
+ raise mock_error
55
+ else
56
+ res = self.send(handler[:name], handler[:route], method_url, params, headers)
57
+ puts "[StripeMock res] #{res}" if @debug == true
58
+ [res, api_key]
59
+ end
60
60
  else
61
61
  puts "WARNING: Unrecognized method + url: [#{method} #{url}]"
62
62
  puts " params: #{params}"
@@ -13,6 +13,7 @@ module StripeMock
13
13
 
14
14
  def new_customer(route, method_url, params, headers)
15
15
  params[:id] ||= new_id('cus')
16
+ params[:card_id] = new_id('cc') if params.delete(:card)
16
17
  customers[ params[:id] ] = Data.test_customer(params)
17
18
  end
18
19
 
@@ -46,8 +47,24 @@ module StripeMock
46
47
  def update_customer(route, method_url, params, headers)
47
48
  route =~ method_url
48
49
  assert_existance :customer, $1, customers[$1]
49
- customers[$1] ||= Data.test_customer(:id => $1)
50
- customers[$1].merge!(params)
50
+
51
+ card_id = new_id('cc') if params.delete(:card)
52
+ cus = customers[$1] ||= Data.test_customer(:id => $1)
53
+ cus.merge!(params)
54
+
55
+ if card_id
56
+ new_card = Data.test_card(id: card_id, customer: cus[:id])
57
+
58
+ if cus[:cards][:count] == 0
59
+ cus[:cards][:count] += 1
60
+ else
61
+ cus[:cards][:data].delete_if {|card| card[:id] == cus[:default_card]}
62
+ end
63
+ cus[:cards][:data] << new_card
64
+ cus[:default_card] = new_card[:id]
65
+ end
66
+
67
+ cus
51
68
  end
52
69
 
53
70
  def get_customer(route, method_url, params, headers)
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.8.3.14"
3
+ VERSION = "1.8.4.1"
4
4
  end
@@ -20,6 +20,15 @@ describe StripeMock::Instance do
20
20
  expect(res[:name]).to eq('String Plan')
21
21
  end
22
22
 
23
+ it "exits gracefully on an unrecognized handler url" do
24
+ dummy_params = {
25
+ "id" => "str_12345",
26
+ "name" => "PLAN"
27
+ }
28
+
29
+ expect { res, api_key = StripeMock.instance.mock_request('post', '/v1/unrecongnized_method', 'api_key', dummy_params) }.to_not raise_error
30
+ end
31
+
23
32
  it "can toggle debug" do
24
33
  StripeMock.toggle_debug(true)
25
34
  expect(StripeMock.instance.debug).to eq(true)
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Customer API' do
4
4
 
5
- it "creates a stripe customer" do
5
+ it "creates a stripe customer with a default card" do
6
6
  customer = Stripe::Customer.create({
7
7
  email: 'johnny@appleseed.com',
8
8
  card: 'some_card_token',
@@ -11,6 +11,27 @@ shared_examples 'Customer API' do
11
11
  expect(customer.id).to match(/^test_cus/)
12
12
  expect(customer.email).to eq('johnny@appleseed.com')
13
13
  expect(customer.description).to eq('a description')
14
+
15
+ expect(customer.cards.count).to eq(1)
16
+ expect(customer.cards.data.length).to eq(1)
17
+ expect(customer.default_card).to_not be_nil
18
+ expect(customer.default_card).to eq customer.cards.data.first.id
19
+
20
+ expect { customer.card }.to raise_error
21
+ end
22
+
23
+ it "creates a stripe customer without a card" do
24
+ customer = Stripe::Customer.create({
25
+ email: 'cardless@appleseed.com',
26
+ description: "no card"
27
+ })
28
+ expect(customer.id).to match(/^test_cus/)
29
+ expect(customer.email).to eq('cardless@appleseed.com')
30
+ expect(customer.description).to eq('no card')
31
+
32
+ expect(customer.cards.count).to eq(0)
33
+ expect(customer.cards.data.length).to eq(0)
34
+ expect(customer.default_card).to be_nil
14
35
  end
15
36
 
16
37
  it "stores a created stripe customer in memory" do
@@ -39,6 +60,7 @@ shared_examples 'Customer API' do
39
60
 
40
61
  expect(customer.id).to eq(original.id)
41
62
  expect(customer.email).to eq(original.email)
63
+ expect(customer.default_card).to eq(original.default_card)
42
64
  expect(customer.subscription).to be_nil
43
65
  end
44
66
 
@@ -74,6 +96,22 @@ shared_examples 'Customer API' do
74
96
  expect(customer.description).to eq('new desc')
75
97
  end
76
98
 
99
+ it "updates a stripe customer's card" do
100
+ original = Stripe::Customer.create(id: 'test_customer_update', card: 'token')
101
+ card = original.cards.data.first
102
+ expect(original.default_card).to eq(card.id)
103
+ expect(original.cards.count).to eq(1)
104
+
105
+ original.card = 'new_token'
106
+ original.save
107
+
108
+ new_card = original.cards.data.first
109
+ expect(original.cards.count).to eq(1)
110
+ expect(original.default_card).to eq(new_card.id)
111
+
112
+ expect(new_card.id).to_not eq(card.id)
113
+ end
114
+
77
115
  it "updates a stripe customer's subscription" do
78
116
  plan = Stripe::Plan.create(id: 'silver')
79
117
  customer = Stripe::Customer.create(id: 'test_customer_sub')
@@ -17,7 +17,7 @@ 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.8.3'
20
+ gem.add_dependency 'stripe', '~> 1.8.4'
21
21
  gem.add_dependency 'jimson-temp'
22
22
  gem.add_dependency 'dante', '>= 0.1.5'
23
23
 
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.3.14
4
+ version: 1.8.4.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-07-18 00:00:00.000000000 Z
12
+ date: 2013-07-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.8.3
21
+ version: 1.8.4
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 1.8.3
29
+ version: 1.8.4
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: jimson-temp
32
32
  requirement: !ruby/object:Gem::Requirement