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,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_stripe_examples
|
3
|
+
|
4
|
+
describe StripeMock::Instance do
|
5
|
+
|
6
|
+
it_behaves_like_stripe do
|
7
|
+
def test_data_source(type); StripeMock.instance.send(type); end
|
8
|
+
end
|
9
|
+
|
10
|
+
before { StripeMock.start }
|
11
|
+
after { StripeMock.stop }
|
12
|
+
|
13
|
+
it "handles both string and symbol hash keys" do
|
14
|
+
string_params = {
|
15
|
+
"id" => "str_abcde",
|
16
|
+
:name => "String Plan"
|
17
|
+
}
|
18
|
+
res, api_key = StripeMock.instance.mock_request('post', '/v1/plans', 'api_key', string_params)
|
19
|
+
expect(res[:id]).to eq('str_abcde')
|
20
|
+
expect(res[:name]).to eq('String Plan')
|
21
|
+
end
|
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
|
+
|
32
|
+
it "can toggle debug" do
|
33
|
+
StripeMock.toggle_debug(true)
|
34
|
+
expect(StripeMock.instance.debug).to eq(true)
|
35
|
+
StripeMock.toggle_debug(false)
|
36
|
+
expect(StripeMock.instance.debug).to eq(false)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should toggle off debug when mock session ends" do
|
40
|
+
StripeMock.toggle_debug(true)
|
41
|
+
|
42
|
+
StripeMock.stop
|
43
|
+
expect(StripeMock.instance).to be_nil
|
44
|
+
|
45
|
+
StripeMock.start
|
46
|
+
expect(StripeMock.instance.debug).to eq(false)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/spec/readme_spec.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
|
2
|
+
describe 'README examples' do
|
3
|
+
|
4
|
+
before { StripeMock.start }
|
5
|
+
after { StripeMock.stop }
|
6
|
+
|
7
|
+
it "creates a stripe customer" do
|
8
|
+
|
9
|
+
# This doesn't touch stripe's servers nor the internet!
|
10
|
+
customer = Stripe::Customer.create({
|
11
|
+
email: 'johnny@appleseed.com',
|
12
|
+
card: 'void_card_token'
|
13
|
+
})
|
14
|
+
expect(customer.email).to eq('johnny@appleseed.com')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
it "mocks a declined card error" do
|
19
|
+
# Prepares an error for the next create charge request
|
20
|
+
StripeMock.prepare_card_error(:card_declined)
|
21
|
+
|
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
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "has built-in card errors" do
|
30
|
+
StripeMock.prepare_card_error(:incorrect_number)
|
31
|
+
StripeMock.prepare_card_error(:invalid_number)
|
32
|
+
StripeMock.prepare_card_error(:invalid_expiry_month)
|
33
|
+
StripeMock.prepare_card_error(:invalid_expiry_year)
|
34
|
+
StripeMock.prepare_card_error(:invalid_cvc)
|
35
|
+
StripeMock.prepare_card_error(:expired_card)
|
36
|
+
StripeMock.prepare_card_error(:incorrect_cvc)
|
37
|
+
StripeMock.prepare_card_error(:card_declined)
|
38
|
+
StripeMock.prepare_card_error(:missing)
|
39
|
+
StripeMock.prepare_card_error(:processing_error)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "mocks a stripe webhook" do
|
43
|
+
event = StripeMock.mock_webhook_event('customer.created')
|
44
|
+
|
45
|
+
customer_object = event.data.object
|
46
|
+
expect(customer_object.id).to_not be_nil
|
47
|
+
expect(customer_object.active_card).to_not be_nil
|
48
|
+
# etc.
|
49
|
+
end
|
50
|
+
|
51
|
+
it "can override default webhook values" do
|
52
|
+
event = StripeMock.mock_webhook_event('customer.created', {
|
53
|
+
:id => 'cus_my_custom_value',
|
54
|
+
:email => 'joe@example.com'
|
55
|
+
})
|
56
|
+
# Alternatively:
|
57
|
+
# event.data.object.id = 'cus_my_custom_value'
|
58
|
+
# event.data.object.email = 'joe@example.com'
|
59
|
+
expect(event.data.object.id).to eq('cus_my_custom_value')
|
60
|
+
expect(event.data.object.email).to eq('joe@example.com')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "generates a stripe card token" do
|
64
|
+
card_token = StripeMock.generate_card_token(last4: "9191", exp_year: 1984)
|
65
|
+
|
66
|
+
cus = Stripe::Customer.create(card: card_token)
|
67
|
+
card = cus.cards.data.first
|
68
|
+
expect(card.last4).to eq("9191")
|
69
|
+
expect(card.exp_year).to eq(1984)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
data/spec/server_spec.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_stripe_examples
|
3
|
+
|
4
|
+
describe 'StripeMock Server' do
|
5
|
+
|
6
|
+
it_behaves_like_stripe do
|
7
|
+
def test_data_source(type); StripeMock.client.get_server_data(type); end
|
8
|
+
end
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
StripeMock.spawn_server
|
12
|
+
end
|
13
|
+
|
14
|
+
after(:all) do
|
15
|
+
StripeMock.kill_server
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
before do
|
20
|
+
@client = StripeMock.start_client
|
21
|
+
end
|
22
|
+
|
23
|
+
after { StripeMock.stop_client(:clear_server_data => true) }
|
24
|
+
|
25
|
+
|
26
|
+
it "uses an RPC client for mock requests" do
|
27
|
+
charge = Stripe::Charge.create(
|
28
|
+
amount: 987,
|
29
|
+
currency: 'USD',
|
30
|
+
card: 'card_token_abcde',
|
31
|
+
description: 'card charge'
|
32
|
+
)
|
33
|
+
expect(charge.amount).to eq(987)
|
34
|
+
expect(charge.currency).to eq('USD')
|
35
|
+
expect(charge.description).to eq('card charge')
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
it "should not clear server data in between client sessions by default" do
|
40
|
+
customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
|
41
|
+
expect(customer.email).to eq('johnny@appleseed.com')
|
42
|
+
|
43
|
+
server_customer_data = StripeMock.client.get_server_data(:customers)[customer.id]
|
44
|
+
expect(server_customer_data).to_not be_nil
|
45
|
+
expect(server_customer_data[:email]).to eq('johnny@appleseed.com')
|
46
|
+
|
47
|
+
StripeMock.stop_client
|
48
|
+
StripeMock.start_client
|
49
|
+
|
50
|
+
server_customer_data = StripeMock.client.get_server_data(:customers)[customer.id]
|
51
|
+
expect(server_customer_data).to_not be_nil
|
52
|
+
expect(server_customer_data[:email]).to eq('johnny@appleseed.com')
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
it "returns a response with symbolized hash keys" do
|
57
|
+
Stripe::Plan.create(id: 'x')
|
58
|
+
response, api_key = StripeMock.redirect_to_mock_server('get', '/v1/plans/x', 'xxx')
|
59
|
+
response.keys.each {|k| expect(k).to be_a(Symbol) }
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
it "can toggle debug" do
|
64
|
+
StripeMock.toggle_debug(true)
|
65
|
+
expect(@client.server_debug?).to eq(true)
|
66
|
+
StripeMock.toggle_debug(false)
|
67
|
+
expect(@client.server_debug?).to eq(false)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
it "should toggle off debug when mock session ends" do
|
72
|
+
StripeMock.toggle_debug(true)
|
73
|
+
|
74
|
+
StripeMock.stop_client
|
75
|
+
expect(StripeMock.client).to be_nil
|
76
|
+
|
77
|
+
StripeMock.start_client
|
78
|
+
expect(StripeMock.client.server_debug?).to eq(false)
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
it "can set the default server pid path" do
|
83
|
+
expect(StripeMock.default_server_pid_path).to eq('./stripe-mock-server.pid')
|
84
|
+
|
85
|
+
orig = StripeMock.default_server_pid_path
|
86
|
+
StripeMock.default_server_pid_path = 'abc'
|
87
|
+
expect(StripeMock.default_server_pid_path).to eq('abc')
|
88
|
+
|
89
|
+
# Set back to original for #kill_server to work properly
|
90
|
+
StripeMock.default_server_pid_path = orig
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
it "doesn't create multiple clients" do
|
95
|
+
result = StripeMock.start_client
|
96
|
+
expect(result.__id__).to eq(@client.__id__)
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
it "raises an error when client is stopped" do
|
101
|
+
expect(@client).to be_a StripeMock::Client
|
102
|
+
expect(@client.state).to eq('ready')
|
103
|
+
|
104
|
+
StripeMock.stop_client
|
105
|
+
expect(@client.state).to eq('closed')
|
106
|
+
expect { @client.clear_server_data }.to raise_error StripeMock::ClosedClientConnectionError
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
it "raises an error when client connection is closed" do
|
111
|
+
expect(@client).to be_a StripeMock::Client
|
112
|
+
expect(@client.state).to eq('ready')
|
113
|
+
|
114
|
+
@client.close!
|
115
|
+
expect(@client.state).to eq('closed')
|
116
|
+
expect(StripeMock.stop_client).to eq(false)
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
it "throws an error when server is not running" do
|
121
|
+
StripeMock.stop_client
|
122
|
+
begin
|
123
|
+
StripeMock.start_client(1515)
|
124
|
+
# We should never get here
|
125
|
+
expect(false).to eq(true)
|
126
|
+
rescue StripeMock::ServerTimeoutError => e
|
127
|
+
expect(e.associated_error).to be_a(Errno::ECONNREFUSED)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples 'Card Token Mocking' do
|
4
|
+
|
5
|
+
it "generates and reads a card token for create customer" do
|
6
|
+
card_token = StripeMock.generate_card_token(last4: "9191", exp_month: 99, exp_year: 3005)
|
7
|
+
|
8
|
+
cus = Stripe::Customer.create(card: card_token)
|
9
|
+
card = cus.cards.data.first
|
10
|
+
expect(card.last4).to eq("9191")
|
11
|
+
expect(card.exp_month).to eq(99)
|
12
|
+
expect(card.exp_year).to eq(3005)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "generated and reads a card token for update customer" do
|
16
|
+
card_token = StripeMock.generate_card_token(last4: "1133", exp_month: 11, exp_year: 2099)
|
17
|
+
|
18
|
+
cus = Stripe::Customer.create()
|
19
|
+
cus.card = card_token
|
20
|
+
cus.save
|
21
|
+
|
22
|
+
card = cus.cards.data.first
|
23
|
+
expect(card.last4).to eq("1133")
|
24
|
+
expect(card.exp_month).to eq(11)
|
25
|
+
expect(card.exp_year).to eq(2099)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples 'Charge API' do
|
4
|
+
|
5
|
+
it "creates a stripe charge item with a card token" do
|
6
|
+
charge = Stripe::Charge.create(
|
7
|
+
amount: 999,
|
8
|
+
currency: 'USD',
|
9
|
+
card: 'card_token_abcde',
|
10
|
+
description: 'card charge'
|
11
|
+
)
|
12
|
+
|
13
|
+
expect(charge.id).to match(/^test_ch/)
|
14
|
+
expect(charge.amount).to eq(999)
|
15
|
+
expect(charge.description).to eq('card charge')
|
16
|
+
expect(charge.captured).to eq(true)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
it "stores a created stripe charge in memory" do
|
21
|
+
charge = Stripe::Charge.create({
|
22
|
+
amount: 333,
|
23
|
+
currency: 'USD',
|
24
|
+
card: 'card_token_333'
|
25
|
+
})
|
26
|
+
charge2 = Stripe::Charge.create({
|
27
|
+
amount: 777,
|
28
|
+
currency: 'USD',
|
29
|
+
card: 'card_token_777'
|
30
|
+
})
|
31
|
+
data = test_data_source(:charges)
|
32
|
+
expect(data[charge.id]).to_not be_nil
|
33
|
+
expect(data[charge.id][:amount]).to eq(333)
|
34
|
+
|
35
|
+
expect(data[charge2.id]).to_not be_nil
|
36
|
+
expect(data[charge2.id][:amount]).to eq(777)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
it "retrieves a stripe charge" do
|
41
|
+
original = Stripe::Charge.create({
|
42
|
+
amount: 777,
|
43
|
+
currency: 'USD',
|
44
|
+
card: 'card_token_abc'
|
45
|
+
})
|
46
|
+
charge = Stripe::Charge.retrieve(original.id)
|
47
|
+
|
48
|
+
expect(charge.id).to eq(original.id)
|
49
|
+
expect(charge.amount).to eq(original.amount)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "cannot retrieve a charge that doesn't exist" do
|
53
|
+
expect { Stripe::Charge.retrieve('nope') }.to raise_error {|e|
|
54
|
+
expect(e).to be_a Stripe::InvalidRequestError
|
55
|
+
expect(e.param).to eq('charge')
|
56
|
+
expect(e.http_status).to eq(404)
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
context "With strict mode toggled off" do
|
62
|
+
|
63
|
+
before { StripeMock.toggle_strict(false) }
|
64
|
+
|
65
|
+
it "retrieves a stripe charge with an id that doesn't exist" do
|
66
|
+
charge = Stripe::Charge.retrieve('test_charge_x')
|
67
|
+
expect(charge.id).to eq('test_charge_x')
|
68
|
+
expect(charge.amount).to_not be_nil
|
69
|
+
expect(charge.card).to_not be_nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
describe 'captured status value' do
|
75
|
+
it "reports captured by default" do
|
76
|
+
charge = Stripe::Charge.create({
|
77
|
+
amount: 777,
|
78
|
+
currency: 'USD',
|
79
|
+
card: 'card_token_abc'
|
80
|
+
})
|
81
|
+
|
82
|
+
expect(charge.captured).to be_true
|
83
|
+
end
|
84
|
+
|
85
|
+
it "reports captured if capture requested" do
|
86
|
+
charge = Stripe::Charge.create({
|
87
|
+
amount: 777,
|
88
|
+
currency: 'USD',
|
89
|
+
card: 'card_token_abc',
|
90
|
+
capture: true
|
91
|
+
})
|
92
|
+
|
93
|
+
expect(charge.captured).to be_true
|
94
|
+
end
|
95
|
+
|
96
|
+
it "reports not captured if capture: false requested" do
|
97
|
+
charge = Stripe::Charge.create({
|
98
|
+
amount: 777,
|
99
|
+
currency: 'USD',
|
100
|
+
card: 'card_token_abc',
|
101
|
+
capture: false
|
102
|
+
})
|
103
|
+
|
104
|
+
expect(charge.captured).to be_false
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "two-step charge (auth, then capture)" do
|
109
|
+
it "changes captured status upon #capture" do
|
110
|
+
charge = Stripe::Charge.create({
|
111
|
+
amount: 777,
|
112
|
+
currency: 'USD',
|
113
|
+
card: 'card_token_abc',
|
114
|
+
capture: false
|
115
|
+
})
|
116
|
+
|
117
|
+
returned_charge = charge.capture
|
118
|
+
expect(charge.captured).to be_true
|
119
|
+
expect(returned_charge.id).to eq(charge.id)
|
120
|
+
expect(returned_charge.captured).to be_true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,218 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples 'Customer API' do
|
4
|
+
|
5
|
+
it "creates a stripe customer with a default card" do
|
6
|
+
customer = Stripe::Customer.create({
|
7
|
+
email: 'johnny@appleseed.com',
|
8
|
+
card: 'some_card_token',
|
9
|
+
description: "a description"
|
10
|
+
})
|
11
|
+
expect(customer.id).to match(/^test_cus/)
|
12
|
+
expect(customer.email).to eq('johnny@appleseed.com')
|
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
|
35
|
+
end
|
36
|
+
|
37
|
+
it "stores a created stripe customer in memory" do
|
38
|
+
customer = Stripe::Customer.create({
|
39
|
+
email: 'johnny@appleseed.com',
|
40
|
+
card: 'some_card_token'
|
41
|
+
})
|
42
|
+
customer2 = Stripe::Customer.create({
|
43
|
+
email: 'bob@bobbers.com',
|
44
|
+
card: 'another_card_token'
|
45
|
+
})
|
46
|
+
data = test_data_source(:customers)
|
47
|
+
expect(data[customer.id]).to_not be_nil
|
48
|
+
expect(data[customer.id][:email]).to eq('johnny@appleseed.com')
|
49
|
+
|
50
|
+
expect(data[customer2.id]).to_not be_nil
|
51
|
+
expect(data[customer2.id][:email]).to eq('bob@bobbers.com')
|
52
|
+
end
|
53
|
+
|
54
|
+
it "retrieves a stripe customer" do
|
55
|
+
original = Stripe::Customer.create({
|
56
|
+
email: 'johnny@appleseed.com',
|
57
|
+
card: 'some_card_token'
|
58
|
+
})
|
59
|
+
customer = Stripe::Customer.retrieve(original.id)
|
60
|
+
|
61
|
+
expect(customer.id).to eq(original.id)
|
62
|
+
expect(customer.email).to eq(original.email)
|
63
|
+
expect(customer.default_card).to eq(original.default_card)
|
64
|
+
expect(customer.subscription).to be_nil
|
65
|
+
end
|
66
|
+
|
67
|
+
it "cannot retrieve a customer that doesn't exist" do
|
68
|
+
expect { Stripe::Customer.retrieve('nope') }.to raise_error {|e|
|
69
|
+
expect(e).to be_a Stripe::InvalidRequestError
|
70
|
+
expect(e.param).to eq('customer')
|
71
|
+
expect(e.http_status).to eq(404)
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
it "retrieves all customers" do
|
76
|
+
Stripe::Customer.create({ email: 'one@one.com' })
|
77
|
+
Stripe::Customer.create({ email: 'two@two.com' })
|
78
|
+
|
79
|
+
all = Stripe::Customer.all
|
80
|
+
expect(all.length).to eq(2)
|
81
|
+
all.map(&:email).should include('one@one.com', 'two@two.com')
|
82
|
+
end
|
83
|
+
|
84
|
+
it "updates a stripe customer" do
|
85
|
+
original = Stripe::Customer.create(id: 'test_customer_update')
|
86
|
+
email = original.email
|
87
|
+
|
88
|
+
original.description = 'new desc'
|
89
|
+
original.save
|
90
|
+
|
91
|
+
expect(original.email).to eq(email)
|
92
|
+
expect(original.description).to eq('new desc')
|
93
|
+
|
94
|
+
customer = Stripe::Customer.retrieve("test_customer_update")
|
95
|
+
expect(customer.email).to eq(original.email)
|
96
|
+
expect(customer.description).to eq('new desc')
|
97
|
+
end
|
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
|
+
|
115
|
+
it "updates a stripe customer's subscription" do
|
116
|
+
plan = Stripe::Plan.create(id: 'silver')
|
117
|
+
customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
|
118
|
+
sub = customer.update_subscription({ :plan => 'silver' })
|
119
|
+
|
120
|
+
expect(sub.object).to eq('subscription')
|
121
|
+
expect(sub.plan.id).to eq('silver')
|
122
|
+
expect(sub.plan.to_hash).to eq(plan.to_hash)
|
123
|
+
|
124
|
+
customer = Stripe::Customer.retrieve('test_customer_sub')
|
125
|
+
expect(customer.subscription).to_not be_nil
|
126
|
+
expect(customer.subscription.id).to eq(sub.id)
|
127
|
+
expect(customer.subscription.plan.id).to eq('silver')
|
128
|
+
expect(customer.subscription.customer).to eq(customer.id)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "throws an error when subscribing a customer with no card" do
|
132
|
+
plan = Stripe::Plan.create(id: 'enterprise', amount: 499)
|
133
|
+
customer = Stripe::Customer.create(id: 'cardless')
|
134
|
+
|
135
|
+
expect { customer.update_subscription({ :plan => 'enterprise' }) }.to raise_error {|e|
|
136
|
+
expect(e).to be_a Stripe::InvalidRequestError
|
137
|
+
expect(e.http_status).to eq(400)
|
138
|
+
expect(e.message).to_not be_nil
|
139
|
+
}
|
140
|
+
end
|
141
|
+
|
142
|
+
it "subscribes a customer with no card to a free plan" do
|
143
|
+
plan = Stripe::Plan.create(id: 'free_tier', amount: 0)
|
144
|
+
customer = Stripe::Customer.create(id: 'cardless')
|
145
|
+
sub = customer.update_subscription({ :plan => 'free_tier' })
|
146
|
+
|
147
|
+
expect(sub.object).to eq('subscription')
|
148
|
+
expect(sub.plan.id).to eq('free_tier')
|
149
|
+
expect(sub.plan.to_hash).to eq(plan.to_hash)
|
150
|
+
|
151
|
+
customer = Stripe::Customer.retrieve('cardless')
|
152
|
+
expect(customer.subscription).to_not be_nil
|
153
|
+
expect(customer.subscription.id).to eq(sub.id)
|
154
|
+
expect(customer.subscription.plan.id).to eq('free_tier')
|
155
|
+
expect(customer.subscription.customer).to eq(customer.id)
|
156
|
+
end
|
157
|
+
|
158
|
+
it "subscribes a customer with no card to a plan with a free trial" do
|
159
|
+
plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
|
160
|
+
customer = Stripe::Customer.create(id: 'cardless')
|
161
|
+
sub = customer.update_subscription({ :plan => 'trial' })
|
162
|
+
|
163
|
+
expect(sub.object).to eq('subscription')
|
164
|
+
expect(sub.plan.id).to eq('trial')
|
165
|
+
expect(sub.plan.to_hash).to eq(plan.to_hash)
|
166
|
+
|
167
|
+
customer = Stripe::Customer.retrieve('cardless')
|
168
|
+
expect(customer.subscription).to_not be_nil
|
169
|
+
expect(customer.subscription.id).to eq(sub.id)
|
170
|
+
expect(customer.subscription.plan.id).to eq('trial')
|
171
|
+
expect(customer.subscription.customer).to eq(customer.id)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "cancels a stripe customer's subscription" do
|
175
|
+
plan = Stripe::Plan.create(id: 'the truth')
|
176
|
+
customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
|
177
|
+
sub = customer.update_subscription({ :plan => 'the truth' })
|
178
|
+
|
179
|
+
result = customer.cancel_subscription
|
180
|
+
expect(result.deleted).to eq(true)
|
181
|
+
expect(result.id).to eq(sub.id)
|
182
|
+
customer = Stripe::Customer.retrieve('test_customer_sub')
|
183
|
+
expect(customer.subscription).to be_nil
|
184
|
+
end
|
185
|
+
|
186
|
+
it "cannot update to a plan that does not exist" do
|
187
|
+
customer = Stripe::Customer.create(id: 'test_customer_sub')
|
188
|
+
expect {
|
189
|
+
customer.update_subscription(plan: 'imagination')
|
190
|
+
}.to raise_error Stripe::InvalidRequestError
|
191
|
+
end
|
192
|
+
|
193
|
+
it "cannot cancel a plan that does not exist" do
|
194
|
+
customer = Stripe::Customer.create(id: 'test_customer_sub')
|
195
|
+
expect {
|
196
|
+
customer.cancel_subscription(plan: 'imagination')
|
197
|
+
}.to raise_error Stripe::InvalidRequestError
|
198
|
+
end
|
199
|
+
|
200
|
+
it "deletes a customer" do
|
201
|
+
customer = Stripe::Customer.create(id: 'test_customer_sub')
|
202
|
+
customer = customer.delete
|
203
|
+
expect(customer.deleted).to be_true
|
204
|
+
end
|
205
|
+
|
206
|
+
context "With strict mode toggled off" do
|
207
|
+
|
208
|
+
before { StripeMock.toggle_strict(false) }
|
209
|
+
|
210
|
+
it "retrieves a stripe customer with an id that doesn't exist" do
|
211
|
+
customer = Stripe::Customer.retrieve('test_customer_x')
|
212
|
+
expect(customer.id).to eq('test_customer_x')
|
213
|
+
expect(customer.email).to_not be_nil
|
214
|
+
expect(customer.description).to_not be_nil
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|