Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/aleph/db/accessors/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def make_select_merged_post_with_message_info_stmt() -> Select:
"item_type"
),
Original.owner.label("owner"),
func.coalesce(Amend.ref, Original.ref).label("ref"),
Original.ref.label("ref"),
func.coalesce(Amend.creation_datetime, Original.creation_datetime).label(
"last_updated"
),
Expand Down
186 changes: 185 additions & 1 deletion tests/api/test_posts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from typing import Dict, Iterable, Sequence
from typing import Dict, Iterable, Sequence, Tuple

import aiohttp
import pytest

from aleph.db.models import MessageDb
from aleph.db.models.posts import PostDb
import datetime as dt

from aleph.types.db_session import DbSessionFactory

POSTS_URI = "/api/v1/posts.json"

Expand Down Expand Up @@ -49,3 +53,183 @@ async def test_get_posts(ccn_api_client, fixture_posts: Sequence[PostDb]):
# 1 POST = 1 message.
posts = await get_posts_expect_success(ccn_api_client)
assert_posts_equal(posts, fixture_posts)


@pytest.fixture
def post_with_refs_and_tags() -> Tuple[MessageDb, PostDb]:
message = MessageDb(
item_hash="1234",
sender="0xdeadbeef",
type="POST",
chain="ETH",
signature=None,
item_type="storage",
item_content=None,
content={},
time=dt.datetime(2023, 5, 1, tzinfo=dt.timezone.utc),
channel=None,
size=254,
)

post = PostDb(
item_hash=message.item_hash,
owner=message.sender,
type=None,
ref="custom-ref",
amends=None,
channel=None,
content={"swap": "this"},
creation_datetime=message.time,
latest_amend=None,
)

return message, post


@pytest.fixture
def amended_post_with_refs_and_tags(post_with_refs_and_tags: Tuple[MessageDb, PostDb]):
original_message, original_post = post_with_refs_and_tags

amend_message = MessageDb(
item_hash="5678",
sender="0xdeadbeef",
type="POST",
chain="ETH",
signature=None,
item_type="storage",
item_content=None,
content={},
time=dt.datetime(2023, 5, 2, tzinfo=dt.timezone.utc),
channel=None,
size=277,
)

amend_post = PostDb(
item_hash=amend_message.item_hash,
owner=original_message.sender,
type="amend",
ref=original_message.item_hash,
amends=original_message.item_hash,
channel=None,
content={"don't": "swap"},
creation_datetime=amend_message.time,
latest_amend=None,
)

return amend_message, amend_post


@pytest.mark.asyncio
async def test_get_posts_refs(
ccn_api_client,
session_factory: DbSessionFactory,
fixture_posts: Sequence[PostDb],
post_with_refs_and_tags: Tuple[MessageDb, PostDb],
):
message_db, post_db = post_with_refs_and_tags

with session_factory() as session:
session.add_all(fixture_posts)
session.add(message_db)
session.add(post_db)
session.commit()

# Match the ref
response = await ccn_api_client.get(
"/api/v0/posts.json", params={"refs": f"{post_db.ref}"}
)
assert response.status == 200
response_json = await response.json()
assert len(response_json["posts"]) == 1
assert response_json["pagination_total"] == 1

post = response_json["posts"][0]
assert post["item_hash"] == post_db.item_hash
assert post["original_item_hash"] == post_db.item_hash
assert post["ref"] == post_db.ref
assert post["content"] == post_db.content

# Unknown ref
response = await ccn_api_client.get(
"/api/v0/posts.json", params={"refs": "not-a-ref"}
)
assert response.status == 200
response_json = await response.json()
assert len(response_json["posts"]) == 0
assert response_json["pagination_total"] == 0

# Search for several refs
response = await ccn_api_client.get(
"/api/v0/posts.json", params={"refs": f"{post_db.ref},not-a-ref"}
)
assert response.status == 200
response_json = await response.json()
assert len(response_json["posts"]) == 1
assert response_json["pagination_total"] == 1

post = response_json["posts"][0]
assert post["item_hash"] == post_db.item_hash
assert post["original_item_hash"] == post_db.item_hash
assert post["ref"] == post_db.ref
assert post["content"] == post_db.content


@pytest.mark.asyncio
async def test_get_amended_posts_refs(
ccn_api_client,
session_factory: DbSessionFactory,
fixture_posts: Sequence[PostDb],
post_with_refs_and_tags: Tuple[MessageDb, PostDb],
amended_post_with_refs_and_tags: Tuple[MessageDb, PostDb],
):
original_message_db, original_post_db = post_with_refs_and_tags
amend_message_db, amend_post_db = amended_post_with_refs_and_tags

original_post_db.latest_amend = amend_post_db.item_hash

with session_factory() as session:
session.add_all(fixture_posts)
session.add(original_message_db)
session.add(original_post_db)
session.add(amend_message_db)
session.add(amend_post_db)
session.commit()

# Match the ref
response = await ccn_api_client.get(
"/api/v0/posts.json", params={"refs": f"{original_post_db.ref}"}
)
assert response.status == 200
response_json = await response.json()
assert len(response_json["posts"]) == 1
assert response_json["pagination_total"] == 1

post = response_json["posts"][0]
assert post["item_hash"] == amend_post_db.item_hash
assert post["original_item_hash"] == original_post_db.item_hash
assert post["ref"] == original_post_db.ref
assert post["content"] == amend_post_db.content

# Unknown ref
response = await ccn_api_client.get(
"/api/v0/posts.json", params={"refs": "not-a-ref"}
)
assert response.status == 200
response_json = await response.json()
assert len(response_json["posts"]) == 0
assert response_json["pagination_total"] == 0

# Search for several refs
response = await ccn_api_client.get(
"/api/v0/posts.json", params={"refs": f"{original_post_db.ref},not-a-ref"}
)
assert response.status == 200
response_json = await response.json()
assert len(response_json["posts"]) == 1
assert response_json["pagination_total"] == 1

post = response_json["posts"][0]
assert post["item_hash"] == amend_post_db.item_hash
assert post["original_item_hash"] == original_post_db.item_hash
assert post["ref"] == original_post_db.ref
assert post["content"] == amend_post_db.content
2 changes: 1 addition & 1 deletion tests/db/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def assert_posts_v0_equal(
):
expected_item_hash = last_amend.item_hash if last_amend else original.item_hash
expected_content = last_amend.content if last_amend else original.content
expected_ref = last_amend.ref if last_amend else original.ref
expected_ref = original.ref
expected_last_updated = (
last_amend.creation_datetime if last_amend else original.creation_datetime
)
Expand Down