diff --git a/app/models/change_password.rb b/app/models/change_password.rb index 22f8e8b..741f8de 100644 --- a/app/models/change_password.rb +++ b/app/models/change_password.rb @@ -18,6 +18,13 @@ class ChangePassword < Task end end + def self.fields_choice + [ + [_('Username'), 'login'], + [_('Email'), 'email'], + ] + end + ################################################### # validations for creating a ChangePassword task diff --git a/app/views/account/forgot_password.rhtml b/app/views/account/forgot_password.rhtml index 1e3748d..0eb043a 100644 --- a/app/views/account/forgot_password.rhtml +++ b/app/views/account/forgot_password.rhtml @@ -4,10 +4,9 @@ <% labelled_form_for :change_password, @change_password, :url => { :action => 'forgot_password' } do |f| %> - <%= f.text_field :login, - :onchange => 'this.value = convToValidUsername( this.value )' %> + <%= labelled_form_field(_('Field'), f.select(:field, ChangePassword.fields_choice)) %> - <%= f.text_field :email %> + <%= f.text_field :value %> <%= f.hidden_field :environment_id, :value => environment.id %> diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 4245a37..330cd65 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -215,12 +215,21 @@ class AccountControllerTest < ActionController::TestCase assert_response :success end - should 'respond to forgotten password change request' do + should 'respond to forgotten password change request with login' do change = ChangePassword.new - ChangePassword.expects(:new).with('login' => 'test', 'email' => 'test@localhost.localdomain').returns(change) + ChangePassword.expects(:new).with('field' => 'login', 'value' => 'test').returns(change) change.expects(:save!).returns(true) - post :forgot_password, :change_password => { :login => 'test', :email => 'test@localhost.localdomain' } + post :forgot_password, :change_password => { :field => 'login', :value => 'test' } + assert_template 'password_recovery_sent' + end + + should 'respond to forgotten password change request with email' do + change = ChangePassword.new + ChangePassword.expects(:new).with('field' => 'email', 'value' => 'test@localhost.localdomain').returns(change) + change.expects(:save!).returns(true) + + post :forgot_password, :change_password => { :field => 'email', :value => 'test@localhost.localdomain' } assert_template 'password_recovery_sent' end @@ -267,7 +276,7 @@ class AccountControllerTest < ActionController::TestCase should 'require password confirmation correctly to enter new pasword' do user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') - change = ChangePassword.create!(:login => 'testuser', :email => 'testuser@example.com', :environment_id => Environment.default.id) + change = ChangePassword.create!(:field => 'login', :value => 'testuser', :environment_id => Environment.default.id) post :new_password, :code => change.code, :change_password => { :password => 'onepass', :password_confirmation => 'another_pass' } assert_response :success @@ -853,7 +862,7 @@ class AccountControllerTest < ActionController::TestCase assert_response :redirect #Redirect on post action - post :forgot_password, :change_password => { :login => 'test', :email => 'test@localhost.localdomain' } + post :forgot_password, :change_password => { :field => 'login', :value => 'test' } assert_response :redirect end diff --git a/test/integration/forgot_password_test.rb b/test/integration/forgot_password_test.rb index 1fb5c6f..116278f 100644 --- a/test/integration/forgot_password_test.rb +++ b/test/integration/forgot_password_test.rb @@ -6,7 +6,7 @@ class ForgotPasswordTest < ActionController::IntegrationTest ActionController::Integration::Session.any_instance.stubs(:https?).returns(true) end - def test_forgot_password + def test_forgot_password_with_login User.destroy_all Profile.destroy_all @@ -19,7 +19,40 @@ class ForgotPasswordTest < ActionController::IntegrationTest assert_response :success assert_tag :tag => 'form', :attributes => { :action => '/account/forgot_password', :method => 'post' } - post '/account/forgot_password', :change_password => { :login => 'forgotten', :email => 'forgotten@localhost.localdomain', :environment_id => Environment.default.id } + post '/account/forgot_password', :change_password => { :field => 'login', :value => 'forgotten', :environment_id => Environment.default.id } + + assert_response :success + assert_template 'password_recovery_sent' + + assert_equal 1, ChangePassword.count + code = ChangePassword.find(:first).code + + get "/account/new_password/#{code}" + assert_response :success + assert_tag :tag => 'form', :attributes => { :action => "/account/new_password/#{code}" } + + post "/account/new_password/#{code}", :change_password => { :password => 'newpass', :password_confirmation => 'newpass'} + assert_response :success + assert_template 'new_password_ok' + assert_tag :tag => 'a', :attributes => { :href => "/account/login" } + + login('forgotten', 'newpass') + end + + def test_forgot_password_with_email + + User.destroy_all + Profile.destroy_all + ChangePassword.destroy_all + + create_user('forgotten', :password => 'test', :password_confirmation => 'test', :email => 'forgotten@localhost.localdomain').activate + + get '/account/forgot_password' + + assert_response :success + assert_tag :tag => 'form', :attributes => { :action => '/account/forgot_password', :method => 'post' } + + post '/account/forgot_password', :change_password => { :field => 'email', :value => 'forgotten@localhost.localdomain', :environment_id => Environment.default.id } assert_response :success assert_template 'password_recovery_sent' -- libgit2 0.21.2