2021-03-23 21:59:51 +00:00
|
|
|
""" testing activitystreams """
|
|
|
|
from unittest.mock import patch
|
|
|
|
from django.test import TestCase
|
|
|
|
from bookwyrm import activitystreams, models
|
|
|
|
|
|
|
|
|
2021-03-23 22:03:05 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
|
|
|
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
|
2021-03-23 21:59:51 +00:00
|
|
|
class Activitystreams(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""using redis to build activity streams"""
|
2021-03-23 21:59:51 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""use a test csv"""
|
2021-03-23 21:59:51 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse", "mouse@mouse.mouse", "password", local=True, localname="mouse"
|
|
|
|
)
|
|
|
|
self.another_user = models.User.objects.create_user(
|
|
|
|
"nutria", "nutria@nutria.nutria", "password", local=True, localname="nutria"
|
|
|
|
)
|
|
|
|
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
|
|
|
self.remote_user = models.User.objects.create_user(
|
|
|
|
"rat",
|
|
|
|
"rat@rat.com",
|
|
|
|
"ratword",
|
|
|
|
local=False,
|
|
|
|
remote_id="https://example.com/users/rat",
|
|
|
|
inbox="https://example.com/users/rat/inbox",
|
|
|
|
outbox="https://example.com/users/rat/outbox",
|
|
|
|
)
|
|
|
|
self.book = models.Edition.objects.create(title="test book")
|
|
|
|
|
|
|
|
class TestStream(activitystreams.ActivityStream):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""test stream, don't have to do anything here"""
|
2021-03-23 21:59:51 +00:00
|
|
|
|
|
|
|
key = "test"
|
|
|
|
|
|
|
|
self.test_stream = TestStream()
|
|
|
|
|
2021-03-23 22:03:05 +00:00
|
|
|
def test_activitystream_class_ids(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the abstract base class for stream objects"""
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.test_stream.stream_id(self.local_user),
|
|
|
|
"{}-test".format(self.local_user.id),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
self.test_stream.unread_id(self.local_user),
|
|
|
|
"{}-test-unread".format(self.local_user.id),
|
|
|
|
)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_abstractstream_get_audience(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user, content="hi", privacy="public"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = self.test_stream.get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
# remote users don't have feeds
|
|
|
|
self.assertFalse(self.remote_user in users)
|
|
|
|
self.assertTrue(self.local_user in users)
|
|
|
|
self.assertTrue(self.another_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_abstractstream_get_audience_direct(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user,
|
|
|
|
content="hi",
|
|
|
|
privacy="direct",
|
|
|
|
)
|
|
|
|
status.mention_users.add(self.local_user)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = self.test_stream.get_audience(status)
|
2021-03-26 19:52:04 +00:00
|
|
|
self.assertEqual(users, [])
|
2021-03-23 21:59:51 +00:00
|
|
|
|
|
|
|
status = models.Comment.objects.create(
|
|
|
|
user=self.remote_user,
|
|
|
|
content="hi",
|
|
|
|
privacy="direct",
|
|
|
|
book=self.book,
|
|
|
|
)
|
|
|
|
status.mention_users.add(self.local_user)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = self.test_stream.get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertTrue(self.local_user in users)
|
|
|
|
self.assertFalse(self.another_user in users)
|
|
|
|
self.assertFalse(self.remote_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_abstractstream_get_audience_followers_remote_user(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user,
|
|
|
|
content="hi",
|
|
|
|
privacy="followers",
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = self.test_stream.get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertFalse(users.exists())
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_abstractstream_get_audience_followers_self(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Comment.objects.create(
|
|
|
|
user=self.local_user,
|
|
|
|
content="hi",
|
|
|
|
privacy="direct",
|
|
|
|
book=self.book,
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = self.test_stream.get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertTrue(self.local_user in users)
|
|
|
|
self.assertFalse(self.another_user in users)
|
|
|
|
self.assertFalse(self.remote_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_abstractstream_get_audience_followers_with_mention(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Comment.objects.create(
|
|
|
|
user=self.remote_user,
|
|
|
|
content="hi",
|
|
|
|
privacy="direct",
|
|
|
|
book=self.book,
|
|
|
|
)
|
|
|
|
status.mention_users.add(self.local_user)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
users = self.test_stream.get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertTrue(self.local_user in users)
|
|
|
|
self.assertFalse(self.another_user in users)
|
|
|
|
self.assertFalse(self.remote_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_abstractstream_get_audience_followers_with_relationship(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
self.remote_user.followers.add(self.local_user)
|
|
|
|
status = models.Comment.objects.create(
|
|
|
|
user=self.remote_user,
|
|
|
|
content="hi",
|
|
|
|
privacy="direct",
|
|
|
|
book=self.book,
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = self.test_stream.get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertFalse(self.local_user in users)
|
|
|
|
self.assertFalse(self.another_user in users)
|
|
|
|
self.assertFalse(self.remote_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_homestream_get_audience(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user, content="hi", privacy="public"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.HomeStream().get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertFalse(users.exists())
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_homestream_get_audience_with_mentions(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user, content="hi", privacy="public"
|
|
|
|
)
|
|
|
|
status.mention_users.add(self.local_user)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.HomeStream().get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertFalse(self.local_user in users)
|
|
|
|
self.assertFalse(self.another_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_homestream_get_audience_with_relationship(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
self.remote_user.followers.add(self.local_user)
|
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user, content="hi", privacy="public"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.HomeStream().get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertTrue(self.local_user in users)
|
|
|
|
self.assertFalse(self.another_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_localstream_get_audience_remote_status(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user, content="hi", privacy="public"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.LocalStream().get_audience(status)
|
2021-03-26 19:52:04 +00:00
|
|
|
self.assertEqual(users, [])
|
2021-03-23 21:59:51 +00:00
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_localstream_get_audience_local_status(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.local_user, content="hi", privacy="public"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.LocalStream().get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertTrue(self.local_user in users)
|
|
|
|
self.assertTrue(self.another_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_localstream_get_audience_unlisted(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.local_user, content="hi", privacy="unlisted"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.LocalStream().get_audience(status)
|
2021-03-26 19:52:04 +00:00
|
|
|
self.assertEqual(users, [])
|
2021-03-23 21:59:51 +00:00
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_federatedstream_get_audience(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user, content="hi", privacy="public"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.FederatedStream().get_audience(status)
|
2021-03-23 21:59:51 +00:00
|
|
|
self.assertTrue(self.local_user in users)
|
|
|
|
self.assertTrue(self.another_user in users)
|
|
|
|
|
2021-04-05 21:17:45 +00:00
|
|
|
def test_federatedstream_get_audience_unlisted(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a list of users that should see a status"""
|
2021-03-23 21:59:51 +00:00
|
|
|
status = models.Status.objects.create(
|
|
|
|
user=self.remote_user, content="hi", privacy="unlisted"
|
|
|
|
)
|
2021-04-05 21:17:45 +00:00
|
|
|
users = activitystreams.FederatedStream().get_audience(status)
|
2021-03-26 19:52:04 +00:00
|
|
|
self.assertEqual(users, [])
|