Commit c31513a2c8af0e6d339f7ac92ee0508ced052b3d

Authored by Rodrigo Souto
1 parent 63ca9209

forgot_password: allow password recovery with email OR login

Also provides infra to any other field in the future.

(ActionItem2857)
app/models/change_password.rb
... ... @@ -18,6 +18,13 @@ class ChangePassword < Task
18 18 end
19 19 end
20 20  
  21 + def self.fields_choice
  22 + [
  23 + [_('Username'), 'login'],
  24 + [_('Email'), 'email'],
  25 + ]
  26 + end
  27 +
21 28 ###################################################
22 29 # validations for creating a ChangePassword task
23 30  
... ...
app/views/account/forgot_password.rhtml
... ... @@ -4,10 +4,9 @@
4 4  
5 5 <% labelled_form_for :change_password, @change_password, :url => { :action => 'forgot_password' } do |f| %>
6 6  
7   - <%= f.text_field :login,
8   - :onchange => 'this.value = convToValidUsername( this.value )' %>
  7 + <%= labelled_form_field(_('Field'), f.select(:field, ChangePassword.fields_choice)) %>
9 8  
10   - <%= f.text_field :email %>
  9 + <%= f.text_field :value %>
11 10  
12 11 <%= f.hidden_field :environment_id, :value => environment.id %>
13 12  
... ...
test/functional/account_controller_test.rb
... ... @@ -215,12 +215,21 @@ class AccountControllerTest &lt; ActionController::TestCase
215 215 assert_response :success
216 216 end
217 217  
218   - should 'respond to forgotten password change request' do
  218 + should 'respond to forgotten password change request with login' do
219 219 change = ChangePassword.new
220   - ChangePassword.expects(:new).with('login' => 'test', 'email' => 'test@localhost.localdomain').returns(change)
  220 + ChangePassword.expects(:new).with('field' => 'login', 'value' => 'test').returns(change)
221 221 change.expects(:save!).returns(true)
222 222  
223   - post :forgot_password, :change_password => { :login => 'test', :email => 'test@localhost.localdomain' }
  223 + post :forgot_password, :change_password => { :field => 'login', :value => 'test' }
  224 + assert_template 'password_recovery_sent'
  225 + end
  226 +
  227 + should 'respond to forgotten password change request with email' do
  228 + change = ChangePassword.new
  229 + ChangePassword.expects(:new).with('field' => 'email', 'value' => 'test@localhost.localdomain').returns(change)
  230 + change.expects(:save!).returns(true)
  231 +
  232 + post :forgot_password, :change_password => { :field => 'email', :value => 'test@localhost.localdomain' }
224 233 assert_template 'password_recovery_sent'
225 234 end
226 235  
... ... @@ -267,7 +276,7 @@ class AccountControllerTest &lt; ActionController::TestCase
267 276  
268 277 should 'require password confirmation correctly to enter new pasword' do
269 278 user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
270   - change = ChangePassword.create!(:login => 'testuser', :email => 'testuser@example.com', :environment_id => Environment.default.id)
  279 + change = ChangePassword.create!(:field => 'login', :value => 'testuser', :environment_id => Environment.default.id)
271 280  
272 281 post :new_password, :code => change.code, :change_password => { :password => 'onepass', :password_confirmation => 'another_pass' }
273 282 assert_response :success
... ... @@ -853,7 +862,7 @@ class AccountControllerTest &lt; ActionController::TestCase
853 862 assert_response :redirect
854 863  
855 864 #Redirect on post action
856   - post :forgot_password, :change_password => { :login => 'test', :email => 'test@localhost.localdomain' }
  865 + post :forgot_password, :change_password => { :field => 'login', :value => 'test' }
857 866 assert_response :redirect
858 867 end
859 868  
... ...
test/integration/forgot_password_test.rb
... ... @@ -6,7 +6,7 @@ class ForgotPasswordTest &lt; ActionController::IntegrationTest
6 6 ActionController::Integration::Session.any_instance.stubs(:https?).returns(true)
7 7 end
8 8  
9   - def test_forgot_password
  9 + def test_forgot_password_with_login
10 10  
11 11 User.destroy_all
12 12 Profile.destroy_all
... ... @@ -19,7 +19,40 @@ class ForgotPasswordTest &lt; ActionController::IntegrationTest
19 19 assert_response :success
20 20 assert_tag :tag => 'form', :attributes => { :action => '/account/forgot_password', :method => 'post' }
21 21  
22   - post '/account/forgot_password', :change_password => { :login => 'forgotten', :email => 'forgotten@localhost.localdomain', :environment_id => Environment.default.id }
  22 + post '/account/forgot_password', :change_password => { :field => 'login', :value => 'forgotten', :environment_id => Environment.default.id }
  23 +
  24 + assert_response :success
  25 + assert_template 'password_recovery_sent'
  26 +
  27 + assert_equal 1, ChangePassword.count
  28 + code = ChangePassword.find(:first).code
  29 +
  30 + get "/account/new_password/#{code}"
  31 + assert_response :success
  32 + assert_tag :tag => 'form', :attributes => { :action => "/account/new_password/#{code}" }
  33 +
  34 + post "/account/new_password/#{code}", :change_password => { :password => 'newpass', :password_confirmation => 'newpass'}
  35 + assert_response :success
  36 + assert_template 'new_password_ok'
  37 + assert_tag :tag => 'a', :attributes => { :href => "/account/login" }
  38 +
  39 + login('forgotten', 'newpass')
  40 + end
  41 +
  42 + def test_forgot_password_with_email
  43 +
  44 + User.destroy_all
  45 + Profile.destroy_all
  46 + ChangePassword.destroy_all
  47 +
  48 + create_user('forgotten', :password => 'test', :password_confirmation => 'test', :email => 'forgotten@localhost.localdomain').activate
  49 +
  50 + get '/account/forgot_password'
  51 +
  52 + assert_response :success
  53 + assert_tag :tag => 'form', :attributes => { :action => '/account/forgot_password', :method => 'post' }
  54 +
  55 + post '/account/forgot_password', :change_password => { :field => 'email', :value => 'forgotten@localhost.localdomain', :environment_id => Environment.default.id }
23 56  
24 57 assert_response :success
25 58 assert_template 'password_recovery_sent'
... ...