def test_no_duplicate_update(self, update_fn): """Should not send update with the same status more than once""" # READY_TO_SHIP have been sent previously status = 'READY_TO_SHIP' self.pickup.status_history[status] = '2017-10-05T03:45:33Z' self.pickup.save() payload = { 'orderId': '230948012', 'partnerId': 786, 'orderStatus': [{ 'orderStatus': 'NEW', 'updatedTime': '2017-10-05T04:45:33Z', 'orderStatusDetail': None }], 'shipPackage': [{ 'trackingId': self.pickup.tracking_number }], 'orderDetailedStatus': [{ 'updatedTime': '2017-10-05T04:45:33Z', 'orderUpdatedTime': '2017-10-05T04:45:32Z', 'orderDetailedStatus': status }] } ret = check_webhook(payload) self.pickup.refresh_from_db() self.assertEqual(ret, self.pickup.id) self.assertEqual(payload['orderDetailedStatus'][0]['updatedTime'], self.pickup.status_history[status]) update_fn.assert_not_called()
def test_no_shippackage(self, update_fn): """Test payload with no shipPackage""" payload = { 'orderId': '230948012', 'partnerId': 786, 'orderStatus': [{ 'orderStatus': 'NEW', 'updatedTime': '2017-10-05T00:12:03Z', 'orderStatusDetail': None }], 'shipPackage': [], 'orderDetailedStatus': [{ 'updatedTime': '2017-10-05T00:12:03Z', 'orderUpdatedTime': '2017-10-05T00:12:03Z', 'orderDetailedStatus': 'NEW' }] } ret = check_webhook(payload) self.assertFalse(ret) update_fn.assert_not_called()
def test_no_orderdetailedstatus(self, update_fn): """Test payload with no orderDetailedStatus""" payload = { 'partnerId': 786, 'orderId': '230948012', 'shipPackage': [{ 'trackingId': 'SM786624753046914659', 'statusHistory': [{ 'shippingStatus': 'IN_TRANSIT', 'shippingLocation': 'Singapore', 'shippingDetail': 'On the way to deliver', 'updatedTime': '2017-10-06T06:30:59Z', 'shippingUpdatedTime': '2017-10-06T05:54:10Z' }] }] } ret = check_webhook(payload) self.assertFalse(ret) update_fn.assert_not_called()
def test_nonexisting_tracking(self, update_fn): """Test with non-existing tracking number in DB""" payload = { 'orderId': '230948012', 'partnerId': 786, 'orderStatus': [{ 'orderStatus': 'NEW', 'updatedTime': '2017-10-05T04:45:33Z', 'orderStatusDetail': None }], 'shipPackage': [{ 'trackingId': 'SM786624753046914659' }], 'orderDetailedStatus': [{ 'updatedTime': '2017-10-05T04:45:33Z', 'orderUpdatedTime': '2017-10-05T04:45:32Z', 'orderDetailedStatus': 'READY_TO_SHIP' }] } ret = check_webhook(payload) self.assertFalse(ret) update_fn.assert_not_called()
def test_statuses(self, update_fn): """Check against statuses""" statuses = [ 'NEW', 'READY_TO_PICK', 'READY_TO_SHIP', 'IN_TRANSIT', 'COMPLETED', 'FAILED_TO_DELIVER', 'CANCELLED', 'REJECTED', ] for status in statuses: update_fn.reset_mock() pickup = PickUp.objects.create( package_uuid=fake_factory.uuid4(), package_id=fake.ean13() + status, tracking_number="030010405038-status-" + status, package_created_at=fake.date_time_between(start_date="-30d", end_date="-15d", tzinfo=dt_tz.utc)) payload = { 'orderId': fake.ean() + '-' + status, 'partnerId': 786, 'orderStatus': [{ 'orderStatus': status, 'updatedTime': fake.date_time_between(start_date="-14d", end_date="now", tzinfo=dt_tz.utc).isoformat(), 'orderStatusDetail': None }], 'shipPackage': [{ 'trackingId': pickup.tracking_number }], 'orderDetailedStatus': [{ 'updatedTime': fake.date_time_between(start_date="-14d", end_date="now", tzinfo=dt_tz.utc).isoformat(), 'orderUpdatedTime': fake.date_time_between(start_date="-14d", end_date="now", tzinfo=dt_tz.utc).isoformat(), 'orderDetailedStatus': status }] } ret = check_webhook(payload) self.assertEqual(ret, pickup.id) pickup.refresh_from_db() self.assertEqual(pickup.order_id, payload['orderId']) self.assertEqual(pickup.last_status, status) self.assertEqual(pickup.status_history[status], payload['orderDetailedStatus'][0]['updatedTime']) if status == 'COMPLETED': self.assertTrue(pickup.is_delivered) else: self.assertFalse(pickup.is_delivered) if status in settings.UPDATE_LAZADA_STATUSES: update_fn.assert_called_once_with( pickup.id, status=status, timestamp=payload['orderDetailedStatus'][0]['updatedTime'], tracking_number=pickup.tracking_number) else: update_fn.assert_not_called()