2021-03-28 21:36:07 +00:00
|
|
|
""" test for app action functionality """
|
2021-04-20 03:01:29 +00:00
|
|
|
from unittest.mock import patch
|
2021-09-28 20:44:49 +00:00
|
|
|
|
2021-04-20 01:12:55 +00:00
|
|
|
from django.contrib.auth.models import Group
|
2021-03-28 21:36:07 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
2021-10-01 05:22:20 +00:00
|
|
|
from bookwyrm.tests.validate_html import validate_html
|
2021-03-28 21:36:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserAdminViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""every response to a get request, html or json"""
|
2021-03-28 21:36:07 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-03-28 21:36:07 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
2021-12-10 03:14:10 +00:00
|
|
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
2021-08-03 17:25:53 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.mouse",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-03-28 21:36:07 +00:00
|
|
|
|
2021-04-19 23:08:01 +00:00
|
|
|
def test_user_admin_list_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-04-19 23:08:01 +00:00
|
|
|
view = views.UserAdminList.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
result = view(request)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-01 05:22:20 +00:00
|
|
|
validate_html(result.render())
|
2021-04-19 23:08:01 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-28 21:36:07 +00:00
|
|
|
def test_user_admin_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-03-28 21:36:07 +00:00
|
|
|
view = views.UserAdmin.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
2021-04-19 23:42:51 +00:00
|
|
|
|
|
|
|
result = view(request, self.local_user.id)
|
|
|
|
|
2021-03-28 21:36:07 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-01 05:22:20 +00:00
|
|
|
validate_html(result.render())
|
2021-03-28 21:36:07 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2021-04-20 01:12:55 +00:00
|
|
|
|
2021-08-03 22:22:51 +00:00
|
|
|
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
|
2021-09-06 23:16:45 +00:00
|
|
|
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
|
2021-08-03 22:22:51 +00:00
|
|
|
@patch("bookwyrm.suggested_users.remove_user_task.delay")
|
|
|
|
def test_user_admin_page_post(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""set the user's group"""
|
2021-04-20 01:12:55 +00:00
|
|
|
group = Group.objects.create(name="editor")
|
|
|
|
self.assertEqual(
|
|
|
|
list(self.local_user.groups.values_list("name", flat=True)), []
|
|
|
|
)
|
|
|
|
|
|
|
|
view = views.UserAdmin.as_view()
|
|
|
|
request = self.factory.post("", {"groups": [group.id]})
|
|
|
|
request.user = self.local_user
|
|
|
|
request.user.is_superuser = True
|
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-04-20 03:01:29 +00:00
|
|
|
result = view(request, self.local_user.id)
|
2021-04-20 01:12:55 +00:00
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-01 05:22:20 +00:00
|
|
|
validate_html(result.render())
|
2021-04-20 01:12:55 +00:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
list(self.local_user.groups.values_list("name", flat=True)), ["editor"]
|
|
|
|
)
|