Only reactivate appropriate users when undoing domain block

This commit is contained in:
Mouse Reeve 2021-04-12 10:13:38 -07:00
parent 93fe091034
commit 878b3c6fe8
3 changed files with 86 additions and 7 deletions

View file

@ -6,13 +6,22 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('bookwyrm', '0065_merge_20210411_1702'), ("bookwyrm", "0065_merge_20210411_1702"),
] ]
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name='user', model_name="user",
name='deactivation_reason', name="deactivation_reason",
field=models.CharField(blank=True, choices=[('self_deletion', 'Self Deletion'), ('moderator_deletion', 'Moderator Deletion'), ('domain_block', 'Domain Block')], max_length=255, null=True), field=models.CharField(
blank=True,
choices=[
("self_deletion", "Self Deletion"),
("moderator_deletion", "Moderator Deletion"),
("domain_block", "Domain Block"),
],
max_length=255,
null=True,
),
), ),
] ]

View file

@ -30,15 +30,18 @@ class FederatedServer(BookWyrmModel):
self.save() self.save()
# deactivate all associated users # deactivate all associated users
self.user_set.update(is_active=False) self.user_set.filter(is_active=True).update(
is_active=False, deactivation_reason="domain_block"
)
def unblock(self): def unblock(self):
""" unblock a server """ """ unblock a server """
self.status = "federated" self.status = "federated"
self.save() self.save()
# TODO: only reactivate users as appropriate self.user_set.filter(deactivation_reason="domain_block").update(
self.user_set.update(is_active=True) is_active=True, deactivation_reason=None
)
@classmethod @classmethod
def is_blocked(cls, url): def is_blocked(cls, url):

View file

@ -0,0 +1,67 @@
""" testing models """
from unittest.mock import patch
from django.test import TestCase
from bookwyrm import models
class FederatedServer(TestCase):
""" federate server management """
def setUp(self):
""" we'll need a user """
self.server = models.FederatedServer.objects.create(server_name="test.server")
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.com",
"ratword",
federated_server=self.server,
local=False,
remote_id="https://example.com/users/rat",
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
)
self.inactive_remote_user = models.User.objects.create_user(
"nutria",
"nutria@nutria.com",
"nutriaword",
federated_server=self.server,
local=False,
remote_id="https://example.com/users/nutria",
inbox="https://example.com/users/nutria/inbox",
outbox="https://example.com/users/nutria/outbox",
is_active=False,
deactivation_reason="self_deletion",
)
def test_block_unblock(self):
""" block a server and all users on it """
self.assertEqual(self.server.status, "federated")
self.assertTrue(self.remote_user.is_active)
self.assertFalse(self.inactive_remote_user.is_active)
self.server.block()
self.assertEqual(self.server.status, "blocked")
self.remote_user.refresh_from_db()
self.assertFalse(self.remote_user.is_active)
self.assertEqual(self.remote_user.deactivation_reason, "domain_block")
self.inactive_remote_user.refresh_from_db()
self.assertFalse(self.inactive_remote_user.is_active)
self.assertEqual(self.inactive_remote_user.deactivation_reason, "self_deletion")
# UNBLOCK
self.server.unblock()
self.assertEqual(self.server.status, "federated")
# user blocked in deactivation is reactivated
self.remote_user.refresh_from_db()
self.assertTrue(self.remote_user.is_active)
self.assertIsNone(self.remote_user.deactivation_reason)
# deleted user remains deleted
self.inactive_remote_user.refresh_from_db()
self.assertFalse(self.inactive_remote_user.is_active)
self.assertEqual(self.inactive_remote_user.deactivation_reason, "self_deletion")