Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow invalid account reactivation #2879

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bookwyrm/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ def deactivate(self):
def reactivate(self):
"""Now you want to come back, huh?"""
# pylint: disable=attribute-defined-outside-init
if not self.allow_reactivation:
return
self.is_active = True
self.deactivation_reason = None
self.allow_reactivation = False
Expand Down
8 changes: 7 additions & 1 deletion bookwyrm/tests/views/landing/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,17 @@ def test_confirm_email_code_get(self, *_):
self.settings.save()

self.local_user.is_active = False
self.local_user.allow_reactivation = True
self.local_user.deactivation_reason = "pending"
self.local_user.confirmation_code = "12345"
self.local_user.save(
broadcast=False,
update_fields=["is_active", "deactivation_reason", "confirmation_code"],
update_fields=[
"is_active",
"allow_reactivation",
"deactivation_reason",
"confirmation_code",
],
)
view = views.ConfirmEmailCode.as_view()
request = self.factory.get("")
Expand Down
21 changes: 21 additions & 0 deletions bookwyrm/tests/views/preferences/test_delete_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,24 @@ def test_reactivate_user_post(self, _):
self.local_user.refresh_from_db()
self.assertTrue(self.local_user.is_active)
self.assertIsNone(self.local_user.deactivation_reason)

def test_reactivate_user_post_disallowed(self, _):
"""Reactivate action under the wrong circumstances"""
self.local_user.is_active = False
self.local_user.save(broadcast=False)

view = views.ReactivateUser.as_view()
form = forms.LoginForm()
form.data["localname"] = "mouse"
form.data["password"] = "password"
request = self.factory.post("", form.data)
request.user = self.local_user
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()

with patch("bookwyrm.views.preferences.delete_user.login"):
view(request)

self.local_user.refresh_from_db()
self.assertFalse(self.local_user.is_active)
5 changes: 4 additions & 1 deletion bookwyrm/views/landing/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def post(self, request):
password,
localname=localname,
local=True,
allow_reactivation=settings.require_confirm_email,
deactivation_reason="pending" if settings.require_confirm_email else None,
is_active=not settings.require_confirm_email,
preferred_timezone=preferred_timezone,
Expand Down Expand Up @@ -105,7 +106,9 @@ def get(self, request, code): # pylint: disable=unused-argument

# look up the user associated with this code
try:
user = models.User.objects.get(confirmation_code=code)
user = models.User.objects.get(
confirmation_code=code, deactivation_reason="pending"
)
except models.User.DoesNotExist:
return TemplateResponse(
request, "confirm_email/confirm_email.html", {"valid": False}
Expand Down