require 'spec_helper'

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

  include Rack::Test::Methods


  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }

    before(:each) do
      post '/sessions', "session[email]" => user.email, "session[password]" => user.password
      rack_mock_session.cookie_jar["remember_token"].should == user.remember_token
    end

    it "empty search" do
      get '/api/search.json'
      last_response.status.should == 200
      JSON.parse(last_response.body).should eql(JSON.parse('{}'))
    end

    it "simple search" do
      @musician = FactoryGirl.create(:user, first_name: "Peach", last_name: "Nothing", email: "user@example.com", musician: true)
      @fan = FactoryGirl.create(:user, first_name: "Peach Peach", last_name: "Grovery", email: "fan@example.com", musician: false)
      @band = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", ["hip hop"], user.id, nil, nil)
      @band2 = Band.save(nil, "Peach", "www.bands2.com", "zomg we rock", "Apex", "NC", "USA", ["hip hop"], user.id, nil, nil)
      
      get '/api/search.json?query=peach'
      last_response.status.should == 200
      response = JSON.parse(last_response.body)

      response["musicians"].length.should == 1
      musician = response["musicians"][0]
      musician["id"].should == @musician.id

      response["fans"].length.should == 1
      fan = response["fans"][0]
      fan["id"].should == @fan.id

      response["bands"].length.should == 2
      bands = response["bands"]
      bands = [bands[0]["id"], bands[1]["id"]]
      bands.should include(@band.id)
      bands.should include(@band2.id)
      
      response["recordings"].should == nil
    end
  end
end
