Browse Source

Allow adding custom collection resources

pull/2690/head
Kat 2 years ago
parent
commit
b36490e4ab
  1. 23
      app/controllers/collection_resources_controller.rb
  2. 1
      app/services/mandatory_collection_resources_service.rb
  3. 27
      spec/features/collection_resources_spec.rb
  4. 38
      spec/requests/collection_resources_controller_spec.rb

23
app/controllers/collection_resources_controller.rb

@ -102,10 +102,31 @@ class CollectionResourcesController < ApplicationController
@collection_resource = CollectionResource.new(year:, log_type:)
end
def create
return render_not_found unless current_user.support? && editable_collection_resource_years.include?(resource_params[:year].to_i)
@collection_resource = CollectionResource.new(resource_params)
@collection_resource.download_filename ||= @collection_resource.file&.original_filename
validate_file(@collection_resource.file)
return render "collection_resources/new" if @collection_resource.errors.any?
begin
CollectionResourcesService.new.upload_collection_resource(@collection_resource.download_filename, @collection_resource.file)
@collection_resource.save!
rescue StandardError
@collection_resource.errors.add(:file, :error_uploading)
return render "collection_resources/new"
end
flash[:notice] = "The #{@collection_resource.log_type} #{text_year_range_format(@collection_resource.year)} #{@collection_resource.display_name} is now available to users."
redirect_to collection_resources_path
end
private
def resource_params
params.require(:collection_resource).permit(:year, :log_type, :resource_type, :file)
params.require(:collection_resource).permit(:year, :log_type, :resource_type, :file, :mandatory, :display_name)
end
def download_resource(filename)

1
app/services/mandatory_collection_resources_service.rb

@ -27,6 +27,7 @@ class MandatoryCollectionResourcesService
year:,
log_type:,
download_filename: download_filename(resource_type, year, log_type),
mandatory: true,
)
end

27
spec/features/collection_resources_spec.rb

@ -5,6 +5,9 @@ RSpec.describe "Collection resources" do
let(:collection_resources_service) { instance_double(CollectionResourcesService, file_exists_on_s3?: true) }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionResourcesHelper).to receive(:editable_collection_resource_years).and_return([2024, 2025])
# rubocop:enable RSpec/AnyInstance
allow(CollectionResourcesService).to receive(:new).and_return(collection_resources_service)
allow(collection_resources_service).to receive(:upload_collection_resource)
allow(collection_resources_service).to receive(:get_file_metadata).and_return({ "content_type" => "application/pdf", "content_length" => 1000 })
@ -165,4 +168,28 @@ RSpec.describe "Collection resources" do
expect(page).to have_content("There was an error uploading this file.")
end
end
context "when uploading an additional resource" do
it "only allows excel files for lettings" do
expect(CollectionResource.count).to eq(0)
visit(new_collection_resource_path(year: 2025, log_type: "sales"))
click_button("Add resource")
expect(page).to have_content("Select which file to upload")
attach_file "file", file_fixture("pdf_file.pdf")
fill_in("collection_resource[display_name]", with: "some file")
click_button("Add resource")
expect(collection_resources_service).to have_received(:upload_collection_resource).with("pdf_file.pdf", anything)
expect(CollectionResource.count).to eq(1)
expect(CollectionResource.first.year).to eq(2025)
expect(CollectionResource.first.log_type).to eq("sales")
expect(CollectionResource.first.resource_type).to be_nil
expect(CollectionResource.first.mandatory).to be_falsey
expect(CollectionResource.first.released_to_user).to be_nil
expect(CollectionResource.first.display_name).to eq("some file")
expect(page).to have_content("The sales 2025 to 2026 some file is now available to users.")
end
end
end

38
spec/requests/collection_resources_controller_spec.rb

@ -540,4 +540,42 @@ RSpec.describe CollectionResourcesController, type: :request do
end
end
end
describe "POST #collection_resources" do
let(:some_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
let(:params) { { collection_resource: { year: 2025, log_type: "sales", file: some_file, display_name: "some file" } } }
context "when user is not signed in" do
it "redirects to the sign in page" do
post collection_resources_path, params: params
expect(response).to redirect_to(new_user_session_path)
end
end
context "when user is signed in as a data coordinator" do
let(:user) { create(:user, :data_coordinator) }
before do
sign_in user
end
it "returns page not found" do
post collection_resources_path, params: params
expect(response).to have_http_status(:not_found)
end
end
context "when user is signed in as a data provider" do
let(:user) { create(:user, :data_provider) }
before do
sign_in user
end
it "returns page not found" do
post collection_resources_path, params: params
expect(response).to have_http_status(:not_found)
end
end
end
end

Loading…
Cancel
Save