diff --git a/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm b/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm index 1a37d8b05..4d52983c3 100644 --- a/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm +++ b/lib/WebGUI/Shop/ShipDriver/USPSInternational.pm @@ -192,7 +192,7 @@ sub _calculateFromXML { foreach my $package (@{ $xmlData->{IntlRateResponse}->{Package} }) { my $id = $package->{ID}; ##Error check for invalid index - if ($id < 0 || $id > $#shippableUnits) { + if ($id < 0 || $id > $#shippableUnits || $id !~ /^\d+$/) { WebGUI::Error::Shop::RemoteShippingRate->throw(error => "Illegal package index returned by USPS: $id"); } if (exists $package->{Error}) { diff --git a/t/Shop/ShipDriver/USPSInternational.t b/t/Shop/ShipDriver/USPSInternational.t index 8d24e5f17..2b9d2b2a8 100644 --- a/t/Shop/ShipDriver/USPSInternational.t +++ b/t/Shop/ShipDriver/USPSInternational.t @@ -25,7 +25,7 @@ use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; use WebGUI::Shop::ShipDriver::USPSInternational; -plan tests => 37; +plan tests => 40; #---------------------------------------------------------------------------- # Init @@ -99,6 +99,14 @@ my $gospels = $bible->setCollateral('variantsJSON', 'variantId', 'new', } ); +my $singlePage = $bible->setCollateral('variantsJSON', 'variantId', 'new', + { + shortdesc => 'Single page from bible', + price => 0.01, varSku => 'page', + weight => 0.0001, quantity => 999999, + } +); + $versionTag->commit; addToCleanup($versionTag); @@ -545,19 +553,55 @@ SKIP: { ####################################################################### # -# Check too heavy for my shipping type +# Check for minimum weight allowed # ####################################################################### $cart->empty; $properties = $driver->get(); -$properties->{shipType} = '9'; +$properties->{shipType} = '9'; +$properties->{addInsurance} = 0; $driver->update($properties); +my $page1 = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $singlePage)); +@shippableUnits = $driver->_getShippableUnits($cart); +$xml = $driver->buildXML($cart, @shippableUnits); +$xmlData = XMLin($xml, + KeepRoot => 1, + ForceArray => ['Package'], +); +cmp_deeply( + $xmlData, + { + IntlRateRequest => { + USERID => $userId, + Package => [ + { + ID => 0, + Pounds => '0', Ounces => '0.1', + Machinable => 'true', Country => 'Netherlands', + MailType => 'Package', + }, + ], + } + }, + 'buildXML: minimum weight' +); + +####################################################################### +# +# Check too heavy for my shipping type +# +####################################################################### SKIP: { skip 'No userId for testing', 2 unless $hasRealUserId; + $cart->empty; + $properties = $driver->get(); + $properties->{shipType} = '9'; + $driver->update($properties); + my $heavyHammer = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer)); $heavyHammer->setQuantity(2); $cost = eval { $driver->calculate($cart); }; @@ -634,13 +678,57 @@ SKIP: { } +####################################################################### +# +# _calculateFromXML +# +####################################################################### + +$cart->empty; +$properties = $driver->get(); +$properties->{shipType} = '9'; +$properties->{addInsurance} = 1; +$driver->update($properties); +$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer)); +@shippableUnits = $driver->_getShippableUnits($cart); + +$cost = eval { $driver->_calculateFromXML( + { + IntlRateResponse => { + Package => [ + { + ID => 11, + Service => [ + { + ID => '9', + Postage => '5.25', + MaxWeight => '70' + }, + ], + }, + ], + }, + }, + @shippableUnits +); }; + +$e = Exception::Class->caught(); +isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', '_calculateFromXML throws an exception for illegal package ids'); +cmp_deeply( + $e, + methods( + error => 'Illegal package index returned by USPS: 11', + ), + '... checking error message', +); + ####################################################################### # # Check for throwing an exception # ####################################################################### -my $userId = $driver->get('userId'); +$userId = $driver->get('userId'); $properties = $driver->get(); $properties->{userId} = '_NO_NO_NO_NO'; $driver->update($properties);