stripe-ruby-mock 1.8.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ html/
2
+ pkg/
3
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/ChangeLog.rdoc ADDED
@@ -0,0 +1,4 @@
1
+ === 0.1.0 / 2013-04-28
2
+
3
+ * Initial release:
4
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2013 Gilbert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # stripe-ruby-mock
2
+
3
+ * Homepage: https://github.com/mindeavor/stripe-ruby-mock#readme
4
+ * Issues: https://github.com/mindeavor/stripe-ruby-mock/issues
5
+
6
+ ## Install
7
+
8
+ $ gem install stripe-ruby-mock
9
+
10
+ ## Features
11
+
12
+ * Easily test against stripe errors
13
+ * No stripe server access required
14
+
15
+ ## Description
16
+
17
+ ** *WARNING: THIS LIBRARY IS INCOMPLETE* **
18
+
19
+ At its core, this library overrides [stripe-ruby's](https://github.com/stripe/stripe-ruby)
20
+ request method to skip all http calls and
21
+ instead directly return test data. This allows you to write and run tests
22
+ without the need to actually hit stripe's servers.
23
+
24
+ You can use stripe-ruby-mock with any ruby testing library. Here's a quick dummy example with RSpec:
25
+
26
+ require 'stripe'
27
+ require 'stripe_mock'
28
+
29
+ describe MyApp do
30
+ before { StripeMock.start }
31
+ after { StripeMock.stop }
32
+
33
+ it "should create a stripe customer" do
34
+
35
+ # This doesn't touch stripe's servers nor the internet!
36
+ customer = Stripe::Customer.create({
37
+ email: 'johnny@appleseed.com',
38
+ card: 'void_card_token'
39
+ })
40
+ expect(customer.email).to eq('johnny@appleseed.com')
41
+ end
42
+ end
43
+
44
+ ## TODO
45
+
46
+ * Cover all stripe urls/methods
47
+ * Mock stripe error responses
48
+ * Create hash for storing/retrieving stripe objects in-memory
49
+
50
+ ## Copyright
51
+
52
+ Copyright (c) 2013 Gilbert
53
+
54
+ See LICENSE.txt for details.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ gem 'rubygems-tasks', '~> 0.2'
8
+ require 'rubygems/tasks'
9
+
10
+ Gem::Tasks.new
11
+ rescue LoadError => e
12
+ warn e.message
13
+ warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
14
+ end
15
+
16
+ begin
17
+ gem 'rspec', '~> 2.4'
18
+ require 'rspec/core/rake_task'
19
+
20
+ RSpec::Core::RakeTask.new
21
+ rescue LoadError => e
22
+ task :spec do
23
+ abort "Please run `gem install rspec` to install RSpec."
24
+ end
25
+ end
26
+
27
+ task :test => :spec
28
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ require 'stripe_mock/version'
2
+ 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
+
25
+ def self.alias_stripe_method(new_name, method_object)
26
+ Stripe.define_singleton_method(new_name) {|*args| method_object.call(*args) }
27
+ end
28
+
29
+ end
@@ -0,0 +1,281 @@
1
+ module StripeMock
2
+ module Data
3
+
4
+ def self.test_customer(params)
5
+ {
6
+ :subscription_history => [],
7
+ :bills => [],
8
+ :charges => [],
9
+ :livemode => false,
10
+ :object => "customer",
11
+ :id => "c_test_customer",
12
+ :active_card => {
13
+ :object => "card",
14
+ :last4 => "4242",
15
+ :type => "Visa",
16
+ :exp_month => 12,
17
+ :exp_year => 2013,
18
+ :fingerprint => "3TQGpK9JoY1GgXPw",
19
+ :country => "US",
20
+ :name => "From Here",
21
+ :address_line1 => nil,
22
+ :address_line2 => nil,
23
+ :address_city => nil,
24
+ :address_state => nil,
25
+ :address_zip => nil,
26
+ :address_country => nil,
27
+ :cvc_check => "pass",
28
+ :address_line1_check => nil,
29
+ :address_zip_check => nil
30
+ },
31
+ :created => 1304114758
32
+ }.merge(params)
33
+ end
34
+
35
+ def self.test_customer_array
36
+ {
37
+ :data => [test_customer, test_customer, test_customer],
38
+ :object => 'list',
39
+ :url => '/v1/customers'
40
+ }
41
+ end
42
+
43
+ def self.test_charge(params={})
44
+ {
45
+ :refunded => false,
46
+ :paid => true,
47
+ :amount => 100,
48
+ :card => {
49
+ :type => "Visa",
50
+ :last4 => "4242",
51
+ :exp_month => 11,
52
+ :country => "US",
53
+ :exp_year => 2012,
54
+ :id => "cc_test_card",
55
+ :object => "card"
56
+ },
57
+ :id => "ch_test_charge",
58
+ :reason => "execute_charge",
59
+ :livemode => false,
60
+ :currency => "usd",
61
+ :object => "charge",
62
+ :created => 1304114826
63
+ }.merge(params)
64
+ end
65
+
66
+ def self.test_charge_array
67
+ {
68
+ :data => [test_charge, test_charge, test_charge],
69
+ :object => 'list',
70
+ :url => '/v1/charges'
71
+ }
72
+ end
73
+
74
+ def self.test_card(params={})
75
+ {
76
+ :type => "Visa",
77
+ :last4 => "4242",
78
+ :exp_month => 11,
79
+ :country => "US",
80
+ :exp_year => 2012,
81
+ :id => "cc_test_card",
82
+ :object => "card"
83
+ }.merge(params)
84
+ end
85
+
86
+ def self.test_coupon(params={})
87
+ {
88
+ :duration => 'repeating',
89
+ :duration_in_months => 3,
90
+ :percent_off => 25,
91
+ :id => "co_test_coupon",
92
+ :object => "coupon"
93
+ }.merge(params)
94
+ end
95
+
96
+ #FIXME nested overrides would be better than hardcoding plan_id
97
+ def self.test_subscription(plan_id="gold")
98
+ {
99
+ :current_period_end => 1308681468,
100
+ :status => "trialing",
101
+ :plan => {
102
+ :interval => "month",
103
+ :amount => 7500,
104
+ :trial_period_days => 30,
105
+ :object => "plan",
106
+ :id => plan_id
107
+ },
108
+ :current_period_start => 1308595038,
109
+ :cancel_at_period_end => false,
110
+ :canceled_at => nil,
111
+ :start => 1308595038,
112
+ :object => "subscription",
113
+ :trial_start => 1308595038,
114
+ :trial_end => 1308681468,
115
+ :customer => "c_test_customer",
116
+ :quantity => 1
117
+ }
118
+ end
119
+
120
+ def self.test_invoice(params={})
121
+ {
122
+ :id => 'in_test_invoice',
123
+ :object => 'invoice',
124
+ :livemode => false,
125
+ :amount_due => 1000,
126
+ :attempt_count => 0,
127
+ :attempted => false,
128
+ :closed => false,
129
+ :currency => 'usd',
130
+ :customer => 'c_test_customer',
131
+ :date => 1349738950,
132
+ :lines => {
133
+ "invoiceitems" => [
134
+ {
135
+ :id => 'ii_test_invoice_item',
136
+ :object => '',
137
+ :livemode => false,
138
+ :amount => 1000,
139
+ :currency => 'usd',
140
+ :customer => 'c_test_customer',
141
+ :date => 1349738950,
142
+ :description => "A Test Invoice Item",
143
+ :invoice => 'in_test_invoice'
144
+ },
145
+ ],
146
+ },
147
+ :paid => false,
148
+ :period_end => 1349738950,
149
+ :period_start => 1349738950,
150
+ :starting_balance => 0,
151
+ :subtotal => 1000,
152
+ :total => 1000,
153
+ :charge => nil,
154
+ :discount => nil,
155
+ :ending_balance => nil,
156
+ :next_payemnt_attempt => 1349825350,
157
+ }.merge(params)
158
+ end
159
+
160
+ def self.test_paid_invoice
161
+ test_invoice.merge({
162
+ :attempt_count => 1,
163
+ :attempted => true,
164
+ :closed => true,
165
+ :paid => true,
166
+ :charge => 'ch_test_charge',
167
+ :ending_balance => 0,
168
+ :next_payment_attempt => nil,
169
+ })
170
+ end
171
+
172
+ def self.test_invoice_customer_array
173
+ {
174
+ :data => [test_invoice],
175
+ :object => 'list',
176
+ :url => '/v1/invoices?customer=test_customer'
177
+ }
178
+ end
179
+
180
+ def self.test_recipient(params={})
181
+ {
182
+ :name => "Stripe User",
183
+ :type => "individual",
184
+ :livemode => false,
185
+ :object => "recipient",
186
+ :id => "rp_test_recipient",
187
+ :active_account => {
188
+ :last4 => "6789",
189
+ :bank_name => "STRIPE TEST BANK",
190
+ :country => "US",
191
+ :object => "bank_account"
192
+ },
193
+ :created => 1304114758,
194
+ :verified => true
195
+ }.merge(params)
196
+ end
197
+
198
+ def self.test_recipient_array
199
+ {
200
+ :data => [test_recipient, test_recipient, test_recipient],
201
+ :object => 'list',
202
+ :url => '/v1/recipients'
203
+ }
204
+ end
205
+
206
+ def self.test_transfer(params={})
207
+ {
208
+ :status => 'pending',
209
+ :amount => 100,
210
+ :account => {
211
+ :object => 'bank_account',
212
+ :country => 'US',
213
+ :bank_name => 'STRIPE TEST BANK',
214
+ :last4 => '6789'
215
+ },
216
+ :recipient => 'test_recipient',
217
+ :fee => 0,
218
+ :fee_details => [],
219
+ :id => "tr_test_transfer",
220
+ :livemode => false,
221
+ :currency => "usd",
222
+ :object => "transfer",
223
+ :date => 1304114826
224
+ }.merge(params)
225
+ end
226
+
227
+ def self.test_transfer_array
228
+ {
229
+ :data => [test_transfer, test_transfer, test_transfer],
230
+ :object => 'list',
231
+ :url => '/v1/transfers'
232
+ }
233
+ end
234
+
235
+ def self.test_invalid_api_key_error
236
+ {
237
+ "error" => {
238
+ "type" => "invalid_request_error",
239
+ "message" => "Invalid API Key provided: invalid"
240
+ }
241
+ }
242
+ end
243
+
244
+ def self.test_invalid_exp_year_error
245
+ {
246
+ "error" => {
247
+ "code" => "invalid_expiry_year",
248
+ "param" => "exp_year",
249
+ "type" => "card_error",
250
+ "message" => "Your card's expiration year is invalid"
251
+ }
252
+ }
253
+ end
254
+
255
+ def self.test_missing_id_error
256
+ {
257
+ :error => {
258
+ :param => "id",
259
+ :type => "invalid_request_error",
260
+ :message => "Missing id"
261
+ }
262
+ }
263
+ end
264
+
265
+ def self.test_api_error
266
+ {
267
+ :error => {
268
+ :type => "api_error"
269
+ }
270
+ }
271
+ end
272
+
273
+ def self.test_delete_discount_response
274
+ {
275
+ :deleted => true,
276
+ :id => "di_test_coupon"
277
+ }
278
+ end
279
+
280
+ end
281
+ end
@@ -0,0 +1,25 @@
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
@@ -0,0 +1,4 @@
1
+ module StripeMock
2
+ # stripe-ruby-mock version
3
+ VERSION = "1.8.1.1"
4
+ end
data/spec/mock_spec.rb ADDED
@@ -0,0 +1,54 @@
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
@@ -0,0 +1,4 @@
1
+ gem 'rspec', '~> 2.4'
2
+ require 'rspec'
3
+ require 'stripe'
4
+ require 'stripe_mock/version'
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/stripe_mock/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "stripe-ruby-mock"
7
+ gem.version = StripeMock::VERSION
8
+ gem.summary = %q{TDD with stripe}
9
+ gem.description = %q{A drop-in library to test stripe without hitting their servers}
10
+ gem.license = "MIT"
11
+ gem.authors = ["Gilbert"]
12
+ gem.email = "gilbertbgarza@gmail.com"
13
+ gem.homepage = "https://github.com/mindeavor/stripe-ruby-mock#readme"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'stripe', '~> 1.8.1'
21
+
22
+ gem.add_development_dependency 'rspec', '~> 2.4'
23
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stripe-ruby-mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.8.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gilbert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: stripe
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.8.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.4'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.4'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rubygems-tasks
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.2'
62
+ description: A drop-in library to test stripe without hitting their servers
63
+ email: gilbertbgarza@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - .rspec
70
+ - ChangeLog.rdoc
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/stripe_mock.rb
75
+ - lib/stripe_mock/data.rb
76
+ - lib/stripe_mock/methods.rb
77
+ - lib/stripe_mock/version.rb
78
+ - spec/mock_spec.rb
79
+ - spec/spec_helper.rb
80
+ - stripe-ruby-mock.gemspec
81
+ homepage: https://github.com/mindeavor/stripe-ruby-mock#readme
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.25
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: TDD with stripe
106
+ test_files:
107
+ - spec/mock_spec.rb
108
+ - spec/spec_helper.rb