2021-10-05 10:06:09 +00:00
|
|
|
""" test for app action functionality """
|
|
|
|
from unittest.mock import patch
|
2021-10-07 21:38:00 +00:00
|
|
|
from django.contrib.auth import decorators
|
2021-10-05 10:06:09 +00:00
|
|
|
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
2021-10-07 21:38:00 +00:00
|
|
|
from bookwyrm import models, views, forms
|
2021-10-05 10:06:09 +00:00
|
|
|
from bookwyrm.tests.validate_html import validate_html
|
|
|
|
|
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
2021-10-05 10:06:09 +00:00
|
|
|
class GroupViews(TestCase):
|
|
|
|
"""view group and edit details"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""we need basic test data and mocks"""
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.mouse",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
|
|
|
|
|
|
|
self.testgroup = models.Group.objects.create(
|
2021-10-10 01:33:11 +00:00
|
|
|
name="Test Group",
|
2021-10-10 01:06:54 +00:00
|
|
|
description="Initial description",
|
2021-10-10 01:33:11 +00:00
|
|
|
user=self.local_user,
|
|
|
|
privacy="public",
|
2021-10-05 10:06:09 +00:00
|
|
|
)
|
|
|
|
self.membership = models.GroupMember.objects.create(
|
|
|
|
group=self.testgroup, user=self.local_user
|
|
|
|
)
|
|
|
|
|
|
|
|
models.SiteSettings.objects.create()
|
|
|
|
|
|
|
|
def test_group_get(self, _):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.Group.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
2021-10-10 01:06:54 +00:00
|
|
|
result = view(request, group_id=self.testgroup.id)
|
2021-10-05 10:06:09 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_usergroups_get(self, _):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.UserGroups.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
2021-10-07 21:38:00 +00:00
|
|
|
result = view(request, username="mouse@local.com")
|
2021-10-05 10:06:09 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-10-16 20:21:47 +00:00
|
|
|
@patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions")
|
|
|
|
def test_findusers_get(self, *_):
|
2021-10-05 10:06:09 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.FindUsers.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
2021-10-10 01:06:54 +00:00
|
|
|
result = view(request, group_id=self.testgroup.id)
|
2021-10-05 10:06:09 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-10-10 01:06:54 +00:00
|
|
|
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")
|
2021-10-10 01:33:11 +00:00
|
|
|
self.assertTrue(
|
|
|
|
models.GroupMember.objects.filter(
|
|
|
|
group=new_group, user=self.local_user
|
|
|
|
).exists()
|
|
|
|
)
|
2021-10-10 01:06:54 +00:00
|
|
|
|
|
|
|
def test_group_edit(self, _):
|
2021-10-07 21:38:00 +00:00
|
|
|
"""test editing a "group" database entry"""
|
|
|
|
|
2021-10-05 10:06:09 +00:00
|
|
|
view = views.Group.as_view()
|
2021-10-10 01:06:54 +00:00
|
|
|
request = self.factory.post(
|
2021-10-10 01:33:11 +00:00
|
|
|
"",
|
|
|
|
{
|
|
|
|
"name": "Updated Group name",
|
|
|
|
"description": "wow",
|
|
|
|
"privacy": "direct",
|
|
|
|
"user": self.local_user.id,
|
|
|
|
},
|
2021-10-10 01:06:54 +00:00
|
|
|
)
|
2021-10-07 21:38:00 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-10-10 01:06:54 +00:00
|
|
|
result = view(request, group_id=self.testgroup.id)
|
2021-10-07 21:38:00 +00:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2021-10-10 01:06:54 +00:00
|
|
|
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")
|