Browse Source

Allow downloading additional collection resources

pull/2690/head
Kat 2 years ago
parent
commit
40edb6689f
  1. 9
      app/controllers/collection_resources_controller.rb
  2. 2
      app/models/collection_resource.rb
  3. 83
      spec/requests/collection_resources_controller_spec.rb

9
app/controllers/collection_resources_controller.rb

@ -25,6 +25,15 @@ class CollectionResourcesController < ApplicationController
download_resource(resource.download_filename)
end
def download_additional_collection_resource
resource = CollectionResource.find_by(id: params[:collection_resource_id])
return render_not_found unless resource
return render_not_found unless resource_for_year_can_be_downloaded?(resource.year)
download_resource(resource.download_filename)
end
def edit
return render_not_found unless current_user.support?

2
app/models/collection_resource.rb

@ -9,7 +9,7 @@ class CollectionResource < ApplicationRecord
if mandatory
download_mandatory_collection_resource_path(log_type:, year:, resource_type:)
else
"/"
collection_resource_download_path(self)
end
end

83
spec/requests/collection_resources_controller_spec.rb

@ -158,11 +158,12 @@ RSpec.describe CollectionResourcesController, type: :request do
end
context "when there are additional resources" do
let!(:collection_resource) { create(:collection_resource, :additional, year: 2025, short_display_name: "additional resource", download_filename: "additional.pdf") }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionResourcesHelper).to receive(:editable_collection_resource_years).and_return([2025])
# rubocop:enable RSpec/AnyInstance
create(:collection_resource, :additional, year: 2025, short_display_name: "additional resource")
create(:collection_resource, :additional, year: 2026, short_display_name: "additional resource 2")
end
@ -171,6 +172,7 @@ RSpec.describe CollectionResourcesController, type: :request do
expect(page).to have_content("additional resource")
expect(page).not_to have_content("additional resource 2")
expect(page).to have_link("additional.pdf", href: collection_resource_download_path(collection_resource))
end
end
end
@ -595,4 +597,83 @@ RSpec.describe CollectionResourcesController, type: :request do
end
end
end
describe "GET #download_additional_collection_resource" do
let(:collection_resource) { create(:collection_resource, :additional, year: 2025, short_display_name: "additional resource") }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionResourcesHelper).to receive(:editable_collection_resource_years).and_return([2025, 2026])
allow_any_instance_of(CollectionResourcesHelper).to receive(:displayed_collection_resource_years).and_return([2025])
# rubocop:enable RSpec/AnyInstance
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
end
context "when user is signed in as a data coordinator" do
let(:user) { create(:user, :data_coordinator) }
context "when the file exists on S3" do
before do
allow(storage_service).to receive(:get_file_io).and_return("file")
get collection_resource_download_path(collection_resource)
end
it "downloads the file" do
expect(response.body).to eq("file")
end
end
context "when the file does not exist on S3" do
before do
allow(storage_service).to receive(:get_file_io).and_return(nil)
get collection_resource_download_path(collection_resource)
end
it "returns page not found" do
expect(response).to have_http_status(:not_found)
end
end
context "when resource id is invalid" do
before do
allow(storage_service).to receive(:get_file_io).and_return(nil)
get collection_resource_download_path(collection_resource_id: "invalid")
end
it "returns page not found" do
expect(response).to have_http_status(:not_found)
end
end
context "when year not in displayed_collection_resource_years" do
let(:collection_resource) { create(:collection_resource, :additional, year: 2026, short_display_name: "additional resource") }
before do
get collection_resource_download_path(collection_resource)
end
it "returns page not found" do
expect(response).to have_http_status(:not_found)
end
end
end
context "when user is signed in as a support user" do
let(:collection_resource) { create(:collection_resource, :additional, year: 2026, short_display_name: "additional resource") }
let(:user) { create(:user, :support) }
context "when year is in editable_collection_resource_years but not in displayed_collection_resource_years" do
before do
allow(storage_service).to receive(:get_file_io).and_return("file")
get collection_resource_download_path(collection_resource)
end
it "downloads the file" do
expect(response.status).to eq(200)
expect(response.body).to eq("file")
end
end
end
end
end

Loading…
Cancel
Save