2015-04-26 12:48:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe FileSizeValidator do
|
|
|
|
let(:validator) { described_class.new(options) }
|
2015-04-26 12:48:37 +05:30
|
|
|
let(:note) { create(:note) }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:attachment) { AttachmentUploader.new(note) }
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
describe 'options uses an integer' do
|
|
|
|
let(:options) { { maximum: 10, attributes: { attachment: attachment } } }
|
|
|
|
|
|
|
|
it 'attachment exceeds maximum limit' do
|
2015-09-11 14:41:01 +05:30
|
|
|
allow(attachment).to receive(:size) { 100 }
|
|
|
|
validator.validate_each(note, :attachment, attachment)
|
|
|
|
expect(note.errors).to have_key(:attachment)
|
2015-04-26 12:48:37 +05:30
|
|
|
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-09-11 14:41:01 +05:30
|
|
|
let(:options) do
|
|
|
|
{
|
2018-03-17 18:26:18 +05:30
|
|
|
maximum: :max_attachment_size,
|
2015-09-11 14:41:01 +05:30
|
|
|
attributes: { attachment: attachment }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(note).to receive(:max_attachment_size) { 10 }
|
2015-04-26 12:48:37 +05:30
|
|
|
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
|