예제 #1
0
async def test_p2p_receive():
    """Test receive from p2p connection."""
    address = "127.0.0.1"
    port = 8000
    m_fet_key = "6d56fd47e98465824aa85dfe620ad3dbf092b772abc6c6a182e458b5c56ad13b"
    ent = entity.Entity.from_hex(m_fet_key)
    p2p_connection = PeerToPeerClientConnection(
        address=ent.public_key_hex,
        provider_addr=address,
        provider_port=port,
        connection_id=P2P_CLIENT_CONNECTION_PUBLIC_ID,
    )
    p2p_connection.loop = asyncio.get_event_loop()

    fake_get_messages_empty = MagicMock(return_value=[])

    s_msg = {
        "FROM": {
            "NODE_ADDRESS": "node_address",
            "SENDER_ADDRESS": "sender_address"
        },
        "TO": {
            "NODE_ADDRESS": "node_address",
            "RECEIVER_ADDRESS": "receiver_address"
        },
        "PROTOCOL": "author/protocol_name:0.1.0",
        "CONTEXT": "context",
        "PAYLOAD": "payload",
    }
    messages = [s_msg]

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=[]):
        with mock.patch.object(
                fetch.p2p.api.http_calls.HTTPCalls,
                "register",
                return_value={"status": "OK"},
        ):
            await p2p_connection.connect()
            assert p2p_connection.connection_status.is_connected is True

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=messages) as mock_receive:
        p2p_connection.channel._httpCall.get_messages = mock_receive
        await asyncio.sleep(2.0)
        envelope = await p2p_connection.receive()
        assert envelope is not None

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "unregister",
                           return_value={"status": "OK"}):
        p2p_connection.channel._httpCall.get_messages = fake_get_messages_empty
        await p2p_connection.disconnect()
        assert p2p_connection.connection_status.is_connected is False
예제 #2
0
async def test_p2p_send():
    """Test the send functionality of the p2p connection."""
    address = "127.0.0.1"
    port = 8000
    m_fet_key = "6d56fd47e98465824aa85dfe620ad3dbf092b772abc6c6a182e458b5c56ad13b"
    ent = entity.Entity.from_hex(m_fet_key)
    p2p_client_connection = PeerToPeerClientConnection(
        address=ent.public_key_hex,
        provider_addr=address,
        provider_port=port,
        connection_id=P2P_CLIENT_CONNECTION_PUBLIC_ID,
    )
    p2p_client_connection.loop = asyncio.get_event_loop()
    envelope = Envelope(
        to="receiver",
        sender="sender",
        protocol_id=UNKNOWN_PROTOCOL_PUBLIC_ID,
        message=b"Hello",
    )

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=[]):
        with mock.patch.object(
                fetch.p2p.api.http_calls.HTTPCalls,
                "register",
                return_value={"status": "OK"},
        ):
            await p2p_client_connection.connect()
            assert p2p_client_connection.connection_status.is_connected is True

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=[]):
        with mock.patch.object(
                fetch.p2p.api.http_calls.HTTPCalls,
                "send_message",
                return_value={"status": "OK"},
        ):
            await p2p_client_connection.send(envelope=envelope)
            # TODO: Consider returning the response from the server in order to be able to assert that the message send!
            assert True

    with mock.patch.object(
            fetch.p2p.api.http_calls.HTTPCalls,
            "unregister",
            return_value={"status": "OK"},
    ):
        await p2p_client_connection.disconnect()
        assert p2p_client_connection.connection_status.is_connected is False