mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 03:21:05 +00:00
Updates admin tests to use perms instead of superuser
Superuser overrides all perms, so this was hiding simple typo-related bugs in the permissions system
This commit is contained in:
parent
2eaffc7249
commit
948f2964ac
9 changed files with 63 additions and 37 deletions
|
@ -1,12 +1,14 @@
|
|||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
from django_celery_beat.models import PeriodicTask, IntervalSchedule
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -26,6 +28,10 @@ class AutomodViews(TestCase):
|
|||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
def test_automod_rules_get(self):
|
||||
|
@ -40,7 +46,6 @@ class AutomodViews(TestCase):
|
|||
view = views.AutoMod.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
@ -58,7 +63,6 @@ class AutomodViews(TestCase):
|
|||
view = views.AutoMod.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
@ -70,7 +74,6 @@ class AutomodViews(TestCase):
|
|||
view = views.AutoMod.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
@ -88,7 +91,6 @@ class AutomodViews(TestCase):
|
|||
view = views.AutoMod.as_view()
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
|
||||
|
@ -109,7 +111,6 @@ class AutomodViews(TestCase):
|
|||
form.data["period"] = "days"
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
response = views.schedule_automod_task(request)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -24,6 +27,10 @@ class DashboardViews(TestCase):
|
|||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
|
@ -32,7 +39,7 @@ class DashboardViews(TestCase):
|
|||
view = views.Dashboard.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -25,6 +27,10 @@ class EmailBlocklistViews(TestCase):
|
|||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
|
@ -33,7 +39,6 @@ class EmailBlocklistViews(TestCase):
|
|||
view = views.EmailBlocklist.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
|
||||
|
@ -46,7 +51,6 @@ class EmailBlocklistViews(TestCase):
|
|||
view = views.EmailBlocklist.as_view()
|
||||
request = self.factory.post("", {"domain": "gmail.com"})
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
|
||||
|
@ -65,7 +69,6 @@ class EmailBlocklistViews(TestCase):
|
|||
view = views.EmailBlocklist.as_view()
|
||||
request = self.factory.post("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request, domain_id=domain.id)
|
||||
self.assertEqual(result.status_code, 302)
|
||||
|
|
|
@ -3,12 +3,14 @@ import os
|
|||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -38,6 +40,10 @@ class FederationViews(TestCase):
|
|||
inbox="https://example.com/users/rat/inbox",
|
||||
outbox="https://example.com/users/rat/outbox",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
|
@ -46,7 +52,7 @@ class FederationViews(TestCase):
|
|||
view = views.Federation.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
|
@ -58,7 +64,6 @@ class FederationViews(TestCase):
|
|||
view = views.FederatedServer.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request, server.id)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
@ -81,7 +86,6 @@ class FederationViews(TestCase):
|
|||
view = views.block_server
|
||||
request = self.factory.post("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
with patch("bookwyrm.suggested_users.bulk_remove_instance_task.delay") as mock:
|
||||
view(request, server.id)
|
||||
|
@ -121,7 +125,6 @@ class FederationViews(TestCase):
|
|||
|
||||
request = self.factory.post("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
with patch("bookwyrm.suggested_users.bulk_add_instance_task.delay") as mock:
|
||||
views.unblock_server(request, server.id)
|
||||
|
@ -147,7 +150,6 @@ class FederationViews(TestCase):
|
|||
view = views.AddFederatedServer.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
@ -164,7 +166,6 @@ class FederationViews(TestCase):
|
|||
view = views.AddFederatedServer.as_view()
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
view(request)
|
||||
server = models.FederatedServer.objects.get()
|
||||
|
@ -196,7 +197,6 @@ class FederationViews(TestCase):
|
|||
},
|
||||
)
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
view(request)
|
||||
server.refresh_from_db()
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -24,6 +27,10 @@ class IPBlocklistViews(TestCase):
|
|||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
|
@ -32,7 +39,6 @@ class IPBlocklistViews(TestCase):
|
|||
view = views.IPBlocklist.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
|
||||
|
@ -48,7 +54,6 @@ class IPBlocklistViews(TestCase):
|
|||
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
|
||||
|
@ -67,7 +72,6 @@ class IPBlocklistViews(TestCase):
|
|||
|
||||
request = self.factory.post("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
view(request, block.id)
|
||||
self.assertFalse(models.IPBlocklist.objects.exists())
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -25,6 +27,11 @@ class LinkDomainViews(TestCase):
|
|||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
|
||||
self.book = models.Edition.objects.create(title="hello")
|
||||
models.FileLink.objects.create(
|
||||
book=self.book,
|
||||
|
@ -39,7 +46,6 @@ class LinkDomainViews(TestCase):
|
|||
view = views.LinkDomain.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request, "pending")
|
||||
|
||||
|
@ -55,7 +61,6 @@ class LinkDomainViews(TestCase):
|
|||
view = views.LinkDomain.as_view()
|
||||
request = self.factory.post("", {"name": "ugh"})
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request, "pending", domain.id)
|
||||
self.assertEqual(result.status_code, 302)
|
||||
|
@ -71,7 +76,6 @@ class LinkDomainViews(TestCase):
|
|||
view = views.update_domain_status
|
||||
request = self.factory.post("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request, domain.id, "approved")
|
||||
self.assertEqual(result.status_code, 302)
|
||||
|
|
|
@ -2,11 +2,13 @@
|
|||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -33,6 +35,10 @@ class ReportViews(TestCase):
|
|||
local=True,
|
||||
localname="rat",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
def test_reports_page(self):
|
||||
|
@ -40,7 +46,6 @@ class ReportViews(TestCase):
|
|||
view = views.ReportsAdmin.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
|
@ -52,7 +57,6 @@ class ReportViews(TestCase):
|
|||
view = views.ReportsAdmin.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
models.Report.objects.create(reporter=self.local_user, user=self.rat)
|
||||
|
||||
result = view(request)
|
||||
|
@ -65,7 +69,6 @@ class ReportViews(TestCase):
|
|||
view = views.ReportAdmin.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
|
||||
|
||||
result = view(request, report.id)
|
||||
|
@ -79,7 +82,6 @@ class ReportViews(TestCase):
|
|||
view = views.ReportAdmin.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)
|
||||
|
@ -95,7 +97,6 @@ class ReportViews(TestCase):
|
|||
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)
|
||||
|
@ -115,7 +116,6 @@ class ReportViews(TestCase):
|
|||
self.assertTrue(self.rat.is_active)
|
||||
request = self.factory.post("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
# de-activate
|
||||
views.suspend_user(request, self.rat.id)
|
||||
|
@ -134,7 +134,6 @@ class ReportViews(TestCase):
|
|||
self.assertTrue(self.rat.is_active)
|
||||
request = self.factory.post("", {"password": "password"})
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
# de-activate
|
||||
with patch(
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
""" test for app action functionality """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import forms, models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -24,6 +27,10 @@ class SiteSettingsViews(TestCase):
|
|||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="admin")
|
||||
self.local_user.groups.set([group])
|
||||
|
||||
self.site = models.SiteSettings.objects.create()
|
||||
|
||||
|
@ -32,7 +39,7 @@ class SiteSettingsViews(TestCase):
|
|||
view = views.Site.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
self.assertIsInstance(result, TemplateResponse)
|
||||
validate_html(result.render())
|
||||
|
@ -51,7 +58,6 @@ class SiteSettingsViews(TestCase):
|
|||
form.data["privacy_policy"] = "blah"
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
|
||||
|
@ -68,7 +74,6 @@ class SiteSettingsViews(TestCase):
|
|||
form = forms.SiteForm()
|
||||
request = self.factory.post("", form.data)
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request)
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ from django.test import TestCase
|
|||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models, views
|
||||
from bookwyrm.management.commands import initdb
|
||||
from bookwyrm.tests.validate_html import validate_html
|
||||
|
||||
|
||||
|
@ -26,6 +27,10 @@ class UserAdminViews(TestCase):
|
|||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
initdb.init_groups()
|
||||
initdb.init_permissions()
|
||||
group = Group.objects.get(name="moderator")
|
||||
self.local_user.groups.set([group])
|
||||
models.SiteSettings.objects.create()
|
||||
|
||||
def test_user_admin_list_page(self):
|
||||
|
@ -33,7 +38,7 @@ class UserAdminViews(TestCase):
|
|||
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)
|
||||
validate_html(result.render())
|
||||
|
@ -44,7 +49,6 @@ class UserAdminViews(TestCase):
|
|||
view = views.UserAdmin.as_view()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
result = view(request, self.local_user.id)
|
||||
|
||||
|
@ -57,15 +61,14 @@ class UserAdminViews(TestCase):
|
|||
@patch("bookwyrm.suggested_users.remove_user_task.delay")
|
||||
def test_user_admin_page_post(self, *_):
|
||||
"""set the user's group"""
|
||||
group = Group.objects.create(name="editor")
|
||||
group = Group.objects.get(name="editor")
|
||||
self.assertEqual(
|
||||
list(self.local_user.groups.values_list("name", flat=True)), []
|
||||
list(self.local_user.groups.values_list("name", flat=True)), ["moderator"]
|
||||
)
|
||||
|
||||
view = views.UserAdmin.as_view()
|
||||
request = self.factory.post("", {"groups": [group.id]})
|
||||
request.user = self.local_user
|
||||
request.user.is_superuser = True
|
||||
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
||||
result = view(request, self.local_user.id)
|
||||
|
|
Loading…
Reference in a new issue