mongoid-rspec icon indicating copy to clipboard operation
mongoid-rspec copied to clipboard

Length validator spec with minimum value doesn't work.

Open joejeet opened this issue 10 years ago • 4 comments

As in rails has_secure_password the maximum length validation (should be less than or equal to 72 characters) is been added automatically for password field.

So, when you add something like this:

it { should validate_length_of(:password).with_minimum(6) }

it add length validation to the array and in array It just takes the first condition which comes true. So, length validation which is of maximum is accepted and ignores any other validation.

looks like this:

@klass.validators_on(@field) => [ Mongoid::Validatable::LengthValidator:0x007facb1068690 @attributes=[:password], @options={:maximum=>72}>, ActiveModel::Validations::ConfirmationValidator:0x007facb106b688 @attributes=[:password], @options={:allow_blank=>true}>, Mongoid::Validatable::LengthValidator:0x007facb103cc48 @attributes=[:password], @options={:if=>"password_confirmation.present?", :minimum=>6}>]

and it gives this error:

Failure/Error: it { should validate_length_of(:password).with_minimum(6) } Expected User to have "length" validator on "password" with minimum of 6; instead got "length" validator on "password" with no minimum

joejeet avatar Nov 04 '14 17:11 joejeet

@joejeet Could you paste here the snippet of your class with has_secure_password, the spec snippet and which version of the mongoid-rspec you are using. Thanks

rodrigopinto avatar Jan 06 '15 16:01 rodrigopinto

Getting same issue with slightly different circumstances. User model:

class User
  include Mongoid::Document
  include ActiveModel::SecurePassword

  field :username, type: String
  field :email, type: String
  field :first_name, type: String
  field :last_name, type: String
  field :password_digest, type: String

  has_secure_password

  validates :password, length: {minimum: 8}

user_spec:

<snip>
it { is_expected.to validate_length_of(:password).with_minimum(8) }
<snip>

Gems:

mongoid (4.0.2)
mongoid-rspec (2.1.0)

vidkun avatar Jun 17 '15 00:06 vidkun

I had the same problem and what should i do to solve it? thx!

Tairy avatar Apr 14 '16 08:04 Tairy

Having a similar issue, I fixed it by adding on: :create to the initial validation (h/t j-dexx on StackOverflow.) It seems also seems as if this issue affects all validation (e.g. presence: true), not just length.

I was also able to reproduce this error outside of rspec when I ran the server, so there's a good chance that a) this isn't an rspec issue or b) I'm having an unrelated bug

However, this causes an issue with password resets (which I'm assuming are a common feature in most of the apps using has_secure_password). A very shitty, temporary fix I implemented was overriding #password= to decorate the functionality with addition validation like this:

def password=(other_password)
   if {validation logic fails}
      error.add(:password, {some error})
   end
   super other_password
end

BenMusch avatar Jul 20 '16 22:07 BenMusch