<commit_before>require 'spec_helper'

describe GithubClient do
  subject { described_class.new 'foo', 'bar' }

  describe 'its constructor' do
    it 'sets client data' do
      subject = described_class.new 'foo', 'bar'
      expect(subject.client_id).to eql 'foo'
      expect(subject.client_secret).to eql 'bar'
    end

    it 'sets token if desired' do
      subject = described_class.new nil, nil, 'baz'
      expect(subject.access_token).to eql 'baz'
    end
  end

  describe '#access_token=' do
    it 'sets a new token' do
      expect do
        subject.access_token = '<PASSWORD>'
      end.to change { subject.access_token }
    end
  end

  describe '#get_access_token!' do
    let(:code)         { "foobaz-#{rand(10000)}" }
    let(:access_token) { "foobar-#{rand(10000)}" }

    before do
      expect(RestClient).to receive(:post).
        with('https://github.com/login/oauth/access_token',
          {
            client_id: subject.client_id,
            client_secret: subject.client_secret,
            code: code
          },
          accept: :json
        ).and_return({ access_token: access_token }.to_json)
    end

    it 'returns retrieved token' do
      expect(subject.get_access_token! code).to eql access_token
    end

    it 'sets access_token' do
      expect do
        subject.get_access_token! code
      end.to change { subject.access_token }
    end
  end

  describe '#fetch' do
    before do
      subject.access_token = '<PASSWORD>'

      expect(RestClient).to receive(:get).
        with('http://oraclocat.local',
          {
            client_id: subject.client_id,
            client_secret: subject.client_secret
          },
          accept: :json,
          "Authorization" => "token #{subject.access_token}"

        ).and_return({ 'hello' => 'negative 1' }.to_json)

    end

    it 'makes the request without token' do
      result = subject.fetch('http://oraclocat.local')
      expect(result).to be_a Hash
    end
  end
end<commit_msg>Refactor out RestClient stubs<commit_after>require 'spec_helper'

describe GithubClient do
  subject { described_class.new 'foo', 'bar' }

  describe 'its constructor' do
    it 'sets client data' do
      subject = described_class.new 'foo', 'bar'
      expect(subject.client_id).to eql 'foo'
      expect(subject.client_secret).to eql 'bar'
    end

    it 'sets token if desired' do
      subject = described_class.new nil, nil, 'baz'
      expect(subject.access_token).to eql 'baz'
    end
  end

  describe '#access_token=' do
    it 'sets a new token' do
      expect do
        subject.access_token = '<PASSWORD>'
      end.to change { subject.access_token }
    end
  end

  describe '#get_access_token!' do
    let(:code)         { "foobaz-#{rand(10000)}" }
    let(:access_token) { "<PASSWORD>-#{rand(10000)}" }

    before do
      expect(RestClient).to receive(:post).
        with('https://github.com/login/oauth/access_token',
          {
            client_id: subject.client_id,
            client_secret: subject.client_secret,
            code: code
          },
          accept: :json
        ).and_return({ access_token: access_token }.to_json)
    end

    it 'returns retrieved token' do
      expect(subject.get_access_token! code).to eql access_token
    end

    it 'sets access_token' do
      expect do
        subject.get_access_token! code
      end.to change { subject.access_token }
    end
  end

  describe '#fetch' do
    it 'makes the request without token' do
      url                  = 'http://oraclocat.local'
      subject.access_token = '<PASSWORD>'
      stub_github_fetch(url) { { 'hello' => 'negative 1' } }

      result = subject.fetch(url)
      expect(result).to be_a Hash
    end
  end

  def stub_github_fetch(url)
    return_value = block_given? ? yield : {}
    expect(RestClient).to receive(:get).
      with(url,
        {
          client_id:     subject.client_id,
          client_secret: subject.client_secret
        },
        accept: :json,
        "Authorization" => "token #{subject.access_token}"
      ).and_return(return_value.to_json)
  end

end
{'fraction_non_alphanumeric': 0.11029948867786706, 'fraction_numerical': 0.005356708059410762, 'mean_word_length': 2.3589533932951756, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 2, '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': '3798528', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1257, 'n_words': 309}