示例#1
0
def test_order_fulfilled(mocker):  # pylint:disable=too-many-arguments
    """
    Test the happy case
    """
    complete_order_mock = mocker.patch("b2b_ecommerce.api.complete_b2b_order")
    order = B2BOrderFactory.create(status=B2BOrder.CREATED)

    data = {}
    for _ in range(5):
        data[FAKE.text()] = FAKE.text()

    data["req_reference_number"] = order.reference_number
    data["decision"] = "ACCEPT"

    fulfill_b2b_order(data)

    order.refresh_from_db()
    assert order.status == B2BOrder.FULFILLED
    assert order.b2breceipt_set.count() == 1
    assert order.b2breceipt_set.first().data == data

    assert B2BOrderAudit.objects.count() == 1
    order_audit = B2BOrderAudit.objects.last()
    assert order_audit.order == order
    assert dict_without_keys(order_audit.data_after,
                             "updated_on") == dict_without_keys(
                                 order.to_dict(), "updated_on")
    complete_order_mock.assert_called_once_with(order)
示例#2
0
def test_ignore_duplicate_cancel():
    """
    If the decision is CANCEL and we already have a duplicate failed order, don't change anything.
    """
    order = B2BOrderFactory.create(status=B2BOrder.FAILED)

    data = {
        "req_reference_number": order.reference_number,
        "decision": "CANCEL"
    }
    fulfill_b2b_order(data)

    assert B2BOrder.objects.count() == 1
    assert B2BOrder.objects.get(id=order.id).status == B2BOrder.FAILED
示例#3
0
def test_not_accept(decision):
    """
    If the decision is not ACCEPT then the order should be marked as failed
    """
    order = B2BOrderFactory.create(status=B2BOrder.CREATED)

    data = {
        "req_reference_number": order.reference_number,
        "decision": decision
    }
    fulfill_b2b_order(data)
    order.refresh_from_db()
    assert B2BOrder.objects.count() == 1
    assert order.status == B2BOrder.FAILED
示例#4
0
def test_error_on_duplicate_order(mocker, order_status, decision):
    """If there is a duplicate message (except for CANCEL), raise an exception"""
    order = B2BOrderFactory.create(status=order_status)

    data = {
        "req_reference_number": order.reference_number,
        "decision": decision
    }
    mocker.patch("ecommerce.views.IsSignedByCyberSource.has_permission",
                 return_value=True)
    with pytest.raises(EcommerceException) as ex:
        fulfill_b2b_order(data)

    assert B2BOrder.objects.count() == 1
    assert B2BOrder.objects.get(id=order.id).status == order_status

    assert ex.value.args[0] == f"{order} is expected to have status 'created'"
示例#5
0
    def post(self, request, *args, **kwargs):  # pylint: disable=unused-argument
        """
        Confirmation from CyberSource which fulfills an existing Order.
        """
        try:
            reference_number = request.data.get("req_reference_number", "")
            if reference_number.startswith(B2BOrder.get_reference_number_prefix()):
                fulfill_b2b_order(request.data)
            elif reference_number.startswith(Order.get_reference_number_prefix()):
                fulfill_order(request.data)
            else:
                raise ParseException(
                    f"Unknown prefix '{reference_number}' for reference number"
                )
        except:
            # Not sure what would cause an error here but make sure we save the receipt
            Receipt.objects.create(data=request.data)
            raise

        # The response does not matter to CyberSource
        return Response(status=status.HTTP_200_OK)