active_shipping 1.0.0.pre2 → 1.0.0.pre3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.gitignore +12 -0
  5. data/.travis.yml +14 -0
  6. data/.yardopts +14 -0
  7. data/Gemfile +3 -0
  8. data/Gemfile.activesupport32 +4 -0
  9. data/Gemfile.activesupport40 +4 -0
  10. data/Gemfile.activesupport41 +4 -0
  11. data/Gemfile.activesupport42 +4 -0
  12. data/Rakefile +23 -0
  13. data/active_shipping.gemspec +32 -0
  14. data/lib/active_shipping.rb +1 -1
  15. data/lib/active_shipping/carrier.rb +1 -1
  16. data/lib/active_shipping/carriers/shipwire.rb +32 -35
  17. data/lib/active_shipping/carriers/ups.rb +2 -2
  18. data/lib/active_shipping/carriers/usps.rb +103 -101
  19. data/lib/active_shipping/label_response.rb +1 -1
  20. data/lib/active_shipping/rate_estimate.rb +76 -15
  21. data/lib/active_shipping/rate_response.rb +20 -0
  22. data/lib/active_shipping/response.rb +17 -0
  23. data/lib/active_shipping/shipment_packer.rb +2 -2
  24. data/lib/active_shipping/shipping_response.rb +24 -2
  25. data/lib/active_shipping/tracking_response.rb +65 -17
  26. data/lib/active_shipping/version.rb +1 -1
  27. data/shipit.rubygems.yml +1 -0
  28. data/test/credentials.yml +47 -0
  29. data/test/fixtures/files/label1.pdf +0 -0
  30. data/test/fixtures/files/ups-shipping-label.gif +0 -0
  31. data/test/fixtures/json/newzealandpost/domestic_book.json +1 -0
  32. data/test/fixtures/json/newzealandpost/domestic_default.json +1 -0
  33. data/test/fixtures/json/newzealandpost/domestic_error.json +1 -0
  34. data/test/fixtures/json/newzealandpost/domestic_poster.json +1 -0
  35. data/test/fixtures/json/newzealandpost/domestic_small_half_pound.json +1 -0
  36. data/test/fixtures/json/newzealandpost/international_book.json +1 -0
  37. data/test/fixtures/json/newzealandpost/international_new_zealand_wii.json +1 -0
  38. data/test/fixtures/json/newzealandpost/international_small_half_pound.json +1 -0
  39. data/test/fixtures/json/newzealandpost/international_wii.json +1 -0
  40. data/test/fixtures/xml/canadapost/example_request.xml +25 -0
  41. data/test/fixtures/xml/canadapost/example_response.xml +130 -0
  42. data/test/fixtures/xml/canadapost/example_response_error.xml +16 -0
  43. data/test/fixtures/xml/canadapost/example_response_french.xml +122 -0
  44. data/test/fixtures/xml/canadapost/example_response_with_nil_value.xml +164 -0
  45. data/test/fixtures/xml/canadapost/example_response_with_postal_outlet.xml +155 -0
  46. data/test/fixtures/xml/canadapost/example_response_with_postal_outlet_french.xml +274 -0
  47. data/test/fixtures/xml/canadapost/example_response_with_strange_delivery_date.xml +130 -0
  48. data/test/fixtures/xml/canadapost_pws/dnc_tracking_details_en.xml +112 -0
  49. data/test/fixtures/xml/canadapost_pws/merchant_details_error.xml +7 -0
  50. data/test/fixtures/xml/canadapost_pws/merchant_details_response.xml +7 -0
  51. data/test/fixtures/xml/canadapost_pws/option_response.xml +13 -0
  52. data/test/fixtures/xml/canadapost_pws/option_response_no_conflicts.xml +7 -0
  53. data/test/fixtures/xml/canadapost_pws/rates_info.xml +190 -0
  54. data/test/fixtures/xml/canadapost_pws/rates_info_error.xml +7 -0
  55. data/test/fixtures/xml/canadapost_pws/receipt_response.xml +42 -0
  56. data/test/fixtures/xml/canadapost_pws/receipt_response_no_priced_options.xml +36 -0
  57. data/test/fixtures/xml/canadapost_pws/register_token_error.xml +7 -0
  58. data/test/fixtures/xml/canadapost_pws/register_token_response.xml +3 -0
  59. data/test/fixtures/xml/canadapost_pws/service_options_response.xml +42 -0
  60. data/test/fixtures/xml/canadapost_pws/services_error.xml +6 -0
  61. data/test/fixtures/xml/canadapost_pws/services_response.xml +32 -0
  62. data/test/fixtures/xml/canadapost_pws/shipment_domestic.xml +69 -0
  63. data/test/fixtures/xml/canadapost_pws/shipment_response.xml +20 -0
  64. data/test/fixtures/xml/canadapost_pws/shipment_us.xml +69 -0
  65. data/test/fixtures/xml/canadapost_pws/tracking_details_en.xml +152 -0
  66. data/test/fixtures/xml/canadapost_pws/tracking_details_en_error.xml +7 -0
  67. data/test/fixtures/xml/canadapost_pws/tracking_details_en_undelivered.xml +116 -0
  68. data/test/fixtures/xml/canadapost_pws/tracking_details_fr.xml +156 -0
  69. data/test/fixtures/xml/canadapost_pws/tracking_details_no_expected_delivery_date.xml +40 -0
  70. data/test/fixtures/xml/fedex/freight_rate_request.xml +82 -0
  71. data/test/fixtures/xml/fedex/freight_rate_response.xml +506 -0
  72. data/test/fixtures/xml/fedex/invalid_fedex_reply.xml +27 -0
  73. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_commercial_rate_request.xml +79 -0
  74. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_request.xml +80 -0
  75. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_response.xml +214 -0
  76. data/test/fixtures/xml/fedex/raterequest_reply.xml +213 -0
  77. data/test/fixtures/xml/fedex/reply_without_notifications.xml +185 -0
  78. data/test/fixtures/xml/fedex/tracking_request.xml +27 -0
  79. data/test/fixtures/xml/fedex/tracking_response.xml +151 -0
  80. data/test/fixtures/xml/fedex/tracking_response_empty_destination.xml +76 -0
  81. data/test/fixtures/xml/fedex/tracking_response_no_destination.xml +139 -0
  82. data/test/fixtures/xml/fedex/tracking_response_no_ship_time.xml +150 -0
  83. data/test/fixtures/xml/fedex/tracking_response_with_estimated_delivery_date.xml +95 -0
  84. data/test/fixtures/xml/fedex/tracking_response_with_shipper_address.xml +71 -0
  85. data/test/fixtures/xml/fedex/unknown_fedex_document_reply.xml +3 -0
  86. data/test/fixtures/xml/kunaki/invalid_state_response.xml +3 -0
  87. data/test/fixtures/xml/kunaki/no_valid_items_response.xml +3 -0
  88. data/test/fixtures/xml/kunaki/successful_rates_response.xml +3 -0
  89. data/test/fixtures/xml/kunaki/unsuccessful_rates_response.xml +9 -0
  90. data/test/fixtures/xml/shipwire/international_rates_response.xml +17 -0
  91. data/test/fixtures/xml/shipwire/new_carrier_rate_response.xml +18 -0
  92. data/test/fixtures/xml/shipwire/no_rates_response.xml +7 -0
  93. data/test/fixtures/xml/shipwire/rates_response.xml +36 -0
  94. data/test/fixtures/xml/shipwire/rates_response_no_estimate.xml +14 -0
  95. data/test/fixtures/xml/stamps/authenticate_user_request.xml +15 -0
  96. data/test/fixtures/xml/stamps/authenticate_user_response.xml +10 -0
  97. data/test/fixtures/xml/stamps/cleanse_address_request.xml +19 -0
  98. data/test/fixtures/xml/stamps/cleanse_address_response.xml +27 -0
  99. data/test/fixtures/xml/stamps/create_indicium_request.xml +69 -0
  100. data/test/fixtures/xml/stamps/create_indicium_response.xml +40 -0
  101. data/test/fixtures/xml/stamps/expired_authenticator_response.xml +15 -0
  102. data/test/fixtures/xml/stamps/get_account_info_request.xml +11 -0
  103. data/test/fixtures/xml/stamps/get_account_info_response.xml +36 -0
  104. data/test/fixtures/xml/stamps/get_purchase_status_request.xml +12 -0
  105. data/test/fixtures/xml/stamps/get_purchase_status_response.xml +16 -0
  106. data/test/fixtures/xml/stamps/get_rates_request.xml +19 -0
  107. data/test/fixtures/xml/stamps/get_rates_response.xml +351 -0
  108. data/test/fixtures/xml/stamps/purchase_postage_request.xml +13 -0
  109. data/test/fixtures/xml/stamps/purchase_postage_response.xml +17 -0
  110. data/test/fixtures/xml/stamps/track_shipment_request.xml +12 -0
  111. data/test/fixtures/xml/stamps/track_shipment_response.xml +45 -0
  112. data/test/fixtures/xml/ups/delivered_shipment_with_refund.xml +290 -0
  113. data/test/fixtures/xml/ups/delivered_shipment_without_events_tracking_response.xml +62 -0
  114. data/test/fixtures/xml/ups/example_tracking_response.xml +53 -0
  115. data/test/fixtures/xml/ups/in_transit_shipment.xml +183 -0
  116. data/test/fixtures/xml/ups/out_for_delivery_shipment.xml +165 -0
  117. data/test/fixtures/xml/ups/shipment_accept_response.xml +42 -0
  118. data/test/fixtures/xml/ups/shipment_confirm_response.xml +33 -0
  119. data/test/fixtures/xml/ups/shipment_from_tiger_direct.xml +222 -0
  120. data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response.xml +1 -0
  121. data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response_with_insured.xml +289 -0
  122. data/test/fixtures/xml/ups/test_real_home_as_residential_destination_with_origin_account_response.xml +311 -0
  123. data/test/fixtures/xml/ups/triple_accept_response.xml +72 -0
  124. data/test/fixtures/xml/ups/triple_confirm_response.xml +32 -0
  125. data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_commercial_base_rate_response.xml +2 -0
  126. data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_commercial_plus_rate_response.xml +258 -0
  127. data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_rate_response.xml +108 -0
  128. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_commercial_base_rate_response.xml +84 -0
  129. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_commercial_plus_rate_response.xml +212 -0
  130. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_rate_response.xml +230 -0
  131. data/test/fixtures/xml/usps/delivered_tracking_response.xml +11 -0
  132. data/test/fixtures/xml/usps/first_class_packages_with_invalid_mail_type_response.xml +12 -0
  133. data/test/fixtures/xml/usps/first_class_packages_with_mail_type_response.xml +16 -0
  134. data/test/fixtures/xml/usps/first_class_packages_without_mail_type_response.xml +12 -0
  135. data/test/fixtures/xml/usps/invalid_xml_tracking_response_error.xml +2 -0
  136. data/test/fixtures/xml/usps/tracking_request.xml +3 -0
  137. data/test/fixtures/xml/usps/tracking_response.xml +13 -0
  138. data/test/fixtures/xml/usps/tracking_response_failure.xml +3 -0
  139. data/test/fixtures/xml/usps/tracking_response_not_available.xml +12 -0
  140. data/test/fixtures/xml/usps/tracking_response_test_error.xml +8 -0
  141. data/test/fixtures/xml/usps/us_rate_request.xml +18 -0
  142. data/test/fixtures/xml/usps/world_rate_request_with_value.xml +20 -0
  143. data/test/fixtures/xml/usps/world_rate_request_without_value.xml +20 -0
  144. data/test/remote/canada_post_pws_platform_test.rb +246 -0
  145. data/test/remote/canada_post_pws_test.rb +171 -0
  146. data/test/remote/canada_post_test.rb +53 -0
  147. data/test/remote/fedex_test.rb +216 -0
  148. data/test/remote/kunaki_test.rb +36 -0
  149. data/test/remote/new_zealand_post_test.rb +147 -0
  150. data/test/remote/shipwire_test.rb +82 -0
  151. data/test/remote/stamps_test.rb +394 -0
  152. data/test/remote/ups_test.rb +257 -0
  153. data/test/remote/usps_test.rb +235 -0
  154. data/test/test_helper.rb +220 -0
  155. data/test/unit/carriers/benchmark_test.rb +18 -0
  156. data/test/unit/carriers/canada_post_pws_rating_test.rb +349 -0
  157. data/test/unit/carriers/canada_post_pws_register_test.rb +75 -0
  158. data/test/unit/carriers/canada_post_pws_shipping_test.rb +244 -0
  159. data/test/unit/carriers/canada_post_pws_tracking_test.rb +154 -0
  160. data/test/unit/carriers/canada_post_test.rb +145 -0
  161. data/test/unit/carriers/fedex_test.rb +408 -0
  162. data/test/unit/carriers/kunaki_test.rb +52 -0
  163. data/test/unit/carriers/new_zealand_post_test.rb +174 -0
  164. data/test/unit/carriers/shipwire_test.rb +187 -0
  165. data/test/unit/carriers/stamps_test.rb +241 -0
  166. data/test/unit/carriers/ups_test.rb +311 -0
  167. data/test/unit/carriers/usps_test.rb +484 -0
  168. data/test/unit/carriers_test.rb +17 -0
  169. data/test/unit/label_response_test.rb +15 -0
  170. data/test/unit/location_test.rb +138 -0
  171. data/test/unit/package_test.rb +68 -0
  172. data/test/unit/rate_estimate_test.rb +34 -0
  173. data/test/unit/response_test.rb +14 -0
  174. data/test/unit/shipment_packer_test.rb +174 -0
  175. metadata +331 -35
  176. metadata.gz.sig +0 -0
  177. data/lib/vendor/quantified/MIT-LICENSE +0 -22
  178. data/lib/vendor/quantified/README.markdown +0 -49
  179. data/lib/vendor/quantified/Rakefile +0 -13
  180. data/lib/vendor/quantified/init.rb +0 -0
  181. data/lib/vendor/quantified/lib/quantified.rb +0 -6
  182. data/lib/vendor/quantified/lib/quantified/attribute.rb +0 -211
  183. data/lib/vendor/quantified/lib/quantified/length.rb +0 -20
  184. data/lib/vendor/quantified/lib/quantified/mass.rb +0 -19
  185. data/lib/vendor/quantified/test/length_test.rb +0 -94
  186. data/lib/vendor/quantified/test/mass_test.rb +0 -96
  187. data/lib/vendor/quantified/test/test_helper.rb +0 -19
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ class CanadaPostPwsRegisterTest < Minitest::Test
4
+ include ActiveShipping::Test::Credentials
5
+ include ActiveShipping::Test::Fixtures
6
+
7
+ def setup
8
+ @cp = CanadaPostPWS.new(credentials(:canada_post_pws))
9
+ end
10
+
11
+ def test_register_merchant
12
+ endpoint = @cp.endpoint + "ot/token"
13
+ response = xml_fixture('canadapost_pws/register_token_response')
14
+ @cp.expects(:ssl_post).with(endpoint, anything, anything).returns(response)
15
+
16
+ response = @cp.register_merchant
17
+ assert response.is_a?(CPPWSRegisterResponse)
18
+ assert_equal "34536456345353534535", response.token_id
19
+ end
20
+
21
+ def test_register_merchant_with_error
22
+ endpoint = @cp.endpoint + "ot/token"
23
+ response = xml_fixture('canadapost_pws/register_token_error')
24
+ http_response = mock
25
+ http_response.stubs(:code).returns('400')
26
+ http_response.stubs(:body).returns(response)
27
+ response_error = ActiveUtils::ResponseError.new(http_response)
28
+ @cp.expects(:ssl_post).with(endpoint, anything, anything).raises(response_error)
29
+
30
+ exception = assert_raises ActiveShipping::ResponseError do
31
+ @cp.register_merchant
32
+ end
33
+
34
+ assert_equal "Platform not active", exception.message
35
+ end
36
+
37
+ def test_register_response_redirect_url
38
+ endpoint = @cp.endpoint + "ot/token"
39
+ response = xml_fixture('canadapost_pws/register_token_response')
40
+ @cp.expects(:ssl_post).with(endpoint, anything, anything).returns(response)
41
+
42
+ response = @cp.register_merchant
43
+ assert "http://www.canadapost.ca/cpotools/apps/drc/merchant?return-url=#{CGI.escape('http://localhost:3000/cp-register/')}&token-id=#{response.token_id}&platform-id=111111111", response.redirect_url('http://localhost:3000/cp-register/', '111111111')
44
+ end
45
+
46
+ def test_retrieve_merchant_details
47
+ endpoint = @cp.endpoint + "ot/token/1234567890"
48
+ response = xml_fixture('canadapost_pws/merchant_details_response')
49
+ @cp.expects(:ssl_get).with(endpoint, anything).returns(response)
50
+
51
+ response = @cp.retrieve_merchant_details(:token_id => '1234567890')
52
+ assert response.is_a?(CPPWSMerchantDetailsResponse)
53
+ assert_equal "1234567890", response.customer_number
54
+ assert_equal "1234567890-", response.contract_number
55
+ assert_equal "1234567890123456", response.username
56
+ assert_equal "12343567890123456789012", response.password
57
+ assert_equal false, response.has_default_credit_card
58
+ end
59
+
60
+ def test_retrieve_merchant_with_error
61
+ endpoint = @cp.endpoint + "ot/token/1234567890"
62
+ response = xml_fixture('canadapost_pws/merchant_details_error')
63
+ http_response = mock
64
+ http_response.stubs(:code).returns('400')
65
+ http_response.stubs(:body).returns(response)
66
+ response_error = ActiveUtils::ResponseError.new(http_response)
67
+ @cp.expects(:ssl_get).with(endpoint, anything).raises(response_error)
68
+
69
+ exception = assert_raises ActiveShipping::ResponseError do
70
+ @cp.retrieve_merchant_details(:token_id => '1234567890')
71
+ end
72
+
73
+ assert_equal "Merchant Details Error", exception.message
74
+ end
75
+ end
@@ -0,0 +1,244 @@
1
+ require 'test_helper'
2
+ class CanadaPostPwsShippingTest < Minitest::Test
3
+ include ActiveShipping::Test::Credentials
4
+ include ActiveShipping::Test::Fixtures
5
+
6
+ def setup
7
+ # 100 grams, 93 cm long, 10 cm diameter, cylinders have different volume calculations
8
+ @pkg1 = Package.new(25, [93, 10], :cylinder => true)
9
+
10
+ # 7.5 lbs, times 16 oz/lb., 15x10x4.5 inches, not grams, not centimetres
11
+ @pkg2 = Package.new( (7.5 * 16), [15, 10, 4.5], :units => :imperial)
12
+
13
+ @line_item1 = line_item_fixture
14
+
15
+ @home_params = {
16
+ :name => "John Smith",
17
+ :company => "test",
18
+ :phone => "613-555-1212",
19
+ :address1 => "123 Elm St.",
20
+ :city => 'Ottawa',
21
+ :province => 'ON',
22
+ :country => 'CA',
23
+ :postal_code => 'K1P 1J1'
24
+ }
25
+
26
+ @dom_params = {
27
+ :name => "Mrs. Smith",
28
+ :company => "",
29
+ :phone => "604-555-1212",
30
+ :address1 => "5000 Oak St.",
31
+ :address2 => "",
32
+ :city => 'Vancouver',
33
+ :province => 'BC',
34
+ :country => 'CA',
35
+ :postal_code => 'V5J 2N2'
36
+ }
37
+
38
+ @us_params = {
39
+ :name => "John Smith",
40
+ :company => "test",
41
+ :phone => "613-555-1212",
42
+ :address1 => "123 Elm St.",
43
+ :address2 => "",
44
+ :city => 'Beverly Hills',
45
+ :province => 'CA',
46
+ :country => 'US',
47
+ :postal_code => '90210'
48
+ }
49
+
50
+ @paris_params = {
51
+ :name => "John Smith",
52
+ :company => "test",
53
+ :phone => "613-555-1212",
54
+ :address1 => "5 avenue Anatole France - Champ de Mars",
55
+ :address2 => "",
56
+ :city => 'Paris',
57
+ :province => '',
58
+ :country => 'FR',
59
+ :postal_code => '75007'
60
+ }
61
+
62
+ @shipping_opts1 = {:dc => true, :cod => :true, :cod_amount => 50.00, :cod_includes_shipping => true,
63
+ :cod_method_of_payment => 'CSH', :cov => true, :cov_amount => 100.00,
64
+ :so => true, :pa18 => true}
65
+
66
+ @default_options = {:customer_number => '123456'}
67
+
68
+ @DEFAULT_RESPONSE = {
69
+ :shipping_id => "406951321983787352",
70
+ :tracking_number => "11111118901234",
71
+ :label_url => "https://ct.soa-gw.canadapost.ca/ers/artifact/c70da5ed5a0d2c32/20238/0"
72
+ }
73
+
74
+ @cp = CanadaPostPWS.new(credentials(:canada_post_pws))
75
+ end
76
+
77
+ def test_build_shipment_customs_node
78
+ options = @default_options.dup
79
+ destination = Location.new(@us_params)
80
+ assert_instance_of XmlNode, response = @cp.shipment_customs_node(destination, @line_item1, options)
81
+ doc = REXML::Document.new(response.to_s)
82
+ assert root_node = doc.elements['customs']
83
+ assert_equal "CAD", root_node.get_text('currency').to_s
84
+ assert items_node = root_node.elements['sku-list']
85
+ assert_equal 2, items_node.size
86
+ assert_equal 199.0, items_node.first.elements['customs-value-per-unit'].text.to_f
87
+ end
88
+
89
+ def test_build_shipment_request_for_domestic
90
+ options = @default_options.dup
91
+ request = @cp.build_shipment_request(@home_params, @dom_params, @pkg1, @line_item1, options)
92
+ refute request.blank?
93
+ end
94
+
95
+ def test_build_shipment_request_for_US
96
+ options = @default_options.dup
97
+ request = @cp.build_shipment_request(@home_params, @us_params, @pkg1, @line_item1, options)
98
+ refute request.blank?
99
+ doc = REXML::Document.new(request)
100
+ assert root_node = doc.elements['non-contract-shipment']
101
+ assert delivery_spec = root_node.elements['delivery-spec']
102
+ assert destination = delivery_spec.elements['destination']
103
+ assert address_details = destination.elements['address-details']
104
+ assert_equal 'US', address_details.get_text('country-code').to_s
105
+ end
106
+
107
+ def test_build_shipment_request_for_international
108
+ options = @default_options.dup
109
+ request = @cp.build_shipment_request(@home_params, @paris_params, @pkg1, @line_item1, options)
110
+ refute request.blank?
111
+ end
112
+
113
+ def test_build_shipment_request_location_object
114
+ options = @default_options.dup
115
+ request = @cp.build_shipment_request(Location.new(@home_params), Location.new(@dom_params), @pkg1, @line_item1, options)
116
+ refute request.blank?
117
+ end
118
+
119
+ def test_create_shipment_request_with_options
120
+ options = @default_options.merge(@shipping_opts1)
121
+ request = @cp.build_shipment_request(@home_params, @paris_params, @pkg1, @line_item1, options)
122
+ refute request.blank?
123
+ doc = REXML::Document.new(request)
124
+ assert root_node = doc.elements['non-contract-shipment']
125
+ assert delivery_spec = root_node.elements['delivery-spec']
126
+ assert options = delivery_spec.elements['options']
127
+ assert_equal 5, options.elements.size
128
+ end
129
+
130
+ def test_build_shipping_request_with_zero_weight
131
+ options = @default_options.merge(@shipping_opts1)
132
+ package = Package.new(0, [93, 10])
133
+ request = @cp.build_shipment_request(@home_params, @dom_params, package, @line_item1, options)
134
+ refute request.blank?
135
+ doc = REXML::Document.new(request)
136
+ assert root_node = doc.elements['non-contract-shipment']
137
+ assert delivery_spec = root_node.elements['delivery-spec']
138
+ assert parcel_node = delivery_spec.elements['parcel-characteristics']
139
+ assert_equal '0.001', parcel_node.get_text('weight').to_s
140
+ end
141
+
142
+ def test_create_shipment_domestic
143
+ options = @default_options.dup
144
+ response = xml_fixture('canadapost_pws/shipment_response')
145
+ @cp.expects(:ssl_post).once.returns(response)
146
+ response = @cp.create_shipment(@home_params, @dom_params, @pkg1, @line_item1, options)
147
+ assert response.is_a?(CPPWSShippingResponse)
148
+ assert_equal @DEFAULT_RESPONSE[:shipping_id], response.shipping_id
149
+ assert_equal @DEFAULT_RESPONSE[:tracking_number], response.tracking_number
150
+ assert_equal @DEFAULT_RESPONSE[:label_url], response.label_url
151
+ end
152
+
153
+ def test_create_shipment_us
154
+ options = @default_options.dup
155
+ response = xml_fixture('canadapost_pws/shipment_response')
156
+ @cp.expects(:ssl_post).once.returns(response)
157
+ response = @cp.create_shipment(@home_params, @us_params, @pkg1, @line_item1, options)
158
+ assert response.is_a?(CPPWSShippingResponse)
159
+ assert_equal @DEFAULT_RESPONSE[:shipping_id], response.shipping_id
160
+ assert_equal @DEFAULT_RESPONSE[:tracking_number], response.tracking_number
161
+ assert_equal @DEFAULT_RESPONSE[:label_url], response.label_url
162
+ end
163
+
164
+ def test_create_shipment_international
165
+ options = @default_options.dup
166
+ response = xml_fixture('canadapost_pws/shipment_response')
167
+ @cp.expects(:ssl_post).once.returns(response)
168
+ response = @cp.create_shipment(@home_params, @us_params, @pkg1, @line_item1, options)
169
+ assert response.is_a?(CPPWSShippingResponse)
170
+ assert_equal @DEFAULT_RESPONSE[:shipping_id], response.shipping_id
171
+ assert_equal @DEFAULT_RESPONSE[:tracking_number], response.tracking_number
172
+ assert_equal @DEFAULT_RESPONSE[:label_url], response.label_url
173
+ end
174
+
175
+ def test_retrieve_shipping_label
176
+ shipping_response = CPPWSShippingResponse.new(true, '', {}, @DEFAULT_RESPONSE)
177
+ @cp.expects(:ssl_get).once.returns(file_fixture('label1.pdf'))
178
+ response = @cp.retrieve_shipping_label(shipping_response)
179
+ assert_equal "%PDF", response[0...4]
180
+ end
181
+
182
+ def test_retrieve_shipment
183
+ options = @default_options.dup
184
+ shipping_response = CPPWSShippingResponse.new(true, '', {}, @DEFAULT_RESPONSE)
185
+ response = xml_fixture('canadapost_pws/shipment_response')
186
+ @cp.expects(:ssl_post).once.returns(response)
187
+ response = @cp.retrieve_shipment(shipping_response.shipping_id, options)
188
+ assert response.is_a?(CPPWSShippingResponse)
189
+ assert_equal @DEFAULT_RESPONSE[:shipping_id], response.shipping_id
190
+ assert_equal @DEFAULT_RESPONSE[:tracking_number], response.tracking_number
191
+ assert_equal @DEFAULT_RESPONSE[:label_url], response.label_url
192
+ end
193
+
194
+ def test_parse_find_shipment_receipt_response
195
+ body = xml_fixture('canadapost_pws/receipt_response')
196
+ response = @cp.parse_shipment_receipt_response(body)
197
+ assert_equal "J4W4T0", response[:final_shipping_point]
198
+ assert_equal "BP BROSSARD", response[:shipping_point_name]
199
+ assert_equal "DOM.EP", response[:service_code]
200
+ assert_equal 15.000, response[:rated_weight]
201
+ assert_equal 18.10, response[:base_amount]
202
+ assert_equal 19.46, response[:pre_tax_amount]
203
+ assert_equal 0.00, response[:gst_amount]
204
+ assert_equal 0.00, response[:pst_amount]
205
+ assert_equal 2.53, response[:hst_amount]
206
+ assert_equal 1, response[:priced_options].size
207
+ assert_equal 0.0, response[:priced_options]['DC']
208
+ assert_equal 21.99, response[:charge_amount]
209
+ assert_equal 'CAD', response[:currency]
210
+ assert_equal 1, response[:expected_transit_days]
211
+ assert_equal '2012-03-14', response[:expected_delivery_date]
212
+ end
213
+
214
+ def test_find_shipment_receipt
215
+ options = @default_options.dup
216
+ xml_response = xml_fixture('canadapost_pws/receipt_response')
217
+ @cp.expects(:ssl_get).once.returns(xml_response)
218
+ response = @cp.find_shipment_receipt('1234567', options)
219
+ assert_equal @cp.parse_shipment_receipt_response(xml_response), response
220
+ end
221
+
222
+ def test_character_limit_on_customs_description
223
+ @line_item1.first.stubs(:name).returns("Some super long description that exceeds the 44 character limit")
224
+ options = @default_options.dup
225
+
226
+ response = xml_fixture('canadapost_pws/shipment_response')
227
+ @cp.expects(:ssl_post).with do |_url, request|
228
+ request_hash = Hash.from_xml(request)
229
+
230
+ assert_equal 44, request_hash['non_contract_shipment']['delivery_spec']['customs']['sku_list']['item'].first['customs_description'].length
231
+ true
232
+ end.once.returns(response)
233
+
234
+ response = @cp.create_shipment(@home_params, @us_params, @pkg1, @line_item1, options)
235
+ assert response.is_a?(CPPWSShippingResponse)
236
+ end
237
+
238
+ def test_parse_find_shipment_receipt_doesnt_break_without_priced_options
239
+ body = xml_fixture('canadapost_pws/receipt_response_no_priced_options')
240
+ response = @cp.parse_shipment_receipt_response(body)
241
+
242
+ assert_equal 0, response[:priced_options].size
243
+ end
244
+ end
@@ -0,0 +1,154 @@
1
+ require 'test_helper'
2
+ class CanadaPostPwsTrackingTest < Minitest::Test
3
+ include ActiveShipping::Test::Credentials
4
+ include ActiveShipping::Test::Fixtures
5
+
6
+ def setup
7
+ # 100 grams, 93 cm long, 10 cm diameter, cylinders have different volume calculations
8
+ @pkg1 = Package.new(25, [93, 10], :cylinder => true)
9
+ # 7.5 lbs, times 16 oz/lb., 15x10x4.5 inches, not grams, not centimetres
10
+ @pkg2 = Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial)
11
+
12
+ @home = Location.new(
13
+ :name => "John Smith",
14
+ :company => "test",
15
+ :phone => "613-555-1212",
16
+ :address1 => "123 Elm St.",
17
+ :city => 'Ottawa',
18
+ :province => 'ON',
19
+ :country => 'CA',
20
+ :postal_code => 'K1P1J1'
21
+ )
22
+
23
+ @dest = Location.new(
24
+ :name => "Frank White",
25
+ :address1 => '999 Wiltshire Blvd',
26
+ :city => 'Beverly Hills',
27
+ :state => 'CA',
28
+ :country => 'US',
29
+ :zip => '90210'
30
+ )
31
+
32
+ @cp = CanadaPostPWS.new(credentials(:canada_post_pws))
33
+ end
34
+
35
+ def test_find_tracking_info_with_valid_pin
36
+ pin = '1371134583769923'
37
+ endpoint = @cp.endpoint + "vis/track/pin/%s/detail" % pin
38
+ response = xml_fixture('canadapost_pws/tracking_details_en')
39
+ @cp.expects(:ssl_get).with(endpoint, anything).returns(response)
40
+
41
+ response = @cp.find_tracking_info(pin)
42
+ assert response.is_a?(CPPWSTrackingResponse)
43
+ end
44
+
45
+ def test_find_tracking_info_with_15_digit_dnc
46
+ dnc = "315052413796541"
47
+ endpoint = @cp.endpoint + "vis/track/dnc/%s/detail" % dnc
48
+ response = xml_fixture('canadapost_pws/dnc_tracking_details_en')
49
+ @cp.expects(:ssl_get).with(endpoint, anything).returns(response)
50
+
51
+ response = @cp.find_tracking_info(dnc)
52
+ assert response.is_a?(CPPWSTrackingResponse)
53
+ end
54
+
55
+ def test_find_tracking_info_when_pin_doesnt_exist
56
+ pin = '1371134583769924'
57
+ response = xml_fixture('canadapost_pws/tracking_details_en_error')
58
+ http_response = mock
59
+ http_response.stubs(:code).returns('400')
60
+ http_response.stubs(:body).returns(response)
61
+ response_error = ActiveUtils::ResponseError.new(http_response)
62
+ @cp.expects(:ssl_get).raises(response_error)
63
+
64
+ exception = assert_raises ActiveShipping::ResponseError do
65
+ @cp.find_tracking_info(pin)
66
+ end
67
+
68
+ assert_equal "No Pin History", exception.message
69
+ end
70
+
71
+ def test_find_tracking_info_with_invalid_pin_format
72
+ pin = '123'
73
+ @cp.expects(:ssl_get).never
74
+
75
+ exception = assert_raises ActiveShipping::ResponseError do
76
+ @cp.find_tracking_info(pin)
77
+ end
78
+ assert_equal "Invalid Pin Format", exception.message
79
+ end
80
+
81
+ # parse_tracking_response
82
+
83
+ def test_parse_tracking_response
84
+ @response = xml_fixture('canadapost_pws/tracking_details_en')
85
+ @cp.expects(:ssl_get).returns(@response)
86
+
87
+ response = @cp.find_tracking_info('1371134583769923', {})
88
+
89
+ assert_equal CPPWSTrackingResponse, response.class
90
+ assert_equal "Xpresspost", response.service_name
91
+ assert_equal Date.parse("2011-02-01"), response.expected_date
92
+ assert_equal "Customer addressing error found; attempting to correct", response.change_reason
93
+ assert_equal "1371134583769923", response.tracking_number
94
+ assert_equal 10, response.shipment_events.size
95
+
96
+ assert_equal response.origin.city, "LACHINE"
97
+ assert_equal response.origin.province, "QC"
98
+
99
+ assert response.origin.is_a?(Location)
100
+ assert response.destination.is_a?(Location)
101
+ assert_equal "G1K4M7", response.destination.to_s
102
+ assert_equal "0001371134", response.customer_number
103
+ assert_equal true, response.delivered?
104
+ assert_equal Time.parse('2011-02-03 16:59:59 UTC'), response.actual_delivery_time
105
+ end
106
+
107
+ def test_parse_tracking_response_no_expected_delivery_date
108
+ @response = xml_fixture('canadapost_pws/tracking_details_no_expected_delivery_date')
109
+ @cp.expects(:ssl_get).returns(@response)
110
+
111
+ response = @cp.find_tracking_info('1371134583769923', {})
112
+
113
+ assert_equal CPPWSTrackingResponse, response.class
114
+ assert_equal "Expedited Parcels", response.service_name
115
+ assert_equal nil, response.expected_date
116
+ assert_equal "8213295707205355", response.tracking_number
117
+ assert_equal 1, response.shipment_events.size
118
+ assert_equal response.origin.city, "MAPLE"
119
+ assert_equal response.origin.province, "ON"
120
+ assert response.origin.is_a?(Location)
121
+ assert response.destination.is_a?(Location)
122
+ assert_equal "J7Y0E4", response.destination.to_s
123
+ assert_equal "0008213295", response.customer_number
124
+ assert_equal false, response.delivered?
125
+ end
126
+
127
+ def test_parse_undelivered_tracking_response
128
+ @response = xml_fixture('canadapost_pws/tracking_details_en_undelivered')
129
+ @cp.expects(:ssl_get).returns(@response)
130
+ response = @cp.find_tracking_info('1371134583769923', {})
131
+
132
+ assert_equal false, response.delivered?
133
+ assert_equal nil, response.actual_delivery_time
134
+ end
135
+
136
+ def test_parse_tracking_response_shipment_events
137
+ @response = xml_fixture('canadapost_pws/tracking_details_en')
138
+ @cp.expects(:ssl_get).returns(@response)
139
+
140
+ response = @cp.find_tracking_info('1371134583769923', {})
141
+ events = response.shipment_events
142
+
143
+ event = events.first
144
+ assert_equal ShipmentEvent, event.class
145
+ assert_equal "1496", event.name
146
+ assert_equal "SAINTE-FOY, QC", event.location
147
+ assert event.time.is_a?(Time)
148
+ assert_equal "Item successfully delivered", event.message
149
+
150
+ timestamps = events.map(&:time)
151
+ ordered = timestamps.dup.sort.reverse # newest => oldest
152
+ assert_equal ordered, timestamps
153
+ end
154
+ end