require 'rails_helper'

feature 'Target files index page' do
  background { i_am_logged_in_as_admin }

  scenario "download_latest when completed with specific type" do
    allow_any_instance_of(TargetFile::MyTarget).to receive(:gzip_file?).and_return false # no gzip for this test
    target_file = create 'target_file/my_target'
    target_file.execute_generation_process
    target_file.should be_completed

    visit "/dunlop/target_files/download_latest/my_target"

    page.response_headers['Content-Disposition'].should match /attachment; filename="my_target-/
    page.body.should eq target_file.original_file.read
  end

  scenario "download_latest when failed with specific type should not return" do
    allow_any_instance_of(TargetFile::MyTarget).to receive(:gzip_file?).and_return false # no gzip for this test
    target_file = create 'target_file/my_target', :failed
    target_file.execute_generation_process
    target_file.should be_failed

    visit "/dunlop/target_files/download_latest/my_target"

    page.body.should be_empty
    page.status_code.should be 404
  end
end

