webpay-mock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 71da9d72eb0dbbb1fea856fa17cb521ae26a79cf
4
+ data.tar.gz: 2e7a1f855395a7072c3287632b22715d68bd778c
5
+ SHA512:
6
+ metadata.gz: 4f62f0cf5f92a6654caf9a685c0005000a05a34296967dcea30fd82e4901d069eabecc29b39894f3a2513bc1df6866f43253fd8e5da342b18afa359b6a39b6b2
7
+ data.tar.gz: 0874e01c7dd5c5b9a310427f6060cd790caab9824be3c8c2e79783eef8ff8fac0185232c054640d5e605454fb012db84c02e761c912f85c8f5a2f87da258468c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-head
6
+ - rbx-2.1.1
7
+ script: "bundle exec rake"
8
+ notifications:
9
+ hipchat:
10
+ rooms:
11
+ secure: ykLsXlebzR9nnb+9ttpCySOg2hE4Ve699s2Owd7YahcZyVNxnG4q1P8Mps4jFlOgPNQ6JwkcB4A6QLavavtJlX7vXTDj1GD+zVAScjbtN/6ZV94S2eHA7hBUXHUwBmH0Cw5glrFoPEDBsQdOYbrOrJKwFNEhUTHdY0SRgEXv5Wo=
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webpay-mock.gemspec
4
+ gemspec
5
+
6
+ platforms :rbx do
7
+ gem 'rubysl'
8
+ gem 'json'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013- WebPay, Inc.
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,60 @@
1
+ # WebPay::Mock
2
+
3
+ WebPay::Mock helps development of WebPay client applications.
4
+
5
+ This generates a response object from request parameters, and wraps WebMock gem for easy end-to-end testing against webpay API server.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'webpay-mock'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install webpay-mock
20
+
21
+ ## Usage
22
+
23
+ ### RSpec
24
+
25
+ In `spec_helper.rb`,
26
+
27
+ ```ruby
28
+ require 'webpay-mock'
29
+
30
+ RSpec.configure do |c|
31
+ c.include WebPay::Mock::WebMockWrapper
32
+ end
33
+ ```
34
+
35
+ In your spec file,
36
+
37
+ ```ruby
38
+ let(:params) { { amount: 1000, currency: 'jpy', card: 'tok_xxxxxxxxx', description: 'test charge' } }
39
+ let!(:response) { webpay_stub(:charges, :create, params: params) }
40
+
41
+ specify { expect(WebPay::Charge.create(params).id).to eq response['id'] }
42
+ ```
43
+
44
+ See [our test cases](https://github.com/tomykaira/webpay-mock/blob/master/spec/webmock_wrapper_spec.rb) for more examples.
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
53
+
54
+ Fixed bugs and new features must be tested.
55
+
56
+ ## License
57
+
58
+ [The MIT License (MIT)](http://opensource.org/licenses/mit-license.html)
59
+
60
+ Copyright (c) 2013- WebPay, Inc.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ require "webpay/mock/version"
2
+
3
+ module WebPay
4
+ module Mock
5
+ # Your code goes here...
6
+ end
7
+ end
8
+
9
+ require 'webpay/mock/util'
10
+ require 'webpay/mock/builder'
11
+ require 'webpay/mock/fake_entity'
12
+ require 'webpay/mock/fake_error'
13
+ require 'webpay/mock/webmock_wrapper'
@@ -0,0 +1,39 @@
1
+ class WebPay::Mock::Builder
2
+ include WebPay::Mock::Util
3
+ PREFIX = {
4
+ charge: 'ch',
5
+ customer: 'cus',
6
+ token: 'tok',
7
+ event: 'evt',
8
+ account: 'acct'
9
+ }.freeze
10
+ ID_LETTERS = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
11
+
12
+ attr_reader :hash
13
+
14
+ def initialize(object)
15
+ @hash = {}
16
+ @hash['object'] = object.to_s
17
+ return if @hash['object'] == 'card'
18
+ @hash['id'] = PREFIX[object.to_sym] + '_' + 15.times.map { ID_LETTERS[rand(ID_LETTERS.length)] }.join
19
+ return if @hash['object'] == 'account'
20
+ @hash['livemode'] = false
21
+ @hash['created'] = Time.now.to_i
22
+ end
23
+
24
+ def []=(key, value)
25
+ @hash[key.to_s] = value
26
+ end
27
+
28
+ def set_from(hash, *allowed_keys)
29
+ allowed_string_keys = allowed_keys.map(&:to_s)
30
+ hash.each do |k, v|
31
+ @hash[k.to_s] = v if allowed_string_keys.empty? || allowed_string_keys.include?(k.to_s)
32
+ end
33
+ self
34
+ end
35
+
36
+ def build
37
+ stringify_keys(@hash)
38
+ end
39
+ end
@@ -0,0 +1,54 @@
1
+ require 'securerandom'
2
+
3
+ module WebPay::Mock::FakeEntity
4
+ include WebPay::Mock::Util
5
+
6
+ def customer_from(params, overrides = {}, base = {})
7
+ Customer.new(base).set_params(params).override(overrides).build
8
+ end
9
+
10
+ def charge_from(params, overrides = {}, base = {})
11
+ Charge.new(base).set_params(params).override(overrides).build
12
+ end
13
+
14
+ def token_from(params, overrides = {})
15
+ Token.new.set_params(params).override(overrides).build
16
+ end
17
+
18
+ def fake_event(overrides = {})
19
+ Event.new.override(overrides).build
20
+ end
21
+
22
+ def fake_account(overrides = {})
23
+ Account.new.override(overrides).build
24
+ end
25
+
26
+ def fake_list(path, proc)
27
+ {
28
+ 'object' => 'list',
29
+ 'url' => '/v1' + path,
30
+ 'count' => 3,
31
+ 'data' => [proc.call, proc.call, proc.call]
32
+ }
33
+ end
34
+
35
+ def card_from(params, overrides = {})
36
+ Card.new.set_params(params).override(overrides).build
37
+ end
38
+
39
+ def fake_card
40
+ Card.new.build
41
+ end
42
+
43
+ def fake_fingerprint
44
+ SecureRandom.hex(20)[0...40]
45
+ end
46
+ end
47
+
48
+ require 'webpay/mock/fake_entity/base'
49
+ require 'webpay/mock/fake_entity/card'
50
+ require 'webpay/mock/fake_entity/charge'
51
+ require 'webpay/mock/fake_entity/customer'
52
+ require 'webpay/mock/fake_entity/token'
53
+ require 'webpay/mock/fake_entity/event'
54
+ require 'webpay/mock/fake_entity/account'
@@ -0,0 +1,18 @@
1
+ module WebPay::Mock::FakeEntity
2
+ class Account < Base
3
+ def object_name
4
+ 'account'
5
+ end
6
+
7
+ def basic_attributes
8
+ { statement_descriptor: nil, details_submitted: false, charge_enabled: false, currencies_supported: ["jpy"], email: 'test@example.com' }
9
+ end
10
+
11
+ def copy_attributes
12
+ []
13
+ end
14
+
15
+ def conversion(key, value)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ class WebPay::Mock::FakeEntity::Base
2
+ include WebPay::Mock::Util
3
+ include WebPay::Mock::FakeEntity
4
+
5
+ attr_reader :builder
6
+
7
+ def initialize(base = {})
8
+ @builder = WebPay::Mock::Builder.new(object_name)
9
+ .set_from(basic_attributes)
10
+ .set_from(base)
11
+ end
12
+
13
+ def set_params(params = {})
14
+ params = stringify_keys(params)
15
+ @builder.set_from(params, *copy_attributes)
16
+ params.each do |k, v|
17
+ response = conversion(k, v)
18
+ @builder.set_from(response) if response
19
+ end
20
+ self
21
+ end
22
+
23
+ def override(overrides = {})
24
+ @builder.set_from(overrides)
25
+ self
26
+ end
27
+
28
+ def build
29
+ @builder.build
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ module WebPay::Mock::FakeEntity
2
+ class Card < Base
3
+ def object_name
4
+ 'card'
5
+ end
6
+
7
+ def basic_attributes
8
+ { exp_year: 2014,
9
+ exp_month: 11,
10
+ fingerprint: fake_fingerprint,
11
+ name: "KEI KUBO",
12
+ country: "JP",
13
+ type: "Visa",
14
+ cvc_check: "pass",
15
+ last4: "4242" }
16
+ end
17
+
18
+ def copy_attributes
19
+ [:exp_month, :exp_year, :name]
20
+ end
21
+
22
+ def conversion(key, value)
23
+ case key
24
+ when 'number'
25
+ { last4: value[-4..-1] }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ module WebPay::Mock::FakeEntity
2
+ class Charge < Base
3
+ def object_name
4
+ 'charge'
5
+ end
6
+
7
+ def basic_attributes
8
+ { amount: 1000, currency: 'jpy', amount_refunded: 0, paid: true, refunded: false, failure_message: nil, captured: true, expire_time: nil, card: fake_card }
9
+ end
10
+
11
+ def copy_attributes
12
+ [:amount, :currency, :description]
13
+ end
14
+
15
+ def conversion(key, value)
16
+ case key
17
+ when 'card'
18
+ { card: value.is_a?(Hash) ? card_from(value) : fake_card }
19
+ when 'customer'
20
+ { card: fake_card, customer: value }
21
+ when 'capture'
22
+ if value == false
23
+ {
24
+ captured: false,
25
+ paid: false,
26
+ expire_time: Time.now.to_i + 60 * 60 * 24 * 7
27
+ }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ module WebPay::Mock::FakeEntity
2
+ class Customer < Base
3
+ def object_name
4
+ 'customer'
5
+ end
6
+
7
+ def basic_attributes
8
+ { email: nil, description: nil, active_card: nil }
9
+ end
10
+
11
+ def copy_attributes
12
+ [:email, :description]
13
+ end
14
+
15
+ def conversion(key, value)
16
+ if key == 'card'
17
+ { active_card: value.is_a?(Hash) ? card_from(value) : fake_card }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module WebPay::Mock::FakeEntity
2
+ class Event < Base
3
+ def object_name
4
+ 'event'
5
+ end
6
+
7
+ def basic_attributes
8
+ { data: {'object' => Charge.new.build}, pending_webhooks: 0, type: 'charge.created' }
9
+ end
10
+
11
+ def copy_attributes
12
+ []
13
+ end
14
+
15
+ def conversion(key, value)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module WebPay::Mock::FakeEntity
2
+ class Token < Base
3
+ def object_name
4
+ 'token'
5
+ end
6
+
7
+ def basic_attributes
8
+ { card: fake_card, used: false }
9
+ end
10
+
11
+ def copy_attributes
12
+ []
13
+ end
14
+
15
+ def conversion(key, value)
16
+ if key == 'card'
17
+ { card: card_from(value) }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ module WebPay::Mock::FakeError
2
+ include WebPay::Mock::Util
3
+
4
+ def bad_request(overrides = {})
5
+ {
6
+ status: 400,
7
+ body: { error: {
8
+ 'type' => 'invalid_request_error',
9
+ 'message' => "can't save charge: Amount can't be blank",
10
+ 'param' => 'amount'
11
+ }.merge(stringify_keys(overrides)) }.to_json
12
+ }
13
+ end
14
+
15
+ def unauthorized(overrides = {})
16
+ {
17
+ status: 401,
18
+ body: { error: {
19
+ 'message' => "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY')."
20
+ }.merge(stringify_keys(overrides)) }.to_json
21
+ }
22
+ end
23
+
24
+ def card_error(overrides = {})
25
+ {
26
+ status: 402,
27
+ body: { error: {
28
+ 'type' => 'card_error',
29
+ 'message' => 'This card cannot be used.',
30
+ 'code' => 'card_declined'
31
+ }.merge(stringify_keys(overrides)) }.to_json
32
+ }
33
+ end
34
+
35
+ def not_found(overrides = {})
36
+ {
37
+ status: 404,
38
+ body: { error: {
39
+ 'type' => 'invalid_request_error',
40
+ 'message' => 'No such charge: ch_bBM4IJ0XF2VIch8',
41
+ 'param' => 'id'
42
+ }.merge(stringify_keys(overrides)) }.to_json
43
+ }
44
+ end
45
+
46
+ def internal_server_error(overrides = {})
47
+ {
48
+ status: 500,
49
+ body: { error: {
50
+ 'type' => 'api_error',
51
+ 'message' => 'Unkown error occured',
52
+ }.merge(stringify_keys(overrides)) }.to_json
53
+ }
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module WebPay::Mock::Util
2
+ def stringify_keys(hash)
3
+ Hash[hash.map { |k, v| [k.to_s, v] }]
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module WebPay
2
+ module Mock
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,111 @@
1
+ require 'webmock'
2
+ require 'json'
3
+
4
+ module WebPay::Mock::WebMockWrapper
5
+ include WebPay::Mock::Util
6
+ include WebPay::Mock::FakeEntity
7
+ include WebPay::Mock::FakeError
8
+ include WebMock::API
9
+
10
+ # Wrapper of "WebMock::API::stub_request()".
11
+ # This provides simple stubbing. Use "stub_request()" and fake response generators to control details.
12
+ #
13
+ # @param [String|Symbol] entity Entity type. One of charges, customers, tokens, events, account
14
+ # @param [String|Symbol] action Action, such as create, retrieve, update, all
15
+ # @param [Hash] options Specifies parameters for request and response.
16
+ # @option option [Hash] base Base object, e.g. the retrieved object on which update is called. Used only in update-like actions.
17
+ # @option option [Hash] params Request parameters (asserted using "with")
18
+ # @option option [Hash] overrides Additional parameters to override response attributes
19
+ # @option option [String] id Id to be included in the path
20
+ # @option option [String] base_url Default is 'https://api.webpay.jp/v1'
21
+ # @option option [Symbol] error Error type to return as the response
22
+ # @return [Hash] object to be returned as JSON
23
+ def webpay_stub(entity, action, options = {})
24
+ base = options.delete(:base) || {}
25
+ params = stringify_keys(options.delete(:params) || {})
26
+ overrides = options.delete(:overrides) || {}
27
+ id = params.delete('id') || options.delete(:id) || base['id']
28
+ base_url = options[:base_url] || 'https://api.webpay.jp/v1'
29
+
30
+ method, path, response =
31
+ case entity.to_sym
32
+ when :charge, :charges
33
+ case action.to_sym
34
+ when :create
35
+ [:post, '/charges', charge_from(params, overrides)]
36
+ when :retrieve
37
+ [:get, '/charges/:id', charge_from({}, { id: id }.merge(overrides))]
38
+ when :refund
39
+ change = { 'id' => id, 'amount_refunded' => params['amount'] }
40
+ change['refunded'] = !(params['amount'] && base['amount'] && params['amount'] != base['amount'])
41
+ [:post, '/charges/:id/refund', charge_from({}, change.merge(overrides), base)]
42
+ when :capture
43
+ [:post, '/charges/:id/capture',
44
+ charge_from({}, { 'id' => id, 'paid' => true, 'captured' => true }.merge(overrides), base)]
45
+ when :all
46
+ [:get, '/charges', fake_list('/charges', lambda { charge_from({}, overrides) })]
47
+ end
48
+ when :customer, :customers
49
+ case action.to_sym
50
+ when :create
51
+ [:post, '/customers', customer_from(params, overrides)]
52
+ when :retrieve
53
+ [:get, '/customers/:id', customer_from({}, { id: id }.merge(overrides))]
54
+ when :update
55
+ [:post, '/customers/:id', customer_from(params, overrides, base)]
56
+ when :delete
57
+ [:delete, '/customers/:id', { 'id' => id, 'deleted' => true }]
58
+ when :all
59
+ [:get, '/customers', fake_list('/customers', lambda { customer_from({}, overrides) })]
60
+ end
61
+ when :token, :tokens
62
+ case action.to_sym
63
+ when :create
64
+ [:post, '/tokens', token_from(params, overrides)]
65
+ when :retrieve
66
+ [:get, '/tokens/:id', token_from({}, { 'id' => id }.merge(overrides))]
67
+ end
68
+ when :event, :events
69
+ case action.to_sym
70
+ when :retrieve
71
+ [:get, '/events/:id', fake_event({ 'id' => id }.merge(overrides))]
72
+ when :all
73
+ [:get, '/events', fake_list('/events', lambda { fake_event(overrides) })]
74
+ end
75
+ when :account
76
+ case action.to_sym
77
+ when :retrieve
78
+ [:get, '/account', fake_account(overrides)]
79
+ when :delete_data
80
+ [:delete, '/account/data', { deleted: true }]
81
+ end
82
+ end
83
+
84
+ if path.include?(':id')
85
+ if id.nil? || id == ''
86
+ raise ArgumentError.new(":id in parameters is required")
87
+ end
88
+ path = path.gsub(':id', id)
89
+ end
90
+
91
+ spec =
92
+ case options[:error]
93
+ when :bad_request
94
+ bad_request
95
+ when :unauthorized
96
+ unauthorized
97
+ when :card_error
98
+ card_error
99
+ when :not_found
100
+ not_found
101
+ when :internal_server_error
102
+ internal_server_error
103
+ else # success
104
+ { body: response.to_json }
105
+ end
106
+
107
+ stub_request(method, base_url + path).with(params).to_return(spec)
108
+
109
+ JSON.parse(spec[:body])
110
+ end
111
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ describe WebPay::Mock::FakeEntity::Account do
3
+ include WebPay::Mock::FakeEntity
4
+
5
+ describe 'default' do
6
+ subject(:account) { fake_account }
7
+ specify { expect(account['id']).to start_with 'acct_' }
8
+ specify { expect(account['object']).to eq 'account' }
9
+ specify { expect(account['charge_enabled']).to eq false }
10
+ specify { expect(account['currencies_supported']).to eq ['jpy'] }
11
+ specify { expect(account['details_submitted']).to eq false }
12
+ specify { expect(account['email']).to eq 'test@example.com' }
13
+ specify { expect(account['statement_descriptor']).to eq nil }
14
+ end
15
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+ describe WebPay::Mock::FakeEntity::Charge do
3
+ include WebPay::Mock::FakeEntity
4
+
5
+ context 'params is empty' do
6
+ subject(:charge) { charge_from({}) }
7
+
8
+ specify { expect(charge['id']).to start_with 'ch_' }
9
+ specify { expect(charge['object']).to eq 'charge' }
10
+ specify { expect(charge['livemode']).to eq false }
11
+ specify { expect(charge['created']).to be_within(2).of(Time.now.to_i) }
12
+ specify { expect(charge['amount']).to eq 1000 }
13
+ specify { expect(charge['card']).to be_a Hash }
14
+ specify { expect(charge['currency']).to eq 'jpy' }
15
+ specify { expect(charge['paid']).to eq true }
16
+ specify { expect(charge['captured']).to eq true }
17
+ specify { expect(charge['refunded']).to eq false }
18
+ specify { expect(charge['amount_refunded']).to eq 0 }
19
+ specify { expect(charge['customer']).to eq nil }
20
+ specify { expect(charge['description']).to eq nil }
21
+ specify { expect(charge['failure_message']).to eq nil }
22
+ specify { expect(charge['expire_time']).to eq nil }
23
+
24
+ subject(:card) { charge['card'] }
25
+ specify { expect(card['object']).to eq 'card' }
26
+ specify { expect(card['exp_year']).to eq 2014 }
27
+ specify { expect(card['exp_month']).to eq 11 }
28
+ specify { expect(card['fingerprint']).to be_a String }
29
+ specify { expect(card['name']).to eq 'KEI KUBO' }
30
+ specify { expect(card['type']).to eq 'Visa' }
31
+ specify { expect(card['cvc_check']).to eq 'pass' }
32
+ specify { expect(card['last4']).to eq '4242' }
33
+ end
34
+
35
+ context 'params has amount, currency, description, card' do
36
+ let(:card_params) { {:number=>"4242-4242-4242-4242",
37
+ :exp_month=>"10",
38
+ :exp_year=>"2019",
39
+ :cvc=>"123",
40
+ :name=>"KIYOKI IPPYO"} }
41
+ subject(:charge) { charge_from({amount: 100, currency: 'usd', description: 'desc', card: card_params}) }
42
+
43
+ specify { expect(charge['amount']).to eq 100 }
44
+ specify { expect(charge['currency']).to eq 'usd' }
45
+ specify { expect(charge['description']).to eq 'desc' }
46
+
47
+ subject(:card) { charge['card'] }
48
+ specify { expect(card['object']).to eq 'card' }
49
+ specify { expect(card['exp_year']).to eq '2019' }
50
+ specify { expect(card['exp_month']).to eq '10' }
51
+ specify { expect(card['fingerprint']).to be_a String }
52
+ specify { expect(card['name']).to eq 'KIYOKI IPPYO' }
53
+ specify { expect(card['type']).to eq 'Visa' }
54
+ specify { expect(card['cvc_check']).to eq 'pass' }
55
+ specify { expect(card['last4']).to eq '4242' }
56
+ end
57
+
58
+ context 'params has customer' do
59
+ let(:customer_id) { 'cus_yyyyyyyyy' }
60
+ subject(:charge) { charge_from(customer: customer_id) }
61
+
62
+ specify { expect(charge['customer']).to eq customer_id }
63
+
64
+ subject(:card) { charge['card'] }
65
+ specify { expect(card['name']).to eq 'KEI KUBO' }
66
+ end
67
+
68
+ context 'capture is false' do
69
+ subject(:charge) { charge_from(capture: false) }
70
+
71
+ specify { expect(charge['captured']).to eq false }
72
+ specify { expect(charge['paid']).to eq false }
73
+ specify { expect(charge['expire_time']).to be_within(2).of(Time.now.to_i + 60 * 60 * 24 * 7) }
74
+ end
75
+
76
+ context 'base' do
77
+ it 'should be used as the default' do
78
+ expect(charge_from({}, {}, {'amount' => 300})['amount']).to eq 300
79
+ end
80
+
81
+ it 'should be weaker than params' do
82
+ expect(charge_from({amount: 500}, {}, { 'amount' => 300})['amount']).to eq 500
83
+ end
84
+
85
+ it 'should be weaker than overrides' do
86
+ expect(charge_from({amount: 500}, {amount: 999}, { 'amount' => 300})['amount']).to eq 999
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ describe WebPay::Mock::FakeEntity::Customer do
3
+ include WebPay::Mock::FakeEntity
4
+
5
+ context 'params is empty' do
6
+ subject(:customer) { customer_from({}) }
7
+
8
+ specify { expect(customer['id']).to start_with 'cus_' }
9
+ specify { expect(customer['object']).to eq 'customer' }
10
+ specify { expect(customer['livemode']).to eq false }
11
+ specify { expect(customer['created']).to be_within(2).of(Time.now.to_i) }
12
+ specify { expect(customer['email']).to eq nil }
13
+ specify { expect(customer['description']).to eq nil }
14
+ specify { expect(customer['active_card']).to eq nil }
15
+ end
16
+
17
+ context 'email and description is given' do
18
+ let(:email) { 'test@example.com' }
19
+ let(:description) { 'desc' }
20
+ subject(:customer) { customer_from(email: email, description: description) }
21
+
22
+ specify { expect(customer['email']).to eq email }
23
+ specify { expect(customer['description']).to eq description }
24
+ end
25
+
26
+ context 'card is hash' do
27
+ let(:card_params) { {:number=>"4242-4242-4242-4242",
28
+ :exp_month=>"10",
29
+ :exp_year=>"2019",
30
+ :cvc=>"123",
31
+ :name=>"KIYOKI IPPYO"} }
32
+ subject(:card) { customer_from(card: card_params)['active_card'] }
33
+
34
+ specify { expect(card['object']).to eq 'card' }
35
+ specify { expect(card['exp_year']).to eq '2019' }
36
+ specify { expect(card['exp_month']).to eq '10' }
37
+ specify { expect(card['fingerprint']).to be_a String }
38
+ specify { expect(card['name']).to eq 'KIYOKI IPPYO' }
39
+ specify { expect(card['type']).to eq 'Visa' }
40
+ specify { expect(card['cvc_check']).to eq 'pass' }
41
+ specify { expect(card['last4']).to eq '4242' }
42
+ end
43
+
44
+ context 'card is token' do
45
+ subject(:card) { customer_from(card: 'tok_xxxxxxxxxxxx')['active_card'] }
46
+
47
+ specify { expect(card['object']).to eq 'card' }
48
+ specify { expect(card['exp_year']).to eq 2014 }
49
+ specify { expect(card['exp_month']).to eq 11 }
50
+ specify { expect(card['fingerprint']).to be_a String }
51
+ specify { expect(card['name']).to eq 'KEI KUBO' }
52
+ specify { expect(card['type']).to eq 'Visa' }
53
+ specify { expect(card['cvc_check']).to eq 'pass' }
54
+ specify { expect(card['last4']).to eq '4242' }
55
+ end
56
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ describe WebPay::Mock::FakeEntity::Event do
3
+ include WebPay::Mock::FakeEntity
4
+
5
+ describe 'default' do
6
+ subject(:event) { fake_event }
7
+ specify { expect(event['id']).to start_with 'evt_' }
8
+ specify { expect(event['object']).to eq 'event' }
9
+ specify { expect(event['livemode']).to eq false }
10
+ specify { expect(event['created']).to be_within(2).of(Time.now.to_i) }
11
+ specify { expect(event['data']).to be_a Hash }
12
+ specify { expect(event['pending_webhooks']).to eq 0 }
13
+ specify { expect(event['type']).to eq 'charge.created' }
14
+
15
+ subject(:data) { event['data'] }
16
+ specify { expect(data['object']['object']).to eq 'charge' }
17
+ specify { expect(data['previous_attributes']).to eq nil }
18
+ end
19
+
20
+ describe 'override for customer.updated event' do
21
+ let(:customer) { customer_from(email: 'test@example.com') }
22
+ let(:previous) { { 'email' => 'old@example.com' } }
23
+ subject(:event) { fake_event(data: { 'object' => customer, 'previous_attributes' => previous }) }
24
+
25
+ subject(:data) { event['data'] }
26
+ specify { expect(data['object']).to eq customer }
27
+ specify { expect(data['previous_attributes']).to eq previous }
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ describe WebPay::Mock::FakeEntity::Token do
3
+ include WebPay::Mock::FakeEntity
4
+
5
+ context 'params is empty' do
6
+ subject(:token) { token_from({}) }
7
+
8
+ specify { expect(token['id']).to start_with 'tok_' }
9
+ specify { expect(token['object']).to eq 'token' }
10
+ specify { expect(token['livemode']).to eq false }
11
+ specify { expect(token['created']).to be_within(2).of(Time.now.to_i) }
12
+ specify { expect(token['card']).to be_a Hash }
13
+ specify { expect(token['used']).to eq false }
14
+
15
+ subject(:card) { token['card'] }
16
+ specify { expect(card['object']).to eq 'card' }
17
+ specify { expect(card['exp_year']).to eq 2014 }
18
+ specify { expect(card['exp_month']).to eq 11 }
19
+ specify { expect(card['fingerprint']).to be_a String }
20
+ specify { expect(card['name']).to eq 'KEI KUBO' }
21
+ specify { expect(card['type']).to eq 'Visa' }
22
+ specify { expect(card['cvc_check']).to eq 'pass' }
23
+ specify { expect(card['last4']).to eq '4242' }
24
+ end
25
+
26
+ context 'params has card' do
27
+ let(:card_params) { {:number=>"4242-4242-4242-9898",
28
+ :exp_month=>"10",
29
+ :exp_year=>"2019",
30
+ :cvc=>"123",
31
+ :name=>"KIYOKI IPPYO"} }
32
+ subject(:card) { token_from({card: card_params})['card'] }
33
+
34
+ specify { expect(card['exp_year']).to eq '2019' }
35
+ specify { expect(card['exp_month']).to eq '10' }
36
+ specify { expect(card['name']).to eq 'KIYOKI IPPYO' }
37
+ specify { expect(card['last4']).to eq '9898' }
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ describe WebPay::Mock::FakeEntity do
3
+ include WebPay::Mock::FakeEntity
4
+
5
+ describe 'fake_list' do
6
+ subject(:list) { i = 0; fake_list('/numbers', lambda { i += 1 }) }
7
+ specify { expect(list['object']).to eq 'list' }
8
+ specify { expect(list['url']).to eq '/v1/numbers' }
9
+ specify { expect(list['count']).to eq 3 }
10
+ specify { expect(list['data']).to eq [1, 2, 3] }
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ require 'webmock/rspec'
2
+ require 'webpay/mock'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ config.order = 'random'
14
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+ require 'webpay'
3
+
4
+ describe WebPay::Mock::WebMockWrapper do
5
+ # In RSpec:
6
+ #
7
+ # RSpec.configure do |c|
8
+ # c.include WebPay::Mock::WebMockWrapper
9
+ # end
10
+ include WebPay::Mock::WebMockWrapper
11
+
12
+ describe 'charges' do
13
+ describe 'create' do
14
+ let(:params) { { amount: 1000, currency: 'jpy', card: 'tok_xxxxxxxxx', description: 'test charge' } }
15
+ let!(:response) { webpay_stub(:charges, :create, params: params) }
16
+
17
+ specify { expect(WebPay::Charge.create(params).id).to eq response['id'] }
18
+ end
19
+
20
+ describe 'retrieve' do
21
+ let(:id) { 'ch_xxxxxxxxx' }
22
+ before { webpay_stub(:charges, :retrieve, id: id) }
23
+ specify { expect(WebPay::Charge.retrieve(id).id).to eq id }
24
+ end
25
+
26
+ describe 'refund' do
27
+ let(:id) { 'ch_xxxxxxxxx' }
28
+ let!(:retrieved) { webpay_stub(:charges, :retrieve, id: id) }
29
+ let!(:refunded) { webpay_stub(:charges, :refund, params: { 'amount' => retrieved['amount'] }, base: retrieved) }
30
+ specify { expect(WebPay::Charge.retrieve(id).refund.refunded).to eq true }
31
+ end
32
+
33
+ describe 'capture' do
34
+ let(:id) { 'ch_xxxxxxxxx' }
35
+ let!(:retrieved) { webpay_stub(:charges, :retrieve, id: id, overrides: { captured: false } ) }
36
+ let!(:captured) { webpay_stub(:charges, :capture, base: retrieved) }
37
+ specify { expect(WebPay::Charge.retrieve(id).captured).to eq false }
38
+ specify { expect(WebPay::Charge.retrieve(id).capture.captured).to eq true }
39
+ end
40
+
41
+ describe 'all' do
42
+ before { webpay_stub(:charges, :all) }
43
+ specify { expect(WebPay::Charge.all.count).to eq 3 }
44
+ end
45
+ end
46
+
47
+ describe 'customers' do
48
+ describe 'create' do
49
+ let!(:response) { webpay_stub(:customers, :create, params: {}) }
50
+ specify { expect(WebPay::Customer.create({}).id).to eq response['id'] }
51
+ end
52
+
53
+ describe 'retrieve' do
54
+ let(:id) { 'cus_xxxxxxxxx' }
55
+ before { webpay_stub(:customers, :retrieve, id: id) }
56
+ specify { expect(WebPay::Customer.retrieve(id).id).to eq id }
57
+ end
58
+
59
+ describe 'update' do
60
+ let(:id) { 'cus_xxxxxxxxx' }
61
+ let!(:retrieved) { webpay_stub(:customers, :retrieve, id: id) }
62
+ let!(:updated) { webpay_stub(:customers, :update, params: { 'email' => 'new@example.com' }, base: retrieved) }
63
+ specify do
64
+ customer = WebPay::Customer.retrieve(id)
65
+ customer.email = 'new@example.com'
66
+ customer.save
67
+ expect(customer.email).to eq 'new@example.com'
68
+ end
69
+ end
70
+
71
+ describe 'delete' do
72
+ let(:id) { 'cus_xxxxxxxxx' }
73
+ let!(:retrieved) { webpay_stub(:customers, :retrieve, id: id) }
74
+ let!(:updated) { webpay_stub(:customers, :delete, id: id) }
75
+ specify do
76
+ customer = WebPay::Customer.retrieve(id)
77
+ expect(customer.delete).to eq true
78
+ end
79
+ end
80
+
81
+ describe 'all' do
82
+ before { webpay_stub(:customers, :all) }
83
+ specify { expect(WebPay::Customer.all.count).to eq 3 }
84
+ end
85
+ end
86
+
87
+ describe 'token' do
88
+ describe 'create' do
89
+ let(:card_params) { {:number=>"4242-4242-4242-4242",
90
+ :exp_month=>"10",
91
+ :exp_year=>"2019",
92
+ :cvc=>"123",
93
+ :name=>"KIYOKI IPPYO"} }
94
+ let!(:response) { webpay_stub(:tokens, :create, params: card_params) }
95
+ specify { expect(WebPay::Token.create(card: card_params).id).to eq response['id'] }
96
+ end
97
+
98
+ describe 'retrieve' do
99
+ let(:id) { 'tok_xxxxxxxxx' }
100
+ before { webpay_stub(:tokens, :retrieve, id: id) }
101
+ specify { expect(WebPay::Token.retrieve(id).id).to eq id }
102
+ end
103
+ end
104
+
105
+ describe 'events' do
106
+ describe 'retrieve' do
107
+ let(:id) { 'evt_xxxxxxxxx' }
108
+ before { webpay_stub(:events, :retrieve, id: id) }
109
+ specify { expect(WebPay::Event.retrieve(id).id).to eq id }
110
+ end
111
+
112
+ describe 'all' do
113
+ before { webpay_stub(:events, :all) }
114
+ specify { expect(WebPay::Event.all.count).to eq 3 }
115
+ end
116
+ end
117
+
118
+ describe 'account' do
119
+ describe 'retrieve' do
120
+ before { webpay_stub(:account, :retrieve) }
121
+ specify { expect(WebPay::Account.retrieve.id).to start_with 'acct_' }
122
+ end
123
+
124
+ describe 'delete_data' do
125
+ before { webpay_stub(:account, :delete_data) }
126
+ specify { expect(WebPay::Account.delete_data).to eq true }
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webpay/mock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "webpay-mock"
8
+ spec.version = WebPay::Mock::VERSION
9
+ spec.authors = ["webpay", "tomykaira"]
10
+ spec.email = ['administrators@webpay.jp', 'tomykaira@webpay.jp']
11
+ spec.description = 'WebPay::Mock helps development of WebPay client applications'
12
+ spec.summary = 'Dummy response generator and wrapper of webmock gem for WebPay'
13
+ spec.homepage = 'https://webpay.jp'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'webmock', '~> 1.13.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'rspec', '~> 2.14.0'
26
+ spec.add_development_dependency 'webpay', '~> 2.1.0'
27
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpay-mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - webpay
8
+ - tomykaira
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: webmock
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 1.13.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 1.13.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '1.3'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 2.14.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.14.0
70
+ - !ruby/object:Gem::Dependency
71
+ name: webpay
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 2.1.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: 2.1.0
84
+ description: WebPay::Mock helps development of WebPay client applications
85
+ email:
86
+ - administrators@webpay.jp
87
+ - tomykaira@webpay.jp
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - .gitignore
93
+ - .travis.yml
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/webpay/mock.rb
99
+ - lib/webpay/mock/builder.rb
100
+ - lib/webpay/mock/fake_entity.rb
101
+ - lib/webpay/mock/fake_entity/account.rb
102
+ - lib/webpay/mock/fake_entity/base.rb
103
+ - lib/webpay/mock/fake_entity/card.rb
104
+ - lib/webpay/mock/fake_entity/charge.rb
105
+ - lib/webpay/mock/fake_entity/customer.rb
106
+ - lib/webpay/mock/fake_entity/event.rb
107
+ - lib/webpay/mock/fake_entity/token.rb
108
+ - lib/webpay/mock/fake_error.rb
109
+ - lib/webpay/mock/util.rb
110
+ - lib/webpay/mock/version.rb
111
+ - lib/webpay/mock/webmock_wrapper.rb
112
+ - spec/fake_entity/account_spec.rb
113
+ - spec/fake_entity/charge_spec.rb
114
+ - spec/fake_entity/customer_spec.rb
115
+ - spec/fake_entity/event_spec.rb
116
+ - spec/fake_entity/token_spec.rb
117
+ - spec/fake_entity_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/webmock_wrapper_spec.rb
120
+ - webpay-mock.gemspec
121
+ homepage: https://webpay.jp
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.0.14
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Dummy response generator and wrapper of webmock gem for WebPay
145
+ test_files:
146
+ - spec/fake_entity/account_spec.rb
147
+ - spec/fake_entity/charge_spec.rb
148
+ - spec/fake_entity/customer_spec.rb
149
+ - spec/fake_entity/event_spec.rb
150
+ - spec/fake_entity/token_spec.rb
151
+ - spec/fake_entity_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spec/webmock_wrapper_spec.rb