Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Strange Behaviour on validate_attachment_content_type #1334

Closed
bamorim opened this issue Sep 4, 2013 · 11 comments · Fixed by #1910
Closed

Strange Behaviour on validate_attachment_content_type #1334

bamorim opened this issue Sep 4, 2013 · 11 comments · Fixed by #1910

Comments

@bamorim
Copy link

bamorim commented Sep 4, 2013

So, I was writing a test to validate the content_type of some attachments.

  it { should validate_attachment_content_type(:image).allowing(
      "image/png", "image/jpg", "image/jpeg"
    ).rejecting(
      "text/plain", "text/html", "text/xml", "application/octet-stream", "application/exe"
    )
  }

And the output from the test was:

  2) Step should validate the content types allowed on attachment image
     Failure/Error: it { should validate_attachment_content_type(:image).allowing(
     NoMethodError:
       undefined method `any?' for nil:NilClass
     # ./spec/models/step_spec.rb:11:in `block (2 levels) in <top (required)>'

Then trying to fix that I discovered that if rejected_types_rejected? must be, for some reason I don't know, called first

So when I placed that code:

class Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher
  def matches? subject
    @subject = subject
    @subject = @subject.new if @subject.class == Class
    @allowed_types && @rejected_types &&
    rejected_types_rejected? && allowed_types_allowed?
  end
end

The return from RSpec went as I expected:

  2) Step should validate the content types allowed on attachment image             
     Failure/Error: it { should validate_attachment_content_type(:image).allowing(
       Expected image:

       Accept content types: image/png, image/jpg, image/jpeg
         image/png, image/jpeg were rejected.

       Reject content types: text/plain, text/html, text/xml, application/octet-stre
am, application/exe
         All were rejected successfully.

I didn't created a Pull Request becouse that sounded really weird to me, so I decided just to open an issue.

Any clues on what might be going wrong? Anyone else can replicate the error there?

Thank you folks.

@bamorim
Copy link
Author

bamorim commented Sep 5, 2013

Ah, by the way, I'm using Rails 3.2.14 and Paperclip 3.5.1.

If no one knows what is the origin of that bug, I'll fork and then create a pull request.

@djcp
Copy link
Contributor

djcp commented Sep 13, 2013

@bamorim - definitely odd. I'm going to attempt to replicate.

@djcp
Copy link
Contributor

djcp commented Sep 13, 2013

Ok. I could not replicate EXCEPT when I put the same MIME type into both the whitelist (:content_type) and blacklist (:not) options.

The test:

require 'spec_helper'

describe Friend do
  include Paperclip::Shoulda::Matchers
  it { should validate_presence_of :name }
  it { should validate_attachment_content_type(:avatar).allowing(
      "image/png", "image/jpg", "image/jpeg"
    ).rejecting(
      "text/plain", "text/html", "text/xml", "application/octet-stream", "application/exe"
    )
  }
end

The model:

class Friend < ActiveRecord::Base
  attr_accessible :name, :avatar
  validates_presence_of :name

  has_attached_file :avatar, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>'
  }

  validates_attachment_content_type :avatar,
    content_type: ["image/png", "image/jpg", "image/jpeg"],
    not: ["text/html", "text/xml", "application/octet-stream", "application/exe"]
end

If I just set the allowed content_type:, the test works fine. If I set content_type and the blacklisted :not as above, the test works fine.

The only way I could get your error was to do something like:

  validates_attachment_content_type :avatar,
    content_type: ["image/png", "image/jpg", "image/jpeg"],
    not: ["image/png", "text/html", "text/xml", "application/octet-stream", "application/exe"]

Note the existence of "image/png" in both. Is it possible you had this going on in your code?

@bamorim
Copy link
Author

bamorim commented Sep 16, 2013

No, actually i didn't have a "not" key in my code at all.
I'll try to fork and replicate this error again. It was really really odd, so probably it will be hard to replicate.

I'll do it in my next free time and then I'll link the repository here.

By the way, you've got the test fixed and running the way I did it? Because my "bugfix" was odd too HAHAHA

@djcp
Copy link
Contributor

djcp commented Sep 20, 2013

I was attempting to replicate the error, I didn't use your monkeypatch. Let us know if you are able to narrow this down at all.

@val-litvak
Copy link

@bamorim can you show us what the model looks like (at least the validates_attachment_content_type or validates declaration)? I might be able to help, just had the same issue, make sure that you are either passing an array of strings with appropriate mime types or a regular expression like:

  validates_attachment_content_type :image,
    :content_type => /\Aimage\/.*\Z/

or

  validates_attachment_content_type :image,
    :content_type => ['image/jpg', 'image/png', 'image/gif']

my specs pass like so:

it { should validate_attachment_content_type(:image)
             .allowing('image/jpg', 'image/png', 'image/gif')
             .rejecting('text/plain', 'text/xml') }

I think that the spec is trying to call #any? on the array of mime types or something of that sort.

@maclover7
Copy link
Contributor

Hi @bamorim ! Is this still an issue for you in Paperclip; I know this issue is from approximately 2 years ago. If it is still an issue, can you please provide the code that's causing you the error? Thanks!

@tute
Copy link
Contributor

tute commented May 15, 2015

ping @bamorim. Is this still an issue with Paperclip?

@maclover7
Copy link
Contributor

Closing due to lack of activity.

@sadfuzzy
Copy link

sadfuzzy commented Dec 3, 2015

@tute, yes, even if you fetch paperclip from git
@bamorim, actually, it works like a charm, when you pass it like this:

.allowing(%w(image/png image/jpeg image/bmp))
.rejecting('image/gif')

@sadfuzzy
Copy link

sadfuzzy commented Dec 3, 2015

I'd make a PR, but have no time, sorry. I think, it needs a test on how params are passed. By the way, my ruby version is ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin14.0], rails is 4.1.5

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants