def test_parses_the_response_correctly(self):
        xml_data = self.load_data('get_fulfillment_order_response.xml')
        httpretty.register_uri(
            httpretty.GET,
            'https://mws.amazonservices.com/FulfillmentOutboundShipment/2010-10-01',
            body=xml_data,
        )

        basket = factories.BasketFactory()
        basket.add_product(factories.ProductFactory())
        order = create_order(basket=basket)

        update_fulfillment_order(
            factories.FulfillmentOrderFactory(order=order)
        )

        fulfillment_order = FulfillmentOrder.objects.all()[0]
        self.assertEquals(FulfillmentOrder.objects.count(), 1)
        self.assertEquals(fulfillment_order.status, 'COMPLETE')

        shipments = FulfillmentShipment.objects.all()
        self.assertEquals(len(shipments), 1)

        expected = {
            'Dkw.3ko298': {
                'shipment_id': 'Dkw.3ko298',
                'status': 'SHIPPED',
                'fulfillment_center_id': 'FCID01',
                'date_shipped': du_parse('2013-10-29T00:50:03Z'),
                'date_estimated_arrival': du_parse('2013-10-30T23:59:59Z'),
            },
        }
        for shipment in shipments:
            for attr, value in expected[shipment.shipment_id].iteritems():
                self.assertEquals(getattr(shipment, attr), value)

        packages = ShipmentPackage.objects.all()
        self.assertEquals(len(packages), 1)

        self.assertEquals(packages[0].tracking_number, 'MPT_1234')
        self.assertEquals(packages[0].carrier_code, 'Magic Parcels')

        shipping_events = ShippingEvent.objects.all()
        self.assertEquals(len(shipping_events), 1)

        self.assertItemsEqual(
            [s.notes for s in shipping_events],
            ['* Shipped package via Magic Parcels with tracking number '
             'MPT_1234']
        )
    def test_parses_the_response_correctly(self):
        xml_data = self.load_data('get_fulfillment_order_response.xml')
        httpretty.register_uri(
            httpretty.GET,
            'https://mws.amazonservices.com/FulfillmentOutboundShipment/2010-10-01',
            body=xml_data,
        )

        basket = factories.BasketFactory()
        basket.add_product(factories.ProductFactory())
        order = create_order(basket=basket)

        update_fulfillment_order(
            factories.FulfillmentOrderFactory(order=order))

        fulfillment_order = FulfillmentOrder.objects.all()[0]
        self.assertEquals(FulfillmentOrder.objects.count(), 1)
        self.assertEquals(fulfillment_order.status, 'COMPLETE')

        shipments = FulfillmentShipment.objects.all()
        self.assertEquals(len(shipments), 1)

        expected = {
            'Dkw.3ko298': {
                'shipment_id': 'Dkw.3ko298',
                'status': 'SHIPPED',
                'fulfillment_center_id': 'FCID01',
                'date_shipped': du_parse('2013-10-29T00:50:03Z'),
                'date_estimated_arrival': du_parse('2013-10-30T23:59:59Z'),
            },
        }
        for shipment in shipments:
            for attr, value in expected[shipment.shipment_id].iteritems():
                self.assertEquals(getattr(shipment, attr), value)

        packages = ShipmentPackage.objects.all()
        self.assertEquals(len(packages), 1)

        self.assertEquals(packages[0].tracking_number, 'MPT_1234')
        self.assertEquals(packages[0].carrier_code, 'Magic Parcels')

        shipping_events = ShippingEvent.objects.all()
        self.assertEquals(len(shipping_events), 1)

        self.assertItemsEqual([s.notes for s in shipping_events], [
            '* Shipped package via Magic Parcels with tracking number '
            'MPT_1234'
        ])
    def test_updates_a_single_order_status(self):
        httpretty.register_uri(
            httpretty.GET,
            'https://mws.amazonservices.com/FulfillmentOutboundShipment/2010-10-01',
            responses=[httpretty.Response(
                self.load_data('get_fulfillment_order_response.xml'),
            )],
        )
        order = update_fulfillment_order(self.fulfillment_order)
        self.assertEquals(order.status, order.COMPLETE)

        shipments = FulfillmentShipment.objects.all()
        self.assertEquals(len(shipments), 1)

        self.assertEquals(shipments[0].status, 'SHIPPED')
        self.assertEquals(shipments[0].shipment_events.count(), 1)

        event = shipments[0].shipment_events.all()[0]
        self.assertEquals(event.event_type.name, 'SHIPPED')
        self.assertSequenceEqual(
            list(event.lines.all()), list(self.order.lines.all()))
        self.assertEquals(
            event.notes,
            "* Shipped package via Magic Parcels with tracking number MPT_1234"
        )

        self.assertEquals(shipments[0].packages.count(), 1)
        package = shipments[0].packages.all()[0]
        self.assertEquals(package.package_number, 2341234)
        self.assertEquals(package.carrier_code, 'Magic Parcels')
        self.assertEquals(package.tracking_number, 'MPT_1234')
    def test_updates_a_single_order_status(self):
        httpretty.register_uri(
            httpretty.GET,
            'https://mws.amazonservices.com/FulfillmentOutboundShipment/2010-10-01',
            responses=[
                httpretty.Response(
                    self.load_data('get_fulfillment_order_response.xml'), )
            ],
        )
        order = update_fulfillment_order(self.fulfillment_order)
        self.assertEquals(order.status, order.COMPLETE)

        shipments = FulfillmentShipment.objects.all()
        self.assertEquals(len(shipments), 1)

        self.assertEquals(shipments[0].status, 'SHIPPED')
        self.assertEquals(shipments[0].shipment_events.count(), 1)

        event = shipments[0].shipment_events.all()[0]
        self.assertEquals(event.event_type.name, 'SHIPPED')
        self.assertSequenceEqual(list(event.lines.all()),
                                 list(self.order.lines.all()))
        self.assertEquals(
            event.notes,
            "* Shipped package via Magic Parcels with tracking number MPT_1234"
        )

        self.assertEquals(shipments[0].packages.count(), 1)
        package = shipments[0].packages.all()[0]
        self.assertEquals(package.package_number, 2341234)
        self.assertEquals(package.carrier_code, 'Magic Parcels')
        self.assertEquals(package.tracking_number, 'MPT_1234')
 def test_updates_an_order_without_shipment_info(self):
     httpretty.register_uri(
         httpretty.GET,
         'https://mws.amazonservices.com/FulfillmentOutboundShipment/2010-10-01',
         responses=[httpretty.Response(
             self.load_data(
                 'get_fulfillment_order_response_without_shipments.xml'))],
     )
     order = update_fulfillment_order(self.fulfillment_order)
     self.assertEquals(order.status, order.PLANNING)
     self.assertEquals(FulfillmentShipment.objects.count(), 0)
 def test_updates_an_order_without_shipment_info(self):
     httpretty.register_uri(
         httpretty.GET,
         'https://mws.amazonservices.com/FulfillmentOutboundShipment/2010-10-01',
         responses=[
             httpretty.Response(
                 self.load_data(
                     'get_fulfillment_order_response_without_shipments.xml')
             )
         ],
     )
     order = update_fulfillment_order(self.fulfillment_order)
     self.assertEquals(order.status, order.PLANNING)
     self.assertEquals(FulfillmentShipment.objects.count(), 0)