def test_tracking_auth_error_parsing(self): with patch("purplship.package.mappers.canadapost.proxy.http") as mock: mock.return_value = AuthError parsed_response = (Tracking.fetch( self.TrackingRequest).from_(gateway).parse()) self.assertEqual(to_dict(parsed_response), to_dict(ParsedAuthError))
def test_tracking_unknown_response_parsing(self): with patch("purplship.package.mappers.canadapost.proxy.http") as mock: mock.return_value = UnknownTrackingNumberResponse parsed_response = (Tracking.fetch( self.TrackingRequest).from_(gateway).parse()) self.assertEqual(to_dict(parsed_response), to_dict(ParsedUnknownTrackingNumberResponse))
def test_parse_intl_quote_response(self): with patch("purplship.package.mappers.usps.proxy.http") as mock: mock.return_value = INTL_RATE_RESPONSE parsed_response = Rating.fetch( self.RateRequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(PARSED_INTL_RATE_RESPONSE))
def test_parse_rate_missing_args_error(self): with patch("purplship.package.mappers.canadapost.proxy.http") as mock: mock.return_value = QuoteMissingArgsError parsed_response = Rating.fetch( self.RateRequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(ParsedQuoteMissingArgsError))
def test_tracking_single_not_found_parsing(self): with patch("purplship.package.mappers.dhl.proxy.http") as mock: mock.return_value = TrackingSingleNotFound parsed_response = (Tracking.fetch( self.TrackingRequest).from_(gateway).parse()) self.assertEqual(to_dict(parsed_response), to_dict(ParsedTrackingSingNotFound))
def test_parse_rate_error_response(self): with patch("purplship.package.mappers.fedex.proxy.http") as mock: mock.return_value = RateErrorResponseXml parsed_response = Rating.fetch( self.RateRequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(ParsedRateErrorResponse))
def test_parse_rate_vol_weight_higher_response(self): with patch("purplship.package.mappers.dhl.proxy.http") as mock: mock.return_value = RateVolWeightHigher parsed_response = Rating.fetch(self.RateRequest).from_(gateway).parse() self.assertEqual( to_dict(parsed_response), to_dict(ParsedRateVolWeightHigher) )
def ship(request: Request): try: try: shipping_request = ShipmentRequest(data=request.data) shipping_request.is_valid(raise_exception=True) response = create_shipment( shipping_request.data, resolve_tracking_url=(lambda trackin_url, shipping: reverse( "Tracking", request=request, kwargs=dict(tracking_number=shipping.tracking_number, carrier_name=shipping.carrier_name)))) if isinstance(response, ErrorResponse): Response(to_dict(response), status=status.HTTP_400_BAD_REQUEST) return Response(to_dict(response), status=status.HTTP_201_CREATED) except ValidationError as ve: logger.exception(ve) return Response(ve.args, status=status.HTTP_400_BAD_REQUEST) except Exception as e: logger.exception(e) return Response(e.args, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_parse_tracking_response(self): with patch("purplship.package.mappers.usps.proxy.http") as mock: mock.return_value = TRACKING_RESPONSE parsed_response = (Tracking.fetch( self.TrackingRequest).from_(gateway).parse()) self.assertEqual(to_dict(parsed_response), to_dict(PARSED_TRACKING_RESPONSE))
def test_parse_publish_rate_shipment_response(self): with patch("purplship.package.mappers.ups.proxy.http") as mock: mock.return_value = ShipmentResponseXML parsed_response = (Shipment.create( self.ShipmentRequest).with_(gateway).parse()) self.assertEqual(to_dict(parsed_response), to_dict(ParsedShipmentResponse))
def rates(request: Request): try: try: rate_request = RateRequest(data=request.data) rate_request.is_valid(raise_exception=True) carrier_ids = rate_request.data.get('carrier_ids', []) carrier_settings_list = (get_carriers(carrier_ids=carrier_ids) if len(carrier_ids) > 0 else get_carriers()) if len(carrier_settings_list) == 0: raise Exception(f'No configured carriers specified') response = fetch_rates(rate_request.data, carrier_settings_list) if isinstance(response, ErrorResponse): Response(to_dict(response), status=status.HTTP_400_BAD_REQUEST) return Response( to_dict(response), status=status.HTTP_207_MULTI_STATUS if len(response.messages) > 0 else status.HTTP_201_CREATED) except ValidationError as ve: logger.exception(ve) return Response(ve.args, status=status.HTTP_400_BAD_REQUEST) except Exception as e: logger.exception(e) return Response(e.args, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_parse_freight_get_rates_response(self): with patch("purplship.freight.mappers.ups.proxy.http") as mock: mock.return_value = FreightRateResponseXML parsed_response = Rating.fetch( self.RateRequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(ParsedFreightRateResponse))
def test_parse_shipment_response(self): with patch("purplship.package.mappers.canadapost.proxy.http") as mocks: mocks.side_effect = [ShipmentResponseXML, LabelResponse] parsed_response = (Shipment.create( self.ShipmentRequest).with_(gateway).parse()) self.assertEqual(to_dict(parsed_response), to_dict(ParsedShipmentResponse))
def test_tracking_response_parsing(self): with patch("purplship.package.mappers.ups.proxy.http") as mock: mock.return_value = TrackingResponseXml parsed_response = (Tracking.fetch( self.TrackingRequest).from_(gateway).parse()) self.assertEqual(to_dict(parsed_response), to_dict(ParsedTrackingResponse))
def test_parse_error_tracking_response(self): with patch("purplship.package.mappers.fedex.proxy.http") as mock: mock.return_value = TrackingErrorResponseXML parsed_response = Tracking.fetch( self.TrackRequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(ParsedTrackingResponseError))
def test_parse_cancellation_pickup_response(self): with patch("purplship.package.mappers.dhl.proxy.http") as mock: mock.return_value = CancelPUResponseXML parsed_response = Pickup.cancel( self.CancelPURequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(ParsedCancelPUResponse))
def test_parse_request_pickup_error(self): with patch("purplship.package.mappers.dhl.proxy.http") as mock: mock.return_value = PickupErrorResponseXML parsed_response = Pickup.book( self.BookPURequest).with_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(ParsedPickupErrorResponse))
def test_parse_modify_pickup_response(self): with patch("purplship.package.mappers.dhl.proxy.http") as mock: mock.return_value = ModifyPURequestXML parsed_response = Pickup.update( self.ModifyPURequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(ParsedModifyPUResponse))
def test_create_rate_request_with_package_preset_missing_weight(self, _): processing_error = (Rating.fetch( RateRequest( **RateWithPresetMissingWeightPayload)).from_(gateway).parse()) self.assertEqual(to_dict(processing_error), to_dict(ProcessingError))
def test_parse_rate_response_errors(self): with patch("purplship.package.mappers.usps.proxy.http") as mock: mock.return_value = ERRORS parsed_response = Rating.fetch( self.RateRequest).from_(gateway).parse() self.assertEqual(to_dict(parsed_response), to_dict(PARSED_ERRORS))