Sha256: ea9edf7ed15285d540a798dadcde13b7a5fcd79174ee028fae8b1557a140893f

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

require 'config_helper'
require 'securerandom'

describe Elastic::AppSearch::Client do
  include_context 'App Search Credentials'
  let(:client) { Elastic::AppSearch::Client.new(client_options) }

  describe '#create_signed_search_key' do
    let(:key) { 'private-xxxxxxxxxxxxxxxxxxxx' }
    let(:api_key_name) { 'private-key' }
    let(:enforced_options) do
      {
        'query' => 'cat'
      }
    end

    subject do
      Elastic::AppSearch::Client.create_signed_search_key(key, api_key_name, enforced_options)
    end

    it 'should build a valid jwt' do
      decoded_token = JWT.decode(subject, key, true, :algorithm => 'HS256')
      expect(decoded_token[0]['api_key_name']).to(eq(api_key_name))
      expect(decoded_token[0]['query']).to(eq('cat'))
    end
  end

  describe 'Requests' do
    it 'should include client name and version in headers' do
      stub_request(:any, "#{client_options[:host_identifier]}.api.swiftype.com/api/as/v1/engines")
      client.list_engines
      expect(WebMock).to(
        have_requested(:get, "https://#{client_options[:host_identifier]}.api.swiftype.com/api/as/v1/engines")
        .with(
          :headers => {
            'X-Swiftype-Client' => 'elastic-app-search-ruby',
            'X-Swiftype-Client-Version' => Elastic::AppSearch::VERSION
          }
        )
      )
    end
  end

  context 'Configuration' do
    context 'host_identifier' do
      it 'sets the base url correctly' do
        client = Elastic::AppSearch::Client.new(:host_identifier => 'host-asdf', :api_key => 'foo')
        expect(client.api_endpoint).to(eq('https://host-asdf.api.swiftype.com/api/as/v1/'))
      end

      it 'sets the base url correctly using deprecated as_host_key' do
        client = Elastic::AppSearch::Client.new(:account_host_key => 'host-asdf', :api_key => 'foo')
        expect(client.api_endpoint).to(eq('https://host-asdf.api.swiftype.com/api/as/v1/'))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
elastic-app-search-7.6.0 spec/client_spec.rb
elastic-app-search-7.5.0 spec/client_spec.rb
elastic-app-search-7.4.1 spec/client_spec.rb