moviewyrm/bookwyrm/tests/views/admin/test_reports.py

165 lines
6 KiB
Python
Raw Normal View History

2021-03-09 00:02:16 +00:00
""" test for app action functionality """
2021-09-09 00:33:43 +00:00
import json
2021-08-03 17:25:53 +00:00
from unittest.mock import patch
2021-09-28 20:44:49 +00:00
2021-03-09 00:02:16 +00:00
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
2021-03-09 02:36:34 +00:00
from bookwyrm import forms, models, views
2021-10-01 05:22:20 +00:00
from bookwyrm.tests.validate_html import validate_html
2021-03-09 00:02:16 +00:00
class ReportViews(TestCase):
2021-04-26 16:15:42 +00:00
"""every response to a get request, html or json"""
2021-03-09 00:02:16 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-03-09 00:02:16 +00:00
self.factory = RequestFactory()
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",
)
self.rat = models.User.objects.create_user(
"rat@local.com",
"rat@mouse.mouse",
"password",
local=True,
localname="rat",
)
2021-08-02 23:05:40 +00:00
models.SiteSettings.objects.create()
2021-03-09 00:02:16 +00:00
def test_reports_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2022-01-11 16:06:52 +00:00
view = views.ReportsAdmin.as_view()
2021-03-09 00:02:16 +00:00
request = self.factory.get("")
request.user = self.local_user
request.user.is_superuser = True
2021-03-09 02:36:34 +00:00
result = view(request)
self.assertIsInstance(result, TemplateResponse)
2021-10-01 05:22:20 +00:00
validate_html(result.render())
2021-03-09 02:36:34 +00:00
self.assertEqual(result.status_code, 200)
def test_reports_page_with_data(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2022-01-11 16:06:52 +00:00
view = views.ReportsAdmin.as_view()
2021-03-09 02:36:34 +00:00
request = self.factory.get("")
request.user = self.local_user
request.user.is_superuser = True
2021-03-12 18:22:03 +00:00
models.Report.objects.create(reporter=self.local_user, user=self.rat)
2021-03-09 02:36:34 +00:00
2021-03-09 00:02:16 +00:00
result = view(request)
self.assertIsInstance(result, TemplateResponse)
2021-10-01 05:22:20 +00:00
validate_html(result.render())
2021-03-09 00:02:16 +00:00
self.assertEqual(result.status_code, 200)
def test_report_page(self):
2021-04-26 16:15:42 +00:00
"""there are so many views, this just makes sure it LOADS"""
2022-01-11 16:06:52 +00:00
view = views.ReportAdmin.as_view()
2021-03-09 00:02:16 +00:00
request = self.factory.get("")
request.user = self.local_user
request.user.is_superuser = True
2021-03-09 01:16:59 +00:00
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
2021-03-09 00:02:16 +00:00
result = view(request, report.id)
self.assertIsInstance(result, TemplateResponse)
2021-10-01 05:22:20 +00:00
validate_html(result.render())
2021-03-09 00:02:16 +00:00
self.assertEqual(result.status_code, 200)
2021-03-09 02:36:34 +00:00
2021-03-13 00:02:23 +00:00
def test_report_comment(self):
2021-04-26 16:15:42 +00:00
"""comment on a report"""
2022-01-11 16:06:52 +00:00
view = views.ReportAdmin.as_view()
2021-03-13 00:02:23 +00:00
request = self.factory.post("", {"note": "hi"})
request.user = self.local_user
request.user.is_superuser = True
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
view(request, report.id)
comment = models.ReportComment.objects.get()
self.assertEqual(comment.user, self.local_user)
self.assertEqual(comment.note, "hi")
self.assertEqual(comment.report, report)
2021-03-09 02:36:34 +00:00
def test_make_report(self):
2021-04-26 16:15:42 +00:00
"""a user reports another user"""
2021-03-09 02:36:34 +00:00
form = forms.ReportForm()
form.data["reporter"] = self.local_user.id
form.data["user"] = self.rat.id
request = self.factory.post("", form.data)
request.user = self.local_user
2022-01-11 16:06:52 +00:00
views.Report.as_view()(request)
2021-03-09 02:36:34 +00:00
report = models.Report.objects.get()
self.assertEqual(report.reporter, self.local_user)
self.assertEqual(report.user, self.rat)
2021-03-12 18:22:03 +00:00
def test_resolve_report(self):
2021-04-26 16:15:42 +00:00
"""toggle report resolution status"""
2021-03-12 18:22:03 +00:00
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
self.assertFalse(report.resolved)
request = self.factory.post("")
request.user = self.local_user
request.user.is_superuser = True
# resolve
views.resolve_report(request, report.id)
report.refresh_from_db()
self.assertTrue(report.resolved)
# un-resolve
views.resolve_report(request, report.id)
report.refresh_from_db()
self.assertFalse(report.resolved)
2021-03-12 19:13:53 +00:00
2021-08-03 20:27:32 +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 23:21:29 +00:00
@patch("bookwyrm.suggested_users.remove_user_task.delay")
def test_suspend_user(self, *_):
2021-04-26 16:15:42 +00:00
"""toggle whether a user is able to log in"""
2021-03-12 19:13:53 +00:00
self.assertTrue(self.rat.is_active)
request = self.factory.post("")
request.user = self.local_user
request.user.is_superuser = True
2021-03-12 23:24:57 +00:00
# de-activate
2021-04-19 23:42:51 +00:00
views.suspend_user(request, self.rat.id)
2021-03-12 19:13:53 +00:00
self.rat.refresh_from_db()
self.assertFalse(self.rat.is_active)
2021-03-12 23:24:57 +00:00
# re-activate
2021-09-08 23:48:23 +00:00
views.unsuspend_user(request, self.rat.id)
2021-03-12 19:13:53 +00:00
self.rat.refresh_from_db()
self.assertTrue(self.rat.is_active)
2021-09-09 00:33:43 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.suggested_users.remove_user_task.delay")
def test_delete_user(self, *_):
"""toggle whether a user is able to log in"""
self.assertTrue(self.rat.is_active)
request = self.factory.post("", {"password": "password"})
request.user = self.local_user
request.user.is_superuser = True
# de-activate
2021-11-12 17:17:00 +00:00
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
) as mock:
2021-09-09 00:33:43 +00:00
views.moderator_delete_user(request, self.rat.id)
self.assertEqual(mock.call_count, 1)
2021-11-12 17:17:00 +00:00
activity = json.loads(mock.call_args[1]["args"][1])
2021-09-09 00:33:43 +00:00
self.assertEqual(activity["type"], "Delete")
self.rat.refresh_from_db()
self.assertFalse(self.rat.is_active)
self.assertEqual(self.rat.deactivation_reason, "moderator_deletion")