Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transitioning password should not set password_changed? true #722

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pduey
Copy link

@pduey pduey commented May 28, 2020

If a password is being transitioned to a new crypto provider, I would expect password_changed? to be falsey, but it is true. This draft PR adds a test to demonstrate it. If you change the definition of transition_password as follows, the test will pass:

def transition_password(attempted_password)
    self.password = attempted_password
+   @password_changed = false
    save(validate: false)
end

In password= @password_changed is set to true, so this just inverts that. Other possible solutions I've thought of:

  1. don't call password= in transition_password, instead call encrypt right there (and whatever else might be required
  2. set a new instance variable @password_transitioned which can be used in conditions like password_changed?

Comment on lines +96 to +109
User.class_eval do
after_update :verify_not_changed, unless: :password_changed?
def verify_not_changed; end
end
ben = users(:ben)
mock_method = MiniTest::Mock.new
mock_method.expect :call, nil
ben.stub :verify_not_changed, mock_method do
transition_password_to(Authlogic::CryptoProviders::BCrypt, ben)
end
mock_method.verify
User.class_eval do
User.skip_callback :update, :after, :verify_not_changed
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something simpler work too, vs using callbacks? I don't think the record would get reloaded, so we could do e.g.

     ben = transition_password_to(Authlogic::CryptoProviders::BCrypt, users(:ben)).first
     refute ben.send(:password_changed?)`

Or, maybe we should just move this assertion into transition_password_to?

@florrain
Copy link

Bump on this @pduey , I'd be interested by this improvement!

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

Successfully merging this pull request may close these issues.

3 participants