From 5a6e5a401291349b746ba2d5d24f0d148ae3d7f4 Mon Sep 17 00:00:00 2001 From: Selase Krakani <849886+s3lase@users.noreply.github.com> Date: Thu, 16 Feb 2023 10:47:01 +0000 Subject: [PATCH] FIX: Unsubscribing via key associated with deleted topic (#20275) Currently, clicking on the unsubscribe link with a `key` associated with a deleted topic results in an HTTP 500 response. This change fixes that by skipping any attempt to run topic related flow if topic isn't present. --- lib/email_controller_helper/topic_email_unsubscriber.rb | 2 ++ spec/requests/email_controller_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/email_controller_helper/topic_email_unsubscriber.rb b/lib/email_controller_helper/topic_email_unsubscriber.rb index eda37b7d667..e31ef94e1b0 100644 --- a/lib/email_controller_helper/topic_email_unsubscriber.rb +++ b/lib/email_controller_helper/topic_email_unsubscriber.rb @@ -8,6 +8,8 @@ module EmailControllerHelper topic = unsubscribe_key.associated_topic + return if topic.blank? + controller.instance_variable_set(:@topic, topic) controller.instance_variable_set( :@watching_topic, diff --git a/spec/requests/email_controller_spec.rb b/spec/requests/email_controller_spec.rb index 57478f38fe5..c9c29a0d0cb 100644 --- a/spec/requests/email_controller_spec.rb +++ b/spec/requests/email_controller_spec.rb @@ -317,6 +317,15 @@ RSpec.describe EmailController do expect(response.body).not_to include("unwatch_category") end + it "displays form even if topic is deleted" do + post.topic.trash! + + navigate_to_unsubscribe + + expect(response.status).to eq(200) + expect(response.body).to include(I18n.t("unsubscribe.all", sitename: SiteSetting.title)) + end + def create_category_user(notification_level) CategoryUser.create!( user_id: user.id,