require 'spec_helper'

describe "Artifact API ", :type => :api do

  include Rack::Test::Methods

  subject { page }

  describe "versioncheck" do
    before do
      @artifact = FactoryGirl.create(:artifact_update)
    end

    it "matches an artifact" do
      get '/api/versioncheck.json', { :os=>'Win32', :product=>'JamClient'}
      last_response.status.should eql(200)
      JSON.parse(last_response.body).should eql({ "version" => @artifact.version, "uri" => Rails.application.config.jam_admin_root_url +  @artifact.uri.url, "size" => @artifact.size, "sha1" => @artifact.sha1})
    end

    it "matches no artifact" do
      @artifact.delete
      get '/api/versioncheck.json', { :os=>'Win32', :product=>'JamClient'}
      last_response.status.should eql(200)
      JSON.parse(last_response.body).should eql({})
    end

     it "fails on bad product" do
      @artifact.delete
      get '/api/versioncheck.json', { :os=>'what', :product=>'JamClient'}
      last_response.status.should eql(422)
      JSON.parse(last_response.body).should eql({ "errors" => {"product" => ['not a valid product']}})
    end
  end

  describe "client_downloads" do

    before do
      @win_client = FactoryGirl.create(:artifact_update, :product => 'JamClient/Win32')
      @mac_client= FactoryGirl.create(:artifact_update, :product => 'JamClient/MacOSX')

      get '/api/artifacts/clients.json'
      last_response.status.should eql(200)
      @response = JSON.parse(last_response.body)

    end

    it { @response.length.should == 2 }

    it { @response[@mac_client.product]["size"].should == @mac_client.size }
    it { @response[@mac_client.product]["uri"].should_not be_nil }

    it { @response["JamClient/Win32"]["size"].should == @win_client.size }
    it { @response["JamClient/Win32"]["uri"].should_not be_nil }
  end
end
