reactive_shipping 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.travis.yml +33 -0
  4. data/.yardopts +13 -0
  5. data/CHANGELOG.md +225 -0
  6. data/CONTRIBUTING.md +23 -0
  7. data/Gemfile +3 -0
  8. data/MIT-LICENSE +21 -0
  9. data/README.md +158 -0
  10. data/Rakefile +35 -0
  11. data/dev.yml +17 -0
  12. data/gemfiles/activesupport42.gemfile +5 -0
  13. data/gemfiles/activesupport50.gemfile +6 -0
  14. data/gemfiles/activesupport51.gemfile +5 -0
  15. data/gemfiles/activesupport52.gemfile +5 -0
  16. data/gemfiles/activesupport_master.gemfile +5 -0
  17. data/lib/certs/eParcel.dtd +111 -0
  18. data/lib/reactive_shipping.rb +26 -0
  19. data/lib/reactive_shipping/address_validation_response.rb +30 -0
  20. data/lib/reactive_shipping/carrier.rb +184 -0
  21. data/lib/reactive_shipping/carriers.rb +35 -0
  22. data/lib/reactive_shipping/carriers/australia_post.rb +248 -0
  23. data/lib/reactive_shipping/carriers/benchmark_carrier.rb +31 -0
  24. data/lib/reactive_shipping/carriers/bogus_carrier.rb +12 -0
  25. data/lib/reactive_shipping/carriers/canada_post.rb +263 -0
  26. data/lib/reactive_shipping/carriers/canada_post_pws.rb +908 -0
  27. data/lib/reactive_shipping/carriers/fedex.rb +797 -0
  28. data/lib/reactive_shipping/carriers/kunaki.rb +155 -0
  29. data/lib/reactive_shipping/carriers/new_zealand_post.rb +260 -0
  30. data/lib/reactive_shipping/carriers/shipwire.rb +178 -0
  31. data/lib/reactive_shipping/carriers/stamps.rb +860 -0
  32. data/lib/reactive_shipping/carriers/ups.rb +1060 -0
  33. data/lib/reactive_shipping/carriers/usps.rb +708 -0
  34. data/lib/reactive_shipping/carriers/usps_returns.rb +86 -0
  35. data/lib/reactive_shipping/delivery_date_estimate.rb +20 -0
  36. data/lib/reactive_shipping/delivery_date_estimates_response.rb +11 -0
  37. data/lib/reactive_shipping/errors.rb +35 -0
  38. data/lib/reactive_shipping/external_return_label_request.rb +417 -0
  39. data/lib/reactive_shipping/external_return_label_response.rb +26 -0
  40. data/lib/reactive_shipping/label.rb +10 -0
  41. data/lib/reactive_shipping/label_response.rb +10 -0
  42. data/lib/reactive_shipping/location.rb +166 -0
  43. data/lib/reactive_shipping/package.rb +165 -0
  44. data/lib/reactive_shipping/package_item.rb +60 -0
  45. data/lib/reactive_shipping/rate_estimate.rb +197 -0
  46. data/lib/reactive_shipping/rate_response.rb +33 -0
  47. data/lib/reactive_shipping/response.rb +44 -0
  48. data/lib/reactive_shipping/shipment_event.rb +22 -0
  49. data/lib/reactive_shipping/shipment_packer.rb +108 -0
  50. data/lib/reactive_shipping/shipping_response.rb +34 -0
  51. data/lib/reactive_shipping/tracking_response.rb +120 -0
  52. data/lib/reactive_shipping/version.rb +3 -0
  53. data/reactive_shipping.gemspec +38 -0
  54. data/shipit.rubygems.yml +1 -0
  55. data/test/console.rb +39 -0
  56. data/test/credentials.yml +76 -0
  57. data/test/fixtures/files/label1.pdf +0 -0
  58. data/test/fixtures/files/ups-shipping-label.gif +0 -0
  59. data/test/fixtures/json/australia_post/calculate_domestic.json +13 -0
  60. data/test/fixtures/json/australia_post/calculate_domestic_2.json +19 -0
  61. data/test/fixtures/json/australia_post/calculate_international.json +12 -0
  62. data/test/fixtures/json/australia_post/calculate_international_2.json +15 -0
  63. data/test/fixtures/json/australia_post/error_message.json +5 -0
  64. data/test/fixtures/json/australia_post/service_domestic.json +117 -0
  65. data/test/fixtures/json/australia_post/service_domestic_2.json +117 -0
  66. data/test/fixtures/json/australia_post/service_international.json +76 -0
  67. data/test/fixtures/json/australia_post/service_international_2.json +59 -0
  68. data/test/fixtures/json/newzealandpost/domestic_book.json +1 -0
  69. data/test/fixtures/json/newzealandpost/domestic_default.json +1 -0
  70. data/test/fixtures/json/newzealandpost/domestic_error.json +1 -0
  71. data/test/fixtures/json/newzealandpost/domestic_poster.json +1 -0
  72. data/test/fixtures/json/newzealandpost/domestic_small_half_pound.json +1 -0
  73. data/test/fixtures/json/newzealandpost/international_book.json +1 -0
  74. data/test/fixtures/json/newzealandpost/international_new_zealand_wii.json +1 -0
  75. data/test/fixtures/json/newzealandpost/international_small_half_pound.json +1 -0
  76. data/test/fixtures/json/newzealandpost/international_wii.json +1 -0
  77. data/test/fixtures/xml/canadapost/example_request.xml +25 -0
  78. data/test/fixtures/xml/canadapost/example_response.xml +130 -0
  79. data/test/fixtures/xml/canadapost/example_response_error.xml +16 -0
  80. data/test/fixtures/xml/canadapost/example_response_french.xml +122 -0
  81. data/test/fixtures/xml/canadapost/example_response_with_nil_value.xml +164 -0
  82. data/test/fixtures/xml/canadapost/example_response_with_postal_outlet.xml +155 -0
  83. data/test/fixtures/xml/canadapost/example_response_with_postal_outlet_french.xml +274 -0
  84. data/test/fixtures/xml/canadapost/example_response_with_strange_delivery_date.xml +130 -0
  85. data/test/fixtures/xml/canadapost_pws/dnc_tracking_details_en.xml +112 -0
  86. data/test/fixtures/xml/canadapost_pws/merchant_details_error.xml +7 -0
  87. data/test/fixtures/xml/canadapost_pws/merchant_details_response.xml +7 -0
  88. data/test/fixtures/xml/canadapost_pws/option_response.xml +13 -0
  89. data/test/fixtures/xml/canadapost_pws/option_response_no_conflicts.xml +7 -0
  90. data/test/fixtures/xml/canadapost_pws/rates_info.xml +190 -0
  91. data/test/fixtures/xml/canadapost_pws/rates_info_error.xml +7 -0
  92. data/test/fixtures/xml/canadapost_pws/receipt_response.xml +42 -0
  93. data/test/fixtures/xml/canadapost_pws/receipt_response_no_priced_options.xml +36 -0
  94. data/test/fixtures/xml/canadapost_pws/register_token_error.xml +7 -0
  95. data/test/fixtures/xml/canadapost_pws/register_token_response.xml +3 -0
  96. data/test/fixtures/xml/canadapost_pws/service_options_response.xml +42 -0
  97. data/test/fixtures/xml/canadapost_pws/services_error.xml +6 -0
  98. data/test/fixtures/xml/canadapost_pws/services_response.xml +32 -0
  99. data/test/fixtures/xml/canadapost_pws/shipment_domestic.xml +69 -0
  100. data/test/fixtures/xml/canadapost_pws/shipment_response.xml +20 -0
  101. data/test/fixtures/xml/canadapost_pws/shipment_us.xml +69 -0
  102. data/test/fixtures/xml/canadapost_pws/tracking_details_en.xml +152 -0
  103. data/test/fixtures/xml/canadapost_pws/tracking_details_en_error.xml +7 -0
  104. data/test/fixtures/xml/canadapost_pws/tracking_details_en_undelivered.xml +116 -0
  105. data/test/fixtures/xml/canadapost_pws/tracking_details_fr.xml +156 -0
  106. data/test/fixtures/xml/canadapost_pws/tracking_details_no_expected_delivery_date.xml +40 -0
  107. data/test/fixtures/xml/fedex/create_shipment_response.xml +2 -0
  108. data/test/fixtures/xml/fedex/freight_rate_request.xml +82 -0
  109. data/test/fixtures/xml/fedex/freight_rate_response.xml +506 -0
  110. data/test/fixtures/xml/fedex/invalid_fedex_reply.xml +27 -0
  111. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_commercial_rate_request.xml +79 -0
  112. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_no_saturday_rate_request.xml +79 -0
  113. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_request.xml +80 -0
  114. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_response.xml +214 -0
  115. data/test/fixtures/xml/fedex/raterequest_reply.xml +213 -0
  116. data/test/fixtures/xml/fedex/raterequest_response_with_ground_home_delivery.xml +206 -0
  117. data/test/fixtures/xml/fedex/reply_without_notifications.xml +185 -0
  118. data/test/fixtures/xml/fedex/tracking_request.xml +29 -0
  119. data/test/fixtures/xml/fedex/tracking_response_bad_tracking_number.xml +20 -0
  120. data/test/fixtures/xml/fedex/tracking_response_delivered_at_door.xml +254 -0
  121. data/test/fixtures/xml/fedex/tracking_response_delivered_at_facility.xml +403 -0
  122. data/test/fixtures/xml/fedex/tracking_response_delivered_with_signature.xml +269 -0
  123. data/test/fixtures/xml/fedex/tracking_response_empty_status_detail.xml +84 -0
  124. data/test/fixtures/xml/fedex/tracking_response_failure_code_9045.xml +52 -0
  125. data/test/fixtures/xml/fedex/tracking_response_failure_code_9080.xml +51 -0
  126. data/test/fixtures/xml/fedex/tracking_response_in_transit.xml +127 -0
  127. data/test/fixtures/xml/fedex/tracking_response_invalid_tracking_number.xml +52 -0
  128. data/test/fixtures/xml/fedex/tracking_response_missing_status_code.xml +89 -0
  129. data/test/fixtures/xml/fedex/tracking_response_multiple_results.xml +100 -0
  130. data/test/fixtures/xml/fedex/tracking_response_not_found.xml +52 -0
  131. data/test/fixtures/xml/fedex/tracking_response_shipment_exception.xml +209 -0
  132. data/test/fixtures/xml/fedex/tracking_response_unable_to_process.xml +32 -0
  133. data/test/fixtures/xml/fedex/tracking_response_with_blank_state.xml +107 -0
  134. data/test/fixtures/xml/fedex/unknown_fedex_document_reply.xml +3 -0
  135. data/test/fixtures/xml/kunaki/invalid_state_response.xml +3 -0
  136. data/test/fixtures/xml/kunaki/no_valid_items_response.xml +3 -0
  137. data/test/fixtures/xml/kunaki/successful_rates_response.xml +3 -0
  138. data/test/fixtures/xml/kunaki/unsuccessful_rates_response.xml +9 -0
  139. data/test/fixtures/xml/shipwire/international_rates_response.xml +17 -0
  140. data/test/fixtures/xml/shipwire/new_carrier_rate_response.xml +18 -0
  141. data/test/fixtures/xml/shipwire/no_rates_response.xml +7 -0
  142. data/test/fixtures/xml/shipwire/rates_response.xml +36 -0
  143. data/test/fixtures/xml/shipwire/rates_response_no_estimate.xml +14 -0
  144. data/test/fixtures/xml/stamps/authenticate_user_request.xml +15 -0
  145. data/test/fixtures/xml/stamps/authenticate_user_response.xml +10 -0
  146. data/test/fixtures/xml/stamps/cleanse_address_request.xml +19 -0
  147. data/test/fixtures/xml/stamps/cleanse_address_response.xml +27 -0
  148. data/test/fixtures/xml/stamps/create_indicium_request.xml +69 -0
  149. data/test/fixtures/xml/stamps/create_indicium_response.xml +40 -0
  150. data/test/fixtures/xml/stamps/expired_authenticator_response.xml +15 -0
  151. data/test/fixtures/xml/stamps/get_account_info_request.xml +11 -0
  152. data/test/fixtures/xml/stamps/get_account_info_response.xml +36 -0
  153. data/test/fixtures/xml/stamps/get_purchase_status_request.xml +12 -0
  154. data/test/fixtures/xml/stamps/get_purchase_status_response.xml +16 -0
  155. data/test/fixtures/xml/stamps/get_rates_request.xml +19 -0
  156. data/test/fixtures/xml/stamps/get_rates_response.xml +351 -0
  157. data/test/fixtures/xml/stamps/purchase_postage_request.xml +13 -0
  158. data/test/fixtures/xml/stamps/purchase_postage_response.xml +17 -0
  159. data/test/fixtures/xml/stamps/track_shipment_request.xml +12 -0
  160. data/test/fixtures/xml/stamps/track_shipment_response.xml +45 -0
  161. data/test/fixtures/xml/ups/access_request.xml +6 -0
  162. data/test/fixtures/xml/ups/delivered_shipment_with_refund.xml +290 -0
  163. data/test/fixtures/xml/ups/delivered_shipment_without_events_tracking_response.xml +62 -0
  164. data/test/fixtures/xml/ups/delivery_dates_response.xml +140 -0
  165. data/test/fixtures/xml/ups/example_tracking_response.xml +53 -0
  166. data/test/fixtures/xml/ups/in_transit_shipment.xml +183 -0
  167. data/test/fixtures/xml/ups/out_for_delivery_shipment.xml +165 -0
  168. data/test/fixtures/xml/ups/package_exceeds_maximum_length.xml +12 -0
  169. data/test/fixtures/xml/ups/rate_single_service.xml +54 -0
  170. data/test/fixtures/xml/ups/rescheduled_shipment.xml +204 -0
  171. data/test/fixtures/xml/ups/shipment_accept_response.xml +42 -0
  172. data/test/fixtures/xml/ups/shipment_confirm_response.xml +33 -0
  173. data/test/fixtures/xml/ups/shipment_from_tiger_direct.xml +222 -0
  174. data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response.xml +290 -0
  175. data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response_with_insured.xml +289 -0
  176. data/test/fixtures/xml/ups/test_real_home_as_residential_destination_with_origin_account_response.xml +311 -0
  177. data/test/fixtures/xml/ups/tracking_request.xml +9 -0
  178. data/test/fixtures/xml/ups/triple_accept_response.xml +72 -0
  179. data/test/fixtures/xml/ups/triple_confirm_response.xml +32 -0
  180. data/test/fixtures/xml/ups/void_shipment_response.xml +11 -0
  181. data/test/fixtures/xml/usps/api_error_rate_response.xml +53 -0
  182. data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_commercial_base_rate_response.xml +2 -0
  183. data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_commercial_plus_rate_response.xml +258 -0
  184. data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_rate_response.xml +108 -0
  185. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_commercial_base_rate_response.xml +84 -0
  186. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_commercial_plus_rate_response.xml +212 -0
  187. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_rate_response.xml +230 -0
  188. data/test/fixtures/xml/usps/first_class_packages_with_invalid_mail_type_response.xml +12 -0
  189. data/test/fixtures/xml/usps/first_class_packages_with_mail_type_response.xml +16 -0
  190. data/test/fixtures/xml/usps/first_class_packages_without_mail_type_response.xml +12 -0
  191. data/test/fixtures/xml/usps/invalid_xml_response.xml +10 -0
  192. data/test/fixtures/xml/usps/invalid_xml_tracking_response_error.xml +2 -0
  193. data/test/fixtures/xml/usps/tracking_request.xml +10 -0
  194. data/test/fixtures/xml/usps/tracking_request_batch.xml +12 -0
  195. data/test/fixtures/xml/usps/tracking_response.xml +162 -0
  196. data/test/fixtures/xml/usps/tracking_response_alt.xml +53 -0
  197. data/test/fixtures/xml/usps/tracking_response_batch.xml +231 -0
  198. data/test/fixtures/xml/usps/tracking_response_failure.xml +11 -0
  199. data/test/fixtures/xml/usps/tracking_response_not_available.xml +12 -0
  200. data/test/fixtures/xml/usps/tracking_response_test_error.xml +8 -0
  201. data/test/fixtures/xml/usps/us_rate_request.xml +18 -0
  202. data/test/fixtures/xml/usps/us_rate_request_large.xml +18 -0
  203. data/test/fixtures/xml/usps/world_rate_request_only_country.xml +22 -0
  204. data/test/fixtures/xml/usps/world_rate_request_with_value.xml +24 -0
  205. data/test/fixtures/xml/usps/world_rate_request_without_value.xml +24 -0
  206. data/test/fixtures/xml/usps_returns/external_return_label_response.xml +2 -0
  207. data/test/fixtures/xml/usps_returns/external_return_label_response_failure.xml +10 -0
  208. data/test/remote/australia_post_test.rb +140 -0
  209. data/test/remote/canada_post_pws_platform_test.rb +259 -0
  210. data/test/remote/canada_post_pws_test.rb +169 -0
  211. data/test/remote/canada_post_test.rb +55 -0
  212. data/test/remote/fedex_test.rb +400 -0
  213. data/test/remote/kunaki_test.rb +37 -0
  214. data/test/remote/new_zealand_post_test.rb +149 -0
  215. data/test/remote/shipwire_test.rb +84 -0
  216. data/test/remote/stamps_test.rb +396 -0
  217. data/test/remote/usps_returns_test.rb +72 -0
  218. data/test/remote/usps_test.rb +243 -0
  219. data/test/test_helper.rb +296 -0
  220. data/test/unit/carrier_test.rb +130 -0
  221. data/test/unit/carriers/australia_post_test.rb +181 -0
  222. data/test/unit/carriers/benchmark_test.rb +18 -0
  223. data/test/unit/carriers/canada_post_pws_rating_test.rb +379 -0
  224. data/test/unit/carriers/canada_post_pws_register_test.rb +76 -0
  225. data/test/unit/carriers/canada_post_pws_shipping_test.rb +258 -0
  226. data/test/unit/carriers/canada_post_pws_test.rb +59 -0
  227. data/test/unit/carriers/canada_post_pws_tracking_test.rb +154 -0
  228. data/test/unit/carriers/canada_post_test.rb +148 -0
  229. data/test/unit/carriers/fedex_test.rb +693 -0
  230. data/test/unit/carriers/kunaki_test.rb +56 -0
  231. data/test/unit/carriers/new_zealand_post_test.rb +177 -0
  232. data/test/unit/carriers/shipwire_test.rb +188 -0
  233. data/test/unit/carriers/stamps_test.rb +245 -0
  234. data/test/unit/carriers/ups_test.rb +580 -0
  235. data/test/unit/carriers/usps_returns_test.rb +45 -0
  236. data/test/unit/carriers/usps_test.rb +633 -0
  237. data/test/unit/carriers_test.rb +16 -0
  238. data/test/unit/external_return_label_request_test.rb +258 -0
  239. data/test/unit/location_test.rb +234 -0
  240. data/test/unit/package_item_test.rb +232 -0
  241. data/test/unit/package_test.rb +404 -0
  242. data/test/unit/rate_estimate_test.rb +93 -0
  243. data/test/unit/response_test.rb +38 -0
  244. data/test/unit/shipment_event_test.rb +20 -0
  245. data/test/unit/shipment_packer_test.rb +212 -0
  246. data/test/unit/tracking_response_test.rb +41 -0
  247. metadata +684 -0
@@ -0,0 +1,72 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteUSPSReturnsTest < ActiveSupport::TestCase
4
+ include ReactiveShipping::Test::Credentials
5
+ include ReactiveShipping::Test::Fixtures
6
+
7
+ def setup
8
+ begin
9
+ creds = credentials(:usps_returns)
10
+ @carrier = USPSReturns.new
11
+ @external_return_label_request =
12
+ ExternalReturnLabelRequest.from_hash(
13
+ :merchant_account_id => creds[:merchant_account_id],
14
+ :mid => creds[:mid],
15
+ :customer_name => 'Joe Dirt',
16
+ :customer_address1 => '122 W Hudson St',
17
+ :customer_address2 => 'Unit 2',
18
+ :customer_city => 'New York',
19
+ :customer_state => 'NY',
20
+ :customer_zipcode => '10013',
21
+ :label_format => 'No Instructions',
22
+ :label_definition => '4X6',
23
+ :recipient_bcc => 'test@gmail.com',
24
+ :service_type_code => '020',
25
+ :address_override_notification => 'true',
26
+ :address_validation => 'true',
27
+ :call_center_or_self_service => 'Customer'
28
+ )
29
+ rescue NoCredentialsFound => e
30
+ skip(e.message)
31
+ end
32
+ end
33
+
34
+ def test_external_return_label_request
35
+ assert_silent do
36
+ @carrier.external_return_label_request(@external_return_label_request, :test => true)
37
+ end
38
+ end
39
+
40
+ def test_external_return_label_with_bad_account
41
+ assert_raises ResponseError do
42
+ @external_return_label_request.merchant_account_id = "1234"
43
+ @carrier.external_return_label_request(@external_return_label_request, :test => true)
44
+ end
45
+ end
46
+
47
+ def test_external_return_label_with_address_validation_false
48
+ assert_silent do
49
+ @external_return_label_request.address_validation = false
50
+ @carrier.external_return_label_request(@external_return_label_request, :test => true)
51
+ end
52
+ end
53
+
54
+ def test_external_return_label_with_address_validation_false_and_bad_address
55
+ assert_silent do
56
+ @external_return_label_request.address_validation = false
57
+ @external_return_label_request.customer_state = 'NJ'
58
+ @external_return_label_request.customer_zipcode = '08829'
59
+ @carrier.external_return_label_request(@external_return_label_request, :test => true)
60
+ end
61
+ end
62
+
63
+ def test_external_return_label_with_address_validation_true_and_bad_address
64
+ assert_raises ResponseError do
65
+ @external_return_label_request.address_validation = true
66
+ @external_return_label_request.customer_state = 'NJ'
67
+ @external_return_label_request.customer_zipcode = '08829'
68
+ @carrier.external_return_label_request(@external_return_label_request, :test => true)
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,243 @@
1
+ require 'test_helper'
2
+
3
+ class RemoteUSPSTest < ActiveSupport::TestCase
4
+ include ReactiveShipping::Test::Credentials
5
+ include ReactiveShipping::Test::Fixtures
6
+
7
+ def setup
8
+ @usps_credentials = credentials(:usps)
9
+ @carrier = USPS.new(@usps_credentials)
10
+ rescue NoCredentialsFound => e
11
+ skip(e.message)
12
+ end
13
+
14
+ def test_tracking_with_bad_number
15
+ assert_raises(ResponseError) do
16
+ @carrier.find_tracking_info('abc123xyz', test: false)
17
+ end
18
+ end
19
+
20
+ def test_zip_to_zip
21
+ response = @carrier.find_rates(
22
+ Location.new(:zip => 40524),
23
+ Location.new(:zip => 40515),
24
+ Package.new(16, [12, 6, 2], :units => :imperial)
25
+ )
26
+
27
+ assert response.success?, response.message
28
+ refute response.rates.empty?
29
+ end
30
+
31
+ def test_just_country_given
32
+ response = @carrier.find_rates(
33
+ location_fixtures[:beverly_hills],
34
+ Location.new(:country => 'CZ'),
35
+ Package.new(100, [5, 10, 20])
36
+ )
37
+ assert response.success?, response.message
38
+ refute response.rates.empty?
39
+ end
40
+
41
+ def test_us_to_canada
42
+ response = @carrier.find_rates(
43
+ location_fixtures[:beverly_hills],
44
+ location_fixtures[:ottawa],
45
+ package_fixtures.values_at(:american_wii),
46
+ :test => true
47
+ )
48
+
49
+ assert response.success?, response.message
50
+ refute response.rates.empty?
51
+ end
52
+
53
+ def test_domestic_rates
54
+ response = @carrier.find_rates(
55
+ location_fixtures[:new_york],
56
+ location_fixtures[:beverly_hills],
57
+ package_fixtures.values_at(:book, :american_wii),
58
+ :test => true
59
+ )
60
+
61
+ assert response.success?, response.message
62
+ assert_instance_of Hash, response.params
63
+ assert_instance_of String, response.xml
64
+ assert_instance_of Array, response.rates
65
+ refute response.rates.empty?
66
+
67
+ rate = response.rates.first
68
+ assert_equal 'USPS', rate.carrier
69
+ assert_equal 'USD', rate.currency
70
+ assert_kind_of Integer, rate.total_price
71
+ assert_kind_of Integer, rate.price
72
+ assert_instance_of String, rate.service_name
73
+ assert_instance_of String, rate.service_code
74
+ assert_instance_of Array, rate.package_rates
75
+ assert_equal package_fixtures.values_at(:book, :american_wii), rate.packages
76
+
77
+ package_rate = rate.package_rates.first
78
+ assert_instance_of Hash, package_rate
79
+ assert_instance_of Package, package_rate[:package]
80
+ refute_nil package_rate[:rate]
81
+
82
+ other_than_two = response.rates.map(&:package_count).reject { |n| n == 2 }
83
+ assert_equal [], other_than_two, "Some RateEstimates do not refer to the right number of packages (#{other_than_two.inspect})"
84
+ end
85
+
86
+ def test_international_rates
87
+ response = @carrier.find_rates(
88
+ location_fixtures[:beverly_hills],
89
+ location_fixtures[:ottawa],
90
+ package_fixtures.values_at(:book, :american_wii),
91
+ :test => true
92
+ )
93
+
94
+ assert response.success?, response.message
95
+ assert_instance_of Hash, response.params
96
+ assert_instance_of String, response.xml
97
+ assert_instance_of Array, response.rates
98
+ refute response.rates.empty?
99
+
100
+ rate = response.rates.first
101
+ assert_equal 'USPS', rate.carrier
102
+ assert_equal 'USD', rate.currency
103
+ assert_kind_of Integer, rate.total_price
104
+ assert_kind_of Integer, rate.price
105
+ assert_instance_of String, rate.service_name
106
+ assert_instance_of String, rate.service_code
107
+ assert_instance_of Array, rate.package_rates
108
+ assert_equal package_fixtures.values_at(:book, :american_wii), rate.packages
109
+
110
+ package_rate = rate.package_rates.first
111
+ assert_instance_of Hash, package_rate
112
+ assert_instance_of Package, package_rate[:package]
113
+ refute_nil package_rate[:rate]
114
+
115
+ other_than_two = response.rates.map(&:package_count).reject { |n| n == 2 }
116
+ assert_equal [], other_than_two, "Some RateEstimates do not refer to the right number of packages (#{other_than_two.inspect})"
117
+ end
118
+
119
+ def test_us_to_us_possession
120
+ response = @carrier.find_rates(
121
+ location_fixtures[:beverly_hills],
122
+ location_fixtures[:puerto_rico],
123
+ package_fixtures.values_at(:american_wii),
124
+ :test => true
125
+ )
126
+
127
+ assert response.success?, response.message
128
+ refute response.rates.empty?
129
+ end
130
+
131
+ def test_bare_packages_domestic
132
+ response = @carrier.find_rates(
133
+ location_fixtures[:beverly_hills], # imperial (U.S. origin)
134
+ location_fixtures[:new_york],
135
+ Package.new(0, 0),
136
+ :test => true
137
+ )
138
+
139
+ assert response.success?, response.message
140
+ refute response.rates.empty?
141
+ end
142
+
143
+ def test_bare_packages_international
144
+ response = @carrier.find_rates(
145
+ location_fixtures[:beverly_hills], # imperial (U.S. origin)
146
+ location_fixtures[:ottawa],
147
+ Package.new(0, 0),
148
+ :test => true
149
+ )
150
+
151
+ assert response.success?, response.message
152
+ refute response.rates.empty?
153
+ end
154
+
155
+ def test_first_class_packages_with_mail_type
156
+ response = @carrier.find_rates(
157
+ location_fixtures[:beverly_hills], # imperial (U.S. origin)
158
+ location_fixtures[:new_york],
159
+ Package.new(0, 0),
160
+
161
+ :test => true,
162
+ :service => :first_class,
163
+ :first_class_mail_type => :parcel
164
+ )
165
+
166
+ assert response.success?, response.message
167
+ refute response.rates.empty?
168
+ end
169
+
170
+ def test_first_class_packages_without_mail_type
171
+ assert_raises(ResponseError, "Invalid First Class Mail Type.") do
172
+ @carrier.find_rates(
173
+ location_fixtures[:beverly_hills], # imperial (U.S. origin)
174
+ location_fixtures[:new_york],
175
+ Package.new(0, 0),
176
+
177
+ :test => true,
178
+ :service => :first_class
179
+ )
180
+ end
181
+ end
182
+
183
+ def test_first_class_packages_with_invalid_mail_type
184
+ assert_raises(ResponseError, "Invalid First Class Mail Type.") do
185
+ @carrier.find_rates(
186
+ location_fixtures[:beverly_hills], # imperial (U.S. origin)
187
+ location_fixtures[:new_york],
188
+ Package.new(0, 0),
189
+
190
+ :test => true,
191
+ :service => :first_class,
192
+ :first_class_mail_tpe => :invalid
193
+ )
194
+ end
195
+ end
196
+
197
+ def test_correct_login_passes_valid_credentials?
198
+ assert USPS.new(@usps_credentials.merge(:test => true)).valid_credentials?
199
+ end
200
+
201
+ def test_wrong_login_fails_in_valid_credentials?
202
+ refute USPS.new(:login => 'ABCDEFGHIJKL', :test => true).valid_credentials?
203
+ end
204
+
205
+ def test_blank_login_fails_in_valid_credentials?
206
+ refute USPS.new(:login => '', :test => true).valid_credentials?
207
+ end
208
+
209
+ def test_nil_login_fails_in_valid_credentials?
210
+ refute USPS.new(:login => nil, :test => true).valid_credentials?
211
+ end
212
+
213
+ def test_must_provide_login_creds_when_instantiating
214
+ assert_raises ArgumentError do
215
+ USPS.new(:test => true)
216
+ end
217
+ end
218
+
219
+ # Uncomment and switch out SPECIAL_COUNTRIES with some other batch to see which
220
+ # countries are currently working. Commented out here just because it's a lot of
221
+ # hits to their server at once:
222
+
223
+ # ALL_COUNTRIES = ActiveUtils::Country.const_get('COUNTRIES').map {|c| c[:alpha2]}
224
+ # SPECIAL_COUNTRIES = USPS.const_get('COUNTRY_NAME_CONVERSIONS').keys.sort
225
+ # NORMAL_COUNTRIES = (ALL_COUNTRIES - SPECIAL_COUNTRIES)
226
+ #
227
+ # SPECIAL_COUNTRIES.each do |code|
228
+ # unless ActiveUtils::Country.find(code).name == USPS.const_get('COUNTRY_NAME_CONVERSIONS')[code]
229
+ # define_method("test_country_#{code}") do
230
+ # response = nil
231
+ # begin
232
+ # response = @carrier.find_rates( location_fixtures[:beverly_hills],
233
+ # Location.new(:country => code),
234
+ # package_fixtures.values_at(:american_wii),
235
+ # :test => true)
236
+ # rescue Exception => e
237
+ # flunk(e.inspect + "\nrequest: " + @carrier.last_request)
238
+ # end
239
+ # assert_not_equal [], response.rates.length
240
+ # end
241
+ # end
242
+ # end
243
+ end
@@ -0,0 +1,296 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'minitest/autorun'
4
+ require "minitest/reporters"
5
+ require 'mocha/mini_test'
6
+ require 'timecop'
7
+ require 'business_time'
8
+
9
+ require 'reactive_shipping'
10
+ require 'logger'
11
+ require 'erb'
12
+ require 'pry'
13
+ require 'vcr'
14
+ require 'webmock/minitest'
15
+
16
+ Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new(detailed_skip: !!ENV["CI"])
17
+
18
+ VCR.configure do |config|
19
+ config.cassette_library_dir = 'test/remote/vcr_cassettes'
20
+ config.allow_http_connections_when_no_cassette = true
21
+ config.hook_into :webmock
22
+ end
23
+
24
+ class ActiveSupport::TestCase
25
+ include ReactiveShipping
26
+
27
+ def logger
28
+ @logger ||= Logger.new(STDERR)
29
+ end
30
+ end
31
+
32
+ module ReactiveShipping::Test
33
+ module Credentials
34
+ class NoCredentialsFound < StandardError
35
+ def initialize(key)
36
+ super("No credentials were found for '#{key}'")
37
+ end
38
+ end
39
+
40
+ LOCAL_CREDENTIALS = ENV['HOME'] + '/.reactive_shipping/credentials.yml'
41
+ DEFAULT_CREDENTIALS = File.dirname(__FILE__) + '/credentials.yml'
42
+
43
+ def credentials(key)
44
+ data = all_credentials[key]
45
+ if data.nil? || data.all? { |k,v| v.nil? || v.to_s.empty? }
46
+ raise NoCredentialsFound.new(key)
47
+ end
48
+ data.symbolize_keys
49
+ end
50
+
51
+ private
52
+
53
+ def all_credentials
54
+ @@all_credentials ||= begin
55
+ [DEFAULT_CREDENTIALS, LOCAL_CREDENTIALS].inject({}) do |credentials, file_name|
56
+ if File.exist?(file_name)
57
+ yaml_data = YAML.load(ERB.new(File.read(file_name)).result(binding)).symbolize_keys
58
+ credentials.merge!(yaml_data)
59
+ end
60
+ credentials
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ module Fixtures
67
+ include ReactiveShipping
68
+
69
+ def xml_fixture(path) # where path is like 'usps/beverly_hills_to_ottawa_response'
70
+ File.read(File.join(File.dirname(__FILE__), 'fixtures', 'xml', "#{path}.xml"))
71
+ end
72
+
73
+ def json_fixture(path) # where path is like 'usps/beverly_hills_to_ottawa_response'
74
+ File.read(File.join(File.dirname(__FILE__), 'fixtures', 'json', "#{path}.json"))
75
+ end
76
+
77
+ def file_fixture(filename)
78
+ File.read(File.join(File.dirname(__FILE__), 'fixtures', 'files', filename), mode: "rb")
79
+ end
80
+
81
+ def package_fixtures
82
+ @package_fixtures ||= {
83
+ :just_ounces => Package.new(16, nil, :units => :imperial),
84
+ :just_grams => Package.new(1000, nil),
85
+ :just_zero_grams => Package.new(0, nil),
86
+ :all_imperial => Package.new(16, [1, 8, 12], :units => :imperial),
87
+ :all_metric => Package.new(1000, [2, 20, 40]),
88
+ :book => Package.new(250, [14, 19, 2]),
89
+ :wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 269.99, :currency => 'GBP'),
90
+ :american_wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 269.99, :currency => 'USD'),
91
+ :new_zealand_wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 269.99, :currency => 'NZD'),
92
+ :worthless_wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 0.0, :currency => 'USD'),
93
+ :poster => Package.new(100, [93, 10], :cylinder => true),
94
+ :small_half_pound => Package.new(8, [1, 1, 1], :units => :imperial),
95
+ :big_half_pound => Package.new((16 * 50), [24, 24, 36], :units => :imperial),
96
+ :chocolate_stuff => Package.new(80, [2, 6, 12], :units => :imperial),
97
+ :frozen_stuff => Package.new(80, [2, 6, 12], :units => :imperial, :dry_ice => {weight: 1.4}),
98
+ :declared_value => Package.new(80, [2, 6, 12], :units => :imperial, :currency => 'USD', :value => 999.99),
99
+ :insured_value => Package.new(80, [2, 6, 12], :units => :imperial, :currency => 'USD', :insured_value => 999.99),
100
+ :tshirts => Package.new(10 * 16, nil, :units => :imperial),
101
+ :shipping_container => Package.new(2200000, [2440, 2600, 6058], :description => '20 ft Standard Container', :units => :metric),
102
+ :largest_gold_bar => Package.new(250000, [45.5, 22.5, 17], :value => 15300000),
103
+ :books => Package.new(64, [4, 8, 6], :units => :imperial, :value => 15300000, :description => 'Books')
104
+ }
105
+ end
106
+
107
+ def location_fixtures
108
+ @location_fixtures ||= {
109
+ :bare_ottawa => Location.new(:country => 'CA', :postal_code => 'K1P 1J1'),
110
+ :bare_beverly_hills => Location.new(:country => 'US', :zip => '90210'),
111
+ :ottawa => Location.new( :country => 'CA',
112
+ :province => 'ON',
113
+ :city => 'Ottawa',
114
+ :address1 => '110 Laurier Avenue West',
115
+ :postal_code => 'K1P 1J1',
116
+ :phone => '1-613-580-2400',
117
+ :fax => '1-613-580-2495',
118
+ :email => 'bob.bobsen@gmail.com'),
119
+ :ottawa_with_name => Location.new( :country => 'CA',
120
+ :province => 'ON',
121
+ :city => 'Ottawa',
122
+ :name => 'Paul Ottawa',
123
+ :address1 => '110 Laurier Avenue West',
124
+ :postal_code => 'K1P 1J1',
125
+ :phone => '1-613-580-2400',
126
+ :fax => '1-613-580-2495'),
127
+ :beverly_hills_with_name => Location.new(
128
+ :name => 'JP Silvashy',
129
+ :country => 'US',
130
+ :state => 'CA',
131
+ :city => 'Beverly Hills',
132
+ :address1 => '455 N. Rexford Dr.',
133
+ :address2 => '3rd Floor',
134
+ :zip => '90210',
135
+ :phone => '1-310-285-1013',
136
+ :fax => '1-310-275-8159'),
137
+ :beverly_hills => Location.new(
138
+ :country => 'US',
139
+ :state => 'CA',
140
+ :city => 'Beverly Hills',
141
+ :address1 => '455 N. Rexford Dr.',
142
+ :address2 => '3rd Floor',
143
+ :zip => '90210',
144
+ :phone => '1-310-285-1013',
145
+ :fax => '1-310-275-8159'),
146
+ :beverly_hills_9_zip => Location.new(
147
+ :country => 'US',
148
+ :state => 'CA',
149
+ :city => 'Beverly Hills',
150
+ :address1 => '455 N. Rexford Dr.',
151
+ :address2 => '3rd Floor',
152
+ :zip => '90210-1234',
153
+ :phone => '1-310-285-1013',
154
+ :fax => '1-310-275-8159'),
155
+ :real_home_as_commercial => Location.new(
156
+ :country => 'US',
157
+ :city => 'Tampa',
158
+ :state => 'FL',
159
+ :company => 'Tampa Company',
160
+ :address1 => '7926 Woodvale Circle',
161
+ :zip => '33615',
162
+ :address_type => 'commercial'), # means that UPS will default to commercial if it doesn't know
163
+ :fake_home_as_commercial => Location.new(
164
+ :country => 'US',
165
+ :state => 'FL',
166
+ :address1 => '123 fake st.',
167
+ :zip => '33615',
168
+ :address_type => 'commercial'),
169
+ :real_google_as_commercial => Location.new(
170
+ :country => 'US',
171
+ :city => 'Mountain View',
172
+ :state => 'CA',
173
+ :address1 => '1600 Amphitheatre Parkway',
174
+ :zip => '94043',
175
+ :address_type => 'commercial'),
176
+ :real_google_with_name_phone => Location.new(
177
+ :name => 'Sergey Brin',
178
+ :country => 'US',
179
+ :city => 'Mountain View',
180
+ :state => 'CA',
181
+ :address1 => '1600 Amphitheatre Parkway',
182
+ :zip => '94043',
183
+ :phone => '1-650-253-0000',
184
+ :address_type => 'commercial'),
185
+ :real_google_as_residential => Location.new(
186
+ :country => 'US',
187
+ :city => 'Mountain View',
188
+ :state => 'CA',
189
+ :address1 => '1600 Amphitheatre Parkway',
190
+ :zip => '94043',
191
+ :address_type => 'residential'), # means that will default to residential if it doesn't know
192
+ :fake_google_as_commercial => Location.new(
193
+ :country => 'US',
194
+ :city => 'Mountain View',
195
+ :state => 'CA',
196
+ :address1 => '123 bogusland dr.',
197
+ :zip => '94043',
198
+ :address_type => 'commercial'),
199
+ :fake_google_as_residential => Location.new(
200
+ :country => 'US',
201
+ :city => 'Mountain View',
202
+ :state => 'CA',
203
+ :address1 => '123 bogusland dr.',
204
+ :zip => '94043',
205
+ :address_type => 'residential'), # means that will default to residential if it doesn't know
206
+ :fake_home_as_residential => Location.new(
207
+ :country => 'US',
208
+ :state => 'FL',
209
+ :address1 => '123 fake st.',
210
+ :zip => '33615',
211
+ :address_type => 'residential'),
212
+ :real_home_as_residential => Location.new(
213
+ :country => 'US',
214
+ :city => 'Tampa',
215
+ :state => 'FL',
216
+ :address1 => '7926 Woodvale Circle',
217
+ :zip => '33615',
218
+ :address_type => 'residential'),
219
+ :london => Location.new(
220
+ :country => 'GB',
221
+ :city => 'London',
222
+ :address1 => '170 Westminster Bridge Rd.',
223
+ :zip => 'SE1 7RW'),
224
+ :new_york => Location.new(
225
+ :country => 'US',
226
+ :city => 'New York',
227
+ :state => 'NY',
228
+ :address1 => '780 3rd Avenue',
229
+ :address2 => 'Suite 2601',
230
+ :zip => '10017'),
231
+ :new_york_with_name => Location.new(
232
+ :name => "Bob Bobsen",
233
+ :country => 'US',
234
+ :city => 'New York',
235
+ :state => 'NY',
236
+ :address1 => '780 3rd Avenue',
237
+ :address2 => 'Suite 2601',
238
+ :zip => '10017',
239
+ :phone => '123-123-1234'),
240
+ :wellington => Location.new(
241
+ :country => 'NZ',
242
+ :city => 'Wellington',
243
+ :address1 => '85 Victoria St',
244
+ :address2 => 'Te Aro',
245
+ :postal_code => '6011'),
246
+ :auckland => Location.new(
247
+ :country => 'NZ',
248
+ :city => 'Auckland',
249
+ :address1 => '192 Victoria St West',
250
+ :postal_code => '1010'),
251
+ :puerto_rico => Location.new(
252
+ :country => 'PR',
253
+ :city => 'Barceloneta',
254
+ :address1 => '1 Nueva St',
255
+ :postal_code => '00617'),
256
+ :annapolis => Location.new(
257
+ :name => 'Big Red',
258
+ :country => 'US',
259
+ :city => 'Annapolis',
260
+ :address1 => '1 Park Place',
261
+ :address2 => '#7',
262
+ :postal_code => '21401'),
263
+ :netherlands => Location.new( :country => 'NL',
264
+ :city => 'Groningen',
265
+ :address1 => 'Aquamarijnstraat 349',
266
+ :postal_code => '9743 PJ',
267
+ :state => 'Groningen'),
268
+ :sydney => Location.new(
269
+ :country => 'AUS',
270
+ :city => 'Sydney',
271
+ :state => 'NSW',
272
+ :address1 => '192 George Street',
273
+ :postal_code => '2000'),
274
+ :melbourne => Location.new(
275
+ :country => 'AUS',
276
+ :city => 'Melbourne',
277
+ :state => 'VIC',
278
+ :address1 => '192 George Street',
279
+ :postal_code => '3108'),
280
+ :kosovo => Location.new(
281
+ :country => 'XK',
282
+ :city => 'Prishtinë',
283
+ :address1 => 'Ahmet Krasniqi',
284
+ :postal_code => '10000')
285
+ }
286
+
287
+ end
288
+
289
+ def line_item_fixture
290
+ @line_item_fixture ||= [
291
+ PackageItem.new("IPod Nano - 8gb - green", 200, 199.00, 2, :sku => "IPOD2008GREEN", :hs_code => "1234.12.12.12"),
292
+ PackageItem.new("IPod Nano - 8gb - black", 200, 199.00, 1, :sku => "IPOD2008GREEN", :hs_code => "1234.12.12.12")
293
+ ]
294
+ end
295
+ end
296
+ end