fix deletion of inbox entries when deleting an user

This commit is contained in:
Georg Gadinger 2023-10-18 21:42:19 +02:00
parent b8fd519e2d
commit dc41f15097
2 changed files with 28 additions and 1 deletions

View File

@ -22,7 +22,8 @@ class Inbox < ApplicationRecord
end end
after_destroy do 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 end
def answer(answer_content, user) def answer(answer_content, user)

26
spec/models/inbox_spec.rb Normal file
View File

@ -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