Skip to content

Commit

Permalink
observation reports - allow only pdfs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Feb 4, 2025
1 parent db3470d commit 3fd61f9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/admin/observation_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def apply_filtering(chain)
f.input :user
f.input :title
f.input :publication_date, as: :date_time_picker, picker_options: {timepicker: false, format: "Y-m-d"}
f.input :attachment, as: :file, hint: f.object&.attachment&.file&.filename
f.input :attachment, as: :file, hint: f.object&.attachment&.file&.filename, input_html: {accept: "application/pdf"}
f.input :observers
end

Expand Down
2 changes: 1 addition & 1 deletion app/uploaders/observation_report_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ObservationReportUploader < ApplicationUploader
def extension_allowlist
%w[pdf doc docx txt csv xml jpg jpeg png exif tiff bmp]
%w[pdf]
end

def filename
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/observation_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
factory :observation_report do
sequence(:title) { |n| "ObservationReportTitle#{n}" }
publication_date { DateTime.current }
attachment { Rack::Test::UploadedFile.new(File.join(Rails.root, "spec", "support", "files", "image.png")) }
attachment { Rack::Test::UploadedFile.new(File.join(Rails.root, "spec", "support", "files", "doc.pdf")) }
observers { build_list(:observer, 1) }

after(:build) do |random_observation_report|
Expand Down
9 changes: 9 additions & 0 deletions spec/models/observation_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
expect(subject).not_to be_valid
expect(subject.errors.messages[:attachment]).to include("can't be blank")
end

it "accepts only pdf files" do
subject.attachment = Rack::Test::UploadedFile.new(File.join(Rails.root, "spec", "support", "files", "image.png"))
expect(subject).not_to be_valid
expect(subject.errors.messages[:attachment]).to include("You are not allowed to upload \"png\" files, allowed types: pdf")
subject = build(:observation_report) # somehow just changing the attachment do not reset that validation error, probably carrierwave bug
subject.attachment = Rack::Test::UploadedFile.new(File.join(Rails.root, "spec", "support", "files", "doc.pdf"))
expect(subject).to be_valid
end
end

describe "hooks" do
Expand Down

0 comments on commit 3fd61f9

Please sign in to comment.