diff --git a/federation/tests/conftest.py b/federation/tests/conftest.py index be6cc8a..11eab6a 100644 --- a/federation/tests/conftest.py +++ b/federation/tests/conftest.py @@ -4,6 +4,7 @@ import pytest # noinspection PyUnresolvedReferences from federation.tests.fixtures.entities import * +from federation.tests.fixtures.types import * from federation.tests.fixtures.keys import get_dummy_private_key diff --git a/federation/tests/entities/activitypub/test_entities.py b/federation/tests/entities/activitypub/test_entities.py index df7f02b..64548d5 100644 --- a/federation/tests/entities/activitypub/test_entities.py +++ b/federation/tests/entities/activitypub/test_entities.py @@ -1,6 +1,11 @@ +from unittest.mock import patch + +from Crypto.PublicKey.RSA import RsaKey + from federation.entities.activitypub.constants import ( CONTEXTS_DEFAULT, CONTEXT_MANUALLY_APPROVES_FOLLOWERS, CONTEXT_LD_SIGNATURES, CONTEXT_HASHTAG, CONTEXT_SENSITIVE) -from federation.entities.activitypub.entities import ActivitypubProfile +from federation.entities.activitypub.entities import ActivitypubProfile, ActivitypubAccept +from federation.types import UserType class TestEntitiesConvertToAS2: @@ -37,3 +42,26 @@ class TestEntitiesConvertToAS2: CONTEXT_MANUALLY_APPROVES_FOLLOWERS, ] assert result.get('type') == 'Person' + + +class TestEntitiesPostReceive: + @patch("federation.utils.activitypub.retrieve_and_parse_profile", autospec=True) + @patch("federation.entities.activitypub.entities.handle_send", autospec=True) + def test_follow_post_receive__sends_correct_accept_back( + self, mock_send, mock_retrieve, activitypubfollow, profile + ): + mock_retrieve.return_value = profile + activitypubfollow.post_receive() + args, kwargs = mock_send.call_args_list[0] + assert isinstance(args[0], ActivitypubAccept) + assert args[0].activity_id.startswith("https://example.com/profile#accept-") + assert args[0].actor_id == "https://example.com/profile" + assert args[0].target_id == "https://localhost/follow" + assert isinstance(args[1], UserType) + assert args[1].id == "https://example.com/profile" + assert isinstance(args[1].private_key, RsaKey) + assert kwargs['recipients'] == [{ + "fid": "https://example.com/private", + "protocol": "activitypub", + "public": False, + }] diff --git a/federation/tests/fixtures/entities.py b/federation/tests/fixtures/entities.py index 53139cd..69d3e36 100644 --- a/federation/tests/fixtures/entities.py +++ b/federation/tests/fixtures/entities.py @@ -2,7 +2,7 @@ import uuid import pytest -from federation.entities.activitypub.entities import ActivitypubPost, ActivitypubAccept +from federation.entities.activitypub.entities import ActivitypubPost, ActivitypubAccept, ActivitypubFollow from federation.entities.base import Profile from federation.entities.diaspora.entities import ( DiasporaPost, DiasporaComment, DiasporaLike, DiasporaProfile, DiasporaRetraction, @@ -21,6 +21,15 @@ def activitypubaccept(): ) +@pytest.fixture +def activitypubfollow(): + return ActivitypubFollow( + activity_id="https://localhost/follow", + actor_id="https://localhost/profile", + target_id="https://example.com/profile", + ) + + @pytest.fixture def profile(): return Profile( @@ -31,6 +40,10 @@ def profile(): id="https://example.com/alice", handle="alice@example.com", guid="guid", + inboxes={ + "private": "https://example.com/private", + "public": "https://example.com/public", + } ) diff --git a/federation/tests/fixtures/types.py b/federation/tests/fixtures/types.py new file mode 100644 index 0000000..a980358 --- /dev/null +++ b/federation/tests/fixtures/types.py @@ -0,0 +1,12 @@ +import pytest + +from federation.tests.fixtures.keys import get_dummy_private_key +from federation.types import UserType + + +@pytest.fixture +def usertype(): + return UserType( + id="https://localhost/profile", + private_key=get_dummy_private_key(), + )