Browse Source

Add file validation to collection resources

pull/2678/head
Kat 2 years ago
parent
commit
a3d73a5627
  1. 23
      app/controllers/collection_resources_controller.rb
  2. 2
      app/models/collection_resource.rb
  3. 142
      spec/features/collection_resources_spec.rb
  4. BIN
      spec/fixtures/files/excel_file.xlsx
  5. BIN
      spec/fixtures/files/pdf_file.pdf

23
app/controllers/collection_resources_controller.rb

@ -52,6 +52,10 @@ class CollectionResourcesController < ApplicationController
@collection_resource = MandatoryCollectionResourcesService.generate_resource(log_type, year, resource_type) @collection_resource = MandatoryCollectionResourcesService.generate_resource(log_type, year, resource_type)
render_not_found unless @collection_resource render_not_found unless @collection_resource
validate_file(file)
return render "collection_resources/edit" if @collection_resource.errors.any?
filename = @collection_resource.download_filename filename = @collection_resource.download_filename
begin begin
UploadCollectionResourcesService.upload_collection_resource(filename, file) UploadCollectionResourcesService.upload_collection_resource(filename, file)
@ -95,4 +99,23 @@ private
def resource_for_year_can_be_updated?(year) def resource_for_year_can_be_updated?(year)
editable_collection_resource_years.include?(year) editable_collection_resource_years.include?(year)
end 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 end

2
app/models/collection_resource.rb

@ -2,7 +2,7 @@ class CollectionResource
include ActiveModel::Model include ActiveModel::Model
include Rails.application.routes.url_helpers 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 def download_path
download_mandatory_collection_resource_path(log_type:, year:, resource_type:) download_mandatory_collection_resource_path(log_type:, year:, resource_type:)

142
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

BIN
spec/fixtures/files/excel_file.xlsx vendored

Binary file not shown.

BIN
spec/fixtures/files/pdf_file.pdf vendored

Binary file not shown.
Loading…
Cancel
Save