From 2e2f4f66e825c602e435d5aab76a62c2414f5d29 Mon Sep 17 00:00:00 2001 From: yoshihara Date: Wed, 8 Apr 2015 15:50:17 +0900 Subject: [PATCH] Fix the bug that password is changed unexpectedly when confirm is wrong model spec is broken, so i fix it. --- app/models/user.rb | 4 +++- spec/features/users_spec.rb | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index f432e62..ae410bd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -57,7 +57,9 @@ class User end def valid_password_confirmation - password == password_confirmation + unless password == password_confirmation + errors.add(:current_password, :wrong_password) + end end def stretching_cost diff --git a/spec/features/users_spec.rb b/spec/features/users_spec.rb index bfc52ab..75c336f 100644 --- a/spec/features/users_spec.rb +++ b/spec/features/users_spec.rb @@ -7,5 +7,44 @@ describe "users" do end describe "edit" do + let!(:user) { build(:user) } + + before do + login_with user + end + + after do + # reset password to the default + FileUtils.rm_rf(User::ENCRYPTED_PASSWORD_FILE) + end + + describe 'to change password' do + let(:password) { 'new_password' } + + before do + visit user_path + fill_in 'user[current_password]', with: user.password + + fill_in 'user[password]', with: password + fill_in 'user[password_confirmation]', with: password_confirmation + find('input[type="submit"]').click + end + + context 'when valid new password/confirmation is input' do + let(:password_confirmation) { password } + + it 'should update users password with new password' do + expect(page).to have_css('.alert-success') + end + end + + context 'when invalid new password/confirmation is input' do + let(:password_confirmation) { 'invalid_password' } + + it 'should not update users password with new password' do + expect(page).to have_css('.alert-danger') + end + end + end end end