# Path: oscar_mws/api.py
# class MWSError(Exception):
#     """
#     Main MWS Exception class
#     """
#     # Allows quick access to the response object.
#     # Do not rely on this attribute, always check if its not None.
#     response = None
#     url = None
# 
# Path: oscar_mws/signals.py

# 
# Path: oscar_mws/fulfillment/gateway.py
# def submit_fulfillment_order(fulfillment_order):
#     """
#     Submits the fulfillment order *fulfillment_order* to MWS requesting
#     the fulfillment by Amazon. If the submission is succesful, the status of
#     the :class:`FulfillmentOrder <oscar_mws.models.FulfillmentOrder>` is
#     set to ``SUBMITTED``. Otherwise, the status is changed to
#     ``SUBMISSION_FAILED``.
# 
#     :param FulfillmentOrder fulfillment_order: A fulfillment order that should
#         be fulfilled by Amazon and has a status of ``SUBMISSION_FAILED`` or
#         ``UNSUBMITTED`` (not enforced).
#     """
#     seller_id = fulfillment_order.merchant.seller_id
#     outbound_api = get_merchant_connection(seller_id, 'outbound')
# 
#     try:
#         outbound_api.create_fulfillment_order(
#             **fulfillment_order.get_order_kwargs())
#     except MWSError:
#         logger.error(
#             "submitting order {} failed".format(
#                 fulfillment_order.fulfillment_id), exc_info=1,
#             extra={'seller_id': seller_id,
#                    'fulfillment_id': fulfillment_order.fulfillment_id,
#                    'order_id': fulfillment_order.order.number})
#         fulfillment_order.status = fulfillment_order.SUBMISSION_FAILED
#     else:
#         fulfillment_order.status = fulfillment_order.SUBMITTED
#         mws_fulfillment_created.send(
#             sender=outbound_api, fulfillment_order=fulfillment_order)
#     fulfillment_order.save()


import mock
from django.test import TestCase
from oscar_mws.api import MWSError
from oscar_mws.signals import mws_fulfillment_created
from oscar_mws.fulfillment.gateway import submit_fulfillment_order
        fulfillment_order_mock.get_order_kwargs = mock.Mock(return_value={})

        def fake_receiver(fulfillment_order, **kwargs):
            self.signal_sent = True
            self.assertEquals(fulfillment_order, fulfillment_order_mock)

        mws_fulfillment_created.connect(fake_receiver)

        with mock.patch('oscar_mws.fulfillment.gateway.get_merchant_connection') as gmc_mock:  # noqa
            connection_mock = mock.Mock()
            gmc_mock.return_value = connection_mock
            outbound_mock = mock.Mock()
            connection_mock.outbound = outbound_mock
            connection_mock.create_fulfillment_order = mock.Mock(
                return_value=None)

            submit_fulfillment_order(fulfillment_order_mock)

        if not self.signal_sent:
            self.fail("MWS order signal not sent")

    def test_is_not_emitted_when_sumitting_to_mws_fails(self):
        self.signal_sent = False
        fulfillment_order_mock = mock.Mock()
        fulfillment_order_mock.get_order_kwargs = mock.Mock(return_value={})

        def fake_receiver(fulfillment_order, **kwargs):
            self.signal_sent = True
            self.assertEquals(fulfillment_order, fulfillment_order_mock)


# Path: oscar_mws/api.py
# class MWSError(Exception):
#     """
#     Main MWS Exception class
#     """
#     # Allows quick access to the response object.
#     # Do not rely on this attribute, always check if its not None.
#     response = None
#     url = None
# 
# Path: oscar_mws/signals.py

# 
# Path: oscar_mws/fulfillment/gateway.py
# def submit_fulfillment_order(fulfillment_order):
#     """
#     Submits the fulfillment order *fulfillment_order* to MWS requesting
#     the fulfillment by Amazon. If the submission is succesful, the status of
#     the :class:`FulfillmentOrder <oscar_mws.models.FulfillmentOrder>` is
#     set to ``SUBMITTED``. Otherwise, the status is changed to
#     ``SUBMISSION_FAILED``.
# 
#     :param FulfillmentOrder fulfillment_order: A fulfillment order that should
#         be fulfilled by Amazon and has a status of ``SUBMISSION_FAILED`` or
#         ``UNSUBMITTED`` (not enforced).
#     """
#     seller_id = fulfillment_order.merchant.seller_id
#     outbound_api = get_merchant_connection(seller_id, 'outbound')
# 
#     try:
#         outbound_api.create_fulfillment_order(
#             **fulfillment_order.get_order_kwargs())
#     except MWSError:
#         logger.error(
#             "submitting order {} failed".format(
#                 fulfillment_order.fulfillment_id), exc_info=1,
#             extra={'seller_id': seller_id,
#                    'fulfillment_id': fulfillment_order.fulfillment_id,
#                    'order_id': fulfillment_order.order.number})
#         fulfillment_order.status = fulfillment_order.SUBMISSION_FAILED
#     else:
#         fulfillment_order.status = fulfillment_order.SUBMITTED
#         mws_fulfillment_created.send(
#             sender=outbound_api, fulfillment_order=fulfillment_order)
#     fulfillment_order.save()


# Path: tests/unit/test_signals.py
import mock
from django.test import TestCase
from oscar_mws.api import MWSError
from oscar_mws.signals import mws_fulfillment_created
from oscar_mws.fulfillment.gateway import submit_fulfillment_order

        fulfillment_order_mock.get_order_kwargs = mock.Mock(return_value={})

        def fake_receiver(fulfillment_order, **kwargs):
            self.signal_sent = True
            self.assertEquals(fulfillment_order, fulfillment_order_mock)

        mws_fulfillment_created.connect(fake_receiver)

        with mock.patch('oscar_mws.fulfillment.gateway.get_merchant_connection') as gmc_mock:  # noqa
            connection_mock = mock.Mock()
            gmc_mock.return_value = connection_mock
            outbound_mock = mock.Mock()
            connection_mock.outbound = outbound_mock
            connection_mock.create_fulfillment_order = mock.Mock(
                return_value=None)

            submit_fulfillment_order(fulfillment_order_mock)

        if not self.signal_sent:
            self.fail("MWS order signal not sent")

    def test_is_not_emitted_when_sumitting_to_mws_fails(self):
        self.signal_sent = False
        fulfillment_order_mock = mock.Mock()
        fulfillment_order_mock.get_order_kwargs = mock.Mock(return_value={})

        def fake_receiver(fulfillment_order, **kwargs):
            self.signal_sent = True
            self.assertEquals(fulfillment_order, fulfillment_order_mock)


        mws_fulfillment_created.connect(fake_receiver)