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