Skip to content

Commit

Permalink
[Validator] Add test for proc validation based on file attribute (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Jan 9, 2024
1 parent ffedd70 commit a2353b3
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 5 deletions.
7 changes: 7 additions & 0 deletions test/dummy/app/models/integration/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Integration
module Validator
def self.table_name_prefix
'integration_validator_'
end
end
end
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
15 changes: 12 additions & 3 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,25 @@
end
end

create_table :documents, force: :cascade do |t|
t.datetime :created_at, precision: 6, null: false
t.datetime :updated_at, precision: 6, null: false
%w(
based_on_a_file_property
).each do |integration_test|
create_table :"integration_validator_#{integration_test.pluralize}", force: :cascade do |t|
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end
end

create_table :integration_matchers, force: :cascade do |t|
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end

create_table :documents, force: :cascade do |t|
t.datetime :created_at, precision: 6, null: false
t.datetime :updated_at, precision: 6, null: false
end

create_table :limit_attachments, force: :cascade do |t|
t.string :name
end
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 @@ -157,6 +157,7 @@ def file_1ko
content_type: 'image/png'
}
end
alias :file_1ko_and_png :file_1ko

def file_2ko
{
Expand All @@ -182,10 +183,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
58 changes: 58 additions & 0 deletions test/validators/integration/integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'test_helper'

describe 'Integration tests' do
include ValidatorHelpers

let(:params) { {} }
let(:model) { klass.new(params) }

describe 'based on a file property' do
let(:klass) { Integration::Validator::BasedOnAFileProperty }

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_sym: :size) }
it { is_expected_to_have_error_options(error_options, validator_sym: :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_sym: :size) }
it { is_expected_to_have_error_options(error_options, validator_sym: :size) }
end
end
end
end
end
8 changes: 6 additions & 2 deletions test/validators/support/validator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def is_expected_to_have_error_options(error_options, **kwargs)
# Rails 6.1.0 changes the form of ActiveModel’s errors collection
# https://github.com/rails/rails/blob/6-1-stable/activemodel/CHANGELOG.md#rails-610-december-09-2020
validator_errors = if Rails.gem_version >= Gem::Version.new('6.1.0')
subject.errors.find { |error| error.options[:validator_type] == validator_sym }.options
subject.errors.find do |error|
error.options[:validator_type] == (kwargs[:validator_sym] || validator_sym)
end.options
else
# For errors before Rails 6.1.0 we do not have error options
return true
Expand All @@ -33,7 +35,9 @@ def is_expected_to_have_error_message(message_key, **kwargs)
# Rails 6.1.0 changes the form of ActiveModel’s errors collection
# https://github.com/rails/rails/blob/6-1-stable/activemodel/CHANGELOG.md#rails-610-december-09-2020
validator_error_message = if Rails.gem_version >= Gem::Version.new('6.1.0')
subject.errors.find { |error| error.options[:validator_type] == validator_sym }.message
subject.errors.find do |error|
error.options[:validator_type] == (kwargs[:validator_sym] || validator_sym)
end.message
else
# For errors before Rails 6.1.0 we do not have error options
return true
Expand Down

0 comments on commit a2353b3

Please sign in to comment.