Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 208 lines (172 sloc) 6.14 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
require './test/helper'

class AttachmentSizeValidatorTest < Test::Unit::TestCase
  def setup
    rebuild_model
    @dummy = Dummy.new
  end

  def build_validator(options)
    @validator = Paperclip::Validators::AttachmentSizeValidator.new(options.merge(
      :attributes => :avatar
    ))
  end

  def self.should_allow_attachment_file_size(size)
    context "when the attachment size is #{size}" do
      should "add error to dummy object" do
        @dummy.stubs(:avatar_file_size).returns(size)
        @validator.validate(@dummy)
        assert @dummy.errors[:avatar_file_size].blank?,
          "Expect an error message on :avatar_file_size, got none."
      end
    end
  end

  def self.should_not_allow_attachment_file_size(size, options = {})
    context "when the attachment size is #{size}" do
      setup do
        @dummy.stubs(:avatar_file_size).returns(size)
        @validator.validate(@dummy)
      end

      should "add error to dummy object" do
        assert @dummy.errors[:avatar_file_size].present?,
          "Unexpected error message on :avatar_file_size"
      end

      if options[:message]
        should "return a correct error message" do
          assert_includes @dummy.errors[:avatar_file_size], options[:message]
        end
      end
    end
  end

  context "with :in option" do
    context "as a range" do
      setup do
        build_validator :in => (5.kilobytes..10.kilobytes)
      end

      should_allow_attachment_file_size(7.kilobytes)
      should_not_allow_attachment_file_size(4.kilobytes)
      should_not_allow_attachment_file_size(11.kilobytes)
    end

    context "as a proc" do
      setup do
        build_validator :in => lambda { |avatar| (5.kilobytes..10.kilobytes) }
      end

      should_allow_attachment_file_size(7.kilobytes)
      should_not_allow_attachment_file_size(4.kilobytes)
      should_not_allow_attachment_file_size(11.kilobytes)
    end
  end

  context "with :greater_than option" do
    context "as number" do
      setup do
        build_validator :greater_than => 10.kilobytes
      end

      should_allow_attachment_file_size 11.kilobytes
      should_not_allow_attachment_file_size 10.kilobytes
    end

    context "as a proc" do
      setup do
        build_validator :greater_than => lambda { |avatar| 10.kilobytes }
      end

      should_allow_attachment_file_size 11.kilobytes
      should_not_allow_attachment_file_size 10.kilobytes
    end
  end

  context "with :less_than option" do
    context "as number" do
      setup do
        build_validator :less_than => 10.kilobytes
      end

      should_allow_attachment_file_size 9.kilobytes
      should_not_allow_attachment_file_size 10.kilobytes
    end

    context "as a proc" do
      setup do
        build_validator :less_than => lambda { |avatar| 10.kilobytes }
      end

      should_allow_attachment_file_size 9.kilobytes
      should_not_allow_attachment_file_size 10.kilobytes
    end
  end

  context "with :greater_than and :less_than option" do
    context "as numbers" do
      setup do
        build_validator :greater_than => 5.kilobytes,
          :less_than => 10.kilobytes
      end

      should_allow_attachment_file_size 7.kilobytes
      should_not_allow_attachment_file_size 5.kilobytes
      should_not_allow_attachment_file_size 10.kilobytes
    end

    context "as a proc" do
      setup do
        build_validator :greater_than => lambda { |avatar| 5.kilobytes },
          :less_than => lambda { |avatar| 10.kilobytes }
      end

      should_allow_attachment_file_size 7.kilobytes
      should_not_allow_attachment_file_size 5.kilobytes
      should_not_allow_attachment_file_size 10.kilobytes
    end
  end

  context "with :message option" do
    context "given a range" do
      setup do
        build_validator :in => (5.kilobytes..10.kilobytes),
          :message => "is invalid. (Between %{min} and %{max} please.)"
      end

      should_not_allow_attachment_file_size 11.kilobytes,
        :message => "is invalid. (Between 5120 Bytes and 10240 Bytes please.)"
    end

    context "given :less_than and :greater_than" do
      setup do
        build_validator :less_than => 10.kilobytes,
          :greater_than => 5.kilobytes,
          :message => "is invalid. (Between %{min} and %{max} please.)"
      end

      should_not_allow_attachment_file_size 11.kilobytes,
        :message => "is invalid. (Between 5120 Bytes and 10240 Bytes please.)"
    end
  end

  context "default error messages" do
    context "given :less_than and :greater_than" do
      setup do
        build_validator :greater_than => 5.kilobytes,
          :less_than => 10.kilobytes
      end

      should_not_allow_attachment_file_size 11.kilobytes,
        :message => "must be less than 10240 Bytes"
      should_not_allow_attachment_file_size 4.kilobytes,
        :message => "must be greater than 5120 Bytes"
    end

    context "given a size range" do
      setup do
        build_validator :in => (5.kilobytes..10.kilobytes)
      end

      should_not_allow_attachment_file_size 11.kilobytes,
        :message => "must be in between 5120 Bytes and 10240 Bytes"
      should_not_allow_attachment_file_size 4.kilobytes,
        :message => "must be in between 5120 Bytes and 10240 Bytes"
    end
  end

  context "using the helper" do
    setup do
      Dummy.validates_attachment_size :avatar, :in => (5.kilobytes..10.kilobytes)
    end

    should "add the validator to the class" do
      assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
    end
  end

  context "given options" do
    should "raise argument error if no required argument was given" do
      assert_raises(ArgumentError) do
        build_validator :message => "Some message"
      end
    end

    (Paperclip::Validators::AttachmentSizeValidator::AVAILABLE_CHECKS).each do |argument|
      should "not raise arguemnt error if #{argument} was given" do
        build_validator argument => 5.kilobytes
      end
    end

    should "not raise argument error if :in was given" do
      build_validator :in => (5.kilobytes..10.kilobytes)
    end
  end
end
Something went wrong with that request. Please try again.