Skip to content

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

26.5 KB
Raw Blame
require 'rails_helper'

RSpec.describe Captain::Tools::HttpTool, type: :model do
  let(:account) { create(:account) }
  let(:assistant) { create(:captain_assistant, account: account) }
  let(:custom_tool) { create(:captain_custom_tool, account: account) }
  let(:tool) { Captain::Tools::PublicHttpTool.new(assistant, custom_tool) }
  let(:tool_context) { Struct.new(:state).new({}) }

  describe '#active?' do
    it 'returns true when custom tool is enabled' do
      custom_tool.update!(enabled: true)

      expect(tool.active?).to be true
    end

    it 'returns false when custom tool is disabled' do
      custom_tool.update!(enabled: false)

      expect(tool.active?).to be false
    end
  end

  describe '#available_in_reply_suggestion?' do
    it 'allows GET tools' do
      custom_tool.update!(http_method: 'GET')

      expect(tool.available_in_reply_suggestion?).to be true
    end

    it 'rejects tools that change data' do
      %w[POST PUT PATCH DELETE].each do |http_method|
        custom_tool.update!(http_method: http_method)

        expect(tool.available_in_reply_suggestion?).to be(false), "expected #{http_method} to be rejected"
      end
    end
  end

  describe '#perform' do
    context 'with GET request' do
      before do
        custom_tool.update!(
          http_method: 'GET',
          endpoint_url: 'https://example.com/orders/123',
          response_template: nil
        )
        stub_request(:get, 'https://example.com/orders/123')
          .to_return(status: 200, body: '{"status": "success"}')
      end

      it 'executes GET request and returns response body' do
        result = tool.perform(tool_context)

        expect(result).to eq('{"status": "success"}')
        expect(WebMock).to have_requested(:get, 'https://example.com/orders/123')
      end
    end

    context 'with POST request' do
      before do
        custom_tool.update!(
          http_method: 'POST',
          endpoint_url: 'https://example.com/orders',
          request_template: '{"order_id": "{{ order_id }}"}',
          response_template: nil
        )
        stub_request(:post, 'https://example.com/orders')
          .with(body: '{"order_id": "123"}', headers: { 'Content-Type' => 'application/json' })
          .to_return(status: 200, body: '{"created": true}')
      end

      it 'executes POST request with rendered body' do
        result = tool.perform(tool_context, order_id: '123')

        expect(result).to eq('{"created": true}')
        expect(WebMock).to have_requested(:post, 'https://example.com/orders')
          .with(body: '{"order_id": "123"}')
      end
    end

    context 'with PUT and PATCH requests' do
      it 'sends the rendered body as JSON' do
        %w[PUT PATCH].each do |http_method|
          custom_tool.update!(
            http_method: http_method,
            endpoint_url: 'https://example.com/orders/123',
            request_template: '{"status": "{{ status }}"}',
            response_template: nil
          )
          stub_request(http_method.downcase.to_sym, 'https://example.com/orders/123').to_return(status: 200, body: '{"updated": true}')

          result = tool.perform(tool_context, status: 'shipped')

          expect(result).to eq('{"updated": true}')
          expect(WebMock).to have_requested(http_method.downcase.to_sym, 'https://example.com/orders/123')
            .with(body: '{"status": "shipped"}', headers: { 'Content-Type' => 'application/json' })
        end
      end
    end

    context 'with DELETE request' do
      it 'sends the request without a body when there is no request template' do
        custom_tool.update!(http_method: 'DELETE', endpoint_url: 'https://example.com/orders/123', response_template: nil)
        sent_request = nil
        stub_request(:delete, 'https://example.com/orders/123')
          .with { |request| sent_request = request }
          .to_return(status: 204, body: '')

        tool.perform(tool_context)

        expect(sent_request.body).to be_blank
        expect(sent_request.headers).not_to have_key('Content-Type')
      end
    end

    context 'with template variables in URL' do
      before do
        custom_tool.update!(
          endpoint_url: 'https://example.com/orders/{{ order_id }}',
          response_template: nil
        )
        stub_request(:get, 'https://example.com/orders/456')
          .to_return(status: 200, body: '{"order_id": "456"}')
      end

      it 'renders URL template with params' do
        result = tool.perform(tool_context, order_id: '456')

        expect(result).to eq('{"order_id": "456"}')
        expect(WebMock).to have_requested(:get, 'https://example.com/orders/456')
      end
    end

    context 'with bearer token authentication' do
      before do
        custom_tool.update!(
          auth_type: 'bearer',
          auth_config: { 'token' => 'secret_bearer_token' },
          endpoint_url: 'https://example.com/data',
          response_template: nil
        )
        stub_request(:get, 'https://example.com/data')
          .with(headers: { 'Authorization' => 'Bearer secret_bearer_token' })
          .to_return(status: 200, body: '{"authenticated": true}')
      end

      it 'adds Authorization header with bearer token' do
        result = tool.perform(tool_context)

        expect(result).to eq('{"authenticated": true}')
        expect(WebMock).to have_requested(:get, 'https://example.com/data')
          .with(headers: { 'Authorization' => 'Bearer secret_bearer_token' })
      end
    end

    context 'with basic authentication' do
      before do
        custom_tool.update!(
          auth_type: 'basic',
          auth_config: { 'username' => 'user123', 'password' => 'pass456' },
          endpoint_url: 'https://example.com/data',
          response_template: nil
        )
        stub_request(:get, 'https://example.com/data')
          .with(basic_auth: %w[user123 pass456])
          .to_return(status: 200, body: '{"authenticated": true}')
      end

      it 'adds basic auth credentials' do
        result = tool.perform(tool_context)

        expect(result).to eq('{"authenticated": true}')
        expect(WebMock).to have_requested(:get, 'https://example.com/data')
          .with(basic_auth: %w[user123 pass456])
      end
    end

    context 'with API key authentication' do
      before do
        custom_tool.update!(
          auth_type: 'api_key',
          auth_config: { 'key' => 'api_key_123', 'name' => 'X-API-Key' },
          endpoint_url: 'https://example.com/data',
          response_template: nil
        )
        stub_request(:get, 'https://example.com/data')
          .with(headers: { 'X-API-Key' => 'api_key_123' })
          .to_return(status: 200, body: '{"authenticated": true}')
      end

      it 'adds API key header' do
        result = tool.perform(tool_context)

        expect(result).to eq('{"authenticated": true}')
        expect(WebMock).to have_requested(:get, 'https://example.com/data')
          .with(headers: { 'X-API-Key' => 'api_key_123' })
      end

      it 'strips the API key header on cross-origin redirects' do
        redirect_url = 'http://example.com/data'
        redirected_headers = nil
        stub_request(:get, 'https://example.com/data').to_return(status: 302, headers: { 'Location' => redirect_url })
        stub_request(:get, redirect_url)
          .with do |request|
            redirected_headers = request.headers.transform_keys(&:downcase)
            true
          end
          .to_return(status: 200, body: '{"authenticated": false}')

        tool.perform(tool_context)

        expect(redirected_headers).not_to include('x-api-key')
      end
    end

    context 'with custom headers' do
      before do
        custom_tool.update!(
          auth_type: 'bearer',
          auth_config: { 'token' => 'secret_bearer_token' },
          headers: { 'Accept' => 'application/vnd.api+json', 'cal-api-version' => '2024-08-13' },
          endpoint_url: 'https://example.com/data',
          response_template: nil
        )
      end

      it 'sends the headers alongside authentication' do
        stub_request(:get, 'https://example.com/data').to_return(status: 200, body: '{"ok": true}')

        tool.perform(tool_context, order_id: '123')

        expect(WebMock).to have_requested(:get, 'https://example.com/data')
          .with(headers: { 'Accept' => 'application/vnd.api+json', 'cal-api-version' => '2024-08-13',
                           'Authorization' => 'Bearer secret_bearer_token' })
      end

      it 'strips the headers on cross-origin redirects' do
        redirect_url = 'http://example.com/data'
        redirected_headers = nil
        stub_request(:get, 'https://example.com/data').to_return(status: 302, headers: { 'Location' => redirect_url })
        stub_request(:get, redirect_url)
          .with do |request|
            redirected_headers = request.headers.transform_keys(&:downcase)
            true
          end
          .to_return(status: 200, body: '{"ok": false}')

        tool.perform(tool_context)

        expect(redirected_headers).not_to include('cal-api-version')
      end
    end

    context 'when combining every header source' do
      let(:conversation) { create(:conversation, account: account) }
      let(:state) do
        {
          account_id: account.id,
          assistant_id: assistant.id,
          conversation: { id: conversation.id, display_id: conversation.display_id },
          contact_inbox: { id: conversation.contact_inbox.id, hmac_verified: true }
        }
      end
      let(:sent_request) { {} }

      before do
        custom_tool.update!(
          http_method: 'POST',
          endpoint_url: 'https://example.com/orders/{{ order_id }}',
          request_template: '{"order_id": "{{ order_id }}"}',
          auth_type: 'api_key',
          auth_config: { 'name' => 'X-API-Key', 'key' => 'secret' },
          headers: { 'cal-api-version' => '2024-08-13', 'Accept' => 'application/json' },
          response_template: nil
        )
        stub_request(:post, 'https://example.com/orders/42')
          .with do |request|
            sent_request.merge!(headers: request.headers, body: request.body)
            true
          end
          .to_return(status: 200, body: '{"ok": true}')
      end

      it 'sends exactly the expected headers and body' do
        tool.perform(Struct.new(:state).new(state), order_id: '42')

        expect(sent_request[:body]).to eq('{"order_id": "42"}')
        expect(sent_request[:headers]).to eq(
          'Accept' => 'application/json',
          'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
          'User-Agent' => 'Ruby',
          'Host' => 'example.com',
          'Cal-Api-Version' => '2024-08-13',
          'X-Api-Key' => 'secret',
          'X-Chatwoot-Account-Id' => account.id.to_s,
          'X-Chatwoot-Assistant-Id' => assistant.id.to_s,
          'X-Chatwoot-Tool-Slug' => custom_tool.slug,
          'X-Chatwoot-Conversation-Id' => conversation.id.to_s,
          'X-Chatwoot-Conversation-Display-Id' => conversation.display_id.to_s,
          'X-Chatwoot-Contact-Inbox-Id' => conversation.contact_inbox.id.to_s,
          'X-Chatwoot-Contact-Inbox-Verified' => 'true',
          'Content-Type' => 'application/json'
        )
      end

      it 'uses the API key when a custom header has the same name in a different case' do
        custom_tool.update!(headers: { 'x-api-key' => 'custom' })

        tool.perform(Struct.new(:state).new(state), order_id: '42')

        expect(sent_request[:headers].select { |name, _| name.casecmp?('x-api-key') }).to eq('X-Api-Key' => 'secret')
      end
    end

    context 'with response template' do
      before do
        custom_tool.update!(
          endpoint_url: 'https://example.com/orders/123',
          response_template: 'Order status: {{ response.status }}, ID: {{ response.order_id }}'
        )
        stub_request(:get, 'https://example.com/orders/123')
          .to_return(status: 200, body: '{"status": "shipped", "order_id": "123"}')
      end

      it 'formats response using template' do
        result = tool.perform(tool_context)

        expect(result).to eq('Order status: shipped, ID: 123')
      end
    end

    context 'when handling errors' do
      it 'refuses to send a request whose rendered URL is not HTTPS' do
        custom_tool.update!(endpoint_url: 'http{% unless secure %}s{% endunless %}://example.com/data', auth_type: 'bearer',
                            auth_config: { 'token' => 'secret' })
        stub_request(:any, /example\.com/)

        result = tool.perform(tool_context, secure: true)

        expect(result).to eq('An error occurred while executing the request')
        expect(WebMock).not_to have_requested(:any, /example\.com/)
      end

      it 'returns generic error message on network failure' do
        custom_tool.update!(endpoint_url: 'https://example.com/data')
        stub_request(:get, 'https://example.com/data').to_raise(SocketError.new('Failed to connect'))

        result = tool.perform(tool_context)

        expect(result).to eq('An error occurred while executing the request')
      end

      it 'marks playground failures for run details without changing production output' do
        custom_tool.update!(endpoint_url: 'https://example.com/data')
        stub_request(:get, 'https://example.com/data').to_raise(SocketError.new('Failed to connect'))
        tool_context.state[:source] = 'playground'

        result = tool.perform(tool_context)

        expect(result).to eq('ERROR: An error occurred while executing the request')
      end

      it 'returns generic error message on timeout' do
        custom_tool.update!(endpoint_url: 'https://example.com/data')
        stub_request(:get, 'https://example.com/data').to_timeout

        result = tool.perform(tool_context)

        expect(result).to eq('An error occurred while executing the request')
      end

      it 'returns generic error message on HTTP 404' do
        custom_tool.update!(endpoint_url: 'https://example.com/data')
        stub_request(:get, 'https://example.com/data').to_return(status: 404, body: 'Not found')

        result = tool.perform(tool_context)

        expect(result).to eq('An error occurred while executing the request')
      end

      it 'returns generic error message on HTTP 500' do
        custom_tool.update!(endpoint_url: 'https://example.com/data')
        stub_request(:get, 'https://example.com/data').to_return(status: 500, body: 'Server error')

        result = tool.perform(tool_context)

        expect(result).to eq('An error occurred while executing the request')
      end

      it 'logs error details' do
        custom_tool.update!(endpoint_url: 'https://example.com/data')
        stub_request(:get, 'https://example.com/data').to_raise(StandardError.new('Test error'))

        expect(Rails.logger).to receive(:error).with(/HttpTool execution error.*Test error/)

        tool.perform(tool_context)
      end
    end

    context 'when integrating with Toolable methods' do
      it 'correctly integrates URL rendering, body rendering, auth, and response formatting' do
        custom_tool.update!(
          http_method: 'POST',
          endpoint_url: 'https://example.com/users/{{ user_id }}/orders',
          request_template: '{"product": "{{ product }}", "quantity": {{ quantity }}}',
          auth_type: 'bearer',
          auth_config: { 'token' => 'integration_token' },
          response_template: 'Created order #{{ response.order_number }} for {{ response.product }}'
        )

        stub_request(:post, 'https://example.com/users/42/orders')
          .with(
            body: '{"product": "Widget", "quantity": 5}',
            headers: {
              'Authorization' => 'Bearer integration_token',
              'Content-Type' => 'application/json'
            }
          )
          .to_return(status: 200, body: '{"order_number": "ORD-789", "product": "Widget"}')

        result = tool.perform(tool_context, user_id: '42', product: 'Widget', quantity: 5)

        expect(result).to eq('Created order #ORD-789 for Widget')
      end
    end

    context 'with metadata headers' do
      let(:conversation) { create(:conversation, account: account) }
      let(:contact) { conversation.contact }
      let(:tool_context_with_state) do
        Struct.new(:state).new({
                                 account_id: account.id,
                                 assistant_id: assistant.id,
                                 conversation: {
                                   id: conversation.id,
                                   display_id: conversation.display_id
                                 },
                                 contact_inbox: {
                                   id: conversation.contact_inbox.id,
                                   hmac_verified: conversation.contact_inbox.hmac_verified
                                 },
                                 contact: {
                                   id: contact.id,
                                   email: contact.email,
                                   phone_number: contact.phone_number
                                 }
                               })
      end

      before do
        custom_tool.update!(
          endpoint_url: 'https://example.com/api/data',
          response_template: nil
        )
      end

      it 'sends the email the customer verified with a one-time code, next to the contact email' do
        allow(Captain::EmailVerification).to receive(:new).with(conversation)
                                                          .and_return(instance_double(Captain::EmailVerification, verified_email: '[email protected]'))
        stub_request(:get, 'https://example.com/api/data').to_return(status: 200, body: '{}')

        tool.perform(tool_context_with_state)

        expect(WebMock).to(have_requested(:get, 'https://example.com/api/data').with do |request|
          request.headers['X-Chatwoot-Verified-Email'] == '[email protected]' && request.headers['X-Chatwoot-Contact-Email'] == contact.email
        end)
      end

      it 'strips the contact and verified email headers on cross-origin redirects' do
        contact.update!(email: '[email protected]', phone_number: '+14155550100')
        tool_context_with_state.state[:contact].merge!(email: contact.email, phone_number: contact.phone_number)
        allow(Captain::EmailVerification).to receive(:new).with(conversation)
                                                          .and_return(instance_double(Captain::EmailVerification, verified_email: '[email protected]'))
        redirected_headers = nil
        stub_request(:get, 'https://example.com/api/data').to_return(status: 302, headers: { 'Location' => 'http://example.com/data' })
        stub_request(:get, 'http://example.com/data').with do |request|
          redirected_headers = request.headers.transform_keys(&:downcase)
          true
        end

        tool.perform(tool_context_with_state)

        expect(redirected_headers.keys).not_to include('x-chatwoot-verified-email', 'x-chatwoot-contact-email', 'x-chatwoot-contact-phone')
      end

      it 'leaves out the verified email header when the customer has not verified an email' do
        stub_request(:get, 'https://example.com/api/data').to_return(status: 200, body: '{}')

        tool.perform(tool_context_with_state)

        expect(WebMock).to(have_requested(:get, 'https://example.com/api/data').with { |request| !request.headers.key?('X-Chatwoot-Verified-Email') })
      end

      it 'includes metadata headers in GET request' do
        stub_request(:get, 'https://example.com/api/data')
          .with(headers: {
                  'X-Chatwoot-Account-Id' => account.id.to_s,
                  'X-Chatwoot-Assistant-Id' => assistant.id.to_s,
                  'X-Chatwoot-Tool-Slug' => custom_tool.slug,
                  'X-Chatwoot-Conversation-Id' => conversation.id.to_s,
                  'X-Chatwoot-Conversation-Display-Id' => conversation.display_id.to_s,
                  'X-Chatwoot-Contact-Inbox-Id' => conversation.contact_inbox.id.to_s,
                  'X-Chatwoot-Contact-Inbox-Verified' => conversation.contact_inbox.hmac_verified.to_s,
                  'X-Chatwoot-Contact-Id' => contact.id.to_s,
                  'X-Chatwoot-Contact-Email' => contact.email
                })
          .to_return(status: 200, body: '{"success": true}')

        tool.perform(tool_context_with_state)

        expect(WebMock).to have_requested(:get, 'https://example.com/api/data')
          .with(headers: {
                  'X-Chatwoot-Account-Id' => account.id.to_s,
                  'X-Chatwoot-Contact-Inbox-Verified' => conversation.contact_inbox.hmac_verified.to_s,
                  'X-Chatwoot-Contact-Email' => contact.email
                })
      end

      it 'includes metadata headers in POST request' do
        custom_tool.update!(http_method: 'POST', request_template: '{"data": "test"}')

        stub_request(:post, 'https://example.com/api/data')
          .with(
            body: '{"data": "test"}',
            headers: {
              'Content-Type' => 'application/json',
              'X-Chatwoot-Account-Id' => account.id.to_s,
              'X-Chatwoot-Tool-Slug' => custom_tool.slug,
              'X-Chatwoot-Contact-Inbox-Verified' => conversation.contact_inbox.hmac_verified.to_s,
              'X-Chatwoot-Contact-Email' => contact.email
            }
          )
          .to_return(status: 200, body: '{"success": true}')

        tool.perform(tool_context_with_state)

        expect(WebMock).to have_requested(:post, 'https://example.com/api/data')
      end

      it 'includes metadata headers along with authentication headers' do
        custom_tool.update!(
          auth_type: 'bearer',
          auth_config: { 'token' => 'test_token' }
        )

        stub_request(:get, 'https://example.com/api/data')
          .with(headers: {
                  'Authorization' => 'Bearer test_token',
                  'X-Chatwoot-Account-Id' => account.id.to_s,
                  'X-Chatwoot-Contact-Inbox-Verified' => conversation.contact_inbox.hmac_verified.to_s,
                  'X-Chatwoot-Contact-Id' => contact.id.to_s
                })
          .to_return(status: 200, body: '{"success": true}')

        tool.perform(tool_context_with_state)

        expect(WebMock).to have_requested(:get, 'https://example.com/api/data')
          .with(headers: {
                  'Authorization' => 'Bearer test_token',
                  'X-Chatwoot-Contact-Id' => contact.id.to_s
                })
      end

      it 'handles missing contact in tool context' do
        tool_context_no_contact = Struct.new(:state).new({
                                                           account_id: account.id,
                                                           assistant_id: assistant.id,
                                                           conversation: {
                                                             id: conversation.id,
                                                             display_id: conversation.display_id
                                                           },
                                                           contact_inbox: {
                                                             id: conversation.contact_inbox.id,
                                                             hmac_verified: conversation.contact_inbox.hmac_verified
                                                           }
                                                         })

        stub_request(:get, 'https://example.com/api/data')
          .with(headers: {
                  'X-Chatwoot-Account-Id' => account.id.to_s,
                  'X-Chatwoot-Conversation-Id' => conversation.id.to_s,
                  'X-Chatwoot-Contact-Inbox-Verified' => conversation.contact_inbox.hmac_verified.to_s
                })
          .to_return(status: 200, body: '{"success": true}')

        tool.perform(tool_context_no_contact)

        expect(WebMock).to have_requested(:get, 'https://example.com/api/data')
      end

      it 'defaults contact inbox verified header to false when contact inbox is missing' do
        tool_context_without_contact_inbox = Struct.new(:state).new({
                                                                      account_id: account.id,
                                                                      assistant_id: assistant.id,
                                                                      conversation: {
                                                                        id: conversation.id,
                                                                        display_id: conversation.display_id
                                                                      },
                                                                      contact: {
                                                                        id: contact.id,
                                                                        email: contact.email
                                                                      }
                                                                    })

        stub_request(:get, 'https://example.com/api/data')
          .with(headers: {
                  'X-Chatwoot-Contact-Inbox-Verified' => 'false'
                })
          .to_return(status: 200, body: '{"success": true}')

        tool.perform(tool_context_without_contact_inbox)

        expect(WebMock).to have_requested(:get, 'https://example.com/api/data')
          .with(headers: { 'X-Chatwoot-Contact-Inbox-Verified' => 'false' })
      end

      it 'includes contact phone when present' do
        contact.update!(phone_number: '+1234567890')
        tool_context_with_state.state[:contact][:phone_number] = '+1234567890'

        stub_request(:get, 'https://example.com/api/data')
          .with(headers: {
                  'X-Chatwoot-Contact-Phone' => '+1234567890'
                })
          .to_return(status: 200, body: '{"success": true}')

        tool.perform(tool_context_with_state)

        expect(WebMock).to have_requested(:get, 'https://example.com/api/data')
          .with(headers: { 'X-Chatwoot-Contact-Phone' => '+1234567890' })
      end

      it 'includes unverified contact inbox status explicitly as false' do
        conversation.contact_inbox.update!(hmac_verified: false)
        tool_context_with_state.state[:contact_inbox][:hmac_verified] = false

        stub_request(:get, 'https://example.com/api/data')
          .with(headers: {
                  'X-Chatwoot-Contact-Inbox-Verified' => 'false'
                })
          .to_return(status: 200, body: '{"success": true}')

        tool.perform(tool_context_with_state)

        expect(WebMock).to have_requested(:get, 'https://example.com/api/data')
          .with(headers: { 'X-Chatwoot-Contact-Inbox-Verified' => 'false' })
      end
    end
  end
end