current password requirement to change password

This commit is contained in:
zhitomirskiyi 2011-01-24 22:39:57 -08:00
parent 716899f919
commit 2314c08265
4 changed files with 25 additions and 40 deletions

View file

@ -27,8 +27,8 @@ class UsersController < ApplicationController
@user.update_attributes(:disable_mail => params[:user][:disable_mail]) @user.update_attributes(:disable_mail => params[:user][:disable_mail])
flash[:notice] = I18n.t 'users.update.email_notifications_changed' flash[:notice] = I18n.t 'users.update.email_notifications_changed'
# change passowrd # change passowrd
elsif params[:user][:password] && params[:user][:password_confirmation] elsif params[:user][:current_password] && params[:user][:password] && params[:user][:password_confirmation]
if @user.update_attributes(:password => params[:user][:password], :password_confirmation => params[:user][:password_confirmation]) if @user.update_with_password(params[:user])
flash[:notice] = I18n.t 'users.update.password_changed' flash[:notice] = I18n.t 'users.update.password_changed'
else else
flash[:error] = I18n.t 'users.update.password_not_changed' flash[:error] = I18n.t 'users.update.password_not_changed'

View file

@ -50,6 +50,9 @@
= form_for @user do |f| = form_for @user do |f|
= f.error_messages = f.error_messages
%p
= f.label :current_password, t('.current_password')
= f.password_field :current_password
%p %p
= f.label :password, t('.new_password') = f.label :password, t('.new_password')
= f.password_field :password = f.password_field :password

View file

@ -205,6 +205,7 @@ en:
change_language: "Change Language" change_language: "Change Language"
change_password: "Change Password" change_password: "Change Password"
new_password: "New Password" new_password: "New Password"
current_password: "Current password"
download_xml: "download my xml" download_xml: "download my xml"
download_photos: "download my photos" download_photos: "download my photos"
your_handle: "Your diaspora handle" your_handle: "Your diaspora handle"

View file

@ -7,14 +7,10 @@ require 'spec_helper'
describe UsersController do describe UsersController do
render_views render_views
let(:user) { alice }
let!(:aspect) { user.aspects.first }
let!(:old_password) { user.encrypted_password }
let!(:old_language) { user.language }
before do before do
sign_in :user, user @user = alice
@aspect = @user.aspects.first
sign_in :user, @user
end end
describe '#export' do describe '#export' do
@ -26,59 +22,44 @@ describe UsersController do
describe '#update' do describe '#update' do
it "doesn't overwrite random attributes" do it "doesn't overwrite random attributes" do
params = { :id => user.id, params = { :id => @user.id,
:user => { :diaspora_handle => "notreal@stuff.com" } } :user => { :diaspora_handle => "notreal@stuff.com" } }
lambda { lambda {
put :update, params put :update, params
}.should_not change(user, :diaspora_handle) }.should_not change(@user, :diaspora_handle)
end end
context 'password updates' do context 'password updates' do
it 'allows a user to change his password' do before do
put(:update, :id => user.id, :user => @password_params = {:current_password => 'bluepin7',
{ :password => "foobaz", :password => "foobaz",
:password_confirmation => "foobaz" } :password_confirmation => "foobaz"}
)
user.reload
user.encrypted_password.should_not == old_password
end end
it 'requires a matching password confirmation' do it "uses devise's update with password" do
put(:update, :id => user.id, :user => @user.should_receive(:update_with_password).with(hash_including(@password_params))
{ :password => "foobarz", @controller.stub!(:current_user).and_return(@user)
:password_confirmation => "not_the_same"} put :update, :id => @user.id, :user => @password_params
)
user.reload
user.encrypted_password.should == old_password
end
it 'does not update if the password fields are left blank' do
put(:update, :id => user.id, :user =>
{ :password => "",
:password_confirmation => ""}
)
user.reload
user.encrypted_password.should == old_password
end end
end end
describe 'language' do describe 'language' do
it 'allow the user to change his language' do it 'allow the user to change his language' do
old_language = 'en' old_language = 'en'
user.language = old_language @user.language = old_language
user.save @user.save
put(:update, :id => user.id, :user => put(:update, :id => @user.id, :user =>
{ :language => "fr"} { :language => "fr"}
) )
user.reload @user.reload
user.language.should_not == old_language @user.language.should_not == old_language
end end
end end
end end
describe '#edit' do describe '#edit' do
it "returns a 200" do it "returns a 200" do
get 'edit', :id => user.id get 'edit', :id => @user.id
response.status.should == 200 response.status.should == 200
end end
end end