Activate pending users when email confirmation is disabled

This commit is contained in:
Mouse Reeve 2022-01-20 15:58:00 -08:00
parent 874e2e0a9c
commit 148f9b6f5e
2 changed files with 43 additions and 15 deletions

View file

@ -90,6 +90,14 @@ class SiteSettings(models.Model):
return get_absolute_url(uploaded) return get_absolute_url(uploaded)
return urljoin(STATIC_FULL_URL, default_path) 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): class SiteInvite(models.Model):
"""gives someone access to create an account on the instance""" """gives someone access to create an account on the instance"""

View file

@ -94,14 +94,28 @@ class SiteModels(TestCase):
self.assertTrue(token.valid()) self.assertTrue(token.valid())
self.assertEqual(token.link, f"https://{settings.DOMAIN}/password-reset/hello") 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""" """Switch from requiring email confirmation to not"""
site = models.SiteSettings.objects.create( site = models.SiteSettings.objects.create(
id=1, name="Fish Town", require_confirm_email=True id=1, name="Fish Town", require_confirm_email=True
) )
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( banned_user = models.User.objects.create_user(
"bookwyrm.activitystreams.populate_stream_task.delay" "rat@local.com",
), patch("bookwyrm.lists_stream.populate_lists_task.delay"): "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( pending_user = models.User.objects.create_user(
"nutria@local.com", "nutria@local.com",
"nutria@nutria.com", "nutria@nutria.com",
@ -109,12 +123,18 @@ class SiteModels(TestCase):
local=True, local=True,
localname="nutria", localname="nutria",
remote_id="https://example.com/users/nutria", remote_id="https://example.com/users/nutria",
is_active=False,
deactivation_reason="pending",
confirmation_code="HELLO", confirmation_code="HELLO",
) )
pending_user.is_active = False
pending_user.deactivation_reason = "pending"
pending_user.save(broadcast=False)
site.require_confirm_email = False site.require_confirm_email = False
site.save() site.save()
pending_user.refresh_from_db() pending_user.refresh_from_db()
self.assertTrue(pending_user.is_active) self.assertTrue(pending_user.is_active)
self.assertIsNone(pending_user.deactivation_reason) self.assertIsNone(pending_user.deactivation_reason)
banned_user.refresh_from_db()
self.assertFalse(banned_user.is_active)
self.assertIsNotNone(banned_user.deactivation_reason)