stripe-ruby-mock 1.8.3.7 → 1.8.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -165,6 +165,53 @@ you'll want to use the executable:
165
165
  $ stripe-mock-server -p 4000
166
166
  $ stripe-mock-server --help
167
167
 
168
+ ## Mocking Webhooks
169
+
170
+ If your application handles stripe webhooks, you are most likely retrieving the event from
171
+ stripe and passing the result to a handler. StripeMock helps you by easily mocking that event:
172
+
173
+ ```ruby
174
+ it "mocks a stripe webhook" do
175
+ event = StripeMock.mock_webhook_event('customer.created')
176
+
177
+ customer_object = event.data.object
178
+ expect(customer_object.id).to_not be_nil
179
+ expect(customer_object.active_card).to_not be_nil
180
+ # etc.
181
+ end
182
+ ```
183
+
184
+ ### Customizing Webhooks
185
+
186
+ By default, StripeMock searches in your `spec/fixtures/stripe_webhooks/` folder for your own, custom webhooks.
187
+ If it finds nothing, it falls back to [test events generated through stripe's webhooktester](lib/stripe_mock/webhook_fixtures/).
188
+
189
+ 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.
190
+
191
+ If you wish to use a different fixture path, you can call set it yourself:
192
+
193
+ StripeMock.webhook_fixture_path = './spec/other/folder/'
194
+
195
+ Also, you can override values whenever you create any webhook event:
196
+
197
+ ```ruby
198
+ it "can override default webhook values" do
199
+ event = StripeMock.mock_webhook_event('customer.created', {
200
+ :data => {
201
+ :object => {
202
+ :id => 'cus_my_custom_value',
203
+ :email => 'joe@example.com'
204
+ }
205
+ }
206
+ })
207
+ # Alternatively:
208
+ # event.data.object.id = 'cus_my_custome_value'
209
+ # event.data.object.email = 'joe@example.com'
210
+ expect(event.data.object.id).to eq('cus_my_custome_value')
211
+ expect(event.data.object.email).to eq('joe@example.com')
212
+ end
213
+ ```
214
+
168
215
  ## TODO
169
216
 
170
217
  * Cover all stripe urls/methods
@@ -1,14 +1,23 @@
1
1
  module StripeMock
2
2
 
3
3
  def self.mock_webhook_event(type, params={})
4
- unless Webhooks.event_list.include?(type)
5
- raise UnsupportedRequestError.new "Unsupported webhook event `#{type}`"
4
+
5
+ fixture_file = File.join(@webhook_fixture_path, "#{type}.json")
6
+
7
+ if File.exists?(fixture_file) == false
8
+ unless Webhooks.event_list.include?(type)
9
+ raise UnsupportedRequestError.new "Unsupported webhook event `#{type}` (Searched in #{@webhook_fixture_path})"
10
+ end
11
+ fixture_file = File.join(@webhook_fixture_fallback_path, "#{type}.json")
6
12
  end
7
- json = MultiJson.load File.read File.join(@webhook_fixture_path, "#{type}.json")
13
+
14
+ json = MultiJson.load File.read(fixture_file)
8
15
 
9
16
  json = Stripe::Util.symbolize_names(json)
10
17
  params = Stripe::Util.symbolize_names(params)
11
- Stripe::Event.construct_from Util.rmerge(json, params)
18
+ json[:data][:object] = Util.rmerge(json[:data][:object], params)
19
+
20
+ Stripe::Event.construct_from(json)
12
21
  end
13
22
 
14
23
  module Webhooks
@@ -20,4 +20,3 @@ module StripeMock
20
20
 
21
21
  end
22
22
  end
23
-
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.8.3.7"
3
+ VERSION = "1.8.3.8"
4
4
  end
data/lib/stripe_mock.rb CHANGED
@@ -31,6 +31,12 @@ require 'stripe_mock/request_handlers/plans.rb'
31
31
  require 'stripe_mock/instance'
32
32
 
33
33
  module StripeMock
34
+
34
35
  lib_dir = File.expand_path(File.dirname(__FILE__), '../..')
35
- @webhook_fixture_path = File.join(lib_dir, 'stripe_mock/webhook_fixtures')
36
+ @webhook_fixture_path = './spec/fixtures/stripe_webhooks/'
37
+ @webhook_fixture_fallback_path = File.join(lib_dir, 'stripe_mock/webhook_fixtures')
38
+
39
+ class << self
40
+ attr_accessor :webhook_fixture_path
41
+ end
36
42
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "val": "success",
3
+ "data": {
4
+ "object": {}
5
+ }
6
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "id": "evt_123",
3
+ "type": "account.updated",
4
+ "data": {
5
+ "object": {}
6
+ }
7
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "data": {
3
+ "object": {}
4
+ }
5
+ }
data/spec/readme_spec.rb CHANGED
@@ -55,4 +55,25 @@ describe 'README examples' do
55
55
  StripeMock.prepare_card_error(:processing_error)
56
56
  end
57
57
 
58
+ it "mocks a stripe webhook" do
59
+ event = StripeMock.mock_webhook_event('customer.created')
60
+
61
+ customer_object = event.data.object
62
+ expect(customer_object.id).to_not be_nil
63
+ expect(customer_object.active_card).to_not be_nil
64
+ # etc.
65
+ end
66
+
67
+ it "can override default webhook values" do
68
+ event = StripeMock.mock_webhook_event('customer.created', {
69
+ :id => 'cus_my_custom_value',
70
+ :email => 'joe@example.com'
71
+ })
72
+ # Alternatively:
73
+ # event.data.object.id = 'cus_my_custom_value'
74
+ # event.data.object.email = 'joe@example.com'
75
+ expect(event.data.object.id).to eq('cus_my_custom_value')
76
+ expect(event.data.object.email).to eq('joe@example.com')
77
+ end
78
+
58
79
  end
data/spec/webhook_spec.rb CHANGED
@@ -5,38 +5,54 @@ describe 'Webhook Generation' do
5
5
  it "matches the list of webhooks with the folder of fixtures" do
6
6
  events = StripeMock::Webhooks.event_list.to_set
7
7
  file_names = Dir['./lib/stripe_mock/webhook_fixtures/*'].map {|f| File.basename(f, '.json')}.to_set
8
+ # The reason we take the difference instead of compare equal is so
9
+ # that a missing event name will show up in the test failure report.
8
10
  expect(events - file_names).to eq(Set.new)
9
11
  expect(file_names - events).to eq(Set.new)
10
12
  end
11
13
 
14
+
15
+ it "first looks in spec/fixtures/stripe_webhooks/ for fixtures by default" do
16
+ event = StripeMock.mock_webhook_event('account.updated')
17
+ expect(event).to be_a(Stripe::Event)
18
+ expect(event.id).to eq('evt_123')
19
+ expect(event.type).to eq('account.updated')
20
+ end
21
+
22
+ it "allows non-standard event names in the project fixture folder" do
23
+ expect {
24
+ event = StripeMock.mock_webhook_event('custom.account.updated')
25
+ }.to_not raise_error
26
+ end
27
+
28
+ it "allows configuring the project fixture folder" do
29
+ StripeMock.webhook_fixture_path = './spec/_dummy/webhooks/'
30
+ expect(StripeMock.webhook_fixture_path).to eq('./spec/_dummy/webhooks/')
31
+
32
+ event = StripeMock.mock_webhook_event('dummy.event')
33
+ expect(event.val).to eq('success')
34
+ end
35
+
12
36
  it "generates an event" do
13
37
  event = StripeMock.mock_webhook_event('customer.created')
14
38
  expect(event).to be_a(Stripe::Event)
15
39
  end
16
40
 
17
- it "takes a hash and deep merges" do
41
+ it "takes a hash and deep merges into the data object" do
18
42
  event = StripeMock.mock_webhook_event('customer.created', {
19
- :data => {
20
- :object => {
21
- :account_balance => 12345
22
- }
23
- }
43
+ :account_balance => 12345
24
44
  })
25
45
  expect(event.data.object.account_balance).to eq(12345)
26
46
  end
27
47
 
28
- it "takes a hash and deep merges arrays" do
48
+ it "takes a hash and deep merges arrays in the data object" do
29
49
  event = StripeMock.mock_webhook_event('invoice.created', {
30
- :data => {
31
- :object => {
32
- :lines => {
33
- :data => [
34
- { :amount => 555,
35
- :plan => { :id => 'wh_test' }
36
- }
37
- ]
50
+ :lines => {
51
+ :data => [
52
+ { :amount => 555,
53
+ :plan => { :id => 'wh_test' }
38
54
  }
39
- }
55
+ ]
40
56
  }
41
57
  })
42
58
  expect(event.data.object.lines.data.first.amount).to eq(555)
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.7
4
+ version: 1.8.3.8
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-19 00:00:00.000000000 Z
12
+ date: 2013-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe
@@ -162,6 +162,9 @@ files:
162
162
  - lib/stripe_mock/webhook_fixtures/transfer.paid.json
163
163
  - lib/stripe_mock/webhook_fixtures/transfer.updated.json
164
164
  - lib/trollop.rb
165
+ - spec/_dummy/webhooks/dummy.event.json
166
+ - spec/fixtures/stripe_webhooks/account.updated.json
167
+ - spec/fixtures/stripe_webhooks/custom.account.updated.json
165
168
  - spec/instance_spec.rb
166
169
  - spec/readme_spec.rb
167
170
  - spec/server_spec.rb
@@ -202,6 +205,9 @@ signing_key:
202
205
  specification_version: 3
203
206
  summary: TDD with stripe
204
207
  test_files:
208
+ - spec/_dummy/webhooks/dummy.event.json
209
+ - spec/fixtures/stripe_webhooks/account.updated.json
210
+ - spec/fixtures/stripe_webhooks/custom.account.updated.json
205
211
  - spec/instance_spec.rb
206
212
  - spec/readme_spec.rb
207
213
  - spec/server_spec.rb