stripe-ruby-mock 1.8.3.9 → 1.8.3.10

Sign up to get free protection for your applications and to get access to all the features.
data/lib/stripe_mock.rb CHANGED
@@ -23,6 +23,7 @@ require 'stripe_mock/api/client'
23
23
  require 'stripe_mock/api/server'
24
24
  require 'stripe_mock/api/errors'
25
25
  require 'stripe_mock/api/webhooks'
26
+ require 'stripe_mock/api/strict'
26
27
  require 'stripe_mock/api/debug'
27
28
 
28
29
  require 'stripe_mock/request_handlers/charges.rb'
@@ -0,0 +1,11 @@
1
+ module StripeMock
2
+
3
+ def self.toggle_strict(toggle)
4
+ if @state == 'local'
5
+ @instance.strict = toggle
6
+ elsif @state == 'remote'
7
+ @client.set_server_strict(toggle)
8
+ end
9
+ end
10
+
11
+ end
@@ -12,7 +12,14 @@ module StripeMock
12
12
  end
13
13
 
14
14
  def mock_request(method, url, api_key, params={}, headers={})
15
- timeout_wrap { @pipe.mock_request(method, url, api_key, params, headers) }
15
+ timeout_wrap do
16
+ @pipe.mock_request(method, url, api_key, params, headers).tap {|result|
17
+ response, api_key = result
18
+ if response.is_a?(Hash) && response['error_raised'] == 'invalid_request'
19
+ raise Stripe::InvalidRequestError.new(*response['error_params'])
20
+ end
21
+ }
22
+ end
16
23
  end
17
24
 
18
25
  def get_server_data(key)
@@ -32,6 +39,14 @@ module StripeMock
32
39
  timeout_wrap { @pipe.debug? }
33
40
  end
34
41
 
42
+ def set_server_strict(toggle)
43
+ timeout_wrap { @pipe.set_strict(toggle) }
44
+ end
45
+
46
+ def server_strict?
47
+ timeout_wrap { @pipe.strict? }
48
+ end
49
+
35
50
  def clear_server_data
36
51
  timeout_wrap { @pipe.clear_data }
37
52
  end
@@ -54,9 +69,6 @@ module StripeMock
54
69
  raise
55
70
  rescue Errno::ECONNREFUSED => e
56
71
  raise StripeMock::ServerTimeoutError.new(e)
57
- rescue StandardError => e
58
- puts "Unexpected StripeMock Client Error: #{e.inspect}"
59
- {}
60
72
  end
61
73
  end
62
74
 
@@ -18,7 +18,7 @@ module StripeMock
18
18
 
19
19
 
20
20
  attr_reader :charges, :customers, :plans
21
- attr_accessor :pending_error, :debug
21
+ attr_accessor :pending_error, :debug, :strict
22
22
 
23
23
  def initialize
24
24
  @customers = {}
@@ -28,6 +28,7 @@ module StripeMock
28
28
  @id_counter = 0
29
29
  @pending_error = nil
30
30
  @debug = false
31
+ @strict = true
31
32
  end
32
33
 
33
34
  def mock_request(method, url, api_key, params={}, headers={})
@@ -62,6 +63,14 @@ module StripeMock
62
63
 
63
64
  private
64
65
 
66
+ def assert_existance(type, id, obj)
67
+ return unless @strict == true
68
+
69
+ if obj.nil?
70
+ raise Stripe::InvalidRequestError.new("No such #{type}: #{id}", type.to_s, 400)
71
+ end
72
+ end
73
+
65
74
  def new_id(prefix)
66
75
  # Stripe ids must be strings
67
76
  "test_#{prefix}_#{@id_counter += 1}"
@@ -14,6 +14,7 @@ module StripeMock
14
14
 
15
15
  def get_charge(route, method_url, params, headers)
16
16
  route =~ method_url
17
+ assert_existance :charge, $1, charges[$1]
17
18
  charges[$1] ||= Data.test_charge(:id => $1)
18
19
  end
19
20
 
@@ -12,26 +12,33 @@ module StripeMock
12
12
  end
13
13
 
14
14
  def new_customer(route, method_url, params, headers)
15
- id = new_id('cus')
16
- customers[id] = Data.test_customer(params.merge :id => id)
15
+ params[:id] ||= new_id('cus')
16
+ customers[ params[:id] ] = Data.test_customer(params)
17
17
  end
18
18
 
19
19
  def new_subscription(route, method_url, params, headers)
20
+ route =~ method_url
21
+ assert_existance :customer, $1, customers[$1]
22
+ assert_existance :plan, params[:plan], plans[ params[:plan] ]
20
23
  Data.test_subscription(params[:plan])
21
24
  end
22
25
 
23
26
  def cancel_subscription(route, method_url, params, headers)
27
+ route =~ method_url
28
+ assert_existance :customer, $1, customers[$1]
24
29
  Data.test_delete_subscription(params[:id])
25
30
  end
26
31
 
27
32
  def update_customer(route, method_url, params, headers)
28
33
  route =~ method_url
34
+ assert_existance :customer, $1, customers[$1]
29
35
  customers[$1] ||= Data.test_customer(:id => $1)
30
36
  customers[$1].merge!(params)
31
37
  end
32
38
 
33
39
  def get_customer(route, method_url, params, headers)
34
40
  route =~ method_url
41
+ assert_existance :customer, $1, customers[$1]
35
42
  customers[$1] ||= Data.test_customer(:id => $1)
36
43
  end
37
44
 
@@ -3,9 +3,10 @@ module StripeMock
3
3
  module Plans
4
4
 
5
5
  def Plans.included(klass)
6
- klass.add_handler 'post /v1/plans', :new_plan
7
- klass.add_handler 'get /v1/plans/(.*)', :get_plan
8
- klass.add_handler 'get /v1/plans', :list_plans
6
+ klass.add_handler 'post /v1/plans', :new_plan
7
+ klass.add_handler 'post /v1/plans/(.*)', :update_plan
8
+ klass.add_handler 'get /v1/plans/(.*)', :get_plan
9
+ klass.add_handler 'get /v1/plans', :list_plans
9
10
  end
10
11
 
11
12
  def new_plan(route, method_url, params, headers)
@@ -13,8 +14,16 @@ module StripeMock
13
14
  plans[ params[:id] ] = Data.test_plan(params)
14
15
  end
15
16
 
17
+ def update_plan(route, method_url, params, headers)
18
+ route =~ method_url
19
+ assert_existance :plan, $1, plans[$1]
20
+ plans[$1] ||= Data.test_plan(:id => $1)
21
+ plans[$1].merge!(params)
22
+ end
23
+
16
24
  def get_plan(route, method_url, params, headers)
17
25
  route =~ method_url
26
+ assert_existance :plan, $1, plans[$1]
18
27
  plans[$1] ||= Data.test_plan(:id => $1)
19
28
  end
20
29
 
@@ -21,7 +21,14 @@ module StripeMock
21
21
  end
22
22
 
23
23
  def mock_request(*args)
24
- @instance.mock_request(*args)
24
+ begin
25
+ @instance.mock_request(*args)
26
+ rescue Stripe::InvalidRequestError => e
27
+ {
28
+ :error_raised => 'invalid_request',
29
+ :error_params => [e.message, e.param, e.http_status, e.http_body, e.json_body]
30
+ }
31
+ end
25
32
  end
26
33
 
27
34
  def get_data(key)
@@ -36,10 +43,12 @@ module StripeMock
36
43
  @instance.debug = toggle
37
44
  end
38
45
 
39
- def debug?
40
- @instance.debug
46
+ def set_strict(toggle)
47
+ @instance.strict = toggle
41
48
  end
42
49
 
50
+ def debug?; @instance.debug; end
51
+ def strict?; @instance.strict; end
43
52
  def ping; true; end
44
53
  end
45
54
 
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.8.3.9"
3
+ VERSION = "1.8.3.10"
4
4
  end
data/spec/server_spec.rb CHANGED
@@ -54,6 +54,7 @@ describe 'StripeMock Server' do
54
54
 
55
55
 
56
56
  it "returns a response with symbolized hash keys" do
57
+ Stripe::Plan.create(id: 'x')
57
58
  response, api_key = StripeMock.redirect_to_mock_server('get', '/v1/plans/x', 'xxx')
58
59
  response.keys.each {|k| expect(k).to be_a(Symbol) }
59
60
  end
@@ -48,13 +48,28 @@ shared_examples 'Charge API' do
48
48
  expect(charge.amount).to eq(original.amount)
49
49
  end
50
50
 
51
- it "retrieves a stripe charge with an id that doesn't exist" do
52
- charge = Stripe::Charge.retrieve('test_charge_x')
53
- expect(charge.id).to eq('test_charge_x')
54
- expect(charge.amount).to_not be_nil
55
- expect(charge.card).to_not be_nil
51
+ it "cannot retrieve a charge that doesn't exist" do
52
+ expect { Stripe::Charge.retrieve('nope') }.to raise_error {|e|
53
+ expect(e).to be_a Stripe::InvalidRequestError
54
+ expect(e.param).to eq('charge')
55
+ expect(e.http_status).to eq(400)
56
+ }
56
57
  end
57
58
 
59
+
60
+ context "With strict mode toggled off" do
61
+
62
+ before { StripeMock.toggle_strict(false) }
63
+
64
+ it "retrieves a stripe charge with an id that doesn't exist" do
65
+ charge = Stripe::Charge.retrieve('test_charge_x')
66
+ expect(charge.id).to eq('test_charge_x')
67
+ expect(charge.amount).to_not be_nil
68
+ expect(charge.card).to_not be_nil
69
+ end
70
+ end
71
+
72
+
58
73
  describe 'captured status value' do
59
74
  it "reports captured by default" do
60
75
  charge = Stripe::Charge.create({
@@ -41,11 +41,12 @@ shared_examples 'Customer API' do
41
41
  expect(customer.email).to eq(original.email)
42
42
  end
43
43
 
44
- it "retrieves a stripe customer with an id that doesn't exist" do
45
- customer = Stripe::Customer.retrieve('test_customer_x')
46
- expect(customer.id).to eq('test_customer_x')
47
- expect(customer.email).to_not be_nil
48
- expect(customer.description).to_not be_nil
44
+ it "cannot retrieve a customer that doesn't exist" do
45
+ expect { Stripe::Customer.retrieve('nope') }.to raise_error {|e|
46
+ expect(e).to be_a Stripe::InvalidRequestError
47
+ expect(e.param).to eq('customer')
48
+ expect(e.http_status).to eq(400)
49
+ }
49
50
  end
50
51
 
51
52
  it "retrieves all customers" do
@@ -58,7 +59,7 @@ shared_examples 'Customer API' do
58
59
  end
59
60
 
60
61
  it "updates a stripe customer" do
61
- original = Stripe::Customer.retrieve("test_customer_update")
62
+ original = Stripe::Customer.create(id: 'test_customer_update')
62
63
  email = original.email
63
64
 
64
65
  original.description = 'new desc'
@@ -73,7 +74,8 @@ shared_examples 'Customer API' do
73
74
  end
74
75
 
75
76
  it "updates a stripe customer's subscription" do
76
- customer = Stripe::Customer.retrieve("test_customer_sub")
77
+ plan = Stripe::Plan.create(id: 'silver')
78
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
77
79
  sub = customer.update_subscription({ :plan => 'silver' })
78
80
 
79
81
  expect(sub.object).to eq('subscription')
@@ -81,10 +83,30 @@ shared_examples 'Customer API' do
81
83
  end
82
84
 
83
85
  it "cancels a stripe customer's subscription" do
84
- customer = Stripe::Customer.retrieve("test_customer_sub")
86
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
85
87
  sub = customer.cancel_subscription
86
88
 
87
89
  expect(sub.deleted).to eq(true)
88
90
  end
89
91
 
92
+ it "cannot reference a plan that does not exist" do
93
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
94
+ expect {
95
+ customer.update_subscription(plan: 'imagination')
96
+ }.to raise_error Stripe::InvalidRequestError
97
+ end
98
+
99
+
100
+ context "With strict mode toggled off" do
101
+
102
+ before { StripeMock.toggle_strict(false) }
103
+
104
+ it "retrieves a stripe customer with an id that doesn't exist" do
105
+ customer = Stripe::Customer.retrieve('test_customer_x')
106
+ expect(customer.id).to eq('test_customer_x')
107
+ expect(customer.email).to_not be_nil
108
+ expect(customer.description).to_not be_nil
109
+ end
110
+ end
111
+
90
112
  end
@@ -57,17 +57,28 @@ shared_examples 'Plan API' do
57
57
  end
58
58
 
59
59
 
60
- it "retrieves a stripe plan with an id that doesn't exist" do
61
- plan = Stripe::Plan.retrieve('test_charge_x')
60
+ it "updates a stripe plan" do
61
+ Stripe::Plan.create(id: 'super_member', amount: 111)
62
62
 
63
- expect(plan.id).to eq('test_charge_x')
64
- expect(plan.amount).to_not be_nil
65
- expect(plan.name).to_not be_nil
63
+ plan = Stripe::Plan.retrieve('super_member')
64
+ expect(plan.amount).to eq(111)
66
65
 
67
- expect(plan.currency).to_not be_nil
68
- expect(plan.interval).to_not be_nil
66
+ plan.amount = 789
67
+ plan.save
68
+ plan = Stripe::Plan.retrieve('super_member')
69
+ expect(plan.amount).to eq(789)
69
70
  end
70
71
 
72
+
73
+ it "cannot retrieve a stripe plan that doesn't exist" do
74
+ expect { Stripe::Plan.retrieve('nope') }.to raise_error {|e|
75
+ expect(e).to be_a Stripe::InvalidRequestError
76
+ expect(e.param).to eq('plan')
77
+ expect(e.http_status).to eq(400)
78
+ }
79
+ end
80
+
81
+
71
82
  it "retrieves all plans" do
72
83
  Stripe::Plan.create({ id: 'Plan One', amount: 54321 })
73
84
  Stripe::Plan.create({ id: 'Plan Two', amount: 98765 })
@@ -78,4 +89,21 @@ shared_examples 'Plan API' do
78
89
  all.map(&:amount).should include(54321, 98765)
79
90
  end
80
91
 
92
+
93
+ context "With strict mode toggled off" do
94
+
95
+ before { StripeMock.toggle_strict(false) }
96
+
97
+ it "can retrieve a stripe plan with an id that doesn't exist" do
98
+ plan = Stripe::Plan.retrieve('test_charge_x')
99
+
100
+ expect(plan.id).to eq('test_charge_x')
101
+ expect(plan.amount).to_not be_nil
102
+ expect(plan.name).to_not be_nil
103
+
104
+ expect(plan.currency).to_not be_nil
105
+ expect(plan.interval).to_not be_nil
106
+ end
107
+ end
108
+
81
109
  end
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.9
4
+ version: 1.8.3.10
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-06-21 00:00:00.000000000 Z
12
+ date: 2013-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe
@@ -111,6 +111,7 @@ files:
111
111
  - lib/stripe_mock/api/errors.rb
112
112
  - lib/stripe_mock/api/instance.rb
113
113
  - lib/stripe_mock/api/server.rb
114
+ - lib/stripe_mock/api/strict.rb
114
115
  - lib/stripe_mock/api/webhooks.rb
115
116
  - lib/stripe_mock/client.rb
116
117
  - lib/stripe_mock/data.rb