require 'spec_helper'

describe ApiFavoritesController, type: :controller do
  render_views

  let(:user) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }
  let(:band) { FactoryGirl.create(:band) }
  let(:music_session) { FactoryGirl.create(:active_music_session, creator: user) }
  let(:claimed_recording) { FactoryGirl.create(:claimed_recording) }

  before(:each) do
    RecordingLiker.delete_all
    ClaimedRecording.delete_all
    controller.current_user = nil
  end

  describe "index" do
    it "insists on login" do
      get :index
      response.status.should == 403
    end

    it "requires user param" do
      controller.current_user = user
      expect { get :index }.to raise_error "user must be specified"
    end

    it "can return nothing" do
      controller.current_user = user
      get :index, { user: user}

      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 0
      json[:since].should be_nil
    end

    it "returns one thing" do
      claimed_recording.touch
      like = FactoryGirl.create(:recording_like, user: user, claimed_recording: claimed_recording, recording: claimed_recording.recording, favorite: true)

      controller.current_user = user
      get :index,  { user: user}

      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1
      json[:since].should be_nil
    end
  end


  describe "update" do

    it "insists on login" do
      post :update, {id: '1'}
      response.status.should == 403
    end

    it "404 if no like for record" do
      controller.current_user = user
      post :update, {id: claimed_recording.id}
      response.status.should == 404
    end

    it "no favorite specified leaves record alone" do
      controller.current_user = user
      like = FactoryGirl.create(:recording_like, user: user, claimed_recording: claimed_recording, recording: claimed_recording.recording, favorite: true)
      post :update, {:format => 'json', id: claimed_recording.id}
      response.status.should == 200

      like.reload
      like.favorite.should be true
    end

    it "can set favorite to false" do
      controller.current_user = user
      like = FactoryGirl.create(:recording_like, user: user, claimed_recording: claimed_recording, recording: claimed_recording.recording, favorite: true)
      post :update, {:format => 'json', id: claimed_recording.id, favorite:false}
      response.status.should == 200

      like.reload
      like.favorite.should be false
    end

  end

end
