##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r9763:c11f5a23fee2
parent child
Show More
@@ -1,291 +1,295
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class AccountController < ApplicationController
18 class AccountController < ApplicationController
19 helper :custom_fields
19 helper :custom_fields
20 include CustomFieldsHelper
20 include CustomFieldsHelper
21
21
22 # prevents login action to be filtered by check_if_login_required application scope filter
22 # prevents login action to be filtered by check_if_login_required application scope filter
23 skip_before_filter :check_if_login_required
23 skip_before_filter :check_if_login_required
24
24
25 # Login request and validation
25 # Login request and validation
26 def login
26 def login
27 if request.get?
27 if request.get?
28 logout_user
28 logout_user
29 else
29 else
30 authenticate_user
30 authenticate_user
31 end
31 end
32 rescue AuthSourceException => e
32 rescue AuthSourceException => e
33 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
33 logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
34 render_error :message => e.message
34 render_error :message => e.message
35 end
35 end
36
36
37 # Log out current user and redirect to welcome page
37 # Log out current user and redirect to welcome page
38 def logout
38 def logout
39 logout_user
39 logout_user
40 redirect_to home_url
40 redirect_to home_url
41 end
41 end
42
42
43 # Lets user choose a new password
43 # Lets user choose a new password
44 def lost_password
44 def lost_password
45 redirect_to(home_url) && return unless Setting.lost_password?
45 redirect_to(home_url) && return unless Setting.lost_password?
46 if params[:token]
46 if params[:token]
47 @token = Token.find_by_action_and_value("recovery", params[:token].to_s)
47 @token = Token.find_by_action_and_value("recovery", params[:token].to_s)
48 if @token.nil? || @token.expired?
48 if @token.nil? || @token.expired?
49 redirect_to home_url
49 redirect_to home_url
50 return
50 return
51 end
51 end
52 @user = @token.user
52 @user = @token.user
53 unless @user && @user.active?
54 redirect_to home_url
55 return
56 end
53 if request.post?
57 if request.post?
54 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
58 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
55 if @user.save
59 if @user.save
56 @token.destroy
60 @token.destroy
57 flash[:notice] = l(:notice_account_password_updated)
61 flash[:notice] = l(:notice_account_password_updated)
58 redirect_to signin_path
62 redirect_to signin_path
59 return
63 return
60 end
64 end
61 end
65 end
62 render :template => "account/password_recovery"
66 render :template => "account/password_recovery"
63 return
67 return
64 else
68 else
65 if request.post?
69 if request.post?
66 user = User.find_by_mail(params[:mail].to_s)
70 user = User.find_by_mail(params[:mail].to_s)
67 # user not found or not active
71 # user not found or not active
68 unless user && user.active?
72 unless user && user.active?
69 flash.now[:error] = l(:notice_account_unknown_email)
73 flash.now[:error] = l(:notice_account_unknown_email)
70 return
74 return
71 end
75 end
72 # user cannot change its password
76 # user cannot change its password
73 unless user.change_password_allowed?
77 unless user.change_password_allowed?
74 flash.now[:error] = l(:notice_can_t_change_password)
78 flash.now[:error] = l(:notice_can_t_change_password)
75 return
79 return
76 end
80 end
77 # create a new token for password recovery
81 # create a new token for password recovery
78 token = Token.new(:user => user, :action => "recovery")
82 token = Token.new(:user => user, :action => "recovery")
79 if token.save
83 if token.save
80 Mailer.lost_password(token).deliver
84 Mailer.lost_password(token).deliver
81 flash[:notice] = l(:notice_account_lost_email_sent)
85 flash[:notice] = l(:notice_account_lost_email_sent)
82 redirect_to signin_path
86 redirect_to signin_path
83 return
87 return
84 end
88 end
85 end
89 end
86 end
90 end
87 end
91 end
88
92
89 # User self-registration
93 # User self-registration
90 def register
94 def register
91 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
95 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
92 if request.get?
96 if request.get?
93 session[:auth_source_registration] = nil
97 session[:auth_source_registration] = nil
94 @user = User.new(:language => Setting.default_language)
98 @user = User.new(:language => Setting.default_language)
95 else
99 else
96 user_params = params[:user] || {}
100 user_params = params[:user] || {}
97 @user = User.new
101 @user = User.new
98 @user.safe_attributes = user_params
102 @user.safe_attributes = user_params
99 @user.admin = false
103 @user.admin = false
100 @user.register
104 @user.register
101 if session[:auth_source_registration]
105 if session[:auth_source_registration]
102 @user.activate
106 @user.activate
103 @user.login = session[:auth_source_registration][:login]
107 @user.login = session[:auth_source_registration][:login]
104 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
108 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
105 if @user.save
109 if @user.save
106 session[:auth_source_registration] = nil
110 session[:auth_source_registration] = nil
107 self.logged_user = @user
111 self.logged_user = @user
108 flash[:notice] = l(:notice_account_activated)
112 flash[:notice] = l(:notice_account_activated)
109 redirect_to :controller => 'my', :action => 'account'
113 redirect_to :controller => 'my', :action => 'account'
110 end
114 end
111 else
115 else
112 @user.login = params[:user][:login]
116 @user.login = params[:user][:login]
113 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
117 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
114 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
118 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
115 end
119 end
116
120
117 case Setting.self_registration
121 case Setting.self_registration
118 when '1'
122 when '1'
119 register_by_email_activation(@user)
123 register_by_email_activation(@user)
120 when '3'
124 when '3'
121 register_automatically(@user)
125 register_automatically(@user)
122 else
126 else
123 register_manually_by_administrator(@user)
127 register_manually_by_administrator(@user)
124 end
128 end
125 end
129 end
126 end
130 end
127 end
131 end
128
132
129 # Token based account activation
133 # Token based account activation
130 def activate
134 def activate
131 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
135 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
132 token = Token.find_by_action_and_value('register', params[:token])
136 token = Token.find_by_action_and_value('register', params[:token])
133 redirect_to(home_url) && return unless token and !token.expired?
137 redirect_to(home_url) && return unless token and !token.expired?
134 user = token.user
138 user = token.user
135 redirect_to(home_url) && return unless user.registered?
139 redirect_to(home_url) && return unless user.registered?
136 user.activate
140 user.activate
137 if user.save
141 if user.save
138 token.destroy
142 token.destroy
139 flash[:notice] = l(:notice_account_activated)
143 flash[:notice] = l(:notice_account_activated)
140 end
144 end
141 redirect_to signin_path
145 redirect_to signin_path
142 end
146 end
143
147
144 private
148 private
145
149
146 def authenticate_user
150 def authenticate_user
147 if Setting.openid? && using_open_id?
151 if Setting.openid? && using_open_id?
148 open_id_authenticate(params[:openid_url])
152 open_id_authenticate(params[:openid_url])
149 else
153 else
150 password_authentication
154 password_authentication
151 end
155 end
152 end
156 end
153
157
154 def password_authentication
158 def password_authentication
155 user = User.try_to_login(params[:username], params[:password])
159 user = User.try_to_login(params[:username], params[:password])
156
160
157 if user.nil?
161 if user.nil?
158 invalid_credentials
162 invalid_credentials
159 elsif user.new_record?
163 elsif user.new_record?
160 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
164 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
161 else
165 else
162 # Valid user
166 # Valid user
163 successful_authentication(user)
167 successful_authentication(user)
164 end
168 end
165 end
169 end
166
170
167 def open_id_authenticate(openid_url)
171 def open_id_authenticate(openid_url)
168 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
172 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
169 if result.successful?
173 if result.successful?
170 user = User.find_or_initialize_by_identity_url(identity_url)
174 user = User.find_or_initialize_by_identity_url(identity_url)
171 if user.new_record?
175 if user.new_record?
172 # Self-registration off
176 # Self-registration off
173 redirect_to(home_url) && return unless Setting.self_registration?
177 redirect_to(home_url) && return unless Setting.self_registration?
174
178
175 # Create on the fly
179 # Create on the fly
176 user.login = registration['nickname'] unless registration['nickname'].nil?
180 user.login = registration['nickname'] unless registration['nickname'].nil?
177 user.mail = registration['email'] unless registration['email'].nil?
181 user.mail = registration['email'] unless registration['email'].nil?
178 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
182 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
179 user.random_password
183 user.random_password
180 user.register
184 user.register
181
185
182 case Setting.self_registration
186 case Setting.self_registration
183 when '1'
187 when '1'
184 register_by_email_activation(user) do
188 register_by_email_activation(user) do
185 onthefly_creation_failed(user)
189 onthefly_creation_failed(user)
186 end
190 end
187 when '3'
191 when '3'
188 register_automatically(user) do
192 register_automatically(user) do
189 onthefly_creation_failed(user)
193 onthefly_creation_failed(user)
190 end
194 end
191 else
195 else
192 register_manually_by_administrator(user) do
196 register_manually_by_administrator(user) do
193 onthefly_creation_failed(user)
197 onthefly_creation_failed(user)
194 end
198 end
195 end
199 end
196 else
200 else
197 # Existing record
201 # Existing record
198 if user.active?
202 if user.active?
199 successful_authentication(user)
203 successful_authentication(user)
200 else
204 else
201 account_pending
205 account_pending
202 end
206 end
203 end
207 end
204 end
208 end
205 end
209 end
206 end
210 end
207
211
208 def successful_authentication(user)
212 def successful_authentication(user)
209 # Valid user
213 # Valid user
210 self.logged_user = user
214 self.logged_user = user
211 # generate a key and set cookie if autologin
215 # generate a key and set cookie if autologin
212 if params[:autologin] && Setting.autologin?
216 if params[:autologin] && Setting.autologin?
213 set_autologin_cookie(user)
217 set_autologin_cookie(user)
214 end
218 end
215 call_hook(:controller_account_success_authentication_after, {:user => user })
219 call_hook(:controller_account_success_authentication_after, {:user => user })
216 redirect_back_or_default :controller => 'my', :action => 'page'
220 redirect_back_or_default :controller => 'my', :action => 'page'
217 end
221 end
218
222
219 def set_autologin_cookie(user)
223 def set_autologin_cookie(user)
220 token = Token.create(:user => user, :action => 'autologin')
224 token = Token.create(:user => user, :action => 'autologin')
221 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
225 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
222 cookie_options = {
226 cookie_options = {
223 :value => token.value,
227 :value => token.value,
224 :expires => 1.year.from_now,
228 :expires => 1.year.from_now,
225 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
229 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
226 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
230 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
227 :httponly => true
231 :httponly => true
228 }
232 }
229 cookies[cookie_name] = cookie_options
233 cookies[cookie_name] = cookie_options
230 end
234 end
231
235
232 # Onthefly creation failed, display the registration form to fill/fix attributes
236 # Onthefly creation failed, display the registration form to fill/fix attributes
233 def onthefly_creation_failed(user, auth_source_options = { })
237 def onthefly_creation_failed(user, auth_source_options = { })
234 @user = user
238 @user = user
235 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
239 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
236 render register_path
240 render register_path
237 end
241 end
238
242
239 def invalid_credentials
243 def invalid_credentials
240 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
244 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
241 flash.now[:error] = l(:notice_account_invalid_creditentials)
245 flash.now[:error] = l(:notice_account_invalid_creditentials)
242 end
246 end
243
247
244 # Register a user for email activation.
248 # Register a user for email activation.
245 #
249 #
246 # Pass a block for behavior when a user fails to save
250 # Pass a block for behavior when a user fails to save
247 def register_by_email_activation(user, &block)
251 def register_by_email_activation(user, &block)
248 token = Token.new(:user => user, :action => "register")
252 token = Token.new(:user => user, :action => "register")
249 if user.save and token.save
253 if user.save and token.save
250 Mailer.register(token).deliver
254 Mailer.register(token).deliver
251 flash[:notice] = l(:notice_account_register_done)
255 flash[:notice] = l(:notice_account_register_done)
252 redirect_to signin_path
256 redirect_to signin_path
253 else
257 else
254 yield if block_given?
258 yield if block_given?
255 end
259 end
256 end
260 end
257
261
258 # Automatically register a user
262 # Automatically register a user
259 #
263 #
260 # Pass a block for behavior when a user fails to save
264 # Pass a block for behavior when a user fails to save
261 def register_automatically(user, &block)
265 def register_automatically(user, &block)
262 # Automatic activation
266 # Automatic activation
263 user.activate
267 user.activate
264 user.last_login_on = Time.now
268 user.last_login_on = Time.now
265 if user.save
269 if user.save
266 self.logged_user = user
270 self.logged_user = user
267 flash[:notice] = l(:notice_account_activated)
271 flash[:notice] = l(:notice_account_activated)
268 redirect_to :controller => 'my', :action => 'account'
272 redirect_to :controller => 'my', :action => 'account'
269 else
273 else
270 yield if block_given?
274 yield if block_given?
271 end
275 end
272 end
276 end
273
277
274 # Manual activation by the administrator
278 # Manual activation by the administrator
275 #
279 #
276 # Pass a block for behavior when a user fails to save
280 # Pass a block for behavior when a user fails to save
277 def register_manually_by_administrator(user, &block)
281 def register_manually_by_administrator(user, &block)
278 if user.save
282 if user.save
279 # Sends an email to the administrators
283 # Sends an email to the administrators
280 Mailer.account_activation_request(user).deliver
284 Mailer.account_activation_request(user).deliver
281 account_pending
285 account_pending
282 else
286 else
283 yield if block_given?
287 yield if block_given?
284 end
288 end
285 end
289 end
286
290
287 def account_pending
291 def account_pending
288 flash[:notice] = l(:notice_account_pending)
292 flash[:notice] = l(:notice_account_pending)
289 redirect_to signin_path
293 redirect_to signin_path
290 end
294 end
291 end
295 end
@@ -1,189 +1,243
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'account_controller'
19 require 'account_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class AccountController; def rescue_action(e) raise e end; end
22 class AccountController; def rescue_action(e) raise e end; end
23
23
24 class AccountControllerTest < ActionController::TestCase
24 class AccountControllerTest < ActionController::TestCase
25 fixtures :users, :roles
25 fixtures :users, :roles
26
26
27 def setup
27 def setup
28 @controller = AccountController.new
28 @controller = AccountController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_login_should_redirect_to_back_url_param
34 def test_login_should_redirect_to_back_url_param
35 # request.uri is "test.host" in test environment
35 # request.uri is "test.host" in test environment
36 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1'
36 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1'
37 assert_redirected_to '/issues/show/1'
37 assert_redirected_to '/issues/show/1'
38 end
38 end
39
39
40 def test_login_should_not_redirect_to_another_host
40 def test_login_should_not_redirect_to_another_host
41 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake'
41 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake'
42 assert_redirected_to '/my/page'
42 assert_redirected_to '/my/page'
43 end
43 end
44
44
45 def test_login_with_wrong_password
45 def test_login_with_wrong_password
46 post :login, :username => 'admin', :password => 'bad'
46 post :login, :username => 'admin', :password => 'bad'
47 assert_response :success
47 assert_response :success
48 assert_template 'login'
48 assert_template 'login'
49 assert_tag 'div',
49 assert_tag 'div',
50 :attributes => { :class => "flash error" },
50 :attributes => { :class => "flash error" },
51 :content => /Invalid user or password/
51 :content => /Invalid user or password/
52 end
52 end
53
53
54 def test_login_should_rescue_auth_source_exception
54 def test_login_should_rescue_auth_source_exception
55 source = AuthSource.create!(:name => 'Test')
55 source = AuthSource.create!(:name => 'Test')
56 User.find(2).update_attribute :auth_source_id, source.id
56 User.find(2).update_attribute :auth_source_id, source.id
57 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
57 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
58
58
59 post :login, :username => 'jsmith', :password => 'jsmith'
59 post :login, :username => 'jsmith', :password => 'jsmith'
60 assert_response 500
60 assert_response 500
61 assert_error_tag :content => /Something wrong/
61 assert_error_tag :content => /Something wrong/
62 end
62 end
63
63
64 def test_login_should_reset_session
64 def test_login_should_reset_session
65 @controller.expects(:reset_session).once
65 @controller.expects(:reset_session).once
66
66
67 post :login, :username => 'jsmith', :password => 'jsmith'
67 post :login, :username => 'jsmith', :password => 'jsmith'
68 assert_response 302
68 assert_response 302
69 end
69 end
70
70
71 def test_logout
71 def test_logout
72 @request.session[:user_id] = 2
72 @request.session[:user_id] = 2
73 get :logout
73 get :logout
74 assert_redirected_to '/'
74 assert_redirected_to '/'
75 assert_nil @request.session[:user_id]
75 assert_nil @request.session[:user_id]
76 end
76 end
77
77
78 def test_logout_should_reset_session
78 def test_logout_should_reset_session
79 @controller.expects(:reset_session).once
79 @controller.expects(:reset_session).once
80
80
81 @request.session[:user_id] = 2
81 @request.session[:user_id] = 2
82 get :logout
82 get :logout
83 assert_response 302
83 assert_response 302
84 end
84 end
85
85
86 def test_get_register_with_registration_on
86 def test_get_register_with_registration_on
87 with_settings :self_registration => '3' do
87 with_settings :self_registration => '3' do
88 get :register
88 get :register
89 assert_response :success
89 assert_response :success
90 assert_template 'register'
90 assert_template 'register'
91 assert_not_nil assigns(:user)
91 assert_not_nil assigns(:user)
92
92
93 assert_tag 'input', :attributes => {:name => 'user[password]'}
93 assert_tag 'input', :attributes => {:name => 'user[password]'}
94 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
94 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
95 end
95 end
96 end
96 end
97
97
98 def test_get_register_with_registration_off_should_redirect
98 def test_get_register_with_registration_off_should_redirect
99 with_settings :self_registration => '0' do
99 with_settings :self_registration => '0' do
100 get :register
100 get :register
101 assert_redirected_to '/'
101 assert_redirected_to '/'
102 end
102 end
103 end
103 end
104
104
105 # See integration/account_test.rb for the full test
105 # See integration/account_test.rb for the full test
106 def test_post_register_with_registration_on
106 def test_post_register_with_registration_on
107 with_settings :self_registration => '3' do
107 with_settings :self_registration => '3' do
108 assert_difference 'User.count' do
108 assert_difference 'User.count' do
109 post :register, :user => {
109 post :register, :user => {
110 :login => 'register',
110 :login => 'register',
111 :password => 'test',
111 :password => 'test',
112 :password_confirmation => 'test',
112 :password_confirmation => 'test',
113 :firstname => 'John',
113 :firstname => 'John',
114 :lastname => 'Doe',
114 :lastname => 'Doe',
115 :mail => 'register@example.com'
115 :mail => 'register@example.com'
116 }
116 }
117 assert_redirected_to '/my/account'
117 assert_redirected_to '/my/account'
118 end
118 end
119 user = User.first(:order => 'id DESC')
119 user = User.first(:order => 'id DESC')
120 assert_equal 'register', user.login
120 assert_equal 'register', user.login
121 assert_equal 'John', user.firstname
121 assert_equal 'John', user.firstname
122 assert_equal 'Doe', user.lastname
122 assert_equal 'Doe', user.lastname
123 assert_equal 'register@example.com', user.mail
123 assert_equal 'register@example.com', user.mail
124 assert user.check_password?('test')
124 assert user.check_password?('test')
125 assert user.active?
125 assert user.active?
126 end
126 end
127 end
127 end
128
128
129 def test_post_register_with_registration_off_should_redirect
129 def test_post_register_with_registration_off_should_redirect
130 with_settings :self_registration => '0' do
130 with_settings :self_registration => '0' do
131 assert_no_difference 'User.count' do
131 assert_no_difference 'User.count' do
132 post :register, :user => {
132 post :register, :user => {
133 :login => 'register',
133 :login => 'register',
134 :password => 'test',
134 :password => 'test',
135 :password_confirmation => 'test',
135 :password_confirmation => 'test',
136 :firstname => 'John',
136 :firstname => 'John',
137 :lastname => 'Doe',
137 :lastname => 'Doe',
138 :mail => 'register@example.com'
138 :mail => 'register@example.com'
139 }
139 }
140 assert_redirected_to '/'
140 assert_redirected_to '/'
141 end
141 end
142 end
142 end
143 end
143 end
144
144
145 def test_get_lost_password_should_display_lost_password_form
145 def test_get_lost_password_should_display_lost_password_form
146 get :lost_password
146 get :lost_password
147 assert_response :success
147 assert_response :success
148 assert_select 'input[name=mail]'
148 assert_select 'input[name=mail]'
149 end
149 end
150
150
151 def test_lost_password_for_active_user_should_create_a_token
151 def test_lost_password_for_active_user_should_create_a_token
152 Token.delete_all
152 Token.delete_all
153 ActionMailer::Base.deliveries.clear
153 ActionMailer::Base.deliveries.clear
154 assert_difference 'ActionMailer::Base.deliveries.size' do
154 assert_difference 'ActionMailer::Base.deliveries.size' do
155 assert_difference 'Token.count' do
155 assert_difference 'Token.count' do
156 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
156 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
157 post :lost_password, :mail => 'JSmith@somenet.foo'
157 post :lost_password, :mail => 'JSmith@somenet.foo'
158 assert_redirected_to '/login'
158 assert_redirected_to '/login'
159 end
159 end
160 end
160 end
161 end
161 end
162
162
163 token = Token.order('id DESC').first
163 token = Token.order('id DESC').first
164 assert_equal User.find(2), token.user
164 assert_equal User.find(2), token.user
165 assert_equal 'recovery', token.action
165 assert_equal 'recovery', token.action
166
166
167 assert_select_email do
167 assert_select_email do
168 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
168 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
169 end
169 end
170 end
170 end
171
171
172 def test_lost_password_for_unknown_user_should_fail
172 def test_lost_password_for_unknown_user_should_fail
173 Token.delete_all
173 Token.delete_all
174 assert_no_difference 'Token.count' do
174 assert_no_difference 'Token.count' do
175 post :lost_password, :mail => 'invalid@somenet.foo'
175 post :lost_password, :mail => 'invalid@somenet.foo'
176 assert_response :success
176 assert_response :success
177 end
177 end
178 end
178 end
179
179
180 def test_lost_password_for_non_active_user_should_fail
180 def test_lost_password_for_non_active_user_should_fail
181 Token.delete_all
181 Token.delete_all
182 assert User.find(2).lock!
182 assert User.find(2).lock!
183
183
184 assert_no_difference 'Token.count' do
184 assert_no_difference 'Token.count' do
185 post :lost_password, :mail => 'JSmith@somenet.foo'
185 post :lost_password, :mail => 'JSmith@somenet.foo'
186 assert_response :success
186 assert_response :success
187 end
187 end
188 end
188 end
189
190 def test_get_lost_password_with_token_should_display_the_password_recovery_form
191 user = User.find(2)
192 token = Token.create!(:action => 'recovery', :user => user)
193
194 get :lost_password, :token => token.value
195 assert_response :success
196 assert_template 'password_recovery'
197
198 assert_select 'input[type=hidden][name=token][value=?]', token.value
199 end
200
201 def test_get_lost_password_with_invalid_token_should_redirect
202 get :lost_password, :token => "abcdef"
203 assert_redirected_to '/'
204 end
205
206 def test_post_lost_password_with_token_should_change_the_user_password
207 user = User.find(2)
208 token = Token.create!(:action => 'recovery', :user => user)
209
210 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
211 assert_redirected_to '/login'
212 user.reload
213 assert user.check_password?('newpass')
214 assert_nil Token.find_by_id(token.id), "Token was not deleted"
215 end
216
217 def test_post_lost_password_with_token_for_non_active_user_should_fail
218 user = User.find(2)
219 token = Token.create!(:action => 'recovery', :user => user)
220 user.lock!
221
222 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
223 assert_redirected_to '/'
224 assert ! user.check_password?('newpass')
225 end
226
227 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
228 user = User.find(2)
229 token = Token.create!(:action => 'recovery', :user => user)
230
231 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
232 assert_response :success
233 assert_template 'password_recovery'
234 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
235
236 assert_select 'input[type=hidden][name=token][value=?]', token.value
237 end
238
239 def test_post_lost_password_with_invalid_token_should_redirect
240 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
241 assert_redirected_to '/'
242 end
189 end
243 end
General Comments 0
You need to be logged in to leave comments. Login now