reactive_shipping 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,16 @@
1
+ require 'test_helper'
2
+
3
+ class CarriersTest < ActiveSupport::TestCase
4
+ test ".find searches by string for a carrier and finds USPS" do
5
+ assert_equal ReactiveShipping::USPS, ReactiveShipping::Carriers.find('usps')
6
+ assert_equal ReactiveShipping::USPS, ReactiveShipping::Carriers.find('USPS')
7
+ end
8
+
9
+ test ".find searches by symbol for a carrier and finds USPS" do
10
+ assert_equal ReactiveShipping::USPS, ReactiveShipping::Carriers.find(:usps)
11
+ end
12
+
13
+ test ".find raises with an unknown carrier" do
14
+ assert_raises(NameError) { ReactiveShipping::Carriers.find(:polar_north) }
15
+ end
16
+ end
@@ -0,0 +1,258 @@
1
+ require 'test_helper'
2
+
3
+ class ExternalReturnLabelRequestTest < ActiveSupport::TestCase
4
+ include ReactiveShipping::Test::Fixtures
5
+
6
+ setup do
7
+ @external_request_label_req =
8
+ ExternalReturnLabelRequest.from_hash(
9
+ customer_name: "Test Customer",
10
+ customer_address1: "122 Hudson St.",
11
+ customer_city: "New York",
12
+ customer_state: "NY",
13
+ customer_zipcode: "10013",
14
+ label_format: "No Instructions",
15
+ label_definition: "4X6",
16
+ service_type_code: "044",
17
+ merchant_account_id: "12345",
18
+ mid: "12345678",
19
+ call_center_or_self_service: "Customer",
20
+ address_override_notification: "true",
21
+ )
22
+ @email = "no-reply@example.com"
23
+ @invalid_email = "not_a_valid_email"
24
+ @anything = "Any string"
25
+ @blank_values = ["", " "]
26
+ end
27
+
28
+ test "#recipient_bcc raises on an invalid email" do
29
+ assert_raises(USPSValidationError) do
30
+ @external_request_label_req.recipient_bcc = @invalid_email
31
+ end
32
+ end
33
+
34
+ test "#recipient_bcc assigns the email" do
35
+ @external_request_label_req.recipient_bcc = @email
36
+ assert_equal @email, @external_request_label_req.recipient_bcc
37
+ end
38
+
39
+ test "#recipient_email raises if invalid" do
40
+ assert_raises(USPSValidationError) do
41
+ @external_request_label_req.recipient_email = @invalid_email
42
+ end
43
+ end
44
+
45
+ test "#recipient_email assigns the email" do
46
+ @external_request_label_req.recipient_email = @email
47
+ assert_equal @email, @external_request_label_req.recipient_email
48
+ end
49
+
50
+ test "#recipient_name accepts anything" do
51
+ @external_request_label_req.recipient_name = @anything
52
+ assert_equal @anything, @external_request_label_req.recipient_name
53
+ end
54
+
55
+ test "#sender_email raises if invalid" do
56
+ assert_raises(USPSValidationError) do
57
+ @external_request_label_req.sender_email = @invalid_email
58
+ end
59
+ end
60
+
61
+ test "#sender_email assigns the email" do
62
+ @external_request_label_req.sender_email = @email
63
+ assert_equal @email, @external_request_label_req.sender_email
64
+ end
65
+
66
+ test "#sender_name assigns the value" do
67
+ @external_request_label_req.sender_name = @anything
68
+ assert_equal @anything, @external_request_label_req.sender_name
69
+ end
70
+
71
+ test "#sender_name raises if blank or nil" do
72
+ @blank_values.each do |blank|
73
+ assert_raises(USPSValidationError) do
74
+ @external_request_label_req.sender_name = blank
75
+ end
76
+ end
77
+ end
78
+
79
+ test "#sender_name raises if nil" do
80
+ assert_raises(USPSValidationError) do
81
+ @external_request_label_req.sender_name = nil
82
+ end
83
+ end
84
+
85
+ test "#image_type accepts a valid image type" do
86
+ ExternalReturnLabelRequest::IMAGE_TYPE.each do |type|
87
+ @external_request_label_req.image_type = type.downcase
88
+ assert_equal type, @external_request_label_req.image_type
89
+ end
90
+ end
91
+
92
+ test "#image_type raises on jpg" do
93
+ assert_raises(USPSValidationError) do
94
+ @external_request_label_req.image_type = "jpg"
95
+ end
96
+ end
97
+
98
+ test "#call_center_or_self_service accepts the valid values defined" do
99
+ ExternalReturnLabelRequest::CALL_CENTER_OR_SELF_SERVICE.each do |cc_or_cs|
100
+ @external_request_label_req.call_center_or_self_service = cc_or_cs
101
+ assert_equal cc_or_cs, @external_request_label_req.call_center_or_self_service
102
+ end
103
+ end
104
+
105
+ test "#call_center_or_self_service raises on an invalid value" do
106
+ assert_raises(USPSValidationError) do
107
+ @external_request_label_req.call_center_or_self_service = "Invalid"
108
+ end
109
+ end
110
+
111
+ test "#packaging_information accepts a value" do
112
+ @external_request_label_req.packaging_information = @anything
113
+ assert_equal @anything, @external_request_label_req.packaging_information
114
+ end
115
+
116
+ test "#packaging_information accepts blank values" do
117
+ @blank_values.each do |blank|
118
+ @external_request_label_req.packaging_information = blank
119
+ assert_equal "", @external_request_label_req.packaging_information
120
+ end
121
+ end
122
+
123
+ test "#packaging_information raises on a value too long" do
124
+ assert_raises(USPSValidationError) do
125
+ @external_request_label_req.packaging_information = "a" * 16
126
+ end
127
+ end
128
+
129
+ test "#packaging_information2 accepts a value" do
130
+ @external_request_label_req.packaging_information2 = @anything
131
+ assert_equal @anything, @external_request_label_req.packaging_information2
132
+ end
133
+
134
+ test "#packaging_information2 accepts blank values" do
135
+ @blank_values.each do |blank|
136
+ @external_request_label_req.packaging_information2 = blank
137
+ assert_equal "", @external_request_label_req.packaging_information2
138
+ end
139
+ end
140
+
141
+ test "#packaging_information2 raises when a value is too long" do
142
+ assert_raises(USPSValidationError) do
143
+ @external_request_label_req.packaging_information2 = "a" * 16
144
+ end
145
+ end
146
+
147
+ test "#customer_address2 accepts a value" do
148
+ @external_request_label_req.customer_address2 = @anything
149
+ assert_equal @anything, @external_request_label_req.customer_address2
150
+ end
151
+
152
+ test "#customer_address2 accepts blank values" do
153
+ @blank_values.each do |blank|
154
+ @external_request_label_req.customer_address2 = blank
155
+ assert_nil @external_request_label_req.customer_address2
156
+ end
157
+ end
158
+
159
+ test "#customer_address2 accepts nil" do
160
+ @external_request_label_req.customer_address2 = nil
161
+ assert_nil @external_request_label_req.customer_address2
162
+ end
163
+
164
+ test "#sanitize scrubs strings" do
165
+ assert_equal "", @external_request_label_req.send(:sanitize,' ')
166
+ assert_equal 'some string', @external_request_label_req.send(:sanitize, 'some string ')
167
+ assert_nil @external_request_label_req.send(:sanitize, {})
168
+ assert_nil @external_request_label_req.send(:sanitize, nil)
169
+ assert_nil @external_request_label_req.send(:sanitize, nil)
170
+ assert_nil @external_request_label_req.send(:sanitize, [])
171
+ assert_equal ExternalReturnLabelRequest::CAP_STRING_LEN, @external_request_label_req.send(:sanitize, (1..100).to_a.join("_")).size
172
+ end
173
+
174
+ test "#to_bool coerces true values" do
175
+ assert_equal true, @external_request_label_req.send(:to_bool, 'yes')
176
+ assert_equal true, @external_request_label_req.send(:to_bool, 'true')
177
+ assert_equal true, @external_request_label_req.send(:to_bool, true)
178
+ assert_equal true, @external_request_label_req.send(:to_bool, '1')
179
+ end
180
+
181
+ test "#to_bool coerces false values" do
182
+ assert_equal false, @external_request_label_req.send(:to_bool, '0')
183
+ assert_equal false, @external_request_label_req.send(:to_bool, 'false')
184
+ assert_equal false, @external_request_label_req.send(:to_bool, false)
185
+ assert_equal false, @external_request_label_req.send(:to_bool, nil, false)
186
+ end
187
+
188
+ test "#validate_range" do
189
+ assert_raises(USPSValidationError) do
190
+ @external_request_label_req.send(:validate_range, '1', 5, 10, __method__)
191
+ end
192
+ assert_raises(USPSValidationError) do
193
+ @external_request_label_req.send(:validate_range, '', 1, 10, __method__)
194
+ end
195
+ @external_request_label_req.send(:validate_range, '5 char', 3, 10, __method__)
196
+ @external_request_label_req.send(:validate_range, '5 char', nil, 10, __method__)
197
+ end
198
+
199
+ test "#validate_string_length" do
200
+ assert_raises(USPSValidationError) do
201
+ @external_request_label_req.send(:validate_string_length, '14 char string', 13, __method__)
202
+ end
203
+ assert_raises(USPSValidationError) do
204
+ @external_request_label_req.send(:validate_string_length, '14 char string', nil, __method__)
205
+ end
206
+ @external_request_label_req.send(:validate_string_length, '14 char string', 14, __method__)
207
+ end
208
+
209
+ test "#validate_set_inclusion" do
210
+ assert_raises(USPSValidationError) do
211
+ @external_request_label_req.send(:validate_set_inclusion, 'not_in_set', ['v1','v2','v3'], __method__)
212
+ end
213
+ assert_raises(USPSValidationError) do
214
+ @external_request_label_req.send(:validate_set_inclusion, 'not_in_set', nil, __method__)
215
+ end
216
+ @external_request_label_req.send(:validate_set_inclusion, 'v1', ['v1','v2','v3'], __method__)
217
+ end
218
+
219
+ test "#validate_email" do
220
+ assert_raises(USPSValidationError) do
221
+ @external_request_label_req.send(:validate_email, @invalid_email, __method__)
222
+ end
223
+ assert_raises(USPSValidationError) do
224
+ @external_request_label_req.send(:validate_email, " ", __method__)
225
+ end
226
+ assert_equal @email, @external_request_label_req.send(:validate_email, @email, __method__)
227
+ assert_equal @email, @external_request_label_req.send(:validate_email, " #{@email} ", __method__)
228
+ end
229
+
230
+ test "#initialize raises with no tag" do
231
+ assert_raises(USPSMissingRequiredTagError) { ExternalReturnLabelRequest.new }
232
+ end
233
+
234
+ test "#initialize passes with valid values and that every key is necessary" do
235
+ sample_hash = {
236
+ :customer_name => "Test Customer",
237
+ :customer_address1 => "122 Hudson St.",
238
+ :customer_city => "New York",
239
+ :customer_state => "NY",
240
+ :customer_zipcode => "10013",
241
+ :label_format => "No Instructions",
242
+ :label_definition => "4X6",
243
+ :service_type_code => "044",
244
+ :merchant_account_id => "12345",
245
+ :mid => "12345678",
246
+ :call_center_or_self_service => "Customer",
247
+ :address_override_notification => "true"
248
+ }
249
+
250
+ assert ExternalReturnLabelRequest.from_hash(sample_hash)
251
+
252
+ sample_hash.keys.each do |k|
253
+ assert_raises(USPSMissingRequiredTagError) do
254
+ ExternalReturnLabelRequest.from_hash(sample_hash.reject {|k, v| k })
255
+ end
256
+ end
257
+ end
258
+ end
@@ -0,0 +1,234 @@
1
+ require 'test_helper'
2
+
3
+ class LocationTest < ActiveSupport::TestCase
4
+ include ReactiveShipping::Test::Fixtures
5
+
6
+ setup do
7
+ @location = location_fixtures[:ottawa]
8
+ @address2 = 'Apt 613'
9
+ @address3 = 'Victory Lane'
10
+ @attributes_hash = {
11
+ country: 'CA',
12
+ zip: '90210',
13
+ territory_code: 'QC',
14
+ town: 'Perth',
15
+ address: '66 Gregory Ave.',
16
+ phone: '515-555-1212',
17
+ fax_number: 'none to speak of',
18
+ email: 'bob.bobsen@gmail.com',
19
+ address_type: :commercial,
20
+ name: "Bob Bobsen",
21
+ }
22
+ end
23
+
24
+ test "#initialize sets a country object" do
25
+ assert_instance_of ActiveUtils::Country, @location.country
26
+ assert_equal 'CA', @location.country_code(:alpha2)
27
+ end
28
+
29
+ test ".from sets up the location from a hash" do
30
+ location = Location.from(@attributes_hash)
31
+
32
+ assert_equal @attributes_hash[:country], location.country_code(:alpha2)
33
+ assert_equal @attributes_hash[:zip], location.zip
34
+ assert_equal @attributes_hash[:territory_code], location.province
35
+ assert_equal @attributes_hash[:town], location.city
36
+ assert_equal @attributes_hash[:address], location.address1
37
+ assert_equal @attributes_hash[:phone], location.phone
38
+ assert_equal @attributes_hash[:fax_number], location.fax
39
+ assert_equal @attributes_hash[:email], location.email
40
+ assert_equal @attributes_hash[:address_type].to_s, location.address_type
41
+ assert_equal @attributes_hash[:name], location.name
42
+ end
43
+
44
+ test ".from sets from an object with properties" do
45
+ object = Class.new do
46
+ def initialize(hash)
47
+ @hash = hash
48
+ end
49
+ def method_missing(method)
50
+ @hash[method]
51
+ end
52
+ def respond_to?(method)
53
+ return false if method == :[]
54
+ true
55
+ end
56
+ end.new(@attributes_hash)
57
+
58
+ location = Location.from(object)
59
+
60
+ assert_equal @attributes_hash[:country], location.country_code(:alpha2)
61
+ assert_equal @attributes_hash[:zip], location.zip
62
+ assert_equal @attributes_hash[:territory_code], location.province
63
+ assert_equal @attributes_hash[:town], location.city
64
+ assert_equal @attributes_hash[:address], location.address1
65
+ assert_equal @attributes_hash[:phone], location.phone
66
+ assert_equal @attributes_hash[:fax_number], location.fax
67
+ assert_equal @attributes_hash[:email], location.email
68
+ assert_equal @attributes_hash[:address_type].to_s, location.address_type
69
+ assert_equal @attributes_hash[:name], location.name
70
+ end
71
+
72
+ test ".from adheres to propery order even if hash access is available" do
73
+ object = Class.new do
74
+ def [](index)
75
+ { province: "California" }[index]
76
+ end
77
+
78
+ def province_code
79
+ "CA"
80
+ end
81
+ end.new
82
+ assert_equal "CA", Location.from(object).province
83
+ end
84
+
85
+ test ".from sets the name to nil if it is not provided" do
86
+ location = Location.from({})
87
+ assert_nil location.name
88
+ end
89
+
90
+ test ".from sets company and company_name from company" do
91
+ location = Location.from(company: "Mine")
92
+
93
+ assert_equal "Mine", location.company
94
+ assert_equal "Mine", location.company_name
95
+ end
96
+
97
+ test ".from sets company and company_name from company_name" do
98
+ location = Location.from(company_name: "Mine")
99
+
100
+ assert_equal "Mine", location.company
101
+ assert_equal "Mine", location.company_name
102
+ end
103
+
104
+ test ".from prioritizes company" do
105
+ location = Location.from(company_name: "from company_name", company: "from company")
106
+
107
+ assert_equal "from company", location.company
108
+ assert_equal "from company", location.company_name
109
+ end
110
+
111
+ test "#prettyprint outputs a readable string" do
112
+ expected = "110 Laurier Avenue West\nOttawa, ON, K1P 1J1\nCanada"
113
+ assert_equal expected, @location.prettyprint
114
+ end
115
+
116
+ test "#to_s outputs a readable string without newlines" do
117
+ expected = "110 Laurier Avenue West Ottawa, ON, K1P 1J1 Canada"
118
+ assert_equal expected, @location.to_s
119
+ end
120
+
121
+ test "#inspect returns a readable string" do
122
+ expected = "110 Laurier Avenue West\nOttawa, ON, K1P 1J1\nCanada\nPhone: 1-613-580-2400\nFax: 1-613-580-2495\nEmail: bob.bobsen@gmail.com"
123
+ assert_equal expected, @location.inspect
124
+ end
125
+
126
+ test "#address_type= assigns a type of address as commercial" do
127
+ refute @location.commercial?
128
+
129
+ @location.address_type = :commercial
130
+ assert @location.commercial?
131
+ refute @location.residential?
132
+ assert_equal "commercial", @location.address_type
133
+ end
134
+
135
+ test "#address_type= assigns a type of address as residential" do
136
+ refute @location.residential?
137
+
138
+ @location.address_type = :residential
139
+ assert @location.residential?
140
+ refute @location.commercial?
141
+ assert_equal "residential", @location.address_type
142
+ end
143
+
144
+ test "#address_type= raises on an invalid assignment" do
145
+ assert_raises(ArgumentError) do
146
+ @location.address_type = :new_address_type
147
+ end
148
+
149
+ assert_nil @location.address_type
150
+ end
151
+
152
+ test "#address_type= cannot blank out the value as nil" do
153
+ @location.address_type = :residential
154
+ assert @location.residential?
155
+
156
+ @location.address_type = nil
157
+ assert @location.residential?
158
+ assert_equal "residential", @location.address_type
159
+ end
160
+
161
+ test "#address_type= cannot blank out the value as empty string" do
162
+ @location.address_type = :residential
163
+ assert @location.residential?
164
+
165
+ @location.address_type = ""
166
+ assert @location.residential?
167
+ assert_equal "residential", @location.address_type
168
+ end
169
+
170
+ test "#to_hash has the expected attributes" do
171
+ expected = %w(address1 address2 address3 address_type city company_name country email fax name phone postal_code province)
172
+
173
+ assert_equal expected, @location.to_hash.stringify_keys.keys.sort
174
+ end
175
+
176
+ test "#to_json returns the JSON values" do
177
+ expected = JSON.parse(@location.to_json).symbolize_keys
178
+
179
+ assert_equal @location.to_hash, expected
180
+ end
181
+
182
+ test "#zip_plus_4 nil without the extra four" do
183
+ zip = "12345"
184
+ location = Location.from(zip: zip)
185
+
186
+ assert_nil location.zip_plus_4
187
+ assert_equal zip, location.zip
188
+ end
189
+
190
+ test "#zip_plus_4 parses without the dash" do
191
+ zip = "12345-9999"
192
+ zip_without_dash = "123459999"
193
+ location = Location.from(zip: zip_without_dash)
194
+
195
+ assert_equal zip, location.zip_plus_4
196
+ assert_equal zip_without_dash, location.zip
197
+ end
198
+
199
+ test "#zip_plus_4 parses with the dash" do
200
+ zip = "12345-9999"
201
+ location = Location.from(zip: zip)
202
+
203
+ assert_equal zip, location.zip_plus_4
204
+ assert_equal zip, location.zip
205
+ end
206
+
207
+ test "#address2_and_3 shows just address2" do
208
+ location = Location.from(address2: @address2)
209
+ assert_equal 'Apt 613', location.address2_and_3
210
+ end
211
+
212
+ test "#address2_and_3 shows just address3" do
213
+ location = Location.from(address3: @address3)
214
+ assert_equal @address3, location.address2_and_3
215
+ end
216
+
217
+ test "#address2_and_3 shows both address2 and address3" do
218
+ location = Location.from(address2: @address2, address3: @address3)
219
+ assert_equal "#{@address2}, #{@address3}", location.address2_and_3
220
+ end
221
+
222
+ test "#address2_and_3 shows an empty string when address2 and address3 are both blank" do
223
+ assert_nil @location.address2
224
+ assert_nil @location.address3
225
+ assert_equal "", @location.address2_and_3
226
+ end
227
+
228
+ test "#== compares locations by attributes" do
229
+ another_location = Location.from(@location.to_hash)
230
+
231
+ assert_equal @location, another_location
232
+ refute_equal @location, Location.new({})
233
+ end
234
+ end