mustwin-stripe-ruby-mock 1.8.4.10
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/.gitignore +5 -0
- data/.rspec +1 -0
- data/.travis.yml +5 -0
- data/ChangeLog.rdoc +4 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +264 -0
- data/Rakefile +28 -0
- data/bin/stripe-mock-server +19 -0
- data/lib/stripe_mock.rb +46 -0
- data/lib/stripe_mock/api/card_tokens.rb +22 -0
- data/lib/stripe_mock/api/client.rb +37 -0
- data/lib/stripe_mock/api/debug.rb +11 -0
- data/lib/stripe_mock/api/errors.rb +41 -0
- data/lib/stripe_mock/api/instance.rb +27 -0
- data/lib/stripe_mock/api/server.rb +24 -0
- data/lib/stripe_mock/api/strict.rb +11 -0
- data/lib/stripe_mock/api/webhooks.rb +64 -0
- data/lib/stripe_mock/client.rb +84 -0
- data/lib/stripe_mock/data.rb +317 -0
- data/lib/stripe_mock/error_queue.rb +23 -0
- data/lib/stripe_mock/errors/closed_client_connection_error.rb +9 -0
- data/lib/stripe_mock/errors/server_timeout_error.rb +12 -0
- data/lib/stripe_mock/errors/stripe_mock_error.rb +15 -0
- data/lib/stripe_mock/errors/uninitialized_instance_error.rb +9 -0
- data/lib/stripe_mock/errors/unstarted_state_error.rb +9 -0
- data/lib/stripe_mock/errors/unsupported_request_error.rb +4 -0
- data/lib/stripe_mock/instance.rb +108 -0
- data/lib/stripe_mock/request_handlers/charges.rb +33 -0
- data/lib/stripe_mock/request_handlers/customers.rb +107 -0
- data/lib/stripe_mock/request_handlers/invoice_items.rb +15 -0
- data/lib/stripe_mock/request_handlers/plans.rb +43 -0
- data/lib/stripe_mock/server.rb +58 -0
- data/lib/stripe_mock/util.rb +22 -0
- data/lib/stripe_mock/version.rb +4 -0
- data/lib/stripe_mock/webhook_fixtures/account.application.deauthorized.json +12 -0
- data/lib/stripe_mock/webhook_fixtures/account.updated.json +24 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.closed.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.updated.json +24 -0
- data/lib/stripe_mock/webhook_fixtures/charge.failed.json +57 -0
- data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +57 -0
- data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +57 -0
- data/lib/stripe_mock/webhook_fixtures/coupon.created.json +22 -0
- data/lib/stripe_mock/webhook_fixtures/coupon.deleted.json +22 -0
- data/lib/stripe_mock/webhook_fixtures/customer.created.json +40 -0
- data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +40 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.created.json +28 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.deleted.json +28 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.updated.json +43 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +34 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +34 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +34 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +47 -0
- data/lib/stripe_mock/webhook_fixtures/customer.updated.json +43 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.created.json +64 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +64 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +64 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +67 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.deleted.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +24 -0
- data/lib/stripe_mock/webhook_fixtures/plan.created.json +20 -0
- data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +20 -0
- data/lib/stripe_mock/webhook_fixtures/plan.updated.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.created.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.failed.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.paid.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.updated.json +26 -0
- data/lib/trollop.rb +782 -0
- data/spec/_dummy/webhooks/dummy.event.json +6 -0
- data/spec/fixtures/stripe_webhooks/account.updated.json +7 -0
- data/spec/fixtures/stripe_webhooks/custom.account.updated.json +5 -0
- data/spec/instance_spec.rb +49 -0
- data/spec/readme_spec.rb +72 -0
- data/spec/server_spec.rb +131 -0
- data/spec/shared_stripe_examples/card_token_examples.rb +28 -0
- data/spec/shared_stripe_examples/charge_examples.rb +123 -0
- data/spec/shared_stripe_examples/customer_examples.rb +218 -0
- data/spec/shared_stripe_examples/error_mock_examples.rb +152 -0
- data/spec/shared_stripe_examples/invoice_item_examples.rb +17 -0
- data/spec/shared_stripe_examples/plan_examples.rb +123 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/stripe_mock_spec.rb +40 -0
- data/spec/support/stripe_examples.rb +18 -0
- data/spec/util_spec.rb +45 -0
- data/spec/webhook_spec.rb +77 -0
- data/stripe-ruby-mock.gemspec +27 -0
- metadata +253 -0
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
def expect_card_error(code, param)
|
4
|
+
expect { Stripe::Charge.create() }.to raise_error {|e|
|
5
|
+
expect(e).to be_a(Stripe::CardError)
|
6
|
+
expect(e.http_status).to eq(402)
|
7
|
+
expect(e.code).to eq(code)
|
8
|
+
expect(e.param).to eq(param)
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
shared_examples 'Stripe Error Mocking' do
|
13
|
+
|
14
|
+
it "mocks a manually given stripe card error" do
|
15
|
+
error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', 444, 'body', 'json body')
|
16
|
+
StripeMock.prepare_error(error)
|
17
|
+
|
18
|
+
expect { Stripe::Customer.create() }.to raise_error {|e|
|
19
|
+
expect(e).to be_a(Stripe::CardError)
|
20
|
+
expect(e.code).to eq('bad_code')
|
21
|
+
expect(e.param).to eq('param_name')
|
22
|
+
expect(e.message).to eq('Test Msg')
|
23
|
+
|
24
|
+
expect(e.http_status).to eq(444)
|
25
|
+
expect(e.http_body).to eq('body')
|
26
|
+
expect(e.json_body).to eq('json body')
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
it "mocks a manually gives stripe invalid request error" do
|
32
|
+
|
33
|
+
error = Stripe::InvalidRequestError.new('Test Invalid', 'param', 987, 'ibody', 'json ibody')
|
34
|
+
StripeMock.prepare_error(error)
|
35
|
+
|
36
|
+
expect { Stripe::Charge.create() }.to raise_error {|e|
|
37
|
+
expect(e).to be_a(Stripe::InvalidRequestError)
|
38
|
+
expect(e.param).to eq('param')
|
39
|
+
expect(e.message).to eq('Test Invalid')
|
40
|
+
|
41
|
+
expect(e.http_status).to eq(987)
|
42
|
+
expect(e.http_body).to eq('ibody')
|
43
|
+
expect(e.json_body).to eq('json ibody')
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
it "mocks a manually gives stripe invalid auth error" do
|
49
|
+
error = Stripe::AuthenticationError.new('Bad Auth', 499, 'abody', 'json abody')
|
50
|
+
StripeMock.prepare_error(error)
|
51
|
+
|
52
|
+
expect { Stripe::Plan.create() }.to raise_error {|e|
|
53
|
+
expect(e).to be_a(Stripe::AuthenticationError)
|
54
|
+
expect(e.message).to eq('Bad Auth')
|
55
|
+
|
56
|
+
expect(e.http_status).to eq(499)
|
57
|
+
expect(e.http_body).to eq('abody')
|
58
|
+
expect(e.json_body).to eq('json abody')
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
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
|
+
|
74
|
+
# # # # # # # # # # # # # #
|
75
|
+
# Card Error Helper Methods
|
76
|
+
# # # # # # # # # # # # # #
|
77
|
+
|
78
|
+
it "raises an error for an unrecognized card error code" do
|
79
|
+
expect { StripeMock.prepare_card_error(:non_existant_error_code) }.to raise_error {|e|
|
80
|
+
expect(e).to be_a(StripeMock::StripeMockError)
|
81
|
+
}
|
82
|
+
end
|
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
|
+
|
90
|
+
it "mocks a card error with a given handler" do
|
91
|
+
StripeMock.prepare_card_error(:incorrect_cvc, :new_customer)
|
92
|
+
expect { Stripe::Charge.create() }.to_not raise_error
|
93
|
+
|
94
|
+
expect { Stripe::Customer.create() }.to raise_error {|e|
|
95
|
+
expect(e).to be_a(Stripe::CardError)
|
96
|
+
expect(e.http_status).to eq(402)
|
97
|
+
expect(e.code).to eq('incorrect_cvc')
|
98
|
+
expect(e.param).to eq('cvc')
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
it "mocks an incorrect number card error" do
|
103
|
+
StripeMock.prepare_card_error(:incorrect_number)
|
104
|
+
expect_card_error 'incorrect_number', 'number'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "mocks an invalid number card error" do
|
108
|
+
StripeMock.prepare_card_error(:invalid_number)
|
109
|
+
expect_card_error 'invalid_number', 'number'
|
110
|
+
end
|
111
|
+
|
112
|
+
it "mocks an invalid expiration month card error" do
|
113
|
+
StripeMock.prepare_card_error(:invalid_expiry_month)
|
114
|
+
expect_card_error 'invalid_expiry_month', 'exp_month'
|
115
|
+
end
|
116
|
+
|
117
|
+
it "mocks an invalid expiration year card error" do
|
118
|
+
StripeMock.prepare_card_error(:invalid_expiry_year)
|
119
|
+
expect_card_error 'invalid_expiry_year', 'exp_year'
|
120
|
+
end
|
121
|
+
|
122
|
+
it "mocks an invalid cvc card error" do
|
123
|
+
StripeMock.prepare_card_error(:invalid_cvc)
|
124
|
+
expect_card_error 'invalid_cvc', 'cvc'
|
125
|
+
end
|
126
|
+
|
127
|
+
it "mocks an expired card error" do
|
128
|
+
StripeMock.prepare_card_error(:expired_card)
|
129
|
+
expect_card_error 'expired_card', 'exp_month'
|
130
|
+
end
|
131
|
+
|
132
|
+
it "mocks an incorrect cvc card error" do
|
133
|
+
StripeMock.prepare_card_error(:incorrect_cvc)
|
134
|
+
expect_card_error 'incorrect_cvc', 'cvc'
|
135
|
+
end
|
136
|
+
|
137
|
+
it "mocks a declined card error" do
|
138
|
+
StripeMock.prepare_card_error(:card_declined)
|
139
|
+
expect_card_error 'card_declined', nil
|
140
|
+
end
|
141
|
+
|
142
|
+
it "mocks a missing card error" do
|
143
|
+
StripeMock.prepare_card_error(:missing)
|
144
|
+
expect_card_error 'missing', nil
|
145
|
+
end
|
146
|
+
|
147
|
+
it "mocks a processing error card error" do
|
148
|
+
StripeMock.prepare_card_error(:processing_error)
|
149
|
+
expect_card_error 'processing_error', nil
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples 'Invoice Item API' do
|
4
|
+
|
5
|
+
it "creates a stripe invoice item" do
|
6
|
+
invoice_item = Stripe::InvoiceItem.create({
|
7
|
+
amount: 1099,
|
8
|
+
customer: 1234,
|
9
|
+
currency: 'USD',
|
10
|
+
description: "invoice item desc"
|
11
|
+
}, 'abcde')
|
12
|
+
|
13
|
+
expect(invoice_item.amount).to eq(1099)
|
14
|
+
expect(invoice_item.description).to eq('invoice item desc')
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples 'Plan API' do
|
4
|
+
|
5
|
+
it "creates a stripe plan" do
|
6
|
+
plan = Stripe::Plan.create(
|
7
|
+
:id => 'pid_1',
|
8
|
+
:name => 'The Mock Plan',
|
9
|
+
:amount => 9900,
|
10
|
+
:currency => 'USD',
|
11
|
+
:interval => 1,
|
12
|
+
:trial_period_days => 30
|
13
|
+
)
|
14
|
+
|
15
|
+
expect(plan.id).to eq('pid_1')
|
16
|
+
expect(plan.name).to eq('The Mock Plan')
|
17
|
+
expect(plan.amount).to eq(9900)
|
18
|
+
|
19
|
+
expect(plan.currency).to eq('USD')
|
20
|
+
expect(plan.interval).to eq(1)
|
21
|
+
expect(plan.trial_period_days).to eq(30)
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
it "stores a created stripe plan in memory" do
|
26
|
+
plan = Stripe::Plan.create(
|
27
|
+
:id => 'pid_2',
|
28
|
+
:name => 'The Memory Plan',
|
29
|
+
:amount => 1100,
|
30
|
+
:currency => 'USD',
|
31
|
+
:interval => 1
|
32
|
+
)
|
33
|
+
plan2 = Stripe::Plan.create(
|
34
|
+
:id => 'pid_3',
|
35
|
+
:name => 'The Bonk Plan',
|
36
|
+
:amount => 7777,
|
37
|
+
:currency => 'USD',
|
38
|
+
:interval => 1
|
39
|
+
)
|
40
|
+
data = test_data_source(:plans)
|
41
|
+
expect(data[plan.id]).to_not be_nil
|
42
|
+
expect(data[plan.id][:amount]).to eq(1100)
|
43
|
+
|
44
|
+
expect(data[plan2.id]).to_not be_nil
|
45
|
+
expect(data[plan2.id][:amount]).to eq(7777)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
it "retrieves a stripe plan" do
|
50
|
+
original = Stripe::Plan.create({
|
51
|
+
amount: 1331
|
52
|
+
})
|
53
|
+
plan = Stripe::Plan.retrieve(original.id)
|
54
|
+
|
55
|
+
expect(plan.id).to eq(original.id)
|
56
|
+
expect(plan.amount).to eq(original.amount)
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
it "updates a stripe plan" do
|
61
|
+
Stripe::Plan.create(id: 'super_member', amount: 111)
|
62
|
+
|
63
|
+
plan = Stripe::Plan.retrieve('super_member')
|
64
|
+
expect(plan.amount).to eq(111)
|
65
|
+
|
66
|
+
plan.amount = 789
|
67
|
+
plan.save
|
68
|
+
plan = Stripe::Plan.retrieve('super_member')
|
69
|
+
expect(plan.amount).to eq(789)
|
70
|
+
end
|
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(404)
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
it "deletes a stripe plan" do
|
82
|
+
Stripe::Plan.create(id: 'super_member', amount: 111)
|
83
|
+
|
84
|
+
plan = Stripe::Plan.retrieve('super_member')
|
85
|
+
expect(plan).to_not be_nil
|
86
|
+
|
87
|
+
plan.delete
|
88
|
+
|
89
|
+
expect { Stripe::Plan.retrieve('super_member') }.to raise_error {|e|
|
90
|
+
expect(e).to be_a Stripe::InvalidRequestError
|
91
|
+
expect(e.param).to eq('plan')
|
92
|
+
expect(e.http_status).to eq(404)
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
it "retrieves all plans" do
|
97
|
+
Stripe::Plan.create({ id: 'Plan One', amount: 54321 })
|
98
|
+
Stripe::Plan.create({ id: 'Plan Two', amount: 98765 })
|
99
|
+
|
100
|
+
all = Stripe::Plan.all
|
101
|
+
expect(all.length).to eq(2)
|
102
|
+
all.map(&:id).should include('Plan One', 'Plan Two')
|
103
|
+
all.map(&:amount).should include(54321, 98765)
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
context "With strict mode toggled off" do
|
108
|
+
|
109
|
+
before { StripeMock.toggle_strict(false) }
|
110
|
+
|
111
|
+
it "can retrieve a stripe plan with an id that doesn't exist" do
|
112
|
+
plan = Stripe::Plan.retrieve('test_charge_x')
|
113
|
+
|
114
|
+
expect(plan.id).to eq('test_charge_x')
|
115
|
+
expect(plan.amount).to_not be_nil
|
116
|
+
expect(plan.name).to_not be_nil
|
117
|
+
|
118
|
+
expect(plan.currency).to_not be_nil
|
119
|
+
expect(plan.interval).to_not be_nil
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
gem 'rspec', '~> 2.4'
|
4
|
+
require 'rspec'
|
5
|
+
require 'stripe'
|
6
|
+
require 'stripe_mock'
|
7
|
+
require 'stripe_mock/server'
|
8
|
+
|
9
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
10
|
+
# in spec/support/ and its subdirectories.
|
11
|
+
Dir["./spec/support/**/*.rb"].each {|f| require f}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StripeMock do
|
4
|
+
|
5
|
+
it "overrides stripe's request method" do
|
6
|
+
StripeMock.start
|
7
|
+
Stripe.request(:xtest, '/', 'abcde') # no error
|
8
|
+
StripeMock.stop
|
9
|
+
end
|
10
|
+
|
11
|
+
it "reverts overriding stripe's request method" do
|
12
|
+
StripeMock.start
|
13
|
+
Stripe.request(:xtest, '/', 'abcde') # no error
|
14
|
+
StripeMock.stop
|
15
|
+
expect { Stripe.request(:x, '/', 'abcde') }.to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it "does not persist data between mock sessions" do
|
19
|
+
StripeMock.start
|
20
|
+
StripeMock.instance.customers[:x] = 9
|
21
|
+
|
22
|
+
StripeMock.stop
|
23
|
+
StripeMock.start
|
24
|
+
|
25
|
+
expect(StripeMock.instance.customers[:x]).to be_nil
|
26
|
+
expect(StripeMock.instance.customers.keys.length).to eq(0)
|
27
|
+
StripeMock.stop
|
28
|
+
end
|
29
|
+
|
30
|
+
it "throws an error when trying to prepare an error before starting" do
|
31
|
+
expect { StripeMock.prepare_error(StandardError.new) }.to raise_error {|e|
|
32
|
+
expect(e).to be_a(StripeMock::UnstartedStateError)
|
33
|
+
}
|
34
|
+
|
35
|
+
expect { StripeMock.prepare_card_error(:card_declined) }.to raise_error {|e|
|
36
|
+
expect(e).to be_a(StripeMock::UnstartedStateError)
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
def require_stripe_examples
|
3
|
+
require 'shared_stripe_examples/card_token_examples'
|
4
|
+
require 'shared_stripe_examples/charge_examples'
|
5
|
+
require 'shared_stripe_examples/customer_examples'
|
6
|
+
require 'shared_stripe_examples/error_mock_examples'
|
7
|
+
require 'shared_stripe_examples/invoice_item_examples'
|
8
|
+
require 'shared_stripe_examples/plan_examples'
|
9
|
+
end
|
10
|
+
|
11
|
+
def it_behaves_like_stripe(&block)
|
12
|
+
it_behaves_like 'Card Token Mocking', &block
|
13
|
+
it_behaves_like 'Charge API', &block
|
14
|
+
it_behaves_like 'Customer API', &block
|
15
|
+
it_behaves_like 'Invoice Item API', &block
|
16
|
+
it_behaves_like 'Plan API', &block
|
17
|
+
it_behaves_like 'Stripe Error Mocking', &block
|
18
|
+
end
|
data/spec/util_spec.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StripeMock::Util do
|
4
|
+
|
5
|
+
it "recursively merges a simple hash" do
|
6
|
+
dest = { x: { y: 50 }, a: 5, b: 3 }
|
7
|
+
source = { x: { y: 999 }, a: 77 }
|
8
|
+
result = StripeMock::Util.rmerge(dest, source)
|
9
|
+
|
10
|
+
expect(result).to eq({ x: { y: 999 }, a: 77, b: 3 })
|
11
|
+
end
|
12
|
+
|
13
|
+
it "recursively merges a nested hash" do
|
14
|
+
dest = { x: { y: 50, z: { m: 44, n: 4 } } }
|
15
|
+
source = { x: { y: 999, z: { n: 55 } } }
|
16
|
+
result = StripeMock::Util.rmerge(dest, source)
|
17
|
+
|
18
|
+
expect(result).to eq({ x: { y: 999, z: { m: 44, n: 55 } } })
|
19
|
+
end
|
20
|
+
|
21
|
+
it "merges array elements" do
|
22
|
+
dest = { x: [ {a: 1}, {b: 2}, {c: 3} ] }
|
23
|
+
source = { x: [ {a: 0}, {a: 0} ] }
|
24
|
+
result = StripeMock::Util.rmerge(dest, source)
|
25
|
+
|
26
|
+
expect(result).to eq({ x: [ {a: 0}, {a: 0, b: 2}, {c: 3} ] })
|
27
|
+
end
|
28
|
+
|
29
|
+
it "treats an array nil element as a skip op" do
|
30
|
+
dest = { x: [ {a: 1}, {b: 2}, {c: 3} ] }
|
31
|
+
source = { x: [ nil, nil, {c: 0} ] }
|
32
|
+
result = StripeMock::Util.rmerge(dest, source)
|
33
|
+
|
34
|
+
expect(result).to eq({ x: [ {a: 1}, {b: 2}, {c: 0} ] })
|
35
|
+
end
|
36
|
+
|
37
|
+
it "treats nil as a replacement otherwise" do
|
38
|
+
dest = { x: 99 }
|
39
|
+
source = { x: nil }
|
40
|
+
result = StripeMock::Util.rmerge(dest, source)
|
41
|
+
|
42
|
+
expect(result).to eq({ x: nil })
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Webhook Generation' do
|
4
|
+
|
5
|
+
it "matches the list of webhooks with the folder of fixtures" do
|
6
|
+
events = StripeMock::Webhooks.event_list.to_set
|
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.
|
10
|
+
expect(events - file_names).to eq(Set.new)
|
11
|
+
expect(file_names - events).to eq(Set.new)
|
12
|
+
end
|
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
|
+
|
36
|
+
it "generates an event" do
|
37
|
+
event = StripeMock.mock_webhook_event('customer.created')
|
38
|
+
expect(event).to be_a(Stripe::Event)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "takes a hash and deep merges into the data object" do
|
42
|
+
event = StripeMock.mock_webhook_event('customer.created', {
|
43
|
+
:account_balance => 12345
|
44
|
+
})
|
45
|
+
expect(event.data.object.account_balance).to eq(12345)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "takes a hash and deep merges arrays in the data object" do
|
49
|
+
event = StripeMock.mock_webhook_event('invoice.created', {
|
50
|
+
:lines => {
|
51
|
+
:data => [
|
52
|
+
{ :amount => 555,
|
53
|
+
:plan => { :id => 'wh_test' }
|
54
|
+
}
|
55
|
+
]
|
56
|
+
}
|
57
|
+
})
|
58
|
+
expect(event.data.object.lines.data.first.amount).to eq(555)
|
59
|
+
expect(event.data.object.lines.data.first.plan.id).to eq('wh_test')
|
60
|
+
# Ensure data from invoice.created.json is still present
|
61
|
+
expect(event.data.object.lines.data.first.type).to eq('subscription')
|
62
|
+
expect(event.data.object.lines.data.first.plan.currency).to eq('usd')
|
63
|
+
end
|
64
|
+
|
65
|
+
it "can generate all events" do
|
66
|
+
StripeMock::Webhooks.event_list.each do |event_name|
|
67
|
+
expect { StripeMock.mock_webhook_event(event_name) }.to_not raise_error
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "raises an error for non-existant event types" do
|
72
|
+
expect {
|
73
|
+
event = StripeMock.mock_webhook_event('cow.bell')
|
74
|
+
}.to raise_error StripeMock::UnsupportedRequestError
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|