fix(security): keep fallback templates running when captain feature is revoked

This commit is contained in:
Tanmay Deep Sharma
2026-07-23 13:29:21 +05:30
parent c86f7539a2
commit bc8ad35f22
2 changed files with 26 additions and 1 deletions
@@ -82,7 +82,9 @@ module Enterprise::MessageTemplates::HookExecutionService
::MessageTemplates::Template::OutOfOffice.perform_if_applicable(conversation)
end
# Only let Captain suppress the fallback templates when it can actually reply; otherwise a revoked
# feature would leave the contact with neither a Captain response nor a greeting/OOO/email-collect.
def captain_handling_conversation?
conversation.pending? && inbox.respond_to?(:captain_assistant) && inbox.captain_assistant.present?
conversation.pending? && inbox.respond_to?(:captain_assistant) && inbox.captain_assistant.present? && captain_feature_enabled?
end
end
@@ -203,6 +203,29 @@ RSpec.describe MessageTemplates::HookExecutionService do
end
end
context 'when the Captain feature is disabled but an assistant is still linked' do
before do
account.disable_features!('captain_integration', 'captain_integration_v2')
conversation.update!(status: :pending)
end
it 'does not schedule captain response job' do
expect(Captain::Conversation::ResponseBuilderJob).not_to receive(:perform_later)
create(:message, conversation: conversation, message_type: :incoming, account: account)
end
it 'falls back to the greeting message instead of leaving the contact without a reply' do
inbox.update!(greeting_enabled: true, greeting_message: 'Hello! How can we help you?', enable_email_collect: false)
expect do
create(:message, conversation: conversation, message_type: :incoming, account: account)
end.to change { conversation.reload.messages.template.count }.by(1)
expect(conversation.reload.messages.template.last.content).to eq('Hello! How can we help you?')
end
end
context 'when Captain is not configured' do
before do
CaptainInbox.where(inbox: inbox).destroy_all