require 'spec_helper'

describe ApiMixesController, type: :controller do
  render_views

  let(:mix) { FactoryGirl.create(:mix) }

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

  describe "download" do

    it "is possible" do
      controller.current_user = mix.recording.owner
      get :download, {id: mix.id}
      response.status.should == 302

      mix.reload
      mix.download_count.should == 1

      get :download, {id: mix.id}
      response.status.should == 302

      mix.reload
      mix.download_count.should == 2
    end


    it "prevents download after limit is reached" do
      mix.download_count = APP_CONFIG.max_audio_downloads
      mix.save!
      controller.current_user = mix.recording.owner
      get :download, {format:'json', id: mix.id}
      response.status.should == 404
      JSON.parse(response.body, symbolize_names: true)[:message].should == "download limit surpassed"
    end


    it "lets admins surpass limit" do
      mix.download_count = APP_CONFIG.max_audio_downloads
      mix.save!
      mix.recording.owner.admin = true
      mix.recording.owner.save!

      controller.current_user = mix.recording.owner
      get :download, {format:'json', id: mix.id}
      response.status.should == 302
      mix.reload
      mix.download_count.should == 101
    end
  end
end
