Account deactivation ✔️

pull/1578/head
Rick Carlino 2019-11-21 11:25:46 -06:00
parent a0e69bdb4a
commit e70695536a
4 changed files with 45 additions and 5 deletions

View File

@ -70,10 +70,12 @@ class User < ApplicationRecord
def deactivate_account
User.transaction do
raise "HALTING ERRONEOUS DELETION" if last_sign_in_at > 3.months.ago
if reload.last_sign_in_at > 3.months.ago
raise "HALTING ERRONEOUS DELETION"
end
# Prevent double deletion / race conditions.
u.update!(last_sign_in_at: Time.now, inactivity_warning_sent_at: nil)
u.delay.destroy!
update!(last_sign_in_at: Time.now, inactivity_warning_sent_at: nil)
delay.destroy!
end
end
end

View File

@ -1,3 +1,3 @@
You have not logged in <%= time_ago_in_words(user.last_sign_in_at) %>. Your account will be automatically deleted after 14 days of inactivity.
You have not logged in <%= time_ago_in_words(@user.last_sign_in_at) %>. Your account will be automatically deleted after 14 days of inactivity.
To halt the deletion process, please log in to your account.
To halt the deletion process, please <%= link_to "login", front_page_url %> to your account in the next 14 days.

View File

@ -0,0 +1,33 @@
require "spec_helper"
describe InactiveAccountJob do
let!(:yes) { FactoryBot.create(:user, last_sign_in_at: 3.years.ago) }
let!(:no) { FactoryBot.create(:user, last_sign_in_at: 3.days.ago) }
it "Processes deletion" do
# === Expect a clean slate.
expect(yes.inactivity_warning_sent_at).to be(nil)
expect(no.inactivity_warning_sent_at).to be(nil)
empty_mail_bag
# === Perform the first (only) warning.
run_jobs_now { InactiveAccountJob.new.perform }
mail = ActionMailer::Base.deliveries.last
# === Expect warnings
expect(yes.reload.inactivity_warning_sent_at).not_to be(nil)
expect(no.reload.inactivity_warning_sent_at).to be(nil)
expect(mail).to be_kind_of(Mail::Message)
expect(mail.to).to include(yes.email)
expect(mail.subject).to eq("Your FarmBot Account Will Be Deleted Due to Inactivity")
expect(mail.body.encoded).to include("You have not logged in about 3 years.")
# === Wind back the clock to simulate inactivty.
yes.update!(inactivity_warning_sent_at: 15.days.ago)
run_jobs_now { InactiveAccountJob.new.perform }
expect(User.where(id: yes.id).count).to eq(0)
end
it "does not delete an account during the waiting period"
it "finishes deletion"
end

View File

@ -1,6 +1,11 @@
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")
end
describe "#new" do
it "Creates a new user" do
expect(User.new).to be_kind_of(User)