class Shoulda::Matchers::ActiveModel::HaveSecurePasswordMatcher

@private

Constants

CORRECT_PASSWORD
EXPECTED_METHODS
INCORRECT_PASSWORD
MESSAGES

Attributes

failure_message[R]
subject[R]

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 54
def description
  "have a secure password"
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 58
def matches?(subject)
  @subject = subject

  if failure = validate
    key, params = failure
    @failure_message = MESSAGES[key] % { subject: subject.class }.merge(params)
  end

  failure.nil?
end

Protected Instance Methods

validate() click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 73
def validate
  missing_methods = EXPECTED_METHODS.select {|m| !subject.respond_to?(m) }

  if missing_methods.present?
    [:method_not_found, { methods: missing_methods.to_sentence }]
  else
    subject.password = CORRECT_PASSWORD
    subject.password_confirmation = CORRECT_PASSWORD

    if not subject.authenticate(CORRECT_PASSWORD)
      [:did_not_authenticate_correct_password, {}]
    elsif subject.authenticate(INCORRECT_PASSWORD)
      [:authenticated_incorrect_password, {}]
    end
  end
end