spreedly-core-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,204 @@
1
+ require 'test_helper'
2
+
3
+
4
+ # In order to run tests
5
+ # 1. cp test/config/spreedly_core.yml.example to test/config/spreedly_core.yml
6
+ # 2. Add your spreedly core credentials to test/config/spreedly_core.yml
7
+ module SpreedlyCore
8
+ class SpreedlyCoreTest < Test::Unit::TestCase
9
+ include TestHelper
10
+ include TestFactory
11
+
12
+ def setup
13
+ config = YAML.load(File.read(File.dirname(__FILE__) + '/config/spreedly_core.yml'))
14
+ SpreedlyCore.configure(config)
15
+ PaymentMethod.reset_additional_required_cc_fields
16
+ end
17
+
18
+ def test_mocked_500_error
19
+ with_disabled_network do
20
+ stub_request(:put, "#{mocked_base_uri_string}/payment_methods/FAKE.xml").
21
+ to_return(:body => '', :status => 500)
22
+ assert_raises InvalidResponse do
23
+ Base.verify_put('/payment_methods/FAKE.xml', :has_key => "test") {}
24
+ end
25
+ end
26
+ end
27
+
28
+ def test_can_get_payment_token
29
+ payment_method = given_a_payment_method(:master,
30
+ :credit_card => {:year => 2015})
31
+ assert_equal "John", payment_method.first_name
32
+ assert_equal "Foo", payment_method.last_name
33
+ assert_equal "XXX", payment_method.verification_value
34
+ assert payment_method.errors.empty?
35
+ assert_equal 4, payment_method.month
36
+ assert_equal 2015, payment_method.year
37
+ end
38
+
39
+ def test_can_find_payment_method
40
+ payment_method = given_a_payment_method
41
+ assert PaymentMethod.find(payment_method.token)
42
+ end
43
+
44
+ def test_not_found_payment_method
45
+ assert_raises InvalidResponse do
46
+ PaymentMethod.find("NOT-FOUND")
47
+ end
48
+ end
49
+
50
+ def test_can_retain_payment_method
51
+ given_a_retained_transaction
52
+ end
53
+
54
+ # Here we change the token to get an invalid response from spreedly core
55
+ def test_bad_response_on_retain
56
+ payment_method = given_a_payment_method
57
+ payment_method.instance_variable_set("@token", "NOT-FOUND")
58
+ assert_raises InvalidResponse do
59
+ payment_method.retain
60
+ end
61
+ end
62
+
63
+ def test_can_not_retain_after_redact
64
+ retained_transaction = given_a_retained_transaction
65
+ payment_method = retained_transaction.payment_method
66
+ redact_transaction = payment_method.redact
67
+ assert redact_transaction.succeeded?
68
+ retained_transaction2 = payment_method.retain
69
+ assert_false retained_transaction2.succeeded?
70
+ end
71
+
72
+ def test_can_redact_payment_method
73
+ given_a_redacted_transaction
74
+ end
75
+
76
+ # Here we change the token to get an invalid response from spreedly core
77
+ def test_bad_response_on_redact
78
+ payment_method = given_a_payment_method
79
+ payment_method.instance_variable_set("@token", "NOT-FOUND")
80
+ assert_raises InvalidResponse do
81
+ payment_method.redact
82
+ end
83
+ end
84
+
85
+ def test_can_make_purchase
86
+ given_a_purchase
87
+ end
88
+
89
+ # Here we change the token to get an invalid response from spreedly core
90
+ def test_bad_response_on_purchase
91
+ payment_method = given_a_payment_method
92
+ payment_method.instance_variable_set("@token", "NOT-FOUND")
93
+ assert_raises InvalidResponse do
94
+ payment_method.purchase(20)
95
+ end
96
+ end
97
+
98
+ def test_can_authorize
99
+ given_an_authorized_transaction
100
+ end
101
+
102
+ # Here we change the token to get an invalid response from spreedly core
103
+ def test_bad_response_on_authorize
104
+ payment_method = given_a_payment_method
105
+ payment_method.instance_variable_set("@token", "NOT-FOUND")
106
+ assert_raises InvalidResponse do
107
+ payment_method.authorize(20)
108
+ end
109
+ end
110
+
111
+ def test_payment_failed
112
+ payment_method = given_a_payment_method(:master, :card_number => :failed)
113
+
114
+ assert transaction = payment_method.purchase(100)
115
+ assert !transaction.succeeded?
116
+ assert_equal("Unable to obtain a successful response from the gateway.",
117
+ transaction.message)
118
+
119
+ assert_equal("Unable to process the transaction.", transaction.response.message)
120
+ end
121
+
122
+ def test_can_capture_after_authorize
123
+ given_a_capture
124
+ end
125
+
126
+ def test_can_capture_partial_after_authorize
127
+ given_a_capture 50
128
+ end
129
+
130
+ # Here we change the token to get an invalid response from spreedly core
131
+ def test_bad_response_on_capture_after_authorize
132
+ transaction = given_an_authorized_transaction
133
+ transaction.instance_variable_set("@token", "NOT-FOUND")
134
+ assert_raises InvalidResponse do
135
+ transaction.capture
136
+ end
137
+ end
138
+
139
+ def test_can_void_after_purchase
140
+ given_a_purchase_void
141
+ end
142
+
143
+ # Here we change the token to get an invalid response from spreedly core
144
+ def test_bad_response_on_void
145
+ purchase = given_a_purchase
146
+ purchase.instance_variable_set("@token", "NOT-FOUND")
147
+ assert_raises InvalidResponse do
148
+ purchase.void
149
+ end
150
+ end
151
+
152
+ def test_can_void_after_capture
153
+ given_a_capture_void
154
+ end
155
+
156
+ def test_can_credit_after_purchase
157
+ given_a_purchase_credit
158
+ end
159
+
160
+ # Here we change the token to get an invalid response from spreedly core
161
+ def test_bad_response_on_credit
162
+ purchase = given_a_purchase
163
+ purchase.instance_variable_set("@token", "NOT-FOUND")
164
+ assert_raises InvalidResponse do
165
+ purchase.credit
166
+ end
167
+ end
168
+
169
+ def test_can_credit_partial_after_purchase
170
+ given_a_purchase_credit(100, 50)
171
+ end
172
+
173
+ def test_can_credit_after_capture
174
+ given_a_capture_credit
175
+ end
176
+
177
+ def test_can_credit_partial_after_capture
178
+ given_a_capture_credit(50, 25)
179
+ end
180
+
181
+
182
+ def test_can_enforce_additional_payment_method_validations
183
+ PaymentMethod.additional_required_cc_fields :state
184
+
185
+ token = PaymentMethod.create_test_token(cc_data(:master))
186
+ assert payment_method = PaymentMethod.find(token)
187
+ assert !payment_method.valid?
188
+ assert_equal 1, payment_method.errors.size
189
+
190
+ assert_equal "State can't be blank", payment_method.errors.first
191
+
192
+ token = PaymentMethod.
193
+ create_test_token(cc_data(:master, :credit_card => {:state => "IL"}))
194
+
195
+ assert payment_method = PaymentMethod.find(token)
196
+
197
+ assert payment_method.valid?
198
+ end
199
+
200
+ def test_can_list_supported_gateways
201
+ assert Gateway.supported_gateways.any?
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,99 @@
1
+ module SpreedlyCore
2
+ module TestFactory
3
+ def given_a_payment_method(cc_card=:master, card_options={})
4
+ token = PaymentMethod.create_test_token cc_data(cc_card, card_options), "customer-42"
5
+ assert payment_method = PaymentMethod.find(token)
6
+ assert_equal "customer-42", payment_method.data
7
+ assert_equal token, payment_method.token
8
+ payment_method
9
+ end
10
+
11
+ def given_a_purchase(purchase_amount=100, ip_address='127.0.0.1')
12
+ payment_method = given_a_payment_method
13
+ assert transaction = payment_method.purchase(purchase_amount, nil, nil, ip_address=nil)
14
+ assert_equal purchase_amount, transaction.amount
15
+ assert_equal "USD", transaction.currency_code
16
+ assert_equal "Purchase", transaction.transaction_type
17
+ assert_equal ip_address, transaction.ip
18
+ assert transaction.succeeded?
19
+ transaction
20
+ end
21
+
22
+ def given_a_retained_transaction
23
+ payment_method = given_a_payment_method
24
+ assert transaction = payment_method.retain
25
+ assert transaction.succeeded?
26
+ assert_equal "RetainPaymentMethod", transaction.transaction_type
27
+ transaction
28
+ end
29
+
30
+ def given_a_redacted_transaction
31
+ retained_transaction = given_a_retained_transaction
32
+ assert payment_method = retained_transaction.payment_method
33
+ transaction = payment_method.redact
34
+ assert transaction.succeeded?
35
+ assert_equal "RedactPaymentMethod", transaction.transaction_type
36
+ assert !transaction.token.blank?
37
+ transaction
38
+ end
39
+
40
+ def given_an_authorized_transaction(amount=100, ip_address='127.0.0.1')
41
+ payment_method = given_a_payment_method
42
+ assert transaction = payment_method.authorize(100, nil, nil, ip_address)
43
+ assert_equal 100, transaction.amount
44
+ assert_equal "USD", transaction.currency_code
45
+ assert_equal ip_address, transaction.ip
46
+ assert_equal AuthorizeTransaction, transaction.class
47
+ transaction
48
+ end
49
+
50
+ def given_a_capture(amount=100, ip_address='127.0.0.1')
51
+ transaction = given_an_authorized_transaction(amount, ip_address)
52
+ capture = transaction.capture(amount, ip_address)
53
+ assert capture.succeeded?
54
+ assert_equal amount, capture.amount
55
+ assert_equal "Capture", capture.transaction_type
56
+ assert_equal ip_address, capture.ip
57
+ assert_equal CaptureTransaction, capture.class
58
+ capture
59
+ end
60
+
61
+ def given_a_purchase_void(ip_address='127.0.0.1')
62
+ purchase = given_a_purchase
63
+ assert void = purchase.void(ip_address)
64
+ assert_equal purchase.token, void.reference_token
65
+ assert_equal ip_address, void.ip
66
+ assert void.succeeded?
67
+ void
68
+ end
69
+
70
+ def given_a_capture_void(ip_address='127.0.0.1')
71
+ capture = given_a_capture
72
+ assert void = capture.void(ip_address)
73
+ assert_equal capture.token, void.reference_token
74
+ assert_equal ip_address, void.ip
75
+ assert void.succeeded?
76
+ void
77
+ end
78
+
79
+ def given_a_purchase_credit(purchase_amount=100, credit_amount=100, ip_address='127.0.0.1')
80
+ purchase = given_a_purchase(purchase_amount, ip_address)
81
+ given_a_credit(purchase, credit_amount, ip_address)
82
+ end
83
+
84
+ def given_a_capture_credit(capture_amount=100, credit_amount=100, ip_address='127.0.0.1')
85
+ capture = given_a_capture(capture_amount, ip_address)
86
+ given_a_credit(capture, credit_amount, ip_address)
87
+ end
88
+
89
+ def given_a_credit(trans, credit_amount=100, ip_address='127.0.0.1')
90
+ assert credit = trans.credit(credit_amount, ip_address)
91
+ assert_equal trans.token, credit.reference_token
92
+ assert_equal credit_amount, credit.amount
93
+ assert_equal ip_address, credit.ip
94
+ assert credit.succeeded?
95
+ assert CreditTransaction, credit.class
96
+ credit
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.setup(:default, :development)
7
+
8
+ Bundler.require(:default, :development)
9
+
10
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
11
+ require 'spreedly_core'
12
+ require 'spreedly_core/test_extensions'
13
+ require 'test_factory'
14
+
15
+ require 'webmock/test_unit'
16
+ # Allow real connections, see https://github.com/bblimke/webmock for more info
17
+ WebMock.allow_net_connect!
18
+
19
+ require 'webmock'
20
+
21
+ class Test::Unit::TestCase
22
+ def assert_false(test, failure_message=nil)
23
+ assert(!test, failure_message)
24
+ end
25
+
26
+ def with_disabled_network(&block)
27
+ WebMock.disable_net_connect!
28
+ block.call
29
+ ensure
30
+ WebMock.allow_net_connect!
31
+ end
32
+
33
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ module SpreedlyCore
4
+ class TransactionTest < Test::Unit::TestCase
5
+ include TestHelper
6
+ include TestFactory
7
+
8
+ def setup
9
+ config = YAML.load(File.read(File.dirname(__FILE__) + '/config/spreedly_core.yml'))
10
+ SpreedlyCore.configure(config)
11
+ PaymentMethod.reset_additional_required_cc_fields
12
+ end
13
+
14
+ def test_not_found_transaction
15
+ assert_raises InvalidResponse do
16
+ Transaction.find("NOT-FOUND")
17
+ end
18
+ end
19
+
20
+ def test_find_returns_retain_transaction_type
21
+ retain = given_a_retained_transaction
22
+ assert_find_transaction(retain, RetainTransaction)
23
+ end
24
+
25
+ def test_find_returns_redact_transaction_type
26
+ redact = given_a_redacted_transaction
27
+ assert_find_transaction(redact, RedactTransaction)
28
+ end
29
+
30
+ def test_find_returns_authorize_transaction_type
31
+ authorize = given_an_authorized_transaction
32
+ assert_find_transaction(authorize, AuthorizeTransaction)
33
+ end
34
+
35
+ def test_find_returns_purchase_transaction_type
36
+ purchase = given_a_purchase
37
+ assert_find_transaction(purchase, PurchaseTransaction)
38
+ end
39
+
40
+ def test_find_returns_capture_transaction_type
41
+ capture = given_a_capture
42
+ assert_find_transaction(capture, CaptureTransaction)
43
+ end
44
+
45
+ def test_find_returns_voided_transaction_type
46
+ void = given_a_capture_void
47
+ assert_find_transaction(void, VoidedTransaction)
48
+ end
49
+
50
+ def test_find_returns_credit_transaction_type
51
+ credit = given_a_capture_credit
52
+ assert_find_transaction(credit, CreditTransaction)
53
+ end
54
+
55
+ protected
56
+
57
+ def assert_find_transaction(trans, expected_class)
58
+ assert actual = Transaction.find(trans.token)
59
+ assert_equal expected_class, actual.class
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreedly-core-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spreedly
9
+ - 403 Labs
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-14 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ requirement: &70176154487420 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - =
21
+ - !ruby/object:Gem::Version
22
+ version: 0.7.7
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70176154487420
26
+ - !ruby/object:Gem::Dependency
27
+ name: ruby-debug19
28
+ requirement: &70176154486600 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70176154486600
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ requirement: &70176154411740 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - =
43
+ - !ruby/object:Gem::Version
44
+ version: 0.8.7
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70176154411740
48
+ - !ruby/object:Gem::Dependency
49
+ name: webmock
50
+ requirement: &70176154410180 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.2
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70176154410180
59
+ description:
60
+ email: support@spreedly.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - README.md
66
+ - Rakefile
67
+ - LICENSE
68
+ - lib/spreedly_core/base.rb
69
+ - lib/spreedly_core/gateway.rb
70
+ - lib/spreedly_core/payment_method.rb
71
+ - lib/spreedly_core/test_extensions.rb
72
+ - lib/spreedly_core/test_gateway.rb
73
+ - lib/spreedly_core/transactions.rb
74
+ - lib/spreedly_core/version.rb
75
+ - lib/spreedly_core.rb
76
+ - test/config/spreedly_core.yml.example
77
+ - test/configuration_test.rb
78
+ - test/factories.rb
79
+ - test/spreedly_core_test.rb
80
+ - test/test_factory.rb
81
+ - test/test_helper.rb
82
+ - test/transaction_test.rb
83
+ homepage: http://github.com/spreedly/spreedly-core-ruby
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: 2785672719963464473
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: 2785672719963464473
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.17
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Ruby API for Spreedly Core
113
+ test_files: []