<commit_before>require 'helper'

describe ProbeDockRSpec::Server do
  let(:api_token){ '<KEY>' }
  let :options do
    {
      name: 'A server',
      api_url: 'http://example.com/api',
      api_token: api_token,
      project_api_id: '0000000000'
    }
  end
  let(:server){ ProbeDockRSpec::Server.new options }
  subject{ server }

  it "should set its attributes" do
    expect(options.keys.inject({}){ |memo,k| memo[k] = subject.send(k); memo }).to eq(options)
  end

  describe "#upload" do
    let(:payload){ { 'foo' => 'bar' } }
    let(:http_responses){ [] }

    before :each do
      ENV.delete_if{ |k,v| k.match(/\APROBE_DOCK_/) }
      allow(HTTParty).to receive(:get){ http_responses.shift }
      allow(HTTParty).to receive(:post){ http_responses.shift }
    end

    describe "when everything works" do
      let(:payload_response){ double code: 202 }
      let(:http_responses){ [ payload_response ] }
      let(:authentication_headers){ { 'Authorization' => "Bearer #{api_token}" } }
      let(:payload_headers){ { 'Content-Type' => 'application/vnd.probe-dock.payload.v1+json' } }

      it "should not raise an error" do
        expect_upload.not_to raise_error
      end

      it "should POST the payload" do
        headers = payload_headers.merge authentication_headers
        expect(HTTParty).to receive(:post).once.with('http://example.com/api/publish', body: Oj.dump(payload, mode: :strict), headers: headers)
        subject.upload payload
      end
    end

    describe "when the payload is not accepted" do
      let(:payload_response){ double code: 401 }
      let(:http_responses){ [ payload_response ] }

      it "should raise an error" do
        expect_upload.to raise_server_error(/expected http 202 accepted/i, res: payload_response)
      end
    end

    describe "without an api url" do
      let(:options){ super().delete_if{ |k,v| k == :api_url } }
      it "should raise an error" do
        expect_upload.to raise_server_error(/missing/, /apiUrl/)
      end
    end

    describe "without an api token" do
      let(:options){ super().delete_if{ |k,v| k == :api_token } }
      it "should raise an error" do
        expect_upload.to raise_server_error(/missing/, /apiToken/)
      end
    end

    describe "without a project api id" do
      let(:options){ super().delete_if{ |k,v| k == :project_api_id } }
      it "should raise an error" do
        expect_upload.to raise_server_error(/missing/, /projectApiId/)
      end
    end
  end

  def expect_upload
    expect{ subject.upload payload }
  end<commit_msg>Added missing server specs.<commit_after>require 'helper'

describe ProbeDockRSpec::Server do
  let(:api_token){ '<KEY>' }
  let :options do
    {
      name: 'A server',
      api_url: 'http://example.com/api',
      api_token: api_token,
      project_api_id: '0000000000'
    }
  end
  let(:server){ ProbeDockRSpec::Server.new options }
  subject{ server }

  it "should set its attributes" do
    expect(options.keys.inject({}){ |memo,k| memo[k] = subject.send(k); memo }).to eq(options)
  end

  it "should have setters" do

    new_options = {
      name: 'Another server',
      api_url: 'http://example.com/api2',
      api_token: api_token.reverse,
      project_api_id: '111111111111'
    }

    new_options.each_pair{ |k,v| server.send "#{k}=".to_sym, v }
    expect(new_options.keys.inject({}){ |memo,k| memo[k] = subject.send(k); memo }).to eq(new_options)
  end

  describe "#clear" do
    it "should clear the configuration" do
      server.clear
      options.keys.each{ |k| expect(subject.send(k)).to be_nil }
    end
  end

  describe "#upload" do
    let(:payload){ { 'foo' => 'bar' } }
    let(:http_responses){ [] }

    before :each do
      ENV.delete_if{ |k,v| k.match(/\APROBE_DOCK_/) }
      allow(HTTParty).to receive(:get){ http_responses.shift }
      allow(HTTParty).to receive(:post){ http_responses.shift }
    end

    describe "when everything works" do
      let(:payload_response){ double code: 202 }
      let(:http_responses){ [ payload_response ] }
      let(:authentication_headers){ { 'Authorization' => "Bearer #{api_token}" } }
      let(:payload_headers){ { 'Content-Type' => 'application/vnd.probe-dock.payload.v1+json' } }

      it "should not raise an error" do
        expect_upload.not_to raise_error
      end

      it "should POST the payload" do
        headers = payload_headers.merge authentication_headers
        expect(HTTParty).to receive(:post).once.with('http://example.com/api/publish', body: Oj.dump(payload, mode: :strict), headers: headers)
        subject.upload payload
      end
    end

    describe "when the payload is not accepted" do
      let(:payload_response){ double code: 401 }
      let(:http_responses){ [ payload_response ] }

      it "should raise an error" do
        expect_upload.to raise_server_error(/expected http 202 accepted/i, res: payload_response)
      end
    end

    describe "without a name" do
      let(:options){ super().delete_if{ |k,v| k == :name } }
      it "should raise an error" do
        expect_upload.to raise_server_error(/missing/, /name/)
      end
    end

    describe "without an api url" do
      let(:options){ super().delete_if{ |k,v| k == :api_url } }
      it "should raise an error" do
        expect_upload.to raise_server_error(/missing/, /apiUrl/)
      end
    end

    describe "without an api token" do
      let(:options){ super().delete_if{ |k,v| k == :api_token } }
      it "should raise an error" do
        expect_upload.to raise_server_error(/missing/, /apiToken/)
      end
    end

    describe "without a project api id" do
      let(:options){ super().delete_if{ |k,v| k == :project_api_id } }
      it "should raise an error" do
        expect_upload.to raise_server_error(/missing/, /projectApiId/)
      end
    end
  end

  def expect_upload
    expect{ subject.upload payload }
  end
{'fraction_non_alphanumeric': 0.12850110337803428, 'fraction_numerical': 0.008996774741130538, 'mean_word_length': 2.9097544790975447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, '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': '615474', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1853, 'n_words': 489}