<commit_before>RSpec.describe FinApps::REST::Connection do

  let(:dummy_class) { Class.new { extend FinApps::REST::Connection } }
  let(:valid_credentials) { {:company_identifier => 'id', :company_token => 'token'} }

  it { expect(dummy_class).to respond_to(:set_up_connection) }

  describe '#set_up_connection' do

    context 'when using valid company credentials' do
      subject(:set_up_connection) { dummy_class.set_up_connection(valid_credentials, {}) }
      it { expect(set_up_connection).to be_an_instance_of(Faraday::Connection) }
      it { expect(set_up_connection.headers).to include({'Accept' => FinApps::REST::Defaults::HEADERS[:accept]}) }
      it { expect(set_up_connection.headers).to include({'User-Agent' => FinApps::REST::Defaults::HEADERS[:user_agent]}) }
      context 'when NOT providing user credentials' do
        it { expect(set_up_connection.builder.handlers).not_to include(Faraday::Request::BasicAuthentication) }
      end
    end

    context 'when using valid user credentials' do
      user_credentials = {:user_identifier => 'user_id', :user_token => '<PASSWORD>'}
      subject(:set_up_connection) { dummy_class.set_up_connection(valid_credentials, user_credentials) }

      it { expect(set_up_connection.builder.handlers).to include(Faraday::Request::BasicAuthentication) }
      it 'sets a proper authorization header' do
        stub_request(:get, "#{FinApps::REST::Defaults::DEFAULTS[:host]}/auth-echo").
            to_return(:status => 200,
                      :body => '',
                      :headers => {})

        response = set_up_connection.get('/auth-echo')
        expect(response.env.request_headers).to include({:Authorization => 'Basic dXNlcl9pZDp1c2VyX3Rva2Vu'})
      end
    end

    context 'using an invalid host_url parameter' do
      subject(:set_up_connection) { dummy_class.set_up_connection(valid_credentials, {:host => 'yahoo.com'}) }
      it { expect { set_up_connection }.to raise_error(FinApps::REST::InvalidArgumentsError) }
    end

  end
end<commit_msg>test for auth<commit_after>require 'base64'

RSpec.describe FinApps::REST::Connection do

  let(:dummy_class) { Class.new { extend FinApps::REST::Connection } }
  let(:valid_credentials) { {:company_identifier => 'id', :company_token => 'token'} }
  let(:user_credentials) { {:user_identifier => 'user_id', :user_token => '<PASSWORD>'} }

  it { expect(dummy_class).to respond_to(:set_up_connection) }

  describe '#set_up_connection' do

    context 'when using valid company credentials' do
      subject(:set_up_connection) { dummy_class.set_up_connection(valid_credentials, {}) }
      it { expect(set_up_connection).to be_an_instance_of(Faraday::Connection) }
      it { expect(set_up_connection.headers).to include({'Accept' => FinApps::REST::Defaults::HEADERS[:accept]}) }
      it { expect(set_up_connection.headers).to include({'User-Agent' => FinApps::REST::Defaults::HEADERS[:user_agent]}) }
      context 'when NOT providing user credentials' do
        it { expect(set_up_connection.builder.handlers).not_to include(Faraday::Request::BasicAuthentication) }
      end
    end

    context 'when using valid user credentials' do
      subject(:set_up_connection) { dummy_class.set_up_connection(valid_credentials, user_credentials) }
      let(:header) { Faraday::Request::BasicAuthentication.header(user_credentials[:user_identifier], user_credentials[:user_token]) }

      it { expect(set_up_connection.builder.handlers).to include(Faraday::Request::BasicAuthentication) }
      it 'sets a proper authorization header' do
        stub_request(:get, "#{FinApps::REST::Defaults::DEFAULTS[:host]}/auth-echo").to_return(:status => 200)
        expect(set_up_connection.get('/auth-echo').env.request_headers).to include({:authorization => header})
      end
    end

    context 'using an invalid host_url parameter' do
      subject(:set_up_connection) { dummy_class.set_up_connection(valid_credentials, {:host => 'yahoo.com'}) }
      it { expect { set_up_connection }.to raise_error(FinApps::REST::InvalidArgumentsError) }
    end

  end
end
{'fraction_non_alphanumeric': 0.13202933985330073, 'fraction_numerical': 0.003178484107579462, 'mean_word_length': 4.721678321678322, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1978742', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1224, 'n_words': 216}