Skip to content

Instantly share code, notes, and snippets.

@gumayunov
Created January 10, 2009 08:58
Show Gist options
  • Save gumayunov/45420 to your computer and use it in GitHub Desktop.
Save gumayunov/45420 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
acts_as_authentic
attr_accessor :old_password
validates_each :old_password, :on => :update, :if => :crypted_password_changed? do |record, attr, old_password|
unless (old_password.nil? || record.valid_old_password?(old_password))
record.errors.add attr, self.acts_as_authentic_config[:old_password_did_not_match_message]
end
end
def valid_old_password?(old_password)
params = [crypted_password_was, old_password, password_salt_was]
self.class.acts_as_authentic_config[:crypto_provider].matches?(*params)
end
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe User, "updating password" do
before :each do
@user = User.spawn
@user.password = "111111"
@user.password_confirmation = "111111"
@user.save!
@incurrect_set = {:password=>"123", :password_confirmation=>"123", :old_password=>"incorrect"}
@correct_set = {:password=>"123", :password_confirmation=>"123", :old_password=>"111111"}
end
it "should be invalid with empty old_password" do
@user.update_attributes(:password=>"123", :password_confirmation=>"123", :old_password=>"").should == false
end
it "should be invalid with incorrent old_password" do
@user.update_attributes(@incurrect_set).should == false
end
it "should update password without old_password" do
@user.update_attributes(:password=>"123", :password_confirmation=>"123").should == true
@user.valid_password?("123").should == true
end
it "should update password with correct old_password and valid new password" do
@user.update_attributes(@correct_set).should == true
@user.valid_password?("123").should == true
end
it "should add configured error message if old_password didn't match" do
User.acts_as_authentic_config[:old_password_did_not_match_message] = "custome message"
@user.update_attributes(:password=>"123", :password_confirmation=>"123", :old_password=>"incorrect")
@user.errors.on(:old_password).should == "custome message"
end
it "should work several times with incorrect old_password" do
@user.update_attributes(@incurrect_set).should == false
@user.update_attributes(@incurrect_set).should == false
end
it "should work several times with correct old_password" do
@user.update_attributes(@correct_set).should == true
@correct_set[:old_password] = "123"
@user.update_attributes(@correct_set).should == true
end
end
describe User, "updating non password fields" do
before :each do
@user = User.spawn
@user.password = "111111"
@user.password_confirmation = "111111"
@user.save!
end
it "should update without old_password" do
@user.update_attributes(:last_name=>'saldon').should == true
end
it "should update with empty old_password" do
@user.update_attributes(:last_name=>'saldon', :old_password=>"").should == true
end
it "should update with incorrect old_password" do
@user.update_attributes(:last_name=>'saldon', :old_password=>"231").should == true
end
it "should update with correct old_password" do
@user.update_attributes(:last_name=>'saldon', :old_password=>"111111").should == true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment