From d5fbdacc0243db94e87bc0fb4dd5f7435e6dc118 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 10 Apr 2021 11:06:28 -0700 Subject: [PATCH] Adds unblock view --- .../templates/settings/federated_server.html | 3 +-- bookwyrm/tests/views/test_federation.py | 23 ++++++++++++++++++- bookwyrm/urls.py | 5 ++++ bookwyrm/views/__init__.py | 2 +- bookwyrm/views/federation.py | 12 ++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/bookwyrm/templates/settings/federated_server.html b/bookwyrm/templates/settings/federated_server.html index 31e0ffb9..249d7106 100644 --- a/bookwyrm/templates/settings/federated_server.html +++ b/bookwyrm/templates/settings/federated_server.html @@ -84,12 +84,11 @@

{% trans "All users from this instance will be deactivated." %}

{% else %} - {% comment %}
+ {% csrf_token %}

{% trans "All users from this instance will be re-activated." %}

- {% endcomment %} {% endif %} diff --git a/bookwyrm/tests/views/test_federation.py b/bookwyrm/tests/views/test_federation.py index 38da37d3..fdafcad0 100644 --- a/bookwyrm/tests/views/test_federation.py +++ b/bookwyrm/tests/views/test_federation.py @@ -56,7 +56,7 @@ class FederationViews(TestCase): result.render() self.assertEqual(result.status_code, 200) - def test_server_page_post(self): + def test_server_page_block(self): """ block a server """ server = models.FederatedServer.objects.create(server_name="hi.there.com") self.remote_user.federated_server = server @@ -76,6 +76,27 @@ class FederationViews(TestCase): # and the user was deactivated self.assertFalse(self.remote_user.is_active) + def test_server_page_unblock(self): + """ unblock a server """ + server = models.FederatedServer.objects.create( + server_name="hi.there.com", status="blocked" + ) + self.remote_user.federated_server = server + self.remote_user.is_active = False + self.remote_user.save() + + request = self.factory.post("") + request.user = self.local_user + request.user.is_superuser = True + + views.federation.unblock_server(request, server.id) + server.refresh_from_db() + self.remote_user.refresh_from_db() + self.assertEqual(server.status, "federated") + # and the user was re-activated + self.assertTrue(self.remote_user.is_active) + + def test_add_view_get(self): """ there are so many views, this just makes sure it LOADS """ # create mode diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 398caade..f2d43a13 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -68,6 +68,11 @@ urlpatterns = [ views.FederatedServer.as_view(), name="settings-federated-server", ), + re_path( + r"^settings/federation/(?P\d+)/unblock?$", + views.federation.unblock_server, + name="settings-federated-server-unblock", + ), re_path( r"^settings/federation/add/?$", views.AddFederatedServer.as_view(), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index de001b31..2dc7a1c6 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -5,7 +5,7 @@ from .block import Block, unblock from .books import Book, EditBook, ConfirmEditBook, Editions from .books import upload_cover, add_description, switch_edition, resolve_book from .directory import Directory -from .federation import Federation, FederatedServer, AddFederatedServer +from .federation import Federation, FederatedServer, AddFederatedServer, unblock_server from .feed import DirectMessage, Feed, Replies, Status from .follow import follow, unfollow from .follow import accept_follow_request, delete_follow_request diff --git a/bookwyrm/views/federation.py b/bookwyrm/views/federation.py index 067fa0d7..8a21c5a5 100644 --- a/bookwyrm/views/federation.py +++ b/bookwyrm/views/federation.py @@ -5,6 +5,7 @@ from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator from django.views import View +from django.views.decorators.http import require_POST from bookwyrm import forms, models from bookwyrm.settings import PAGE_LENGTH @@ -91,3 +92,14 @@ class FederatedServer(View): server = get_object_or_404(models.FederatedServer, id=server) server.block() return redirect("settings-federated-server", server.id) + + +@login_required +@require_POST +@permission_required("bookwyrm.control_federation", raise_exception=True) +# pylint: disable=unused-argument +def unblock_server(request, server): + """ unblock a server """ + server = get_object_or_404(models.FederatedServer, id=server) + server.unblock() + return redirect("settings-federated-server", server.id)