mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-15 21:31:26 +00:00
add tests for groups
This commit is contained in:
parent
18735bdd42
commit
8ae20928bb
2 changed files with 158 additions and 14 deletions
116
bookwyrm/tests/models/test_group.py
Normal file
116
bookwyrm/tests/models/test_group.py
Normal file
|
@ -0,0 +1,116 @@
|
|||
""" 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"""
|
||||
|
||||
self.owner_user = models.User.objects.create_user(
|
||||
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
|
||||
)
|
||||
|
||||
self.rat = models.User.objects.create_user(
|
||||
"rat", "rat@rat.rat", "ratword", local=True, localname="rat"
|
||||
)
|
||||
|
||||
self.badger = models.User.objects.create_user(
|
||||
"badger", "badger@badger.badger", "badgerword", local=True, localname="badger"
|
||||
)
|
||||
|
||||
self.capybara = models.User.objects.create_user(
|
||||
"capybara", "capybara@capybara.capybara", "capybaraword", local=True, localname="capybara"
|
||||
)
|
||||
|
||||
self.public_group = models.Group.objects.create(
|
||||
name="Public Group",
|
||||
description="Initial description",
|
||||
user=self.owner_user,
|
||||
privacy="public"
|
||||
)
|
||||
|
||||
self.private_group = models.Group.objects.create(
|
||||
name="Private Group",
|
||||
description="Top secret",
|
||||
user=self.owner_user,
|
||||
privacy="direct"
|
||||
)
|
||||
|
||||
self.followers_only_group = models.Group.objects.create(
|
||||
name="Followers Group",
|
||||
description="No strangers",
|
||||
user=self.owner_user,
|
||||
privacy="followers"
|
||||
)
|
||||
|
||||
self.followers_list = models.List.objects.create(
|
||||
name="Followers List",
|
||||
curation="group",
|
||||
privacy="followers",
|
||||
group=self.public_group,
|
||||
user=self.owner_user
|
||||
)
|
||||
|
||||
self.private_list = models.List.objects.create(
|
||||
name="Private List",
|
||||
privacy="direct",
|
||||
curation="group",
|
||||
group=self.public_group,
|
||||
user=self.owner_user
|
||||
)
|
||||
|
||||
models.GroupMember.objects.create(
|
||||
group=self.private_group, user=self.badger
|
||||
)
|
||||
models.GroupMember.objects.create(
|
||||
group=self.followers_only_group, user=self.badger
|
||||
)
|
||||
models.GroupMember.objects.create(
|
||||
group=self.public_group, user=self.capybara
|
||||
)
|
||||
|
||||
def test_group_members_can_see_followers_only_groups(self, _):
|
||||
"""follower-only 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.followers_only_group in rat_groups)
|
||||
self.assertTrue(self.followers_only_group in badger_groups)
|
||||
|
||||
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"""
|
||||
|
||||
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()
|
||||
|
||||
self.assertFalse(self.followers_list in rat_lists)
|
||||
self.assertFalse(self.followers_list in badger_lists)
|
||||
self.assertTrue(self.followers_list in capybara_lists)
|
||||
|
||||
def test_group_members_can_see_private_lists(self, _):
|
||||
"""private group booklists should not be excluded from group booklist listing for group members"""
|
||||
|
||||
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()
|
||||
|
||||
self.assertFalse(self.private_list in rat_lists)
|
||||
self.assertFalse(self.private_list in badger_lists)
|
||||
self.assertTrue(self.private_list in capybara_lists)
|
|
@ -39,7 +39,10 @@ class GroupViews(TestCase):
|
|||
)
|
||||
|
||||
self.testgroup = models.Group.objects.create(
|
||||
id=999, name="Test Group", user=self.local_user, privacy="public"
|
||||
name="Test Group",
|
||||
description="Initial description",
|
||||
user=self.local_user,
|
||||
privacy="public"
|
||||
)
|
||||
self.membership = models.GroupMember.objects.create(
|
||||
group=self.testgroup, user=self.local_user
|
||||
|
@ -52,7 +55,7 @@ class GroupViews(TestCase):
|
|||
view = views.Group.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
result = view(request, group_id=999)
|
||||
result = view(request, group_id=self.testgroup.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
@ -72,25 +75,50 @@ class GroupViews(TestCase):
|
|||
view = views.FindUsers.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
result = view(request, group_id=999)
|
||||
result = view(request, group_id=self.testgroup.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_group_post(self, _):
|
||||
def test_group_create(self, _):
|
||||
"""create group view"""
|
||||
view = views.UserGroups.as_view()
|
||||
request = self.factory.post(
|
||||
"",
|
||||
{
|
||||
"name": "A group",
|
||||
"description": "wowzers",
|
||||
"privacy": "unlisted",
|
||||
"user": self.local_user.id,
|
||||
},
|
||||
)
|
||||
request.user = self.local_user
|
||||
result = view(request, "username")
|
||||
|
||||
self.assertEqual(result.status_code, 302)
|
||||
new_group = models.Group.objects.filter(name="A group").get()
|
||||
self.assertEqual(new_group.description, "wowzers")
|
||||
self.assertEqual(new_group.privacy, "unlisted")
|
||||
self.assertTrue(models.GroupMember.objects.filter(group=new_group, user=self.local_user).exists())
|
||||
|
||||
def test_group_edit(self, _):
|
||||
"""test editing a "group" database entry"""
|
||||
|
||||
view = views.Group.as_view()
|
||||
group_fields = {
|
||||
"name": "Updated Group",
|
||||
"privacy": "private",
|
||||
"description": "Test description",
|
||||
"user": self.local_user,
|
||||
}
|
||||
request = self.factory.post("", group_fields)
|
||||
request = self.factory.post(
|
||||
"",
|
||||
{
|
||||
"name": "Updated Group name",
|
||||
"description": "wow",
|
||||
"privacy": "direct",
|
||||
"user": self.local_user.id,
|
||||
}
|
||||
)
|
||||
request.user = self.local_user
|
||||
|
||||
result = view(request, group_id=999)
|
||||
result = view(request, group_id=self.testgroup.id)
|
||||
self.assertEqual(result.status_code, 302)
|
||||
|
||||
# TODO: test that group was updated.
|
||||
self.testgroup.refresh_from_db()
|
||||
self.assertEqual(self.testgroup.name, "Updated Group name")
|
||||
self.assertEqual(self.testgroup.description, "wow")
|
||||
self.assertEqual(self.testgroup.privacy, "direct")
|
||||
|
|
Loading…
Reference in a new issue