From dc41f150971f11e5ce45f5775541806b84ca342f Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Wed, 18 Oct 2023 21:42:19 +0200 Subject: [PATCH] fix deletion of inbox entries when deleting an user --- app/models/inbox.rb | 3 ++- spec/models/inbox_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 spec/models/inbox_spec.rb diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 1a1a0066..b12beb29 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -22,7 +22,8 @@ class Inbox < ApplicationRecord end after_destroy do - user.touch(:inbox_updated_at) + # user might not exist at this point (account deleted, records are cleaned up async) + user&.touch(:inbox_updated_at) end def answer(answer_content, user) diff --git a/spec/models/inbox_spec.rb b/spec/models/inbox_spec.rb new file mode 100644 index 00000000..9a00635a --- /dev/null +++ b/spec/models/inbox_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe Inbox, type: :model do + describe "associations" do + it { should belong_to(:user) } + it { should belong_to(:question) } + end + + describe "before_destroy" do + let(:user) { FactoryBot.create(:user) } + let(:question) { FactoryBot.create(:question, author_is_anonymous: true) } + + it "does not fail if the user wants to delete their account" do + Inbox.create(user:, question:) + + # this deletes the User record and enqueues the deletion of all + # associated records in sidekiq + user.destroy! + + # so let's drain the queues + expect { Sidekiq::Worker.drain_all }.not_to raise_error + end + end +end