From 60c6b56d45ed78642237173a7b5ece20ccf8282b Mon Sep 17 00:00:00 2001 From: Connor Ferguson Date: Thu, 8 Apr 2021 14:45:48 -0600 Subject: [PATCH] Implemented filetype validations and tests for image attachments This commit implements validations for content type. Specifically,this new validation ensures image file types .png/.jpeg/.jpg/.gif are the only accepted file formats at this time, but can be edited by altering the accepted_image_types method to allow other formats should the developer desire to implement that functionality. This method was tested by building the objects and testing validity with both a valid image file and an invalid .txt file. Commit History: Created validation tests for images Implemented validation of filetypes Renamed validation Patched file not found error for url Core test update to be compatible with new validation Core test update to be compatible with new validation Updated test to have file names Implemented compatible syntax for AS and Paperclip Separate NoFileError to another branch Refactored validation tests Returned mistakenly deleted white space Removed describe block --- .../spree/image/active_storage_attachment.rb | 16 ++++++++++++++-- core/lib/spree/testing_support/fixtures/file.txt | 1 + core/spec/lib/search/base_spec.rb | 5 +++-- core/spec/models/spree/image_spec.rb | 12 ++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 core/lib/spree/testing_support/fixtures/file.txt diff --git a/core/app/models/spree/image/active_storage_attachment.rb b/core/app/models/spree/image/active_storage_attachment.rb index c4f87d09e08..d7ec4d3e218 100644 --- a/core/app/models/spree/image/active_storage_attachment.rb +++ b/core/app/models/spree/image/active_storage_attachment.rb @@ -7,6 +7,10 @@ module Spree::Image::ActiveStorageAttachment delegate :width, :height, to: :attachment, prefix: true included do + validates :attachment, presence: true + validate :attachment_is_an_image + validate :supported_content_type + has_attachment :attachment, styles: { mini: '48x48>', @@ -15,7 +19,15 @@ module Spree::Image::ActiveStorageAttachment large: '1200x1200>' }, default_style: :product - validates :attachment, presence: true - validate :attachment_is_an_image + + def supported_content_type + unless attachment.content_type.in?(accepted_image_types) + errors.add(:attachment, :content_type_not_supported) + end + end + + def accepted_image_types + %w(image/jpeg image/jpg image/png image/gif) + end end end diff --git a/core/lib/spree/testing_support/fixtures/file.txt b/core/lib/spree/testing_support/fixtures/file.txt new file mode 100644 index 00000000000..a496efee84a --- /dev/null +++ b/core/lib/spree/testing_support/fixtures/file.txt @@ -0,0 +1 @@ +This is a text file diff --git a/core/spec/lib/search/base_spec.rb b/core/spec/lib/search/base_spec.rb index a27f58e2f44..a5eab4bea9f 100644 --- a/core/spec/lib/search/base_spec.rb +++ b/core/spec/lib/search/base_spec.rb @@ -22,11 +22,12 @@ context "when include_images is included in the initialization params" do let(:params) { { include_images: true, keyword: @product1.name, taxon: @taxon.id } } + let(:image_file) { File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'blank.jpg')) } subject { described_class.new(params).retrieve_products } before do - @product1.master.images.create(attachment_file_name: "Test", position: 2) - @product1.master.images.create(attachment_file_name: "Test1", position: 1) + @product1.master.images.create(attachment_file_name: 'test', attachment: image_file, position: 2) + @product1.master.images.create(attachment_file_name: 'test1', attachment: image_file, position: 1) @product1.reload end diff --git a/core/spec/models/spree/image_spec.rb b/core/spec/models/spree/image_spec.rb index 2ddcd85a7f0..96665e009bf 100644 --- a/core/spec/models/spree/image_spec.rb +++ b/core/spec/models/spree/image_spec.rb @@ -9,6 +9,18 @@ let(:default_style) { :product } end + it 'is valid when attachment has allowed content type' do + image = build(:image, + attachment: File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'blank.jpg'))) + expect(image).to be_valid + end + + it 'is not valid when attachment has restricted content type' do + image = build(:image, + attachment: File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'file.txt'))) + expect(image).to_not be_valid + end + describe 'attachment details' do let(:image_file) { File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'blank.jpg')) } subject { create(:image, attachment: image_file) }