diff options
author | Michael Manfre | 2022-11-26 12:09:31 -0500 |
---|---|---|
committer | GitHub | 2022-11-26 10:09:31 -0700 |
commit | 849c221aeeee89bdb61a88b4e6080481ecfeb934 (patch) | |
tree | 323b5a720adf214f4bbbbdaadbcd30249bcdcec5 /tests | |
parent | c7588583927e004e10599912f6d7b76413d52730 (diff) | |
download | takahe-849c221aeeee89bdb61a88b4e6080481ecfeb934.tar.gz takahe-849c221aeeee89bdb61a88b4e6080481ecfeb934.tar.bz2 takahe-849c221aeeee89bdb61a88b4e6080481ecfeb934.zip |
Local-only posting
Diffstat (limited to 'tests')
-rw-r--r-- | tests/activities/models/test_post_targets.py | 107 | ||||
-rw-r--r-- | tests/conftest.py | 27 |
2 files changed, 131 insertions, 3 deletions
diff --git a/tests/activities/models/test_post_targets.py b/tests/activities/models/test_post_targets.py new file mode 100644 index 0000000..1e4bbc4 --- /dev/null +++ b/tests/activities/models/test_post_targets.py @@ -0,0 +1,107 @@ +import pytest +from asgiref.sync import async_to_sync + +from activities.models import Post +from users.models import Follow + + +@pytest.mark.django_db +def test_post_targets_simple(identity, other_identity, remote_identity): + """ + Tests that a simple top level post returns the correct targets. + """ + # Test a post with no mentions targets author + post = Post.objects.create( + content="<p>Hello</p>", + author=identity, + local=True, + ) + targets = async_to_sync(post.aget_targets)() + assert targets == {identity} + + # Test remote reply targets original post author + Post.objects.create( + content="<p>Reply</p>", + author=remote_identity, + local=False, + in_reply_to=post.absolute_object_uri(), + ) + targets = async_to_sync(post.aget_targets)() + assert targets == {identity} + + # Test a post with local and remote mentions + post = Post.objects.create( + content="<p>Hello @test and @other</p>", + author=identity, + local=True, + ) + # Mentions are targeted + post.mentions.add(remote_identity) + post.mentions.add(other_identity) + targets = async_to_sync(post.aget_targets)() + # Targets everyone + assert targets == {identity, other_identity, remote_identity} + + # Test remote post with mentions + post.local = False + post.save() + targets = async_to_sync(post.aget_targets)() + # Only targets locals + assert targets == {identity, other_identity} + + +@pytest.mark.django_db +def test_post_local_only(identity, other_identity, remote_identity): + """ + Tests that a simple top level post returns the correct targets. + """ + # Test a short username (remote) + post = Post.objects.create( + content="<p>Hello @test and @other</p>", + author=identity, + local=True, + visibility=Post.Visibilities.local_only, + ) + post.mentions.add(remote_identity) + post.mentions.add(other_identity) + + # Remote mention is not targeted + post.mentions.add(remote_identity) + targets = async_to_sync(post.aget_targets)() + assert targets == {identity, other_identity} + + +@pytest.mark.django_db +def test_post_followers(identity, other_identity, remote_identity): + + Follow.objects.create(source=other_identity, target=identity) + Follow.objects.create(source=remote_identity, target=identity) + + # Test Public post w/o mentions targets self and followers + post = Post.objects.create( + content="<p>Hello</p>", + author=identity, + local=True, + visibility=Post.Visibilities.public, + ) + targets = async_to_sync(post.aget_targets)() + assert targets == {identity, other_identity, remote_identity} + + # Remote post only targets local followers + post.local = False + post.save() + targets = async_to_sync(post.aget_targets)() + assert targets == {identity, other_identity} + + # Local Only post only targets local followers + post.local = True + post.visibility = Post.Visibilities.local_only + post.save() + targets = async_to_sync(post.aget_targets)() + assert targets == {identity, other_identity} + + # Mentioned posts do not target unmentioned followers + post.visibility = Post.Visibilities.mentioned + post.save() + targets = async_to_sync(post.aget_targets)() + assert targets == {identity} diff --git a/tests/conftest.py b/tests/conftest.py index 48ee95a..d506c5c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -68,11 +68,16 @@ def user() -> User: @pytest.fixture @pytest.mark.django_db -def identity(user): +def domain() -> Domain: + return Domain.objects.create(domain="example.com", local=True, public=True) + + +@pytest.fixture +@pytest.mark.django_db +def identity(user, domain) -> Identity: """ Creates a basic test identity with a user and domain. """ - domain = Domain.objects.create(domain="example.com", local=True, public=True) identity = Identity.objects.create( actor_uri="https://example.com/@test@example.com/", username="test", @@ -85,8 +90,24 @@ def identity(user): @pytest.fixture +def other_identity(user, domain) -> Identity: + """ + Creates a different basic test identity with a user and domain. + """ + identity = Identity.objects.create( + actor_uri="https://example.com/@other@example.com/", + username="other", + domain=domain, + name="Other User", + local=True, + ) + identity.users.set([user]) + return identity + + +@pytest.fixture @pytest.mark.django_db -def remote_identity(): +def remote_identity() -> Identity: """ Creates a basic remote test identity with a domain. """ |