Skip to content

Commit

Permalink
test message content
Browse files Browse the repository at this point in the history
  • Loading branch information
odesenfans committed Sep 15, 2023
1 parent 7622921 commit 76fd748
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 35 deletions.
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
from time import sleep
from typing import Union

import aiohttp
import fastapi.applications
import pytest
import pytest_asyncio
import uvicorn

from mock_ccn import app as mock_ccn_app
Expand Down Expand Up @@ -65,8 +67,22 @@ def mock_ccn() -> str:
yield url


@pytest_asyncio.fixture
async def mock_ccn_client(mock_ccn: str):
async with aiohttp.ClientSession(mock_ccn) as client:
yield client


@pytest.fixture
def executor_server(mock_ccn: str) -> str:
assert mock_ccn, "The mock CCN server must be running"

host, port = "127.0.0.1", 8081
with run_http_app(app="aleph_vrf.executor.main:app", host=host, port=port):
yield f"http://{host}:{port}"


@pytest_asyncio.fixture
async def executor_client(executor_server: str) -> aiohttp.ClientSession:
async with aiohttp.ClientSession(executor_server) as client:
yield client
159 changes: 124 additions & 35 deletions tests/executor/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

import aiohttp
import pytest
from aleph_message.models import ItemType, MessageType, PostContent, Chain, ItemHash
from aleph.sdk import AlephClient
from aleph_message.models import (
ItemType,
MessageType,
PostContent,
Chain,
ItemHash,
PostMessage,
)

from aleph_vrf.models import VRFRequest, VRFResponseHash, VRFResponse, VRFRandomBytes
from aleph_vrf.utils import binary_to_bytes, verify
Expand Down Expand Up @@ -66,10 +74,95 @@ def mock_vrf_request() -> VRFRequest:
return vrf_request


def assert_vrf_hash_matches_request(
response_hash: VRFResponseHash, vrf_request: VRFRequest, request_item_hash: ItemHash
):
assert response_hash.nb_bytes == vrf_request.nb_bytes
assert response_hash.nonce == vrf_request.nonce
assert response_hash.request_id == vrf_request.request_id
assert response_hash.execution_id # This should be a UUID4
assert response_hash.vrf_request == request_item_hash
assert response_hash.random_bytes_hash
assert response_hash.message_hash


def assert_random_number_matches_request(
random_bytes: VRFRandomBytes,
response_hash: VRFResponseHash,
vrf_request: VRFRequest,
):
assert random_bytes.request_id == vrf_request.request_id
assert random_bytes.execution_id == response_hash.execution_id
assert random_bytes.vrf_request == response_hash.vrf_request
assert random_bytes.random_bytes_hash == response_hash.random_bytes_hash

assert verify(
random_bytes=binary_to_bytes(random_bytes.random_bytes),
nonce=vrf_request.nonce,
random_hash=response_hash.random_bytes_hash,
)


def assert_vrf_response_hash_equal(
response_hash: VRFResponseHash, expected_response_hash: VRFResponseHash
):
assert response_hash.nb_bytes == expected_response_hash.nb_bytes
assert response_hash.nonce == expected_response_hash.nonce
assert response_hash.request_id == expected_response_hash.request_id
assert response_hash.execution_id == expected_response_hash.execution_id
assert response_hash.vrf_request == expected_response_hash.vrf_request
assert response_hash.random_bytes_hash == expected_response_hash.random_bytes_hash
# We do not check message_hash as it can be None in the aleph message but set in the API response


async def assert_aleph_message_matches_response_hash(
ccn_url: str, response_hash: VRFResponseHash
) -> PostMessage:
assert response_hash.message_hash

async with AlephClient(api_server=ccn_url) as client:
message = await client.get_message(
response_hash.message_hash, message_type=PostMessage
)

message_response_hash = VRFResponseHash.parse_obj(message.content.content)
assert_vrf_response_hash_equal(message_response_hash, response_hash)

return message


def assert_vrf_random_bytes_equal(
random_bytes: VRFRandomBytes, expected_random_bytes: VRFRandomBytes
):
assert random_bytes.request_id == expected_random_bytes.request_id
assert random_bytes.execution_id == expected_random_bytes.execution_id
assert random_bytes.vrf_request == expected_random_bytes.vrf_request
assert random_bytes.random_bytes == expected_random_bytes.random_bytes
assert random_bytes.random_bytes_hash == expected_random_bytes.random_bytes_hash
assert random_bytes.random_number == expected_random_bytes.random_number
# We do not check message_hash as it can be None in the aleph message but set in the API response


async def assert_aleph_message_matches_random_bytes(
ccn_url: str, random_bytes: VRFRandomBytes
) -> PostMessage:
assert random_bytes.message_hash

async with AlephClient(api_server=ccn_url) as client:
message = await client.get_message(
random_bytes.message_hash, message_type=PostMessage
)

message_random_bytes = VRFRandomBytes.parse_obj(message.content.content)
assert_vrf_random_bytes_equal(message_random_bytes, random_bytes)

return message


@pytest.mark.asyncio
async def test_normal_request_flow(
mock_ccn: str,
executor_server: str,
mock_ccn_client: aiohttp.ClientSession,
executor_client: aiohttp.ClientSession,
mock_vrf_request: VRFRequest,
):
"""
Expand All @@ -83,41 +176,37 @@ async def test_normal_request_flow(
message_dict = make_post_message(mock_vrf_request, sender=sender)
item_hash = ItemHash(message_dict["item_hash"])

async with aiohttp.ClientSession(mock_ccn) as ccn_client:
resp = await ccn_client.post(
"/api/v0/messages",
json={"message": message_dict, "sync": True},
)
assert resp.status == 200, await resp.text()

async with aiohttp.ClientSession(executor_server) as executor_client:
resp = await executor_client.post(f"/generate/{item_hash}")
assert resp.status == 200, await resp.text()
response_json = await resp.json()

response_hash = VRFResponseHash.parse_obj(response_json["data"])
resp = await mock_ccn_client.post(
"/api/v0/messages",
json={"message": message_dict, "sync": True},
)
assert resp.status == 200, await resp.text()

assert response_hash.nb_bytes == mock_vrf_request.nb_bytes
assert response_hash.nonce == mock_vrf_request.nonce
assert response_hash.request_id == mock_vrf_request.request_id
assert response_hash.execution_id # This should be a UUID4
assert response_hash.vrf_request == item_hash
assert response_hash.random_bytes_hash
assert response_hash.message_hash
resp = await executor_client.post(f"/generate/{item_hash}")
assert resp.status == 200, await resp.text()
response_json = await resp.json()

resp = await executor_client.post(f"/publish/{response_hash.message_hash}")
assert resp.status == 200, await resp.text()
response_json = await resp.json()
response_hash = VRFResponseHash.parse_obj(response_json["data"])

random_bytes = VRFRandomBytes.parse_obj(response_json["data"])
assert_vrf_hash_matches_request(response_hash, mock_vrf_request, item_hash)
random_hash_message = await assert_aleph_message_matches_response_hash(
mock_ccn_client._base_url, response_hash
)

assert random_bytes.request_id == mock_vrf_request.request_id
assert random_bytes.execution_id == response_hash.execution_id
assert random_bytes.vrf_request == item_hash
assert random_bytes.random_bytes_hash == response_hash.random_bytes_hash
resp = await executor_client.post(f"/publish/{response_hash.message_hash}")
assert resp.status == 200, await resp.text()
response_json = await resp.json()

assert verify(
random_bytes=binary_to_bytes(random_bytes.random_bytes),
nonce=mock_vrf_request.nonce,
random_hash=response_hash.random_bytes_hash,
random_bytes = VRFRandomBytes.parse_obj(response_json["data"])
assert_random_number_matches_request(
random_bytes=random_bytes,
response_hash=response_hash,
vrf_request=mock_vrf_request,
)
random_number_message = await assert_aleph_message_matches_random_bytes(
mock_ccn_client._base_url, random_bytes
)

# Sanity checks on the message
assert random_number_message.sender == random_hash_message.sender
assert random_number_message.chain == random_hash_message.chain

0 comments on commit 76fd748

Please sign in to comment.