-
Notifications
You must be signed in to change notification settings - Fork 21
Fix: general 500 error on posts endpoint #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import json | ||
| from pathlib import Path | ||
| import pytest_asyncio | ||
| from aleph.model.messages import Message | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture | ||
| async def fixture_messages(test_db): | ||
| fixtures_dir = Path(__file__).parent / "fixtures" | ||
| fixtures_file = fixtures_dir / "fixture_messages.json" | ||
|
|
||
| with fixtures_file.open() as f: | ||
| messages = json.load(f) | ||
|
|
||
| await Message.collection.insert_many(messages) | ||
| return messages |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from typing import Dict, Iterable | ||
|
|
||
| import aiohttp | ||
| import pytest | ||
| from aleph_message.models import MessageType | ||
|
|
||
| from .utils import get_messages_by_keys | ||
|
|
||
| POSTS_URI = "/api/v0/posts.json" | ||
|
|
||
|
|
||
| def assert_posts_equal(posts: Iterable[Dict], expected_messages: Iterable[Dict]): | ||
| posts_by_hash = {post["item_hash"]: post for post in posts} | ||
|
|
||
| for expected_message in expected_messages: | ||
| post = posts_by_hash[expected_message["item_hash"]] | ||
| assert "_id" not in post | ||
|
|
||
| assert post["chain"] == expected_message["chain"] | ||
| assert post["channel"] == expected_message["channel"] | ||
| assert post["sender"] == expected_message["sender"] | ||
| assert post["signature"] == expected_message["signature"] | ||
|
|
||
| if expected_message.get("forgotten_by", []): | ||
| assert post["content"] is None | ||
| continue | ||
|
|
||
| if "content" not in expected_message["content"]: | ||
| # TODO: there is a problem with the spec of posts: they can be specified | ||
| # without an internal "content" field, which does not break the | ||
| # endpoint but returns the content of message["content"] instead. | ||
| # We skip the issue for now. | ||
| continue | ||
|
|
||
| assert post["content"] == expected_message["content"]["content"] | ||
|
|
||
|
|
||
| async def get_posts(api_client, **params) -> aiohttp.ClientResponse: | ||
| return await api_client.get(POSTS_URI, params=params) | ||
|
|
||
|
|
||
| async def get_posts_expect_success(api_client, **params): | ||
| response = await get_posts(api_client, **params) | ||
| assert response.status == 200, await response.text() | ||
odesenfans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data = await response.json() | ||
| return data["posts"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_posts(fixture_messages, ccn_api_client): | ||
| # The POST messages in the fixtures file do not amend one another, so we should have | ||
| # 1 POST = 1 message. | ||
| post_messages = get_messages_by_keys( | ||
| fixture_messages, | ||
| type=MessageType.post, | ||
| ) | ||
| posts = await get_posts_expect_success(ccn_api_client) | ||
|
|
||
| assert_posts_equal(posts, post_messages) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from typing import Dict, Iterable, List, Callable | ||
|
|
||
|
|
||
| def get_messages_by_predicate( | ||
| messages: Iterable[Dict], predicate: Callable[[Dict], bool] | ||
| ) -> List[Dict]: | ||
| """ | ||
| Filters messages based on a user-provided predicate | ||
| (=a function/lambda operating on a single message). | ||
| """ | ||
|
|
||
| return [msg for msg in messages if predicate(msg)] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this function really useful ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use it a ton in another PR, just thought I'd introduce it here so that it's already in. I can rename 'predicate' to 'cond', that works as well. |
||
|
|
||
|
|
||
| def get_messages_by_keys(messages: Iterable[Dict], **keys) -> List[Dict]: | ||
odesenfans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Filters messages based on user-provided keys. | ||
|
|
||
| Example: | ||
| >>> filtered_messages = get_messages_by_keys( | ||
| >>> message_list, item_hash="some-hash", channel="MY-CHANNEL" | ||
| >>> ) | ||
|
|
||
| """ | ||
| return get_messages_by_predicate( | ||
| messages, lambda msg: all(msg[k] == v for k, v in keys.items()) | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.