Be less noisy when cancelling account deletion
parent
b3eb1fff13
commit
947d205c09
|
@ -71,14 +71,17 @@ class User < ApplicationRecord
|
|||
|
||||
def deactivate_account
|
||||
User.transaction do
|
||||
if reload.last_sign_in_at > 3.months.ago
|
||||
raise "HALTING ERRONEOUS DELETION"
|
||||
end
|
||||
# Prevent double deletion / race conditions.
|
||||
update!(last_sign_in_at: Time.now, inactivity_warning_sent_at: nil)
|
||||
email = self.email
|
||||
delay.destroy!
|
||||
puts "INACTIVITY DELETION FOR #{email}" unless Rails.env.test?
|
||||
if reload.last_sign_in_at > 3.months.ago
|
||||
puts "CANCEL DEACTIVATION FOR #{email}" unless Rails.env.test?
|
||||
update!(inactivity_warning_sent_at: nil)
|
||||
return # <== DON'T DELETE THIS LINE!!!
|
||||
else
|
||||
# Prevent double deletion / race conditions.
|
||||
update!(last_sign_in_at: Time.now, inactivity_warning_sent_at: nil)
|
||||
delay.destroy!
|
||||
puts "INACTIVITY DELETION FOR #{email}" unless Rails.env.test?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,8 +2,15 @@ require "spec_helper"
|
|||
|
||||
describe User do
|
||||
it "prevents accidental deactivation" do
|
||||
u = FactoryBot.create(:user, last_sign_in_at: Time.now)
|
||||
expect { u.deactivate_account }.to raise_error("HALTING ERRONEOUS DELETION")
|
||||
time = Time.now
|
||||
u = FactoryBot.create(:user,
|
||||
last_sign_in_at: time,
|
||||
inactivity_warning_sent_at: time)
|
||||
expect(u.inactivity_warning_sent_at).to_not be(nil)
|
||||
expect(u.last_sign_in_at).to_not be(time)
|
||||
u.deactivate_account
|
||||
expect(u.inactivity_warning_sent_at).to be(nil)
|
||||
expect(u.last_sign_in_at).to_not be(time)
|
||||
end
|
||||
|
||||
describe "#new" do
|
||||
|
|
Loading…
Reference in New Issue