summaryrefslogtreecommitdiffstats
path: root/tests/activities/models/test_post_targets.py
blob: 1e4bbc403274a05923ec29028bd1a0576b94ca8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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}