tanga_active_utils 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ # using the default shipit config
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_utils/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tanga_active_utils"
7
+ s.version = ActiveUtils::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Shopify"]
10
+ s.email = ["developers@jadedpixel.com"]
11
+ s.homepage = "http://github.com/shopify/active_utils"
12
+ s.summary = %q{Common utils used by active_merchant, active_fulfillment, and active_shipping}
13
+ s.license = 'MIT'
14
+
15
+ s.rubyforge_project = "active_utils"
16
+
17
+ s.add_dependency('activesupport', '>= 2.3.11')
18
+ s.add_dependency('i18n')
19
+
20
+ s.add_development_dependency('rake')
21
+ s.add_development_dependency('mocha')
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_utils'
2
+
3
+ require 'minitest/autorun'
4
+
5
+ require 'mocha'
6
+ require 'mocha/mini_test'
7
+
8
+ include ActiveMerchant
@@ -0,0 +1,177 @@
1
+ require 'test_helper'
2
+
3
+ class ConnectionTest < Minitest::Test
4
+
5
+ def setup
6
+ @ok = stub(:code => 200, :message => 'OK', :body => 'success')
7
+
8
+ @endpoint = 'https://example.com/tx.php'
9
+ @connection = ActiveMerchant::Connection.new(@endpoint)
10
+ @connection.logger = stub(:info => nil, :debug => nil, :error => nil)
11
+ end
12
+
13
+ def test_connection_endpoint_parses_string_to_uri
14
+ assert_equal URI.parse(@endpoint), @connection.endpoint
15
+ end
16
+
17
+ def test_connection_endpoint_accepts_uri
18
+ endpoint = URI.parse(@endpoint)
19
+ connection = ActiveMerchant::Connection.new(endpoint)
20
+ assert_equal endpoint, connection.endpoint
21
+ end
22
+
23
+ def test_connection_endpoint_raises_uri_error
24
+ assert_raises URI::InvalidURIError do
25
+ ActiveMerchant::Connection.new("not a URI")
26
+ end
27
+ end
28
+
29
+ def test_successful_get_request
30
+ @connection.logger.expects(:info).twice
31
+ Net::HTTP.any_instance.expects(:get).with('/tx.php', {}).returns(@ok)
32
+ response = @connection.request(:get, nil, {})
33
+ assert_equal 'success', response.body
34
+ end
35
+
36
+ def test_successful_post_request
37
+ Net::HTTP.any_instance.expects(:post).with('/tx.php', 'data', ActiveMerchant::Connection::RUBY_184_POST_HEADERS).returns(@ok)
38
+ response = @connection.request(:post, 'data', {})
39
+ assert_equal 'success', response.body
40
+ end
41
+
42
+ def test_successful_put_request
43
+ Net::HTTP.any_instance.expects(:put).with('/tx.php', 'data', {}).returns(@ok)
44
+ response = @connection.request(:put, 'data', {})
45
+ assert_equal 'success', response.body
46
+ end
47
+
48
+ def test_successful_delete_request
49
+ Net::HTTP.any_instance.expects(:delete).with('/tx.php', {}).returns(@ok)
50
+ response = @connection.request(:delete, nil, {})
51
+ assert_equal 'success', response.body
52
+ end
53
+
54
+ def test_get_raises_argument_error_if_passed_data
55
+ assert_raises(ArgumentError) do
56
+ @connection.request(:get, 'data', {})
57
+ end
58
+ end
59
+
60
+ def test_request_raises_when_request_method_not_supported
61
+ assert_raises(ArgumentError) do
62
+ @connection.request(:head, nil, {})
63
+ end
64
+ end
65
+
66
+ def test_override_max_retries
67
+ refute_equal 1, @connection.max_retries
68
+ @connection.max_retries = 1
69
+ assert_equal 1, @connection.max_retries
70
+ end
71
+
72
+ def test_override_ssl_version
73
+ refute_equal :SSLv3, @connection.ssl_version
74
+ @connection.ssl_version = :SSLv3
75
+ assert_equal :SSLv3, @connection.ssl_version
76
+ end
77
+
78
+ def test_default_read_timeout
79
+ assert_equal ActiveMerchant::Connection::READ_TIMEOUT, @connection.read_timeout
80
+ end
81
+
82
+ def test_override_read_timeout
83
+ @connection.read_timeout = 20
84
+ assert_equal 20, @connection.read_timeout
85
+ end
86
+
87
+ def test_default_open_timeout
88
+ @connection.open_timeout = 20
89
+ assert_equal 20, @connection.open_timeout
90
+ end
91
+
92
+ def test_default_verify_peer
93
+ assert_equal ActiveMerchant::Connection::VERIFY_PEER, @connection.verify_peer
94
+ end
95
+
96
+ def test_override_verify_peer
97
+ @connection.verify_peer = false
98
+ assert_equal false, @connection.verify_peer
99
+ end
100
+
101
+ def test_default_ca_file
102
+ assert_equal ActiveMerchant::Connection::CA_FILE, @connection.ca_file
103
+ assert_equal ActiveMerchant::Connection::CA_FILE, @connection.send(:http).ca_file
104
+ end
105
+
106
+ def test_override_ca_file
107
+ @connection.ca_file = "/bogus"
108
+ assert_equal "/bogus", @connection.ca_file
109
+ assert_equal "/bogus", @connection.send(:http).ca_file
110
+ end
111
+
112
+ def test_default_ca_path
113
+ assert_equal ActiveMerchant::Connection::CA_PATH, @connection.ca_path
114
+ assert_equal ActiveMerchant::Connection::CA_PATH, @connection.send(:http).ca_path
115
+ end
116
+
117
+ def test_override_ca_path
118
+ @connection.ca_path = "/bogus"
119
+ assert_equal "/bogus", @connection.ca_path
120
+ assert_equal "/bogus", @connection.send(:http).ca_path
121
+ end
122
+
123
+ def test_unrecoverable_exception
124
+ @connection.logger.expects(:info).once
125
+ Net::HTTP.any_instance.expects(:post).raises(EOFError)
126
+
127
+ assert_raises(ActiveMerchant::ConnectionError) do
128
+ @connection.request(:post, '')
129
+ end
130
+ end
131
+
132
+ def test_failure_then_success_with_recoverable_exception
133
+ @connection.logger.expects(:info).never
134
+ Net::HTTP.any_instance.expects(:post).times(2).raises(Errno::ECONNREFUSED).then.returns(@ok)
135
+
136
+ @connection.request(:post, '')
137
+ end
138
+
139
+ def test_failure_limit_reached
140
+ @connection.logger.expects(:info).once
141
+ Net::HTTP.any_instance.expects(:post).times(ActiveMerchant::Connection::MAX_RETRIES).raises(Errno::ECONNREFUSED)
142
+
143
+ assert_raises(ActiveMerchant::ConnectionError) do
144
+ @connection.request(:post, '')
145
+ end
146
+ end
147
+
148
+ def test_failure_then_success_with_retry_safe_enabled
149
+ Net::HTTP.any_instance.expects(:post).times(2).raises(EOFError).then.returns(@ok)
150
+
151
+ @connection.retry_safe = true
152
+
153
+ @connection.request(:post, '')
154
+ end
155
+
156
+ def test_mixture_of_failures_with_retry_safe_enabled
157
+ Net::HTTP.any_instance.expects(:post).times(3).raises(Errno::EHOSTUNREACH).
158
+ raises(Errno::ECONNREFUSED).
159
+ raises(EOFError)
160
+
161
+ @connection.retry_safe = true
162
+
163
+ assert_raises(ActiveMerchant::ConnectionError) do
164
+ @connection.request(:post, '')
165
+ end
166
+ end
167
+
168
+ def test_failure_with_ssl_certificate
169
+ @connection.logger.expects(:error).once
170
+ Net::HTTP.any_instance.expects(:post).raises(OpenSSL::X509::CertificateError)
171
+
172
+ assert_raises(ActiveMerchant::ClientCertificateError) do
173
+ @connection.request(:post, '')
174
+ end
175
+ end
176
+
177
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class CountryCodeTest < Minitest::Test
4
+ def test_alpha2_country_code
5
+ code = CountryCode.new('CA')
6
+ assert_equal 'CA', code.value
7
+ assert_equal 'CA', code.to_s
8
+ assert_equal :alpha2, code.format
9
+ end
10
+
11
+ def test_lower_alpha2_country_code
12
+ code = CountryCode.new('ca')
13
+ assert_equal 'CA', code.value
14
+ assert_equal 'CA', code.to_s
15
+ assert_equal :alpha2, code.format
16
+ end
17
+
18
+ def test_alpha2_country_code
19
+ code = CountryCode.new('CAN')
20
+ assert_equal :alpha3, code.format
21
+ end
22
+
23
+ def test_numeric_code
24
+ code = CountryCode.new('004')
25
+ assert_equal :numeric, code.format
26
+ end
27
+
28
+ def test_invalid_code_format
29
+ assert_raises(CountryCodeFormatError){ CountryCode.new('Canada') }
30
+ end
31
+ end
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ class CountryTest < Minitest::Test
4
+ def test_country_from_hash
5
+ country = Country.new(:name => 'Canada', :alpha2 => 'CA', :alpha3 => 'CAN', :numeric => '124')
6
+ assert_equal 'CA', country.code(:alpha2).value
7
+ assert_equal 'CAN', country.code(:alpha3).value
8
+ assert_equal '124', country.code(:numeric).value
9
+ assert_equal 'Canada', country.to_s
10
+ end
11
+
12
+ def test_country_for_alpha2_code
13
+ country = Country.find('CA')
14
+ assert_equal 'CA', country.code(:alpha2).value
15
+ assert_equal 'CAN', country.code(:alpha3).value
16
+ assert_equal '124', country.code(:numeric).value
17
+ assert_equal 'Canada', country.to_s
18
+ end
19
+
20
+ def test_country_for_alpha3_code
21
+ country = Country.find('CAN')
22
+ assert_equal 'Canada', country.to_s
23
+ end
24
+
25
+ def test_country_for_numeric_code
26
+ country = Country.find('124')
27
+ assert_equal 'Canada', country.to_s
28
+ end
29
+
30
+ def test_find_country_by_name
31
+ country = Country.find('Canada')
32
+ assert_equal 'Canada', country.to_s
33
+ end
34
+
35
+ def test_find_unknown_country_name
36
+ assert_raises(InvalidCountryCodeError) do
37
+ Country.find('Asskickistan')
38
+ end
39
+ end
40
+
41
+ def test_find_australia
42
+ country = Country.find('AU')
43
+ assert_equal 'AU', country.code(:alpha2).value
44
+
45
+ country = Country.find('Australia')
46
+ assert_equal 'AU', country.code(:alpha2).value
47
+ end
48
+
49
+ def test_find_united_kingdom
50
+ country = Country.find('GB')
51
+ assert_equal 'GB', country.code(:alpha2).value
52
+
53
+ country = Country.find('United Kingdom')
54
+ assert_equal 'GB', country.code(:alpha2).value
55
+ end
56
+
57
+ def test_raise_on_nil_name
58
+ assert_raises(InvalidCountryCodeError) do
59
+ Country.find(nil)
60
+ end
61
+ end
62
+
63
+ def test_country_names_are_alphabetized
64
+ country_names = Country::COUNTRIES.map { | each | each[:name] }
65
+ assert_equal(country_names.sort, country_names)
66
+ end
67
+
68
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class CurrencyCodeTest < Minitest::Test
4
+ def test_is_iso_should_return_true_for_iso_currencies
5
+ assert CurrencyCode.is_iso?('CAD')
6
+ assert CurrencyCode.is_iso?('USD')
7
+ assert CurrencyCode.is_iso?('TWD')
8
+ end
9
+
10
+ def test_is_iso_should_return_false_for_non_iso_currencies
11
+ assert !CurrencyCode.is_iso?('NTD')
12
+ assert !CurrencyCode.is_iso?('RMB')
13
+ end
14
+
15
+ def test_standardize_should_not_change_iso_currencies
16
+ assert_equal 'CAD', CurrencyCode.standardize('CAD')
17
+ assert_equal 'USD', CurrencyCode.standardize('usd')
18
+ assert_equal 'TWD', CurrencyCode.standardize('TWD')
19
+ end
20
+
21
+ def test_standardize_should_convert_known_non_iso_to_iso
22
+ assert_equal 'TWD', CurrencyCode.standardize('NTD')
23
+ assert_equal 'CNY', CurrencyCode.standardize('rmb')
24
+ end
25
+
26
+ def test_standardize_should_raise_for_unknwon_currencies
27
+ assert_raises CurrencyCode::InvalidCurrencyCodeError do
28
+ CurrencyCode.standardize('Not Real')
29
+ end
30
+ end
31
+
32
+ def test_nil_code_should_raise_InvalidCurrencyCodeError
33
+ assert_raises CurrencyCode::InvalidCurrencyCodeError do
34
+ CurrencyCode.standardize(nil)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,141 @@
1
+ require 'test_helper'
2
+ require 'openssl'
3
+ require 'net/http'
4
+
5
+ class NetworkConnectionRetriesTest < Minitest::Test
6
+ class MyNewError < StandardError
7
+ end
8
+
9
+ include NetworkConnectionRetries
10
+
11
+ def setup
12
+ @logger = stubs(:logger)
13
+ @requester = stubs(:requester)
14
+ @ok = stub(:code => 200, :message => 'OK', :body => 'success')
15
+ end
16
+
17
+ def test_unrecoverable_exception
18
+ assert_raises(ActiveMerchant::ConnectionError) do
19
+ retry_exceptions do
20
+ raise EOFError
21
+ end
22
+ end
23
+ end
24
+
25
+ def test_invalid_response_error
26
+ assert_raises(ActiveMerchant::InvalidResponseError) do
27
+ retry_exceptions do
28
+ raise Zlib::BufError
29
+ end
30
+ end
31
+ end
32
+
33
+ def test_unrecoverable_exception_logged_if_logger_provided
34
+ @logger.expects(:info).once
35
+ assert_raises(ActiveMerchant::ConnectionError) do
36
+ retry_exceptions :logger => @logger do
37
+ raise EOFError
38
+ end
39
+ end
40
+ end
41
+
42
+ def test_failure_then_success_with_recoverable_exception
43
+ @requester.expects(:post).times(2).raises(Errno::ECONNREFUSED).then.returns(@ok)
44
+
45
+ retry_exceptions do
46
+ @requester.post
47
+ end
48
+ end
49
+
50
+ def test_failure_limit_reached
51
+ @requester.expects(:post).times(ActiveMerchant::NetworkConnectionRetries::DEFAULT_RETRIES).raises(Errno::ECONNREFUSED)
52
+
53
+ assert_raises(ActiveMerchant::ConnectionError) do
54
+ retry_exceptions do
55
+ @requester.post
56
+ end
57
+ end
58
+ end
59
+
60
+ def test_failure_limit_reached_logs_final_error
61
+ @logger.expects(:info).times(3)
62
+ @requester.expects(:post).times(ActiveMerchant::NetworkConnectionRetries::DEFAULT_RETRIES).raises(Errno::ECONNREFUSED)
63
+
64
+ assert_raises(ActiveMerchant::ConnectionError) do
65
+ retry_exceptions(:logger => @logger) do
66
+ @requester.post
67
+ end
68
+ end
69
+ end
70
+
71
+ def test_failure_then_success_with_retry_safe_enabled
72
+ @requester.expects(:post).times(2).raises(EOFError).then.returns(@ok)
73
+
74
+ retry_exceptions :retry_safe => true do
75
+ @requester.post
76
+ end
77
+ end
78
+
79
+ def test_failure_then_success_logs_success
80
+ @logger.expects(:info).with(regexp_matches(/dropped/))
81
+ @logger.expects(:info).with(regexp_matches(/success/))
82
+ @requester.expects(:post).times(2).raises(EOFError).then.returns(@ok)
83
+
84
+ retry_exceptions(:logger => @logger, :retry_safe => true) do
85
+ @requester.post
86
+ end
87
+ end
88
+
89
+ def test_mixture_of_failures_with_retry_safe_enabled
90
+ @requester.expects(:post).times(3).raises(Errno::EHOSTUNREACH).
91
+ raises(Errno::ECONNREFUSED).
92
+ raises(EOFError)
93
+
94
+ assert_raises(ActiveMerchant::ConnectionError) do
95
+ retry_exceptions :retry_safe => true do
96
+ @requester.post
97
+ end
98
+ end
99
+ end
100
+
101
+ def test_failure_with_ssl_certificate
102
+ @requester.expects(:post).raises(OpenSSL::X509::CertificateError)
103
+
104
+ assert_raises(ActiveMerchant::ClientCertificateError) do
105
+ retry_exceptions do
106
+ @requester.post
107
+ end
108
+ end
109
+ end
110
+
111
+ def test_failure_with_ssl_certificate_logs_error_if_logger_specified
112
+ @logger.expects(:error).once
113
+ @requester.expects(:post).raises(OpenSSL::X509::CertificateError)
114
+
115
+ assert_raises(ActiveMerchant::ClientCertificateError) do
116
+ retry_exceptions :logger => @logger do
117
+ @requester.post
118
+ end
119
+ end
120
+ end
121
+
122
+ def test_failure_with_additional_exceptions_specified
123
+ @requester.expects(:post).raises(MyNewError)
124
+
125
+ assert_raises(ActiveMerchant::ConnectionError) do
126
+ retry_exceptions :connection_exceptions => {MyNewError => "my message"} do
127
+ @requester.post
128
+ end
129
+ end
130
+ end
131
+
132
+ def test_failure_without_additional_exceptions_specified
133
+ @requester.expects(:post).raises(MyNewError)
134
+
135
+ assert_raises(MyNewError) do
136
+ retry_exceptions do
137
+ @requester.post
138
+ end
139
+ end
140
+ end
141
+ end