BigW Consortium Gitlab

file_size_validator_spec.rb 1.4 KB
Newer Older
1 2
require 'spec_helper'

3
describe FileSizeValidator do
4
  let(:validator) { described_class.new(options) }
5 6 7 8 9 10 11
  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
12 13 14
      allow(attachment).to receive(:size) { 100 }
      validator.validate_each(note, :attachment, attachment)
      expect(note.errors).to have_key(:attachment)
15 16 17 18 19 20 21 22 23 24
    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
25 26
    let(:options) do
      {
27
        maximum: :max_attachment_size,
28 29 30 31
        attributes: { attachment: attachment }
      }
    end

32
    before do
33
      expect(note).to receive(:max_attachment_size) { 10 }
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    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