stripe-ruby-mock 1.8.1.1 → 1.8.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +59 -21
- data/lib/stripe_mock.rb +8 -25
- data/lib/stripe_mock/api/errors.rb +30 -0
- data/lib/stripe_mock/api/instance.rb +27 -0
- data/lib/stripe_mock/data.rb +38 -17
- data/lib/stripe_mock/instance.rb +57 -0
- data/lib/stripe_mock/request_handlers/charges.rb +22 -0
- data/lib/stripe_mock/request_handlers/customers.rb +34 -0
- data/lib/stripe_mock/request_handlers/invoice_items.rb +15 -0
- data/lib/stripe_mock/version.rb +1 -1
- data/spec/readme_spec.rb +45 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/stripe/charge_spec.rb +61 -0
- data/spec/stripe/customer_spec.rb +76 -0
- data/spec/stripe/error_mock_spec.rb +122 -0
- data/spec/stripe/invoice_item_spec.rb +20 -0
- data/spec/stripe_mock_spec.rb +30 -0
- data/stripe-ruby-mock.gemspec +2 -2
- metadata +23 -8
- data/lib/stripe_mock/methods.rb +0 -25
- data/spec/mock_spec.rb +0 -54
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# stripe-ruby-mock
|
2
2
|
|
3
|
-
* Homepage: https://github.com/mindeavor/stripe-ruby-mock
|
3
|
+
* Homepage: https://github.com/mindeavor/stripe-ruby-mock
|
4
4
|
* Issues: https://github.com/mindeavor/stripe-ruby-mock/issues
|
5
5
|
|
6
6
|
## Install
|
@@ -9,8 +9,8 @@
|
|
9
9
|
|
10
10
|
## Features
|
11
11
|
|
12
|
-
* Easily test against stripe errors
|
13
12
|
* No stripe server access required
|
13
|
+
* Easily test against stripe errors
|
14
14
|
|
15
15
|
## Description
|
16
16
|
|
@@ -23,29 +23,67 @@ without the need to actually hit stripe's servers.
|
|
23
23
|
|
24
24
|
You can use stripe-ruby-mock with any ruby testing library. Here's a quick dummy example with RSpec:
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
26
|
+
```ruby
|
27
|
+
require 'stripe'
|
28
|
+
require 'stripe_mock'
|
29
|
+
|
30
|
+
describe MyApp do
|
31
|
+
before { StripeMock.start }
|
32
|
+
after { StripeMock.stop }
|
33
|
+
|
34
|
+
it "creates a stripe customer" do
|
35
|
+
|
36
|
+
# This doesn't touch stripe's servers nor the internet!
|
37
|
+
customer = Stripe::Customer.create({
|
38
|
+
email: 'johnny@appleseed.com',
|
39
|
+
card: 'void_card_token'
|
40
|
+
})
|
41
|
+
expect(customer.email).to eq('johnny@appleseed.com')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## Mocking Errors
|
47
|
+
|
48
|
+
Tired of manually inputting fake credit card numbers to test against errors? Consider it a thing of the past!
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
it "mocks a declined card error" do
|
52
|
+
# Prepares an error for the next stripe request
|
53
|
+
StripeMock.prepare_card_error(:card_declined)
|
54
|
+
|
55
|
+
begin
|
56
|
+
# Note: The next request of ANY type will raise your prepared error
|
57
|
+
Stripe::Charge.create()
|
58
|
+
rescue Stripe::CardError => error
|
59
|
+
expect(error.http_status).to eq(402)
|
60
|
+
expect(error.code).to eq('card_declined')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
You can also set your own custom Stripe error using `prepare_error`:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
it "raises a custom error" do
|
69
|
+
custom_error = Stripe::AuthenticationError.new('Did not provide favourite colour', 400)
|
70
|
+
StripeMock.prepare_error(custom_error)
|
71
|
+
|
72
|
+
begin
|
73
|
+
# Note: The next request of ANY type will raise your prepared error
|
74
|
+
Stripe::Invoice.create()
|
75
|
+
rescue Stripe::AuthenticationError => error
|
76
|
+
expect(error.http_status).to eq(400)
|
77
|
+
expect(error.message).to eq('Did not provide favourite colour')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
43
81
|
|
44
82
|
## TODO
|
45
83
|
|
46
84
|
* Cover all stripe urls/methods
|
47
|
-
*
|
48
|
-
*
|
85
|
+
* Create hash for storing/retrieving all stripe objects in-memory
|
86
|
+
* Currently implemented for: **Customers** and **Charges**
|
49
87
|
|
50
88
|
## Copyright
|
51
89
|
|
data/lib/stripe_mock.rb
CHANGED
@@ -1,29 +1,12 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
require 'stripe_mock/version'
|
2
4
|
require 'stripe_mock/data'
|
3
|
-
require 'stripe_mock/methods'
|
4
|
-
|
5
|
-
module StripeMock
|
6
|
-
|
7
|
-
@@init = false
|
8
|
-
@@enabled = false
|
9
|
-
|
10
|
-
def self.start
|
11
|
-
if @@init == false
|
12
|
-
@@request_method = Stripe.method(:request)
|
13
|
-
@@init = true
|
14
|
-
end
|
15
|
-
alias_stripe_method :request, Methods.method(:mock_request)
|
16
|
-
@@enabled = true
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.stop
|
20
|
-
return unless @@enabled == true
|
21
|
-
alias_stripe_method :request, @@request_method
|
22
|
-
@@enabled = false
|
23
|
-
end
|
24
5
|
|
25
|
-
|
26
|
-
|
27
|
-
end
|
6
|
+
require 'stripe_mock/api/instance'
|
7
|
+
require 'stripe_mock/api/errors'
|
28
8
|
|
29
|
-
|
9
|
+
require 'stripe_mock/request_handlers/charges.rb'
|
10
|
+
require 'stripe_mock/request_handlers/customers.rb'
|
11
|
+
require 'stripe_mock/request_handlers/invoice_items.rb'
|
12
|
+
require 'stripe_mock/instance'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.prepare_error(stripe_error)
|
4
|
+
instance.pending_error = stripe_error
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.prepare_card_error(code)
|
8
|
+
return if instance.nil?
|
9
|
+
args = card_error_args[code]
|
10
|
+
instance.pending_error = Stripe::CardError.new(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def self.card_error_args
|
16
|
+
@__map = {
|
17
|
+
incorrect_number: ["The card number is incorrect", 'number', 'incorrect_number', 402],
|
18
|
+
invalid_number: ["The card number is not a valid credit card number", 'number', 'invalid_number', 402],
|
19
|
+
invalid_expiry_month: ["The card's expiration month is invalid", 'exp_month', 'invalid_expiry_month', 402],
|
20
|
+
invalid_expiry_year: ["The card's expiration year is invalid", 'exp_year', 'invalid_expiry_year', 402],
|
21
|
+
invalid_cvc: ["The card's security code is invalid", 'cvc', 'invalid_cvc', 402],
|
22
|
+
expired_card: ["The card has expired", 'exp_month', 'expired_card', 402],
|
23
|
+
incorrect_cvc: ["The card's security code is incorrect", 'cvc', 'incorrect_cvc', 402],
|
24
|
+
card_declined: ["The card was declined", nil, 'card_declined', 402],
|
25
|
+
missing: ["There is no card on a customer that is being charged.", nil, 'missing', 402],
|
26
|
+
processing_error: ["An error occurred while processing the card", nil, 'processing_error', 402],
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
@@first_start = true
|
4
|
+
@@instance = nil
|
5
|
+
|
6
|
+
def self.start
|
7
|
+
if @@first_start == true
|
8
|
+
@@original_request_method = Stripe.method(:request)
|
9
|
+
@@first_start = false
|
10
|
+
end
|
11
|
+
@@instance = Instance.new
|
12
|
+
alias_stripe_method :request, @@instance.method(:mock_request)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.stop
|
16
|
+
return if @@instance.nil?
|
17
|
+
alias_stripe_method :request, @@original_request_method
|
18
|
+
@@instance = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.alias_stripe_method(new_name, method_object)
|
22
|
+
Stripe.define_singleton_method(new_name) {|*args| method_object.call(*args) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.instance; @@instance; end
|
26
|
+
|
27
|
+
end
|
data/lib/stripe_mock/data.rb
CHANGED
@@ -3,6 +3,8 @@ module StripeMock
|
|
3
3
|
|
4
4
|
def self.test_customer(params)
|
5
5
|
{
|
6
|
+
:email => 'stripe_mock@example.com',
|
7
|
+
:description => 'an auto-generated stripe customer data mock',
|
6
8
|
:subscription_history => [],
|
7
9
|
:bills => [],
|
8
10
|
:charges => [],
|
@@ -42,24 +44,43 @@ module StripeMock
|
|
42
44
|
|
43
45
|
def self.test_charge(params={})
|
44
46
|
{
|
45
|
-
:
|
46
|
-
:
|
47
|
-
:
|
48
|
-
:
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
47
|
+
id: "ch_1fD6uiR9FAA2zc",
|
48
|
+
object: "charge",
|
49
|
+
created: 1366194027,
|
50
|
+
livemode: false,
|
51
|
+
paid: true,
|
52
|
+
amount: 0,
|
53
|
+
currency: "usd",
|
54
|
+
refunded: false,
|
55
|
+
fee: 0,
|
56
|
+
fee_details: [
|
57
|
+
],
|
58
|
+
card: {
|
59
|
+
object: "card",
|
60
|
+
last4: "4242",
|
61
|
+
type: "Visa",
|
62
|
+
exp_month: 12,
|
63
|
+
exp_year: 2013,
|
64
|
+
fingerprint: "3TQGpK9JoY1GgXPw",
|
65
|
+
country: "US",
|
66
|
+
name: "name",
|
67
|
+
address_line1: nil,
|
68
|
+
address_line2: nil,
|
69
|
+
address_city: nil,
|
70
|
+
address_state: nil,
|
71
|
+
address_zip: nil,
|
72
|
+
address_country: nil,
|
73
|
+
cvc_check: nil,
|
74
|
+
address_line1_check: nil,
|
75
|
+
address_zip_check: nil
|
56
76
|
},
|
57
|
-
:
|
58
|
-
:
|
59
|
-
:
|
60
|
-
:
|
61
|
-
:
|
62
|
-
:
|
77
|
+
captured: false,
|
78
|
+
failure_message: nil,
|
79
|
+
amount_refunded: 0,
|
80
|
+
customer: nil,
|
81
|
+
invoice: nil,
|
82
|
+
description: nil,
|
83
|
+
dispute: nil
|
63
84
|
}.merge(params)
|
64
85
|
end
|
65
86
|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module StripeMock
|
2
|
+
class Instance
|
3
|
+
|
4
|
+
# Handlers are ordered by priority
|
5
|
+
@@handlers = []
|
6
|
+
|
7
|
+
def self.add_handler(route, name)
|
8
|
+
@@handlers << {
|
9
|
+
:route => %r{^#{route}$},
|
10
|
+
:name => name
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
include StripeMock::RequestHandlers::Charges
|
15
|
+
include StripeMock::RequestHandlers::Customers
|
16
|
+
include StripeMock::RequestHandlers::InvoiceItems
|
17
|
+
|
18
|
+
|
19
|
+
attr_reader :charges, :customers
|
20
|
+
attr_accessor :pending_error
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@customers = {}
|
24
|
+
@charges = {}
|
25
|
+
@id_counter = 0
|
26
|
+
@pending_error = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def mock_request(method, url, api_key, params={}, headers={})
|
30
|
+
return {} if method == :xtest
|
31
|
+
|
32
|
+
if @pending_error
|
33
|
+
raise @pending_error
|
34
|
+
@pending_error = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
method_url = "#{method} #{url}"
|
38
|
+
handler = @@handlers.find {|h| method_url =~ h[:route] }
|
39
|
+
|
40
|
+
if handler
|
41
|
+
self.send handler[:name], handler[:route], method_url, params, headers
|
42
|
+
else
|
43
|
+
puts "WARNING: Unrecognized method + url: [#{method} #{url}]"
|
44
|
+
puts " params: #{params}"
|
45
|
+
{}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def new_id
|
52
|
+
# Stripe ids must be strings
|
53
|
+
(@id_counter += 1).to_s
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Charges
|
4
|
+
|
5
|
+
def Charges.included(klass)
|
6
|
+
klass.add_handler 'post /v1/charges', :new_charge
|
7
|
+
klass.add_handler 'get /v1/charges/(.*)', :get_charge
|
8
|
+
end
|
9
|
+
|
10
|
+
def new_charge(route, method_url, params, headers)
|
11
|
+
id = new_id
|
12
|
+
charges[id] = Data.test_charge(params.merge :id => id)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_charge(route, method_url, params, headers)
|
16
|
+
route =~ method_url
|
17
|
+
charges[$1] ||= Data.test_charge(:id => $1)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Customers
|
4
|
+
|
5
|
+
def Customers.included(klass)
|
6
|
+
klass.add_handler 'post /v1/customers', :new_customer
|
7
|
+
klass.add_handler 'post /v1/customers/(.*)/subscription', :new_subscription
|
8
|
+
klass.add_handler 'post /v1/customers/(.*)', :update_customer
|
9
|
+
klass.add_handler 'get /v1/customers/(.*)', :get_customer
|
10
|
+
end
|
11
|
+
|
12
|
+
def new_customer(route, method_url, params, headers)
|
13
|
+
id = new_id
|
14
|
+
customers[id] = Data.test_customer(params.merge :id => id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def new_subscription(route, method_url, params, headers)
|
18
|
+
Data.test_subscription(params[:plan])
|
19
|
+
end
|
20
|
+
|
21
|
+
def update_customer(route, method_url, params, headers)
|
22
|
+
route =~ method_url
|
23
|
+
customers[$1] ||= Data.test_customer(:id => $1)
|
24
|
+
customers[$1].merge!(params)
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_customer(route, method_url, params, headers)
|
28
|
+
route =~ method_url
|
29
|
+
customers[$1] ||= Data.test_customer(:id => $1)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module InvoiceItems
|
4
|
+
|
5
|
+
def InvoiceItems.included(klass)
|
6
|
+
klass.add_handler 'post /v1/invoiceitems', :new_invoice_item
|
7
|
+
end
|
8
|
+
|
9
|
+
def new_invoice_item(route, method_url, params, headers)
|
10
|
+
Data.test_invoice(params)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/stripe_mock/version.rb
CHANGED
data/spec/readme_spec.rb
ADDED
@@ -0,0 +1,45 @@
|
|
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 stripe request
|
20
|
+
StripeMock.prepare_card_error(:card_declined)
|
21
|
+
|
22
|
+
begin
|
23
|
+
# Note: The next request of ANY type will raise your prepared error
|
24
|
+
Stripe::Charge.create()
|
25
|
+
rescue Stripe::CardError => error
|
26
|
+
expect(error.http_status).to eq(402)
|
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
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Charge API' do
|
4
|
+
|
5
|
+
before { StripeMock.start }
|
6
|
+
after { StripeMock.stop }
|
7
|
+
|
8
|
+
it "creates a stripe charge item with a card token" do
|
9
|
+
charge = Stripe::Charge.create(
|
10
|
+
amount: 999,
|
11
|
+
currency: 'USD',
|
12
|
+
card: 'card_token_abcde',
|
13
|
+
description: 'card charge'
|
14
|
+
)
|
15
|
+
|
16
|
+
expect(charge.amount).to eq(999)
|
17
|
+
expect(charge.description).to eq('card charge')
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
it "stores a created stripe charge in memory" do
|
22
|
+
charge = Stripe::Charge.create({
|
23
|
+
amount: 333,
|
24
|
+
currency: 'USD',
|
25
|
+
card: 'card_token_333'
|
26
|
+
})
|
27
|
+
charge2 = Stripe::Charge.create({
|
28
|
+
amount: 777,
|
29
|
+
currency: 'USD',
|
30
|
+
card: 'card_token_777'
|
31
|
+
})
|
32
|
+
data = StripeMock.instance.charges
|
33
|
+
expect(data[charge.id]).to_not be_nil
|
34
|
+
expect(data[charge.id][:amount]).to eq(333)
|
35
|
+
|
36
|
+
expect(data[charge2.id]).to_not be_nil
|
37
|
+
expect(data[charge2.id][:amount]).to eq(777)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
it "retrieves a stripe charge" do
|
42
|
+
original = Stripe::Charge.create({
|
43
|
+
amount: 777,
|
44
|
+
currency: 'USD',
|
45
|
+
card: 'card_token_abc'
|
46
|
+
})
|
47
|
+
charge = Stripe::Charge.retrieve(original.id)
|
48
|
+
|
49
|
+
expect(charge.id).to eq(original.id)
|
50
|
+
expect(charge.amount).to eq(original.amount)
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
it "retrieves a stripe charge with an id that doesn't exist" do
|
55
|
+
charge = Stripe::Charge.retrieve('test_charge_x')
|
56
|
+
expect(charge.id).to eq('test_charge_x')
|
57
|
+
expect(charge.amount).to_not be_nil
|
58
|
+
expect(charge.card).to_not be_nil
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Customer API' do
|
4
|
+
|
5
|
+
before { StripeMock.start }
|
6
|
+
after { StripeMock.stop }
|
7
|
+
|
8
|
+
it "creates a stripe customer" do
|
9
|
+
customer = Stripe::Customer.create({
|
10
|
+
email: 'johnny@appleseed.com',
|
11
|
+
card: 'some_card_token',
|
12
|
+
description: "a description"
|
13
|
+
})
|
14
|
+
expect(customer.email).to eq('johnny@appleseed.com')
|
15
|
+
expect(customer.description).to eq('a description')
|
16
|
+
end
|
17
|
+
|
18
|
+
it "stores a created stripe customer in memory" do
|
19
|
+
customer = Stripe::Customer.create({
|
20
|
+
email: 'johnny@appleseed.com',
|
21
|
+
card: 'some_card_token'
|
22
|
+
})
|
23
|
+
customer2 = Stripe::Customer.create({
|
24
|
+
email: 'bob@bobbers.com',
|
25
|
+
card: 'another_card_token'
|
26
|
+
})
|
27
|
+
data = StripeMock.instance.customers
|
28
|
+
expect(data[customer.id]).to_not be_nil
|
29
|
+
expect(data[customer.id][:email]).to eq('johnny@appleseed.com')
|
30
|
+
|
31
|
+
expect(data[customer2.id]).to_not be_nil
|
32
|
+
expect(data[customer2.id][:email]).to eq('bob@bobbers.com')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "retrieves a stripe customer" do
|
36
|
+
original = Stripe::Customer.create({
|
37
|
+
email: 'johnny@appleseed.com',
|
38
|
+
card: 'some_card_token'
|
39
|
+
})
|
40
|
+
customer = Stripe::Customer.retrieve(original.id)
|
41
|
+
|
42
|
+
expect(customer.id).to eq(original.id)
|
43
|
+
expect(customer.email).to eq(original.email)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "retrieves a stripe customer with an id that doesn't exist" do
|
47
|
+
customer = Stripe::Customer.retrieve('test_customer_x')
|
48
|
+
expect(customer.id).to eq('test_customer_x')
|
49
|
+
expect(customer.email).to_not be_nil
|
50
|
+
expect(customer.description).to_not be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
it "updates a stripe customer" do
|
54
|
+
original = Stripe::Customer.retrieve("test_customer_update")
|
55
|
+
email = original.email
|
56
|
+
|
57
|
+
original.description = 'new desc'
|
58
|
+
original.save
|
59
|
+
|
60
|
+
expect(original.email).to eq(email)
|
61
|
+
expect(original.description).to eq('new desc')
|
62
|
+
|
63
|
+
customer = Stripe::Customer.retrieve("test_customer_update")
|
64
|
+
expect(customer.email).to eq(original.email)
|
65
|
+
expect(customer.description).to eq('new desc')
|
66
|
+
end
|
67
|
+
|
68
|
+
it "updates a stripe customer's subscription" do
|
69
|
+
customer = Stripe::Customer.retrieve("test_customer_sub")
|
70
|
+
sub = customer.update_subscription({ :plan => 'silver' })
|
71
|
+
|
72
|
+
expect(sub.object).to eq('subscription')
|
73
|
+
expect(sub.plan.id).to eq('silver')
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,122 @@
|
|
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
|
+
describe 'Stripe Error Mocking' do
|
13
|
+
|
14
|
+
before { StripeMock.start }
|
15
|
+
after { StripeMock.stop }
|
16
|
+
|
17
|
+
it "mocks a manually gives stripe card error" do
|
18
|
+
|
19
|
+
error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', 444, 'body', 'json body')
|
20
|
+
StripeMock.prepare_error(error)
|
21
|
+
|
22
|
+
expect { Stripe::Customer.create() }.to raise_error {|e|
|
23
|
+
expect(e).to be_a(Stripe::CardError)
|
24
|
+
expect(e.code).to eq('bad_code')
|
25
|
+
expect(e.param).to eq('param_name')
|
26
|
+
expect(e.message).to eq('Test Msg')
|
27
|
+
|
28
|
+
expect(e.http_status).to eq(444)
|
29
|
+
expect(e.http_body).to eq('body')
|
30
|
+
expect(e.json_body).to eq('json body')
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
it "mocks a manually gives stripe invalid request error" do
|
36
|
+
|
37
|
+
error = Stripe::InvalidRequestError.new('Test Invalid', 'param', 987, 'ibody', 'json ibody')
|
38
|
+
StripeMock.prepare_error(error)
|
39
|
+
|
40
|
+
expect { Stripe::Charge.create() }.to raise_error {|e|
|
41
|
+
expect(e).to be_a(Stripe::InvalidRequestError)
|
42
|
+
expect(e.param).to eq('param')
|
43
|
+
expect(e.message).to eq('Test Invalid')
|
44
|
+
|
45
|
+
expect(e.http_status).to eq(987)
|
46
|
+
expect(e.http_body).to eq('ibody')
|
47
|
+
expect(e.json_body).to eq('json ibody')
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
it "mocks a manually gives stripe invalid auth error" do
|
53
|
+
|
54
|
+
error = Stripe::AuthenticationError.new('Bad Auth', 499, 'abody', 'json abody')
|
55
|
+
StripeMock.prepare_error(error)
|
56
|
+
|
57
|
+
expect { Stripe::Invoice.create() }.to raise_error {|e|
|
58
|
+
expect(e).to be_a(Stripe::AuthenticationError)
|
59
|
+
expect(e.message).to eq('Bad Auth')
|
60
|
+
|
61
|
+
expect(e.http_status).to eq(499)
|
62
|
+
expect(e.http_body).to eq('abody')
|
63
|
+
expect(e.json_body).to eq('json abody')
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# # # # # # # # # # # # # #
|
69
|
+
# Card Error Helper Methods
|
70
|
+
# # # # # # # # # # # # # #
|
71
|
+
|
72
|
+
it "mocks an incorrect number card error" do
|
73
|
+
StripeMock.prepare_card_error(:incorrect_number)
|
74
|
+
expect_card_error 'incorrect_number', 'number'
|
75
|
+
end
|
76
|
+
|
77
|
+
it "mocks an invalid number card error" do
|
78
|
+
StripeMock.prepare_card_error(:invalid_number)
|
79
|
+
expect_card_error 'invalid_number', 'number'
|
80
|
+
end
|
81
|
+
|
82
|
+
it "mocks an invalid expiration month card error" do
|
83
|
+
StripeMock.prepare_card_error(:invalid_expiry_month)
|
84
|
+
expect_card_error 'invalid_expiry_month', 'exp_month'
|
85
|
+
end
|
86
|
+
|
87
|
+
it "mocks an invalid expiration year card error" do
|
88
|
+
StripeMock.prepare_card_error(:invalid_expiry_year)
|
89
|
+
expect_card_error 'invalid_expiry_year', 'exp_year'
|
90
|
+
end
|
91
|
+
|
92
|
+
it "mocks an invalid cvc card error" do
|
93
|
+
StripeMock.prepare_card_error(:invalid_cvc)
|
94
|
+
expect_card_error 'invalid_cvc', 'cvc'
|
95
|
+
end
|
96
|
+
|
97
|
+
it "mocks an expired card error" do
|
98
|
+
StripeMock.prepare_card_error(:expired_card)
|
99
|
+
expect_card_error 'expired_card', 'exp_month'
|
100
|
+
end
|
101
|
+
|
102
|
+
it "mocks an incorrect cvc card error" do
|
103
|
+
StripeMock.prepare_card_error(:incorrect_cvc)
|
104
|
+
expect_card_error 'incorrect_cvc', 'cvc'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "mocks a declined card error" do
|
108
|
+
StripeMock.prepare_card_error(:card_declined)
|
109
|
+
expect_card_error 'card_declined', nil
|
110
|
+
end
|
111
|
+
|
112
|
+
it "mocks a missing card error" do
|
113
|
+
StripeMock.prepare_card_error(:missing)
|
114
|
+
expect_card_error 'missing', nil
|
115
|
+
end
|
116
|
+
|
117
|
+
it "mocks a processing error card error" do
|
118
|
+
StripeMock.prepare_card_error(:processing_error)
|
119
|
+
expect_card_error 'processing_error', nil
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Invoice Item API' do
|
4
|
+
|
5
|
+
before { StripeMock.start }
|
6
|
+
after { StripeMock.stop }
|
7
|
+
|
8
|
+
it "creates a stripe invoice item" do
|
9
|
+
invoice_item = Stripe::InvoiceItem.create({
|
10
|
+
amount: 1099,
|
11
|
+
customer: 1234,
|
12
|
+
currency: 'USD',
|
13
|
+
description: "invoice item desc"
|
14
|
+
}, 'abcde')
|
15
|
+
|
16
|
+
expect(invoice_item.amount).to eq(1099)
|
17
|
+
expect(invoice_item.description).to eq('invoice item desc')
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,30 @@
|
|
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
|
+
end
|
data/stripe-ruby-mock.gemspec
CHANGED
@@ -10,14 +10,14 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.license = "MIT"
|
11
11
|
gem.authors = ["Gilbert"]
|
12
12
|
gem.email = "gilbertbgarza@gmail.com"
|
13
|
-
gem.homepage = "https://github.com/mindeavor/stripe-ruby-mock
|
13
|
+
gem.homepage = "https://github.com/mindeavor/stripe-ruby-mock"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ['lib']
|
19
19
|
|
20
|
-
gem.add_dependency 'stripe', '~> 1.8.
|
20
|
+
gem.add_dependency 'stripe', '~> 1.8.3'
|
21
21
|
|
22
22
|
gem.add_development_dependency 'rspec', '~> 2.4'
|
23
23
|
gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
|
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.
|
4
|
+
version: 1.8.3.0
|
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-
|
12
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: stripe
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.8.
|
21
|
+
version: 1.8.3
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 1.8.
|
29
|
+
version: 1.8.3
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rspec
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,13 +72,23 @@ files:
|
|
72
72
|
- README.md
|
73
73
|
- Rakefile
|
74
74
|
- lib/stripe_mock.rb
|
75
|
+
- lib/stripe_mock/api/errors.rb
|
76
|
+
- lib/stripe_mock/api/instance.rb
|
75
77
|
- lib/stripe_mock/data.rb
|
76
|
-
- lib/stripe_mock/
|
78
|
+
- lib/stripe_mock/instance.rb
|
79
|
+
- lib/stripe_mock/request_handlers/charges.rb
|
80
|
+
- lib/stripe_mock/request_handlers/customers.rb
|
81
|
+
- lib/stripe_mock/request_handlers/invoice_items.rb
|
77
82
|
- lib/stripe_mock/version.rb
|
78
|
-
- spec/
|
83
|
+
- spec/readme_spec.rb
|
79
84
|
- spec/spec_helper.rb
|
85
|
+
- spec/stripe/charge_spec.rb
|
86
|
+
- spec/stripe/customer_spec.rb
|
87
|
+
- spec/stripe/error_mock_spec.rb
|
88
|
+
- spec/stripe/invoice_item_spec.rb
|
89
|
+
- spec/stripe_mock_spec.rb
|
80
90
|
- stripe-ruby-mock.gemspec
|
81
|
-
homepage: https://github.com/mindeavor/stripe-ruby-mock
|
91
|
+
homepage: https://github.com/mindeavor/stripe-ruby-mock
|
82
92
|
licenses:
|
83
93
|
- MIT
|
84
94
|
post_install_message:
|
@@ -104,5 +114,10 @@ signing_key:
|
|
104
114
|
specification_version: 3
|
105
115
|
summary: TDD with stripe
|
106
116
|
test_files:
|
107
|
-
- spec/
|
117
|
+
- spec/readme_spec.rb
|
108
118
|
- spec/spec_helper.rb
|
119
|
+
- spec/stripe/charge_spec.rb
|
120
|
+
- spec/stripe/customer_spec.rb
|
121
|
+
- spec/stripe/error_mock_spec.rb
|
122
|
+
- spec/stripe/invoice_item_spec.rb
|
123
|
+
- spec/stripe_mock_spec.rb
|
data/lib/stripe_mock/methods.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module StripeMock
|
2
|
-
module Methods
|
3
|
-
|
4
|
-
def self.mock_request(method, url, api_key, params={}, headers={})
|
5
|
-
return {} if method == :xtest
|
6
|
-
|
7
|
-
# Ordered from most specific to least specific
|
8
|
-
case "#{method} #{url}"
|
9
|
-
when 'post /v1/customers'
|
10
|
-
Data.test_customer(params)
|
11
|
-
when 'post /v1/invoiceitems'
|
12
|
-
Data.test_invoice(params)
|
13
|
-
when %r{post /v1/customers/(.*)/subscription}
|
14
|
-
Data.test_subscription(params[:plan])
|
15
|
-
when %r{get /v1/customers/(.*)}
|
16
|
-
Data.test_customer :id => $1
|
17
|
-
else
|
18
|
-
puts "WARNING: Unrecognized method + url: [#{method} #{url}]"
|
19
|
-
puts " params: #{params}"
|
20
|
-
{}
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
data/spec/mock_spec.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'stripe_mock'
|
3
|
-
|
4
|
-
describe StripeMock do
|
5
|
-
|
6
|
-
before { StripeMock.start }
|
7
|
-
after { StripeMock.stop }
|
8
|
-
|
9
|
-
it "should override stripe's request method" do
|
10
|
-
Stripe.request(:xtest, '/', 'abcde') # no error
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should revert overriding stripe's request method" do
|
14
|
-
Stripe.request(:xtest, '/', 'abcde') # no error
|
15
|
-
StripeMock.stop
|
16
|
-
expect { Stripe.request(:x, '/', 'abcde') }.to raise_error
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should create a stripe customer" do
|
20
|
-
customer = Stripe::Customer.create({
|
21
|
-
email: 'johnny@appleseed.com',
|
22
|
-
card: 'some_card_token',
|
23
|
-
description: "a description"
|
24
|
-
})
|
25
|
-
expect(customer.email).to eq('johnny@appleseed.com')
|
26
|
-
expect(customer.description).to eq('a description')
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should retrieve a stripe customer" do
|
30
|
-
customer = Stripe::Customer.retrieve("test_customer")
|
31
|
-
expect(customer.id).to eq('test_customer')
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should update a stripe customer's subscription" do
|
35
|
-
customer = Stripe::Customer.retrieve("test_customer")
|
36
|
-
sub = customer.update_subscription({ :plan => 'silver' })
|
37
|
-
|
38
|
-
expect(sub.object).to eq('subscription')
|
39
|
-
expect(sub.plan.identifier).to eq('silver')
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should create a stripe invoice item" do
|
43
|
-
invoice = Stripe::InvoiceItem.create({
|
44
|
-
amount: 1099,
|
45
|
-
customer: 1234,
|
46
|
-
currency: 'USD',
|
47
|
-
description: "invoice desc"
|
48
|
-
}, 'abcde')
|
49
|
-
|
50
|
-
expect(invoice.amount).to eq(1099)
|
51
|
-
expect(invoice.description).to eq('invoice desc')
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|