Sha256: af35d91a0b893d6596cb9919104fd1ec6d1838265b8fb8e45cabb57e1603b118

Contents?: true

Size: 1.64 KB

Versions: 30

Compression:

Stored size: 1.64 KB

Contents

require 'spec_helper'

describe Paperclip::Validators::MediaTypeSpoofDetectionValidator do
  before do
    rebuild_model
    @dummy = Dummy.new
  end

  def build_validator(options = {})
    @validator = Paperclip::Validators::MediaTypeSpoofDetectionValidator.new(options.merge(
      attributes: :avatar
    ))
  end

  it "is on the attachment without being explicitly added" do
    assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :media_type_spoof_detection }
  end

  it "is not on the attachment when explicitly rejected" do
    rebuild_model validate_media_type: false
    assert Dummy.validators_on(:avatar).none?{ |validator| validator.kind == :media_type_spoof_detection }
  end

  it "returns default error message for spoofed media type" do
    build_validator
    file = File.new(fixture_file("5k.png"), "rb")
    @dummy.avatar.assign(file)

    detector = mock("detector", :spoofed? => true)
    Paperclip::MediaTypeSpoofDetector.stubs(:using).returns(detector)
    @validator.validate(@dummy)

    assert_equal I18n.t("errors.messages.spoofed_media_type"), @dummy.errors[:avatar].first
  end

  it "runs when attachment is dirty" do
    build_validator
    file = File.new(fixture_file("5k.png"), "rb")
    @dummy.avatar.assign(file)
    Paperclip::MediaTypeSpoofDetector.stubs(:using).returns(stub(:spoofed? => false))

    @dummy.valid?

    assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.once }
  end

  it "does not run when attachment is not dirty" do
    Paperclip::MediaTypeSpoofDetector.stubs(:using).never
    @dummy.valid?
    assert_received(Paperclip::MediaTypeSpoofDetector, :using){|e| e.never }
  end
end

Version data entries

30 entries across 28 versions & 6 rubygems

Version Path
kt-paperclip-5.4.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
kt-paperclip-4.4.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-6.1.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-6.0.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-5.3.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-5.2.1 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-5.2.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/paperclip-5.1.0/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-5.1.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-5.0.0 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-4.3.7 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip_jk-5.0.0.beta2 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-5.0.0.beta2 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-5.0.0.beta1 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-4.3.6 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-4.3.5 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-4.3.4 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-4.3.3 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb
paperclip-4.3.2 spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb