From b5f8df642a4c087ccbd50da7be60534f6244abca Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Tue, 24 Oct 2023 21:20:19 +0300 Subject: [PATCH] Disallow account deletion after confirmation --- app/controllers/accounts_controller.rb | 14 +++++++++----- test/controllers/accounts_controller_test.rb | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 63da1293f..db9721010 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -53,12 +53,16 @@ class AccountsController < ApplicationController end def destroy - current_user.soft_destroy! + if current_user.deletion_allowed? + current_user.soft_destroy! - session.delete(:user) - session_expires_automatically + session.delete(:user) + session_expires_automatically - flash[:notice] = t ".success" - redirect_to root_path + flash[:notice] = t ".success" + redirect_to root_path + else + head :bad_request + end end end diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb index 7546c3797..131292f41 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -152,4 +152,23 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest # Make sure we have a button to "go public" assert_select "form.button_to[action='/user/go_public']", true end + + def test_destroy_allowed + user = create(:user) + session_for(user) + + delete account_path + assert_response :redirect + end + + def test_destroy_not_allowed + with_user_account_deletion_delay(24) do + user = create(:user) + create(:changeset, :user => user, :created_at => Time.now.utc) + session_for(user) + + delete account_path + assert_response :bad_request + end + end end -- 2.45.1