class Shoulda::Matchers::ActiveModel::HaveSecurePasswordMatcher

@private

Constants

CORRECT_PASSWORD
INCORRECT_PASSWORD
MESSAGES

Attributes

failure_message[R]
subject[R]

Public Class Methods

new(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 54
def initialize(attribute)
  @attribute = attribute.to_sym
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 58
def description
  "have a secure password, defined on #{@attribute} attribute"
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 74
def failure_message_when_negated
  MESSAGES[:should_not_have_secure_password] %
    { subject: @subject.class, description: description }
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 62
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 83
def validate
  missing_methods = expected_methods.reject do |m|
    subject.respond_to?(m)
  end

  if missing_methods.present?
    [:method_not_found, { methods: missing_methods.to_sentence }]
  else
    subject.send("#{@attribute}=", CORRECT_PASSWORD)
    subject.send("#{@attribute}_confirmation=", CORRECT_PASSWORD)

    if not subject.send(authenticate_method, CORRECT_PASSWORD)
      [:did_not_authenticate_correct_password,
       { attribute: @attribute },]
    elsif subject.send(authenticate_method, INCORRECT_PASSWORD)
      [:authenticated_incorrect_password, { attribute: @attribute }]
    end
  end
end

Private Instance Methods

authenticate_method() click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 115
def authenticate_method
  if @attribute == :password
    :authenticate
  else
    "authenticate_#{@attribute}".to_sym
  end
end
expected_methods() click to toggle source
# File lib/shoulda/matchers/active_model/have_secure_password_matcher.rb, line 105
def expected_methods
  @_expected_methods ||= %I[
    #{authenticate_method}
    #{@attribute}=
    #{@attribute}_confirmation=
    #{@attribute}_digest
    #{@attribute}_digest=
  ]
end