def test_cancel_running_transfer_success(db_session, debtor):
    p.initiate_running_transfer(D_ID, TEST_UUID, *acc_id(D_ID, C_ID), 1000,
                                'fmt', 'test')
    coordinator_request_id = PrepareTransferSignal.query.one(
    ).coordinator_request_id

    t = p.cancel_running_transfer(D_ID, TEST_UUID)
    assert t.is_finalized
    assert t.error_code == SC_CANCELED_BY_THE_SENDER

    t = p.cancel_running_transfer(D_ID, TEST_UUID)
    assert t.is_finalized
    assert t.error_code == SC_CANCELED_BY_THE_SENDER

    p.process_prepared_issuing_transfer_signal(
        debtor_id=D_ID,
        creditor_id=ROOT_CREDITOR_ID,
        transfer_id=777,
        recipient=str(C_ID),
        locked_amount=1000,
        coordinator_id=D_ID,
        coordinator_request_id=coordinator_request_id,
    )
    t = p.cancel_running_transfer(D_ID, TEST_UUID)
    assert t.is_finalized
    assert t.error_code == SC_CANCELED_BY_THE_SENDER
def test_cancel_running_transfer_failure(db_session, debtor):
    p.initiate_running_transfer(D_ID, TEST_UUID, *acc_id(D_ID, C_ID), 1000,
                                'fmt', 'test')
    coordinator_request_id = PrepareTransferSignal.query.one(
    ).coordinator_request_id

    p.process_prepared_issuing_transfer_signal(
        debtor_id=D_ID,
        creditor_id=ROOT_CREDITOR_ID,
        transfer_id=777,
        recipient=str(C_ID),
        locked_amount=1000,
        coordinator_id=D_ID,
        coordinator_request_id=coordinator_request_id,
    )
    with pytest.raises(p.ForbiddenTransferCancellation):
        p.cancel_running_transfer(D_ID, TEST_UUID)
    with pytest.raises(p.ForbiddenTransferCancellation):
        p.cancel_running_transfer(D_ID, TEST_UUID)

    p.process_finalized_issuing_transfer_signal(
        debtor_id=D_ID,
        creditor_id=p.ROOT_CREDITOR_ID,
        transfer_id=777,
        coordinator_id=D_ID,
        coordinator_request_id=coordinator_request_id,
        committed_amount=1000,
        status_code=SC_OK,
        total_locked_amount=0,
    )
    with pytest.raises(p.ForbiddenTransferCancellation):
        p.cancel_running_transfer(D_ID, TEST_UUID)
    with pytest.raises(p.ForbiddenTransferCancellation):
        p.cancel_running_transfer(D_ID, TEST_UUID)
def test_rejected_transfer(db_session, debtor):
    p.initiate_running_transfer(D_ID, TEST_UUID, *acc_id(D_ID, C_ID), 1000,
                                'fmt', 'test')
    pts = PrepareTransferSignal.query.all()[0]
    p.process_rejected_issuing_transfer_signal(
        coordinator_id=D_ID,
        coordinator_request_id=pts.coordinator_request_id,
        status_code='TEST',
        total_locked_amount=0,
        debtor_id=D_ID,
        creditor_id=p.ROOT_CREDITOR_ID,
    )
    assert len(FinalizeTransferSignal.query.all()) == 0

    it_list = RunningTransfer.query.all()
    assert len(it_list) == 1
    it = it_list[0]
    assert it.is_finalized
    assert it.error_code == 'TEST'

    p.process_rejected_issuing_transfer_signal(
        coordinator_id=D_ID,
        coordinator_request_id=pts.coordinator_request_id,
        status_code='TEST',
        total_locked_amount=0,
        debtor_id=D_ID,
        creditor_id=p.ROOT_CREDITOR_ID,
    )
    it_list == RunningTransfer.query.all()
    assert len(it_list) == 1 and it_list[0].is_finalized
def test_successful_transfer(db_session, debtor):
    recipient_uri, recipient = acc_id(D_ID, C_ID)
    assert len(PrepareTransferSignal.query.all()) == 0
    p.initiate_running_transfer(D_ID, TEST_UUID, recipient_uri, recipient,
                                1000, 'fmt', 'test')
    pts_list = PrepareTransferSignal.query.all()
    assert len(pts_list) == 1
    pts = pts_list[0]
    assert pts.debtor_id == D_ID
    assert pts.coordinator_request_id is not None
    assert pts.amount == 1000
    assert pts.recipient == recipient
    coordinator_request_id = pts.coordinator_request_id

    p.process_prepared_issuing_transfer_signal(
        debtor_id=D_ID,
        creditor_id=ROOT_CREDITOR_ID,
        transfer_id=777,
        recipient=str(C_ID),
        locked_amount=1000,
        coordinator_id=D_ID,
        coordinator_request_id=coordinator_request_id,
    )
    assert len(PrepareTransferSignal.query.all()) == 1
    fpts_list = FinalizeTransferSignal.query.all()
    assert len(fpts_list) == 1
    fpts = fpts_list[0]
    assert fpts.debtor_id == D_ID
    assert fpts.creditor_id == ROOT_CREDITOR_ID
    assert fpts.transfer_id is not None
    assert fpts.committed_amount == 1000
    assert fpts.transfer_note == 'test'
    assert fpts.__marshmallow_schema__.dump(fpts)['transfer_note'] == 'test'

    rt_list = RunningTransfer.query.all()
    assert len(rt_list) == 1
    rt = rt_list[0]
    assert rt.is_settled
    assert rt.transfer_id is not None
    it_list = RunningTransfer.query.all()
    assert len(it_list) == 1
    it = it_list[0]
    assert not it.is_finalized
    assert it.error_code is None

    p.process_finalized_issuing_transfer_signal(
        debtor_id=D_ID,
        creditor_id=p.ROOT_CREDITOR_ID,
        transfer_id=777,
        coordinator_id=D_ID,
        coordinator_request_id=coordinator_request_id,
        committed_amount=1000,
        status_code=SC_OK,
        total_locked_amount=0,
    )
    it_list = RunningTransfer.query.all()
    assert len(it_list) == 1
    it = it_list[0]
    assert it.is_finalized
    assert it.error_code is None
def test_too_many_initiated_transfers(db_session, debtor):
    recipient_uri, recipient = acc_id(D_ID, C_ID)
    Debtor.get_instance(D_ID).running_transfers_count = 1
    db_session.add(
        RunningTransfer(
            debtor_id=D_ID,
            transfer_uuid=TEST_UUID,
            recipient=recipient,
            recipient_uri=recipient_uri,
            amount=1000,
            transfer_note_format='',
            transfer_note='',
        ))
    db_session.commit()
    assert len(RunningTransfer.query.all()) == 1
    assert p.get_debtor(D_ID).running_transfers_count == 1
    for i in range(1, 10):
        suffix = '{:0>4}'.format(i)
        uuid = f'123e4567-e89b-12d3-a456-42665544{suffix}',
        p.initiate_running_transfer(D_ID, uuid, *acc_id(D_ID, C_ID), 1000, '',
                                    '', 10)
    assert len(RunningTransfer.query.all()) == 10
    assert p.get_debtor(D_ID).running_transfers_count == 10
    with pytest.raises(p.TooManyRunningTransfers):
        p.initiate_running_transfer(D_ID,
                                    '123e4567-e89b-12d3-a456-426655440010',
                                    *acc_id(D_ID, C_ID), 1000, '', '', 10)
def test_initiate_running_transfer(db_session, debtor):
    recipient_uri, recipient = acc_id(D_ID, C_ID)
    assert len(RunningTransfer.query.all()) == 0
    assert p.get_debtor_transfer_uuids(D_ID) == []
    t = p.initiate_running_transfer(D_ID, TEST_UUID, recipient_uri, recipient,
                                    1000, 'fmt', 'test')
    assert len(RunningTransfer.query.all()) == 1
    assert t.debtor_id == D_ID
    assert t.transfer_uuid == TEST_UUID
    assert t.recipient == recipient
    assert t.recipient_uri == recipient_uri
    assert t.amount == 1000
    assert t.transfer_note_format == 'fmt'
    assert t.transfer_note == 'test'
    assert not t.is_settled
    assert not t.is_finalized
    with pytest.raises(p.TransferExists):
        p.initiate_running_transfer(D_ID, TEST_UUID, *acc_id(D_ID, C_ID), 1000,
                                    'fmt', 'test')
    with pytest.raises(p.TransfersConflict):
        p.initiate_running_transfer(D_ID, TEST_UUID, *acc_id(D_ID, C_ID), 1001,
                                    'fmt', 'test')
    with pytest.raises(p.DebtorDoesNotExist):
        p.initiate_running_transfer(1234567890, TEST_UUID,
                                    *acc_id(1234567890, C_ID), 1001, 'fmt',
                                    'test')
    assert len(p.get_debtor_transfer_uuids(D_ID)) == 1
    assert len(RunningTransfer.query.all()) == 1

    p.delete_running_transfer(D_ID, TEST_UUID)
    assert len(RunningTransfer.query.all()) == 0
示例#7
0
    def post(self, transfer_creation_request, debtorId):
        """Initiate a credit-issuing transfer."""

        # Verify the recipient.
        recipient_uri = transfer_creation_request['recipient_identity']['uri']
        try:
            recipient_debtor_id, recipient = parse_account_uri(recipient_uri)
        except ValueError:
            abort(422,
                  errors={
                      'json': {
                          'recipient': {
                              'uri': ['The URI can not be recognized.']
                          }
                      }
                  })
        if recipient_debtor_id != debtorId:
            abort(422,
                  errors={
                      'json': {
                          'recipient': {
                              'uri': ['Invalid recipient account.']
                          }
                      }
                  })

        uuid = transfer_creation_request['transfer_uuid']
        location = url_for('transfers.TransferEndpoint',
                           _external=True,
                           debtorId=debtorId,
                           transferUuid=uuid)
        try:
            transfer = procedures.initiate_running_transfer(
                debtor_id=debtorId,
                transfer_uuid=uuid,
                amount=transfer_creation_request['amount'],
                recipient_uri=recipient_uri,
                recipient=recipient,
                transfer_note_format=transfer_creation_request[
                    'transfer_note_format'],
                transfer_note=transfer_creation_request['transfer_note'],
                max_actions_per_month=current_app.
                config['APP_MAX_TRANSFERS_PER_MONTH'],
            )
        except (procedures.TooManyManagementActions,
                procedures.TooManyRunningTransfers):
            abort(403)
        except procedures.DebtorDoesNotExist:
            abort(404)
        except procedures.TransfersConflict:
            abort(409)
        except procedures.TransferExists:
            return redirect(location, code=303)

        return transfer, {'Location': location}