stripe-ruby-mock 1.8.3.10 → 1.8.3.11
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 +30 -27
- data/lib/stripe_mock.rb +1 -0
- data/lib/stripe_mock/api/client.rb +6 -4
- data/lib/stripe_mock/api/errors.rb +7 -4
- data/lib/stripe_mock/client.rb +2 -1
- data/lib/stripe_mock/error_queue.rb +23 -0
- data/lib/stripe_mock/instance.rb +13 -10
- data/lib/stripe_mock/version.rb +1 -1
- data/spec/readme_spec.rb +6 -22
- data/spec/shared_stripe_examples/error_mocks.rb +19 -4
- metadata +2 -1
data/README.md
CHANGED
@@ -42,39 +42,20 @@ describe MyApp do
|
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
|
-
## Mocking Errors
|
45
|
+
## Mocking Card Errors
|
46
46
|
|
47
47
|
Tired of manually inputting fake credit card numbers to test against errors? Tire no more!
|
48
48
|
|
49
49
|
```ruby
|
50
50
|
it "mocks a declined card error" do
|
51
|
-
# Prepares an error for the next
|
51
|
+
# Prepares an error for the next create charge request
|
52
52
|
StripeMock.prepare_card_error(:card_declined)
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
expect(error.code).to eq('card_declined')
|
60
|
-
end
|
61
|
-
end
|
62
|
-
```
|
63
|
-
|
64
|
-
You can also set your own custom Stripe error using `prepare_error`:
|
65
|
-
|
66
|
-
```ruby
|
67
|
-
it "raises a custom error" do
|
68
|
-
custom_error = Stripe::AuthenticationError.new('Did not provide favourite colour', 400)
|
69
|
-
StripeMock.prepare_error(custom_error)
|
70
|
-
|
71
|
-
begin
|
72
|
-
# Note: The next request of ANY type will raise your prepared error
|
73
|
-
Stripe::Invoice.create()
|
74
|
-
rescue Stripe::AuthenticationError => error
|
75
|
-
expect(error.http_status).to eq(400)
|
76
|
-
expect(error.message).to eq('Did not provide favourite colour')
|
77
|
-
end
|
54
|
+
expect { Stripe::Charge.create }.to raise_error {|e|
|
55
|
+
expect(e).to be_a Stripe::CardError
|
56
|
+
expect(e.http_status).to eq(402)
|
57
|
+
expect(e.code).to eq('card_declined')
|
58
|
+
}
|
78
59
|
end
|
79
60
|
```
|
80
61
|
|
@@ -95,6 +76,28 @@ StripeMock.prepare_card_error(:processing_error)
|
|
95
76
|
|
96
77
|
You can see the details of each error in [lib/stripe_mock/api/errors.rb](lib/stripe_mock/api/errors.rb)
|
97
78
|
|
79
|
+
### Custom Errors
|
80
|
+
|
81
|
+
To raise an error on a specific type of request, take a look at the [request handlers folder](lib/stripe_mock/request_handlers/) and pass a method name to `StripeMock.prepare_error`.
|
82
|
+
|
83
|
+
If you wanted to raise an error for creating a new customer, for instance, you would do the following:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
it "raises a custom error for specific actions" do
|
87
|
+
custom_error = StandardError.new("Please knock first.")
|
88
|
+
|
89
|
+
StripeMock.prepare_error(custom_error, :new_customer)
|
90
|
+
|
91
|
+
expect { Stripe::Charge.create }.to_not raise_error
|
92
|
+
expect { Stripe::Customer.create }.to raise_error {|e|
|
93
|
+
expect(e).to be_a StandardError
|
94
|
+
expect(e.message).to eq("Please knock first.")
|
95
|
+
}
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
In the above example, `:new_customer` is the name of a method from [customers.rb](lib/stripe_mock/request_handlers/customers.rb).
|
100
|
+
|
98
101
|
## Running the Mock Server
|
99
102
|
|
100
103
|
Sometimes you want your test stripe data to persist for a bit, such as during integration tests
|
@@ -186,7 +189,7 @@ If it finds nothing, it falls back to [test events generated through stripe's we
|
|
186
189
|
|
187
190
|
You can name events whatever you like in your `spec/fixtures/stripe_webhooks/` folder. However, if you try to call a non-existant event that's not in that folder, StripeMock will throw an error.
|
188
191
|
|
189
|
-
If you wish to use a different fixture path, you can
|
192
|
+
If you wish to use a different fixture path, you can set it yourself:
|
190
193
|
|
191
194
|
StripeMock.webhook_fixture_path = './spec/other/folder/'
|
192
195
|
|
data/lib/stripe_mock.rb
CHANGED
@@ -25,11 +25,13 @@ module StripeMock
|
|
25
25
|
private
|
26
26
|
|
27
27
|
def self.redirect_to_mock_server(method, url, api_key, params={}, headers={})
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
handler = Instance.handler_for_method_url("#{method} #{url}")
|
29
|
+
mock_error = client.error_queue.error_for_handler_name(handler[:name])
|
30
|
+
if mock_error
|
31
|
+
client.error_queue.dequeue
|
32
|
+
raise mock_error
|
31
33
|
end
|
32
|
-
Stripe::Util.symbolize_names
|
34
|
+
Stripe::Util.symbolize_names client.mock_request(method, url, api_key, params, headers)
|
33
35
|
end
|
34
36
|
|
35
37
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module StripeMock
|
2
2
|
|
3
|
-
def self.prepare_error(stripe_error)
|
3
|
+
def self.prepare_error(stripe_error, *handler_names)
|
4
|
+
handler_names.push(:all) if handler_names.count == 0
|
5
|
+
|
4
6
|
if @state == 'local'
|
5
|
-
instance.
|
7
|
+
instance.error_queue.queue(stripe_error, handler_names)
|
6
8
|
elsif @state == 'remote'
|
7
|
-
|
9
|
+
client.error_queue.queue(stripe_error, handler_names)
|
8
10
|
else
|
9
11
|
raise UnstartedStateError
|
10
12
|
end
|
@@ -13,10 +15,11 @@ module StripeMock
|
|
13
15
|
def self.prepare_card_error(code)
|
14
16
|
args = CardErrors.argument_map[code]
|
15
17
|
raise StripeMockError.new("Unrecognized stripe card error code: #{code}") if args.nil?
|
16
|
-
self.prepare_error Stripe::CardError.new(*args)
|
18
|
+
self.prepare_error Stripe::CardError.new(*args), :new_charge
|
17
19
|
end
|
18
20
|
|
19
21
|
module CardErrors
|
22
|
+
|
20
23
|
def self.argument_map
|
21
24
|
@__map ||= {
|
22
25
|
incorrect_number: ["The card number is incorrect", 'number', 'incorrect_number', 402],
|
data/lib/stripe_mock/client.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module StripeMock
|
2
2
|
|
3
3
|
class Client
|
4
|
-
attr_reader :port, :state
|
4
|
+
attr_reader :port, :state, :error_queue
|
5
5
|
|
6
6
|
def initialize(port)
|
7
7
|
@port = port
|
@@ -9,6 +9,7 @@ module StripeMock
|
|
9
9
|
# Ensure client can connect to server
|
10
10
|
timeout_wrap { @pipe.ping }
|
11
11
|
@state = 'ready'
|
12
|
+
@error_queue = ErrorQueue.new
|
12
13
|
end
|
13
14
|
|
14
15
|
def mock_request(method, url, api_key, params={}, headers={})
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module StripeMock
|
2
|
+
class ErrorQueue
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@queue = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def queue(error, handler_names)
|
9
|
+
@queue << handler_names.map {|n| [n, error]}
|
10
|
+
end
|
11
|
+
|
12
|
+
def error_for_handler_name(handler_name)
|
13
|
+
return nil if @queue.count == 0
|
14
|
+
triggers = @queue.first
|
15
|
+
(triggers.assoc(:all) || triggers.assoc(handler_name) || [])[1]
|
16
|
+
end
|
17
|
+
|
18
|
+
def dequeue
|
19
|
+
@queue.shift
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/stripe_mock/instance.rb
CHANGED
@@ -11,14 +11,18 @@ module StripeMock
|
|
11
11
|
}
|
12
12
|
end
|
13
13
|
|
14
|
+
def self.handler_for_method_url(method_url)
|
15
|
+
@@handlers.find {|h| method_url =~ h[:route] }
|
16
|
+
end
|
17
|
+
|
14
18
|
include StripeMock::RequestHandlers::Charges
|
15
19
|
include StripeMock::RequestHandlers::Customers
|
16
20
|
include StripeMock::RequestHandlers::InvoiceItems
|
17
21
|
include StripeMock::RequestHandlers::Plans
|
18
22
|
|
19
23
|
|
20
|
-
attr_reader :charges, :customers, :plans
|
21
|
-
attr_accessor :
|
24
|
+
attr_reader :charges, :customers, :plans, :error_queue
|
25
|
+
attr_accessor :debug, :strict
|
22
26
|
|
23
27
|
def initialize
|
24
28
|
@customers = {}
|
@@ -26,7 +30,7 @@ module StripeMock
|
|
26
30
|
@plans = {}
|
27
31
|
|
28
32
|
@id_counter = 0
|
29
|
-
@
|
33
|
+
@error_queue = ErrorQueue.new
|
30
34
|
@debug = false
|
31
35
|
@strict = true
|
32
36
|
end
|
@@ -42,15 +46,14 @@ module StripeMock
|
|
42
46
|
puts " #{params}"
|
43
47
|
end
|
44
48
|
|
45
|
-
if @pending_error
|
46
|
-
raise @pending_error
|
47
|
-
@pending_error = nil
|
48
|
-
end
|
49
|
-
|
50
49
|
method_url = "#{method} #{url}"
|
51
|
-
handler =
|
50
|
+
handler = Instance.handler_for_method_url(method_url)
|
51
|
+
mock_error = @error_queue.error_for_handler_name(handler[:name])
|
52
52
|
|
53
|
-
if handler
|
53
|
+
if handler && mock_error
|
54
|
+
@error_queue.dequeue
|
55
|
+
raise mock_error
|
56
|
+
elsif handler
|
54
57
|
res = self.send(handler[:name], handler[:route], method_url, params, headers)
|
55
58
|
puts "[StripeMock res] #{res}" if @debug == true
|
56
59
|
[res, api_key]
|
data/lib/stripe_mock/version.rb
CHANGED
data/spec/readme_spec.rb
CHANGED
@@ -16,30 +16,14 @@ describe 'README examples' do
|
|
16
16
|
|
17
17
|
|
18
18
|
it "mocks a declined card error" do
|
19
|
-
# Prepares an error for the next
|
19
|
+
# Prepares an error for the next create charge request
|
20
20
|
StripeMock.prepare_card_error(:card_declined)
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
expect(error.code).to eq('card_declined')
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
it "raises a custom error" do
|
33
|
-
custom_error = Stripe::AuthenticationError.new('Did not provide favourite colour', 400)
|
34
|
-
StripeMock.prepare_error(custom_error)
|
35
|
-
|
36
|
-
begin
|
37
|
-
# Note: The next request of ANY type will raise your prepared error
|
38
|
-
Stripe::Invoice.create()
|
39
|
-
rescue Stripe::AuthenticationError => error
|
40
|
-
expect(error.http_status).to eq(400)
|
41
|
-
expect(error.message).to eq('Did not provide favourite colour')
|
42
|
-
end
|
22
|
+
expect { Stripe::Charge.create }.to raise_error {|e|
|
23
|
+
expect(e).to be_a Stripe::CardError
|
24
|
+
expect(e.http_status).to eq(402)
|
25
|
+
expect(e.code).to eq('card_declined')
|
26
|
+
}
|
43
27
|
end
|
44
28
|
|
45
29
|
it "has built-in card errors" do
|
@@ -11,8 +11,7 @@ end
|
|
11
11
|
|
12
12
|
shared_examples 'Stripe Error Mocking' do
|
13
13
|
|
14
|
-
it "mocks a manually
|
15
|
-
|
14
|
+
it "mocks a manually given stripe card error" do
|
16
15
|
error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', 444, 'body', 'json body')
|
17
16
|
StripeMock.prepare_error(error)
|
18
17
|
|
@@ -47,11 +46,10 @@ shared_examples 'Stripe Error Mocking' do
|
|
47
46
|
|
48
47
|
|
49
48
|
it "mocks a manually gives stripe invalid auth error" do
|
50
|
-
|
51
49
|
error = Stripe::AuthenticationError.new('Bad Auth', 499, 'abody', 'json abody')
|
52
50
|
StripeMock.prepare_error(error)
|
53
51
|
|
54
|
-
expect { Stripe::
|
52
|
+
expect { Stripe::Plan.create() }.to raise_error {|e|
|
55
53
|
expect(e).to be_a(Stripe::AuthenticationError)
|
56
54
|
expect(e.message).to eq('Bad Auth')
|
57
55
|
|
@@ -62,6 +60,17 @@ shared_examples 'Stripe Error Mocking' do
|
|
62
60
|
end
|
63
61
|
|
64
62
|
|
63
|
+
it "raises a custom error for specific actions" do
|
64
|
+
custom_error = StandardError.new("Please knock first.")
|
65
|
+
StripeMock.prepare_error(custom_error, :new_customer)
|
66
|
+
|
67
|
+
expect { Stripe::Charge.create }.to_not raise_error
|
68
|
+
expect { Stripe::Customer.create }.to raise_error {|e|
|
69
|
+
expect(e).to be_a StandardError
|
70
|
+
expect(e.message).to eq("Please knock first.")
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
65
74
|
# # # # # # # # # # # # # #
|
66
75
|
# Card Error Helper Methods
|
67
76
|
# # # # # # # # # # # # # #
|
@@ -72,6 +81,12 @@ shared_examples 'Stripe Error Mocking' do
|
|
72
81
|
}
|
73
82
|
end
|
74
83
|
|
84
|
+
it "only raises a card error when a card charge is attempted" do
|
85
|
+
StripeMock.prepare_card_error(:card_declined)
|
86
|
+
expect { Stripe::Customer.create(id: 'x') }.to_not raise_error
|
87
|
+
expect { Stripe::Charge.create() }.to raise_error Stripe::CardError
|
88
|
+
end
|
89
|
+
|
75
90
|
it "mocks an incorrect number card error" do
|
76
91
|
StripeMock.prepare_card_error(:incorrect_number)
|
77
92
|
expect_card_error 'incorrect_number', 'number'
|
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.
|
4
|
+
version: 1.8.3.11
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- lib/stripe_mock/api/webhooks.rb
|
116
116
|
- lib/stripe_mock/client.rb
|
117
117
|
- lib/stripe_mock/data.rb
|
118
|
+
- lib/stripe_mock/error_queue.rb
|
118
119
|
- lib/stripe_mock/errors/closed_client_connection_error.rb
|
119
120
|
- lib/stripe_mock/errors/server_timeout_error.rb
|
120
121
|
- lib/stripe_mock/errors/stripe_mock_error.rb
|