Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] Add test for proc validation based on file attribute (#177) #237

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: integration_validator_based_on_a_file_properties
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Integration::Validator::BasedOnAFileProperty < ApplicationRecord
has_one_attached :picture
validates :picture,
content_type: ['image/png', 'image/jpg', 'image/gif'],
size: { less_than: -> (record) { record.picture.blob.content_type == "image/png" ? 15.kilobytes : 5.kilobytes} }
end
1 change: 1 addition & 0 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
end

%w(
based_on_a_file_property
zero_byte_image
).each do |integration_test|
create_table :"integration_validator_#{integration_test.pluralize}", force: :cascade do |t|
Expand Down
Binary file added test/dummy/public/file_17ko_and_png.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/dummy/public/file_7ko_and_jpg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions test/support/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def file_1ko
content_type: 'image/png'
}
end
alias :file_1ko_and_png :file_1ko

def file_2ko
{
Expand All @@ -218,10 +219,26 @@ def file_7ko
}
end

def file_7ko_and_jpg
{
io: File.open(Rails.root.join('public', 'file_7ko_and_jpg.jpg')),
filename: 'file_7ko_and_jpg',
content_type: 'image/jpg'
}
end

def file_10ko
{
io: File.open(Rails.root.join('public', 'file_10ko')),
filename: 'file_10ko',
content_type: 'text/html'
}
end

def file_17ko_and_png
{
io: File.open(Rails.root.join('public', 'file_17ko_and_png.png')),
filename: 'file_17ko_and_png',
content_type: 'image/png'
}
end
48 changes: 48 additions & 0 deletions test/validators/integration/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,52 @@
it { is_expected_to_have_error_options(error_options, validator: :processable_image) }
end
end

describe 'based on a file property' do
let(:model) { integration_test_class::BasedOnAFileProperty.new(params) }

describe 'when setting size constraints based on the content type' do
describe "when passed a file with the right size and content content type" do
subject { model.picture.attach(file_1ko_and_png) and model }

it { is_expected_to_be_valid }
end

describe "when passed a file with a content type that should accept higher file size (<= 15.kilobytes)" do
describe "and with a higher size that the one that can be accepted for all content types" do
subject { model.picture.attach(file_17ko_and_png) and model }

let(:error_options) do
{
file_size: '17 KB',
min_size: nil,
max_size: '15 KB'
}
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options, validator: :size) }
it { is_expected_to_have_error_options(error_options, validator: :size) }
end
end

describe "when passed a file with a content type that should accept less file size (<= 5.kilobytes)" do
describe "and with a higher size that the one that should be accepted" do
subject { model.picture.attach(file_7ko_and_jpg) and model }

let(:error_options) do
{
file_size: '7 KB',
min_size: nil,
max_size: '5 KB'
}
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options, validator: :size) }
it { is_expected_to_have_error_options(error_options, validator: :size) }
end
end
end
end
end
Loading