require "spec_helper"

describe "Harvest time entries" do
  let(:client) { Clockpuncher.client "harvest", username: "pooh", password: "bah", subdomain: "punch" }

  describe "fetching all" do
    subject { client.time_entries.all project_id: 1, from: Date.parse("2011-12-15"), to: Date.parse("2011-12-16") }

    before do
      stub_request(:get, "https://pooh:bah@punch.harvestapp.com/projects/1/entries").
        with(query: { from: "20111215", to: "20111216" })
    end

    it "should generate the correct API request" do
      subject

      a_request(:get, "https://pooh:bah@punch.harvestapp.com/projects/1/entries").
        with(
          query: { from: "20111215", to: "20111216" },
          headers: {
            "Accept" => "application/xml",
            "Content-Type" => "application/xml; charset=utf-8",
            "User-Agent" => "Clockpuncher/#{Clockpuncher::VERSION}"
          }
        ).
        should have_been_made.once
    end
  end

  describe "fetching one" do
    subject { client.time_entries.find 1 }

    before do
      stub_request(:get, "https://pooh:bah@punch.harvestapp.com/daily/show/1")
    end

    it "should generate the correct API request" do
      subject

      a_request(:get, "https://pooh:bah@punch.harvestapp.com/daily/show/1").
        with(
          headers: {
            "Accept" => "application/xml",
            "Content-Type" => "application/xml; charset=utf-8",
            "User-Agent" => "Clockpuncher/#{Clockpuncher::VERSION}"
          }
        ).
        should have_been_made.once
    end
  end
end
