diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 199cb3e..80686b5 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -75,6 +75,7 @@ class AccountController < PublicController # Posts back. def forgot_password @change_password = ChangePassword.new(params[:change_password]) + if request.post? begin @change_password.save! diff --git a/app/models/change_password.rb b/app/models/change_password.rb index 3f80698..48892f8 100644 --- a/app/models/change_password.rb +++ b/app/models/change_password.rb @@ -72,9 +72,10 @@ class ChangePassword < Task def create_message hostname = self.requestor.environment.default_hostname code = self.code + url = generate_url(:host => hostname, :controller => 'account', :action => 'new_password', :code => code) lambda do - _("In order to change your password, please visit the following address:\n\n%s") % url_for(:host => hostname, :controller => 'account', :action => 'new_password', :code => code) + _("In order to change your password, please visit the following address:\n\n%s") % url end end diff --git a/lib/noosfero/url.rb b/lib/noosfero/url.rb index 2c33227..9fe14dd 100644 --- a/lib/noosfero/url.rb +++ b/lib/noosfero/url.rb @@ -45,11 +45,11 @@ module Noosfero::URL Noosfero::URL.config['path'] end - def url_for(options) + def generate_url(options) local_options = {} local_options[:port] = self.port unless self.port.nil? - url = super(local_options.merge(options)) + url = url_for(local_options.merge(options)) if self.path.blank? url diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 37ad537..6715077 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -184,10 +184,10 @@ class AccountControllerTest < Test::Unit::TestCase should 'respond to forgotten password change request' do change = ChangePassword.new - ChangePassword.expects(:new).returns(change) + ChangePassword.expects(:new).with('login' => 'test', 'email' => 'test@localhost.localdomain').returns(change) change.expects(:save!).returns(true) - post :forgot_password + post :forgot_password, :change_password => { :login => 'test', :email => 'test@localhost.localdomain' } assert_template 'password_recovery_sent' end diff --git a/test/integration/forgot_password_test.rb b/test/integration/forgot_password_test.rb new file mode 100644 index 0000000..99a9ca2 --- /dev/null +++ b/test/integration/forgot_password_test.rb @@ -0,0 +1,38 @@ +require "#{File.dirname(__FILE__)}/../test_helper" + +class ForgotPasswordTest < ActionController::IntegrationTest + + def test_forgot_password + + User.destroy_all + Profile.destroy_all + ChangePassword.destroy_all + + User.create!(:login => 'forgotten', :password => 'test', :password_confirmation => 'test', :email => 'forgotten@localhost.localdomain') + + get '/account/forgot_password' + + 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' } + + 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" } + + assert User.find_by_login('forgotten').authenticated?('newpass') + end + +end diff --git a/test/unit/noosfero_url_test.rb b/test/unit/noosfero_url_test.rb index 2d208fd..1574eab 100644 --- a/test/unit/noosfero_url_test.rb +++ b/test/unit/noosfero_url_test.rb @@ -37,13 +37,13 @@ class NoosferoURLTest < Test::Unit::TestCase should 'add path when needed' do self.stubs(:path).returns('/somepath') self.stubs(:port).returns(nil) - assert_equal('http://example.com/somepath/', url_for(:host => 'example.com', :controller => 'home')) + assert_equal('http://example.com/somepath/', generate_url(:host => 'example.com', :controller => 'home')) end should 'not add path when it is not needed' do self.stubs(:path).returns(nil) self.stubs(:port).returns(nil) - assert_equal('http://example.com/', url_for(:host => 'example.com', :controller => 'home')) + assert_equal('http://example.com/', generate_url(:host => 'example.com', :controller => 'home')) end end -- libgit2 0.21.2