diff --git a/app/controllers/collection_resources_controller.rb b/app/controllers/collection_resources_controller.rb index 3bec72371..266be6d39 100644 --- a/app/controllers/collection_resources_controller.rb +++ b/app/controllers/collection_resources_controller.rb @@ -52,6 +52,10 @@ class CollectionResourcesController < ApplicationController @collection_resource = MandatoryCollectionResourcesService.generate_resource(log_type, year, resource_type) render_not_found unless @collection_resource + validate_file(file) + + return render "collection_resources/edit" if @collection_resource.errors.any? + filename = @collection_resource.download_filename begin UploadCollectionResourcesService.upload_collection_resource(filename, file) @@ -95,4 +99,23 @@ private def resource_for_year_can_be_updated?(year) editable_collection_resource_years.include?(year) end + + def validate_file(file) + return @collection_resource.errors.add(:file, "Select which file to upload") unless file + return @collection_resource.errors.add(:file, "The file is above 100MB") if file.size > 100.megabytes + + argv = %W[file --brief --mime-type -- #{file.path}] + output = `#{argv.shelljoin}` + + case @collection_resource.resource_type + when "paper_form" + unless output.match?(/application\/pdf/) + @collection_resource.errors.add(:file, "The paper form must be a PDF.") + end + when "bulk_upload_template", "bulk_upload_specification" + unless output.match?(/application\/vnd\.ms-excel|application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet/) + @collection_resource.errors.add(:file, "The #{@collection_resource.short_display_name.downcase} must be a Microsoft Excel file.") + end + end + end end diff --git a/app/models/collection_resource.rb b/app/models/collection_resource.rb index 442594d8c..d5c3c895b 100644 --- a/app/models/collection_resource.rb +++ b/app/models/collection_resource.rb @@ -2,7 +2,7 @@ class CollectionResource include ActiveModel::Model include Rails.application.routes.url_helpers - attr_accessor :resource_type, :display_name, :short_display_name, :year, :log_type, :download_filename + attr_accessor :resource_type, :display_name, :short_display_name, :year, :log_type, :download_filename, :file def download_path download_mandatory_collection_resource_path(log_type:, year:, resource_type:) diff --git a/spec/features/collection_resources_spec.rb b/spec/features/collection_resources_spec.rb new file mode 100644 index 000000000..306b4355d --- /dev/null +++ b/spec/features/collection_resources_spec.rb @@ -0,0 +1,142 @@ +require "rails_helper" + +RSpec.describe "Collection resources" do + let(:user) { create(:user, :support) } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + end + + context "when uploading paper form" do + it "only allows pdf files for lettings" do + visit("/collection-resources/lettings/2024/paper_form/edit") + + click_button("Save changes") + + expect(page).to have_content("Select which file to upload") + + expect(page).to have_content("Change the paper form") + expect(page).to have_content("Lettings 2024 to 2025") + + attach_file "file", file_fixture("excel_file.xlsx") + click_button("Save changes") + + expect(page).to have_content("The paper form must be a PDF.") + + attach_file "file", file_fixture("pdf_file.pdf") + click_button("Save changes") + + expect(page).not_to have_content("The paper form must be a PDF.") + end + + it "only allows pdf files for sales" do + visit("/collection-resources/sales/2024/paper_form/edit") + + click_button("Save changes") + + expect(page).to have_content("Select which file to upload") + + expect(page).to have_content("Change the paper form") + expect(page).to have_content("Sales 2024 to 2025") + + attach_file "file", file_fixture("excel_file.xlsx") + click_button("Save changes") + + expect(page).to have_content("The paper form must be a PDF.") + + attach_file "file", file_fixture("pdf_file.pdf") + click_button("Save changes") + + expect(page).not_to have_content("The paper form must be a PDF.") + end + end + + context "when uploading bu template" do + it "only allows excel files for lettings" do + visit("/collection-resources/lettings/2024/bulk_upload_template/edit") + + click_button("Save changes") + + expect(page).to have_content("Select which file to upload") + + expect(page).to have_content("Change the bulk upload template") + expect(page).to have_content("Lettings 2024 to 2025") + + attach_file "file", file_fixture("pdf_file.pdf") + click_button("Save changes") + + expect(page).to have_content("The bulk upload template must be a Microsoft Excel file.") + + attach_file "file", file_fixture("excel_file.xlsx") + click_button("Save changes") + + expect(page).not_to have_content("The bulk upload template must be a Microsoft Excel file.") + end + + it "only allows excel files for sales" do + visit("/collection-resources/sales/2024/bulk_upload_template/edit") + + click_button("Save changes") + + expect(page).to have_content("Select which file to upload") + + expect(page).to have_content("Change the bulk upload template") + expect(page).to have_content("Sales 2024 to 2025") + + attach_file "file", file_fixture("pdf_file.pdf") + click_button("Save changes") + + expect(page).to have_content("The bulk upload template must be a Microsoft Excel file.") + + attach_file "file", file_fixture("excel_file.xlsx") + click_button("Save changes") + + expect(page).not_to have_content("The bulk upload template must be a Microsoft Excel file.") + end + end + + context "when uploading bu specification" do + it "only allows excel files for lettings" do + visit("/collection-resources/lettings/2024/bulk_upload_specification/edit") + + click_button("Save changes") + + expect(page).to have_content("Select which file to upload") + + expect(page).to have_content("Change the bulk upload specification") + expect(page).to have_content("Lettings 2024 to 2025") + + attach_file "file", file_fixture("pdf_file.pdf") + click_button("Save changes") + + expect(page).to have_content("The bulk upload specification must be a Microsoft Excel file.") + + attach_file "file", file_fixture("excel_file.xlsx") + click_button("Save changes") + + expect(page).not_to have_content("The bulk upload specification must be a Microsoft Excel file.") + end + + it "only allows excel files for sales" do + visit("/collection-resources/sales/2024/bulk_upload_specification/edit") + + click_button("Save changes") + + expect(page).to have_content("Select which file to upload") + + expect(page).to have_content("Change the bulk upload specification") + expect(page).to have_content("Sales 2024 to 2025") + + attach_file "file", file_fixture("pdf_file.pdf") + click_button("Save changes") + + expect(page).to have_content("The bulk upload specification must be a Microsoft Excel file.") + + attach_file "file", file_fixture("excel_file.xlsx") + click_button("Save changes") + + expect(page).not_to have_content("The bulk upload specification must be a Microsoft Excel file.") + end + end +end diff --git a/spec/fixtures/files/excel_file.xlsx b/spec/fixtures/files/excel_file.xlsx new file mode 100644 index 000000000..dcc5aaaf3 Binary files /dev/null and b/spec/fixtures/files/excel_file.xlsx differ diff --git a/spec/fixtures/files/pdf_file.pdf b/spec/fixtures/files/pdf_file.pdf new file mode 100644 index 000000000..65a01d0a9 Binary files /dev/null and b/spec/fixtures/files/pdf_file.pdf differ