From 272e146dec33628e410fef73f1556743d3a2479d Mon Sep 17 00:00:00 2001 From: Nigel Ramsay Date: Fri, 26 Nov 2010 16:57:46 +1300 Subject: [PATCH] Added remote test case implemented find_rates --- .../shipping/carriers/new_zealand_post.rb | 22 +++++------ test/remote/new_zealand_post_test.rb | 39 +++++++++++++++++++ test/test_helper.rb | 12 ++++-- test/unit/carriers/new_zealand_post_test.rb | 4 +- 4 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 test/remote/new_zealand_post_test.rb diff --git a/lib/active_shipping/shipping/carriers/new_zealand_post.rb b/lib/active_shipping/shipping/carriers/new_zealand_post.rb index cfbed72f0..4a9c3b7ff 100644 --- a/lib/active_shipping/shipping/carriers/new_zealand_post.rb +++ b/lib/active_shipping/shipping/carriers/new_zealand_post.rb @@ -18,7 +18,11 @@ def requirements # Override with whatever you need to get the rates def find_rates(origin, destination, packages, options = {}) - + packages = Array(packages) + request_hash = build_rectangular_request_params(origin, destination, packages, options) + url = URL + '?' + request_hash.to_param + xml_response = ssl_get(url) + parse_rate_response(origin, destination, packages, xml_response, options) end def maximum_weight @@ -27,25 +31,17 @@ def maximum_weight protected - def node_text_or_nil(xml_node) - xml_node ? xml_node.text : nil - end - # Override in subclasses for non-U.S.-based carriers. def self.default_location - Location.new( :country => 'NZ', - :city => 'Wellington', - :address1 => '455 Rexford Dr', - :zip => '6012', - :phone => '') + Location.new(:postal_code => '6011') end private def build_rectangular_request_params(origin, destination, line_items = [], options = {}) params = { - :postcode_src => origin[:postal_code], - :postcode_dest => destination[:postal_code], + :postcode_src => origin.postal_code, + :postcode_dest => destination.postal_code, :api_key => @options[:api_key] } @@ -78,7 +74,7 @@ def parse_rate_response(origin, destination, packages, response, options={}) RateResponse.new(true, "Success", Hash.from_xml(response), :rates => rate_estimates, :xml => response) else - error_message = response_message(response_message) + error_message = response_message(xml) RateResponse.new(false, error_message, Hash.from_xml(response), :rates => rate_estimates, :xml => response) end end diff --git a/test/remote/new_zealand_post_test.rb b/test/remote/new_zealand_post_test.rb new file mode 100644 index 000000000..03fe2f6f8 --- /dev/null +++ b/test/remote/new_zealand_post_test.rb @@ -0,0 +1,39 @@ +require 'test_helper' + +class NewZealandPostTest < Test::Unit::TestCase + + def setup + @packages = TestFixtures.packages + @locations = TestFixtures.locations + @carrier = NewZealandPost.new(fixtures(:new_zealand_post).merge(:test => true)) + end + + def test_valid_credentials + assert @carrier.valid_credentials? + end + + def test_successful_rates_request + response = @carrier.find_rates( + @locations[:wellington], + @locations[:wellington], + @packages.values_at(:book, :wii)) + + assert response.is_a?(RateResponse) + assert response.success? + assert response.rates.any? + assert response.rates.first.is_a?(RateEstimate) + end + + def test_failure_rates_request + begin + @carrier.find_rates( + @locations[:wellington], + @locations[:wellington], + @packages[:shipping_container]) + + flunk "expected an ActiveMerchant::Shipping::ResponseError to be raised" + rescue ActiveMerchant::Shipping::ResponseError => e + assert_equal 'length Value is too large', e.message + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 55c60f5b1..2ece85d74 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -74,7 +74,8 @@ module TestFixtures :poster => Package.new(100, [93,10], :cylinder => true), :small_half_pound => Package.new(8, [1,1,1], :units => :imperial), :big_half_pound => Package.new((16 * 50), [24,24,36], :units => :imperial), - :chocolate_stuff => Package.new(80, [2,6,12], :units => :imperial) + :chocolate_stuff => Package.new(80, [2,6,12], :units => :imperial), + :shipping_container => Package.new(2200000, [2440, 2600, 6058], :description => '20 ft Standard Container', :units => :metric) } @@locations = { @@ -161,8 +162,13 @@ module TestFixtures :state => 'NY', :address1 => '780 3rd Avenue', :address2 => 'Suite 2601', - :zip => '10017') - + :zip => '10017'), + :wellington => Location.new( + :country => 'NZ', + :city => 'Wellington', + :address1 => '85 Victoria St', + :address2 => 'Te Aro', + :postal_code => '6011') } end diff --git a/test/unit/carriers/new_zealand_post_test.rb b/test/unit/carriers/new_zealand_post_test.rb index d56289e9b..1fdff2ac5 100644 --- a/test/unit/carriers/new_zealand_post_test.rb +++ b/test/unit/carriers/new_zealand_post_test.rb @@ -10,8 +10,8 @@ def setup @response = xml_fixture('newzealandpost/example_response') @bad_response = xml_fixture('newzealandpost/example_response_error') - @origin = {:postal_code => "6011"} - @destination = {:postal_code => "6012"} + @origin = Location.new(:postal_code => "6011") + @destination = Location.new(:postal_code => "6012") @line_items = [Package.new(400, [25, 15, 2], :description => "Edmonds Cookbook",