require "rails_helper"

feature "User managing misc releases" do
  let(:current_user) { create(:user) }
  let(:project) { create(:project, members: current_user, account: current_user.primary_account) }

  context "when signed in as account manager" do
    before do
      sign_in current_user
    end

    scenario "Download All is visible" do
      create(:misc_release_with_contract_template, :native, project: project)

      visit project_misc_releases_path(project)

      expect(page).to have_content download_all_button
    end

    scenario "Downloading PDF of native misc release is possible" do
      native_release = create(:misc_release_with_contract_template, :native, project: project)

      visit project_misc_releases_path(project)

      click_link *view_release_pdf_link_for(native_release)
      expect(content_type).to eq('application/pdf')
    end
  end

  context "when the user is manager(project manager)" do
    let(:current_user) { create(:user, :manager) }

    before do
      sign_in current_user
    end

    scenario "Download action in Manage menu is not visible" do
      create(:misc_release_with_contract_template, :native, project: project)

      visit project_misc_releases_path(project)

      expect(page).to have_link("Download", exact: true, count: 0)
    end
  end

  private

  def download_all_button
    'Download All'
  end

  def view_release_pdf_link_for(release)
    ['Download', href: misc_release_contracts_path(release, format: 'pdf')]
  end
end
