From 874e2e0a9c88d381bb85b55d7c5c645793e110ec Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 20 Jan 2022 15:19:52 -0800 Subject: [PATCH 1/2] Adds test for activating users on change to registration mode --- bookwyrm/tests/models/test_site.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/bookwyrm/tests/models/test_site.py b/bookwyrm/tests/models/test_site.py index d23f7988..48223cf8 100644 --- a/bookwyrm/tests/models/test_site.py +++ b/bookwyrm/tests/models/test_site.py @@ -93,3 +93,28 @@ class SiteModels(TestCase): token = models.PasswordReset.objects.create(user=self.local_user, code="hello") self.assertTrue(token.valid()) self.assertEqual(token.link, f"https://{settings.DOMAIN}/password-reset/hello") + + def test_change_confirmation_scheme(self): + """Switch from requiring email confirmation to not""" + site = models.SiteSettings.objects.create( + id=1, name="Fish Town", require_confirm_email=True + ) + with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( + "bookwyrm.activitystreams.populate_stream_task.delay" + ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): + pending_user = models.User.objects.create_user( + "nutria@local.com", + "nutria@nutria.com", + "nutriaword", + local=True, + localname="nutria", + remote_id="https://example.com/users/nutria", + is_active=False, + deactivation_reason="pending", + confirmation_code="HELLO", + ) + site.require_confirm_email = False + site.save() + pending_user.refresh_from_db() + self.assertTrue(pending_user.is_active) + self.assertIsNone(pending_user.deactivation_reason) From 148f9b6f5ebbef6c1848ba7890d704ed7e3cdac7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 20 Jan 2022 15:58:00 -0800 Subject: [PATCH 2/2] Activate pending users when email confirmation is disabled --- bookwyrm/models/site.py | 8 +++++ bookwyrm/tests/models/test_site.py | 50 +++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index 5d91553e..b2119e23 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -90,6 +90,14 @@ class SiteSettings(models.Model): return get_absolute_url(uploaded) return urljoin(STATIC_FULL_URL, default_path) + def save(self, *args, **kwargs): + """if require_confirm_email is disabled, make sure no users are pending""" + if not self.require_confirm_email: + User.objects.filter(is_active=False, deactivation_reason="pending").update( + is_active=True, deactivation_reason=None + ) + super().save(*args, **kwargs) + class SiteInvite(models.Model): """gives someone access to create an account on the instance""" diff --git a/bookwyrm/tests/models/test_site.py b/bookwyrm/tests/models/test_site.py index 48223cf8..05882268 100644 --- a/bookwyrm/tests/models/test_site.py +++ b/bookwyrm/tests/models/test_site.py @@ -94,27 +94,47 @@ class SiteModels(TestCase): self.assertTrue(token.valid()) self.assertEqual(token.link, f"https://{settings.DOMAIN}/password-reset/hello") - def test_change_confirmation_scheme(self): + @patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") + @patch("bookwyrm.suggested_users.remove_user_task.delay") + @patch("bookwyrm.activitystreams.populate_stream_task.delay") + @patch("bookwyrm.lists_stream.populate_lists_task.delay") + def test_change_confirmation_scheme(self, *_): """Switch from requiring email confirmation to not""" site = models.SiteSettings.objects.create( id=1, name="Fish Town", require_confirm_email=True ) - with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( - "bookwyrm.activitystreams.populate_stream_task.delay" - ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): - pending_user = models.User.objects.create_user( - "nutria@local.com", - "nutria@nutria.com", - "nutriaword", - local=True, - localname="nutria", - remote_id="https://example.com/users/nutria", - is_active=False, - deactivation_reason="pending", - confirmation_code="HELLO", - ) + banned_user = models.User.objects.create_user( + "rat@local.com", + "rat@rat.com", + "ratword", + local=True, + localname="rat", + remote_id="https://example.com/users/rat", + confirmation_code="HELLO", + ) + banned_user.is_active = False + banned_user.deactivation_reason = "banned" + banned_user.save(broadcast=False) + + pending_user = models.User.objects.create_user( + "nutria@local.com", + "nutria@nutria.com", + "nutriaword", + local=True, + localname="nutria", + remote_id="https://example.com/users/nutria", + confirmation_code="HELLO", + ) + pending_user.is_active = False + pending_user.deactivation_reason = "pending" + pending_user.save(broadcast=False) site.require_confirm_email = False site.save() + pending_user.refresh_from_db() self.assertTrue(pending_user.is_active) self.assertIsNone(pending_user.deactivation_reason) + + banned_user.refresh_from_db() + self.assertFalse(banned_user.is_active) + self.assertIsNotNone(banned_user.deactivation_reason)