2021-10-10 01:06:54 +00:00
|
|
|
""" testing models """
|
|
|
|
from unittest.mock import patch
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookwyrm import models, settings
|
|
|
|
|
|
|
|
|
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
|
|
|
class Group(TestCase):
|
|
|
|
"""some activitypub oddness ahead"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set up for tests"""
|
|
|
|
|
2021-10-16 05:42:08 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
|
|
|
self.owner_user = models.User.objects.create_user(
|
|
|
|
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
|
|
|
self.rat = models.User.objects.create_user(
|
|
|
|
"rat", "rat@rat.rat", "ratword", local=True, localname="rat"
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
|
|
|
self.badger = models.User.objects.create_user(
|
|
|
|
"badger",
|
|
|
|
"badger@badger.badger",
|
|
|
|
"badgerword",
|
|
|
|
local=True,
|
|
|
|
localname="badger",
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
|
|
|
self.capybara = models.User.objects.create_user(
|
|
|
|
"capybara",
|
|
|
|
"capybara@capybara.capybara",
|
|
|
|
"capybaraword",
|
|
|
|
local=True,
|
|
|
|
localname="capybara",
|
|
|
|
)
|
2021-10-10 01:06:54 +00:00
|
|
|
|
|
|
|
self.public_group = models.Group.objects.create(
|
2021-10-10 01:33:11 +00:00
|
|
|
name="Public Group",
|
2021-10-10 01:06:54 +00:00
|
|
|
description="Initial description",
|
2021-10-10 01:33:11 +00:00
|
|
|
user=self.owner_user,
|
|
|
|
privacy="public",
|
2021-10-10 01:06:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.private_group = models.Group.objects.create(
|
2021-10-10 01:33:11 +00:00
|
|
|
name="Private Group",
|
2021-10-10 01:06:54 +00:00
|
|
|
description="Top secret",
|
2021-10-10 01:33:11 +00:00
|
|
|
user=self.owner_user,
|
|
|
|
privacy="direct",
|
2021-10-10 01:06:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.followers_only_group = models.Group.objects.create(
|
2021-10-10 01:33:11 +00:00
|
|
|
name="Followers Group",
|
2021-10-10 01:06:54 +00:00
|
|
|
description="No strangers",
|
2021-10-10 01:33:11 +00:00
|
|
|
user=self.owner_user,
|
|
|
|
privacy="followers",
|
2021-10-10 01:06:54 +00:00
|
|
|
)
|
|
|
|
|
2021-10-10 01:33:11 +00:00
|
|
|
models.GroupMember.objects.create(group=self.private_group, user=self.badger)
|
2021-10-10 01:06:54 +00:00
|
|
|
models.GroupMember.objects.create(
|
|
|
|
group=self.followers_only_group, user=self.badger
|
|
|
|
)
|
2021-10-10 01:33:11 +00:00
|
|
|
models.GroupMember.objects.create(group=self.public_group, user=self.capybara)
|
2021-10-10 01:06:54 +00:00
|
|
|
|
|
|
|
def test_group_members_can_see_private_groups(self, _):
|
|
|
|
"""direct privacy group should not be excluded from group listings for group members viewing"""
|
|
|
|
|
|
|
|
rat_groups = models.Group.privacy_filter(self.rat).all()
|
|
|
|
badger_groups = models.Group.privacy_filter(self.badger).all()
|
|
|
|
|
|
|
|
self.assertFalse(self.private_group in rat_groups)
|
|
|
|
self.assertTrue(self.private_group in badger_groups)
|
|
|
|
|
|
|
|
def test_group_members_can_see_followers_only_lists(self, _):
|
|
|
|
"""follower-only group booklists should not be excluded from group booklist listing for group members who do not follower list owner"""
|
|
|
|
|
2021-10-16 20:21:47 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
followers_list = models.List.objects.create(
|
|
|
|
name="Followers List",
|
|
|
|
curation="group",
|
|
|
|
privacy="followers",
|
|
|
|
group=self.public_group,
|
|
|
|
user=self.owner_user,
|
|
|
|
)
|
|
|
|
|
2021-10-10 01:06:54 +00:00
|
|
|
rat_lists = models.List.privacy_filter(self.rat).all()
|
|
|
|
badger_lists = models.List.privacy_filter(self.badger).all()
|
|
|
|
capybara_lists = models.List.privacy_filter(self.capybara).all()
|
|
|
|
|
2021-10-16 20:21:47 +00:00
|
|
|
self.assertFalse(followers_list in rat_lists)
|
|
|
|
self.assertFalse(followers_list in badger_lists)
|
|
|
|
self.assertTrue(followers_list in capybara_lists)
|
2021-10-10 01:33:11 +00:00
|
|
|
|
2021-10-10 01:06:54 +00:00
|
|
|
def test_group_members_can_see_private_lists(self, _):
|
|
|
|
"""private group booklists should not be excluded from group booklist listing for group members"""
|
|
|
|
|
2021-10-16 20:21:47 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
|
|
|
|
private_list = models.List.objects.create(
|
|
|
|
name="Private List",
|
|
|
|
privacy="direct",
|
|
|
|
curation="group",
|
|
|
|
group=self.public_group,
|
|
|
|
user=self.owner_user,
|
|
|
|
)
|
|
|
|
|
2021-10-10 01:06:54 +00:00
|
|
|
rat_lists = models.List.privacy_filter(self.rat).all()
|
|
|
|
badger_lists = models.List.privacy_filter(self.badger).all()
|
|
|
|
capybara_lists = models.List.privacy_filter(self.capybara).all()
|
|
|
|
|
2021-10-16 20:21:47 +00:00
|
|
|
self.assertFalse(private_list in rat_lists)
|
|
|
|
self.assertFalse(private_list in badger_lists)
|
|
|
|
self.assertTrue(private_list in capybara_lists)
|