require 'spec_helper'

describe ApiJamTrackMixdownsController, type: :controller do
  render_views

  let(:user) { FactoryGirl.create(:user) }
  let(:jam_track) { FactoryGirl.create(:jam_track) }
  let(:mixdown) { FactoryGirl.create(:jam_track_mixdown, user: user, jam_track: jam_track) }
  let(:jam_track_right) { FactoryGirl.create(:jam_track_right, jam_track: jam_track, user:user)}
  let(:package) {FactoryGirl.create(:jam_track_mixdown_package, jam_track_mixdown: mixdown)}

  before(:each) do
    controller.current_user = user
    JamTrackMixdown.destroy_all
  end

  describe "index" do

    it "one result" do

      # make a mixdown with no packages
      get :index, {id: mixdown.jam_track.id}
      response.status.should eq(200)
      json = JSON.parse(response.body)
      json["next"].should be_nil
      json["count"].should eq(1)
      json["mixdowns"][0]["settings"].should eq({"speed" => 5})

      # and then add a package
      package = FactoryGirl.create(:jam_track_mixdown_package, jam_track_mixdown: mixdown)

      get :index, {id: mixdown.jam_track.id}
      response.status.should eq(200)
      json = JSON.parse(response.body)
      json["next"].should be_nil
      json["count"].should eq(1)
      json["mixdowns"][0]["packages"][0]["signing_state"].should eq('QUIET')
    end
  end

  describe "create" do

    it "success" do
      post :create, {:format => 'json', jamTrackID: jam_track.id, name: 'some name', description: 'some description', settings: {speed:5}}

      response.status.should eq(200)

      json = JSON.parse(response.body)
      json["name"].should eq('some name')
      json["jam_track_id"].should eq(jam_track.id)
      json["description"].should eq('some description')
      json["settings"].should eq({"speed" => 5})
      json["packages"].should eq([])
    end

    it "validates name" do
      post :create, {:format => 'json', jamTrackID: jam_track.id, description: 'some description', settings: {speed:5}}

      response.status.should eq(422)

      json = JSON.parse(response.body)
      json["errors"]["name"].should eq(["can't be blank"])
    end
  end

  describe "enqueue" do
    it "success" do

      jam_track_right.touch
      post :enqueue, {:format => 'json', id: mixdown.id, file_type: JamTrackMixdownPackage::FILE_TYPE_AAC, encrypt_type: nil, sample_rate: 48}

      response.status.should eq(200)

      json = JSON.parse(response.body)
      puts json
      json["id"].should_not be_nil

      package = JamTrackMixdownPackage.find(json["id"])
      package.file_type.should eq(JamTrackMixdownPackage::FILE_TYPE_AAC)
      package.encrypt_type.should eq(nil)
      package.sample_rate.should eq(48)
    end

    it "validates file_type" do
      jam_track_right.touch
      post :enqueue, {:format => 'json', id: mixdown.id, file_type: 'wrong', encrypt_type: nil, sample_rate: 48}

      response.status.should eq(422)

      json = JSON.parse(response.body)
      json["errors"]["file_type"].should eq(["is not included in the list"])
    end

    it "finds existing package to enqueue" do
      jam_track_right.touch
      package.touch
      JamTrackMixdownPackage.count.should eq(1)

      package.jam_track_mixdown.should eq(mixdown)
      post :enqueue, {:format => 'json', id: mixdown.id, file_type: package.file_type, encrypt_type: package.encrypt_type, sample_rate: package.sample_rate}

      response.status.should eq(200)

      json = JSON.parse(response.body)
      puts json
      json["id"].should eq(package.id)
      JamTrackMixdownPackage.count.should eq(1)
    end
  end

  describe "download" do

    it "enqueues if not available" do

      jam_track_right.touch
      package.touch

      post :download, {:format => 'json', id: mixdown.id, file_type: package.file_type, encrypt_type: package.encrypt_type, sample_rate: package.sample_rate}

      response.status.should eq(202)

      json = JSON.parse(response.body)
      json["message"].should eq("not available, digitally signing Jam Track Mixdown offline.")

      package.reload
      package.signing_state.should eq('QUEUED')
    end

    it "success" do

      jam_track_right.touch
      package.touch
      package.enqueue_if_needed
      package.signed = true
      package.url = 'some/bogus/place'
      package.save!

      post :download, {:format => 'json', id: mixdown.id, file_type: package.file_type, encrypt_type: package.encrypt_type, sample_rate: package.sample_rate}

      response.status.should eq(302)

      response['Location'].should include('/some/bogus/place')

    end

  end
end

