2015-03-20 12:11:12 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe FileSizeValidator do
|
2017-07-25 17:09:00 +00:00
|
|
|
let(:validator) { described_class.new(options) }
|
2015-03-20 12:11:12 +00:00
|
|
|
let(:attachment) { AttachmentUploader.new }
|
|
|
|
let(:note) { create(:note) }
|
|
|
|
|
|
|
|
describe 'options uses an integer' do
|
|
|
|
let(:options) { { maximum: 10, attributes: { attachment: attachment } } }
|
|
|
|
|
|
|
|
it 'attachment exceeds maximum limit' do
|
2015-06-23 05:25:40 +00:00
|
|
|
allow(attachment).to receive(:size) { 100 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).to have_key(:attachment)
|
2015-03-20 12:11:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'attachment under maximum limit' do
|
|
|
|
allow(attachment).to receive(:size) { 1 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).not_to have_key(:attachment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'options uses a symbol' do
|
2015-06-22 20:00:54 +00:00
|
|
|
let(:options) do
|
|
|
|
{
|
2017-08-10 16:39:26 +00:00
|
|
|
maximum: :max_attachment_size,
|
2015-06-22 20:00:54 +00:00
|
|
|
attributes: { attachment: attachment }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-03-20 12:11:12 +00:00
|
|
|
before do
|
2017-08-10 16:39:26 +00:00
|
|
|
expect(note).to receive(:max_attachment_size) { 10 }
|
2015-03-20 12:11:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'attachment exceeds maximum limit' do
|
|
|
|
allow(attachment).to receive(:size) { 100 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).to have_key(:attachment)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'attachment under maximum limit' do
|
|
|
|
allow(attachment).to receive(:size) { 1 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).not_to have_key(:attachment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|