Skip to content

Commit

Permalink
Implemented filetype validations and tests for image attachments
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cpfergus1 committed Apr 12, 2021
1 parent e878076 commit 60c6b56
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
16 changes: 14 additions & 2 deletions core/app/models/spree/image/active_storage_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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>',
Expand All @@ -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
1 change: 1 addition & 0 deletions core/lib/spree/testing_support/fixtures/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a text file
5 changes: 3 additions & 2 deletions core/spec/lib/search/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions core/spec/models/spree/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 60c6b56

Please sign in to comment.