Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

7.6 KB
Raw Blame
require 'rails_helper'

RSpec.describe Mailbox::ConversationFinderStrategies::ReferencesStrategy do
  let(:account) { create(:account) }
  let(:email_channel) { create(:channel_email, email: '[email protected]', account: account) }
  let(:conversation) { create(:conversation, inbox: email_channel.inbox, account: account) }
  let(:mail) { Mail.new }

  describe '#find' do
    context 'when references has message-specific pattern' do
      before do
        conversation.update!(uuid: '12345678-1234-1234-1234-123456789012')
        mail.to = '[email protected]'
        mail.references = 'conversation/12345678-1234-1234-1234-123456789012/messages/[email protected]'
      end

      it 'extracts UUID and returns conversation' do
        strategy = described_class.new(mail)
        expect(strategy.find).to eq(conversation)
      end
    end

    context 'when references has conversation fallback pattern' do
      before do
        conversation.update!(uuid: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')
        mail.to = '[email protected]'
        mail.references = "account/#{account.id}/conversation/[email protected]"
      end

      it 'extracts UUID and returns conversation' do
        strategy = described_class.new(mail)
        expect(strategy.find).to eq(conversation)
      end
    end

    context 'when references matches message source_id' do
      let(:message) do
        conversation.messages.create!(
          source_id: '[email protected]',
          account_id: account.id,
          message_type: 'outgoing',
          inbox_id: email_channel.inbox.id,
          content: 'Original message'
        )
      end

      before do
        message # Create the message
        mail.to = '[email protected]'
        mail.references = '[email protected]'
      end

      it 'finds conversation from message source_id' do
        strategy = described_class.new(mail)
        expect(strategy.find).to eq(conversation)
      end
    end

    context 'when references matches a forwarded email' do
      before do
        conversation.messages.create!(
          source_id: 'forward/[email protected]',
          account_id: account.id,
          message_type: 'outgoing',
          inbox_id: email_channel.inbox.id,
          content: 'Forwarded message',
          content_attributes: { forwarded_message_id: 1 }
        )
        mail.to = '[email protected]'
        mail.references = 'forward/[email protected]'
      end

      it 'returns nil so the reply starts a new conversation' do
        strategy = described_class.new(mail)
        expect(strategy.find).to be_nil
      end
    end

    context 'when references has multiple values' do
      before do
        conversation.update!(uuid: '12345678-1234-1234-1234-123456789012')
        mail.to = '[email protected]'
        mail.references = [
          '[email protected]',
          'conversation/12345678-1234-1234-1234-123456789012/messages/[email protected]',
          '[email protected]'
        ]
      end

      it 'finds conversation from any reference' do
        strategy = described_class.new(mail)
        expect(strategy.find).to eq(conversation)
      end
    end

    context 'when references is blank' do
      it 'returns nil' do
        strategy = described_class.new(mail)
        expect(strategy.find).to be_nil
      end
    end

    context 'when references does not match any pattern or source_id' do
      before do
        mail.references = '[email protected]'
      end

      it 'returns nil' do
        strategy = described_class.new(mail)
        expect(strategy.find).to be_nil
      end
    end

    context 'with channel validation' do
      context 'when conversation belongs to the correct channel' do
        before do
          conversation.update!(uuid: '12345678-1234-1234-1234-123456789012')
          mail.to = '[email protected]'
          mail.references = 'conversation/12345678-1234-1234-1234-123456789012/messages/[email protected]'
        end

        it 'returns the conversation' do
          strategy = described_class.new(mail)
          expect(strategy.find).to eq(conversation)
        end
      end

      context 'when conversation belongs to a different channel' do
        let(:other_email_channel) { create(:channel_email, email: '[email protected]', account: account) }
        let(:other_conversation) do
          create(
            :conversation,
            inbox: other_email_channel.inbox,
            account: account
          )
        end

        before do
          other_conversation.update!(uuid: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')
          # Mail is addressed to [email protected] but references conversation from [email protected]
          mail.to = '[email protected]'
          mail.references = 'conversation/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/messages/[email protected]'
        end

        it 'returns nil (prevents cross-channel hijacking)' do
          strategy = described_class.new(mail)
          expect(strategy.find).to be_nil
        end
      end

      context 'when channel cannot be determined from mail' do
        before do
          conversation.update!(uuid: '12345678-1234-1234-1234-123456789012')
          mail.to = '[email protected]' # Email not associated with any channel
          mail.references = 'conversation/12345678-1234-1234-1234-123456789012/messages/[email protected]'
        end

        it 'returns nil' do
          strategy = described_class.new(mail)
          expect(strategy.find).to be_nil
        end
      end

      context 'when mail has multiple recipients including correct channel' do
        before do
          conversation.update!(uuid: '12345678-1234-1234-1234-123456789012')
          mail.to = ['[email protected]', '[email protected]']
          mail.references = 'conversation/12345678-1234-1234-1234-123456789012/messages/[email protected]'
        end

        it 'finds the correct channel and returns conversation' do
          strategy = described_class.new(mail)
          expect(strategy.find).to eq(conversation)
        end
      end
    end

    context 'when UUID exists but conversation does not' do
      before do
        mail.to = '[email protected]'
        mail.references = 'conversation/99999999-9999-9999-9999-999999999999/messages/[email protected]'
      end

      it 'returns nil' do
        strategy = described_class.new(mail)
        expect(strategy.find).to be_nil
      end
    end

    context 'with malformed references pattern' do
      before do
        mail.references = 'conversation/not-a-uuid/messages/[email protected]'
      end

      it 'returns nil' do
        strategy = described_class.new(mail)
        expect(strategy.find).to be_nil
      end
    end

    context 'when first reference fails channel validation but second succeeds' do
      let(:other_email_channel) { create(:channel_email, email: '[email protected]', account: account) }
      let(:other_conversation) do
        create(
          :conversation,
          inbox: other_email_channel.inbox,
          account: account
        )
      end

      before do
        conversation.update!(uuid: '12345678-1234-1234-1234-123456789012')
        other_conversation.update!(uuid: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')

        mail.to = '[email protected]'
        mail.references = [
          'conversation/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/messages/[email protected]', # Wrong channel
          'conversation/12345678-1234-1234-1234-123456789012/messages/[email protected]'  # Correct channel
        ]
      end

      it 'skips invalid reference and returns conversation from valid reference' do
        strategy = described_class.new(mail)
        expect(strategy.find).to eq(conversation)
      end
    end
  end
end