Esempio n. 1
0
 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))
Esempio n. 2
0
 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))
Esempio n. 3
0
 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))
Esempio n. 4
0
 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))
Esempio n. 5
0
 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))
Esempio n. 6
0
 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))
Esempio n. 7
0
 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)
         )
Esempio n. 8
0
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)
Esempio n. 9
0
 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))
Esempio n. 10
0
 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))
Esempio n. 11
0
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)
Esempio n. 12
0
 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))
Esempio n. 13
0
    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))
Esempio n. 14
0
    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))
Esempio n. 15
0
    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))
Esempio n. 16
0
    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))
Esempio n. 17
0
    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))
Esempio n. 18
0
    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))
Esempio n. 19
0
    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))
Esempio n. 20
0
 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))