moviewyrm/bookwyrm/tests/views/test_reports.py

137 lines
4.7 KiB
Python
Raw Normal View History

2021-03-09 00:02:16 +00:00
""" test for app action functionality """
2021-03-12 23:24:57 +00:00
from unittest.mock import patch
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-03-09 00:02:16 +00:00
class ReportViews(TestCase):
""" every response to a get request, html or json """
def setUp(self):
""" we need basic test data and mocks """
self.factory = RequestFactory()
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
2021-03-09 02:36:34 +00:00
self.rat = models.User.objects.create_user(
2021-03-09 01:16:59 +00:00
"rat@local.com",
"rat@mouse.mouse",
"password",
local=True,
localname="rat",
)
2021-03-09 00:02:16 +00:00
models.SiteSettings.objects.create()
def test_reports_page(self):
""" there are so many views, this just makes sure it LOADS """
view = views.Reports.as_view()
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)
result.render()
self.assertEqual(result.status_code, 200)
def test_reports_page_with_data(self):
""" there are so many views, this just makes sure it LOADS """
view = views.Reports.as_view()
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)
result.render()
self.assertEqual(result.status_code, 200)
def test_report_page(self):
""" there are so many views, this just makes sure it LOADS """
view = views.Report.as_view()
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)
result.render()
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):
""" comment on a report """
view = views.Report.as_view()
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):
""" a user reports another user """
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
views.make_report(request)
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):
""" toggle report resolution status """
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
def test_deactivate_user(self):
""" toggle whether a user is able to log in """
self.assertTrue(self.rat.is_active)
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
request = self.factory.post("")
request.user = self.local_user
request.user.is_superuser = True
2021-03-12 23:24:57 +00:00
# de-activate
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
views.deactivate_user(request, report.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
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
views.deactivate_user(request, report.id)
2021-03-12 19:13:53 +00:00
self.rat.refresh_from_db()
self.assertTrue(self.rat.is_active)