##// END OF EJS Templates
Use browser language as default when registering....
Jean-Philippe Lang -
r10759:cabfece11ba4
parent child
Show More
@@ -1,296 +1,296
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?
53 unless @user && @user.active?
54 redirect_to home_url
54 redirect_to home_url
55 return
55 return
56 end
56 end
57 if request.post?
57 if request.post?
58 @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]
59 if @user.save
59 if @user.save
60 @token.destroy
60 @token.destroy
61 flash[:notice] = l(:notice_account_password_updated)
61 flash[:notice] = l(:notice_account_password_updated)
62 redirect_to signin_path
62 redirect_to signin_path
63 return
63 return
64 end
64 end
65 end
65 end
66 render :template => "account/password_recovery"
66 render :template => "account/password_recovery"
67 return
67 return
68 else
68 else
69 if request.post?
69 if request.post?
70 user = User.find_by_mail(params[:mail].to_s)
70 user = User.find_by_mail(params[:mail].to_s)
71 # user not found or not active
71 # user not found or not active
72 unless user && user.active?
72 unless user && user.active?
73 flash.now[:error] = l(:notice_account_unknown_email)
73 flash.now[:error] = l(:notice_account_unknown_email)
74 return
74 return
75 end
75 end
76 # user cannot change its password
76 # user cannot change its password
77 unless user.change_password_allowed?
77 unless user.change_password_allowed?
78 flash.now[:error] = l(:notice_can_t_change_password)
78 flash.now[:error] = l(:notice_can_t_change_password)
79 return
79 return
80 end
80 end
81 # create a new token for password recovery
81 # create a new token for password recovery
82 token = Token.new(:user => user, :action => "recovery")
82 token = Token.new(:user => user, :action => "recovery")
83 if token.save
83 if token.save
84 Mailer.lost_password(token).deliver
84 Mailer.lost_password(token).deliver
85 flash[:notice] = l(:notice_account_lost_email_sent)
85 flash[:notice] = l(:notice_account_lost_email_sent)
86 redirect_to signin_path
86 redirect_to signin_path
87 return
87 return
88 end
88 end
89 end
89 end
90 end
90 end
91 end
91 end
92
92
93 # User self-registration
93 # User self-registration
94 def register
94 def register
95 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]
96 if request.get?
96 if request.get?
97 session[:auth_source_registration] = nil
97 session[:auth_source_registration] = nil
98 @user = User.new(:language => Setting.default_language)
98 @user = User.new(:language => current_language.to_s)
99 else
99 else
100 user_params = params[:user] || {}
100 user_params = params[:user] || {}
101 @user = User.new
101 @user = User.new
102 @user.safe_attributes = user_params
102 @user.safe_attributes = user_params
103 @user.admin = false
103 @user.admin = false
104 @user.register
104 @user.register
105 if session[:auth_source_registration]
105 if session[:auth_source_registration]
106 @user.activate
106 @user.activate
107 @user.login = session[:auth_source_registration][:login]
107 @user.login = session[:auth_source_registration][:login]
108 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
108 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
109 if @user.save
109 if @user.save
110 session[:auth_source_registration] = nil
110 session[:auth_source_registration] = nil
111 self.logged_user = @user
111 self.logged_user = @user
112 flash[:notice] = l(:notice_account_activated)
112 flash[:notice] = l(:notice_account_activated)
113 redirect_to my_account_path
113 redirect_to my_account_path
114 end
114 end
115 else
115 else
116 @user.login = params[:user][:login]
116 @user.login = params[:user][:login]
117 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?
118 @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]
119 end
119 end
120
120
121 case Setting.self_registration
121 case Setting.self_registration
122 when '1'
122 when '1'
123 register_by_email_activation(@user)
123 register_by_email_activation(@user)
124 when '3'
124 when '3'
125 register_automatically(@user)
125 register_automatically(@user)
126 else
126 else
127 register_manually_by_administrator(@user)
127 register_manually_by_administrator(@user)
128 end
128 end
129 end
129 end
130 end
130 end
131 end
131 end
132
132
133 # Token based account activation
133 # Token based account activation
134 def activate
134 def activate
135 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
135 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
136 token = Token.find_by_action_and_value('register', params[:token])
136 token = Token.find_by_action_and_value('register', params[:token])
137 redirect_to(home_url) && return unless token and !token.expired?
137 redirect_to(home_url) && return unless token and !token.expired?
138 user = token.user
138 user = token.user
139 redirect_to(home_url) && return unless user.registered?
139 redirect_to(home_url) && return unless user.registered?
140 user.activate
140 user.activate
141 if user.save
141 if user.save
142 token.destroy
142 token.destroy
143 flash[:notice] = l(:notice_account_activated)
143 flash[:notice] = l(:notice_account_activated)
144 end
144 end
145 redirect_to signin_path
145 redirect_to signin_path
146 end
146 end
147
147
148 private
148 private
149
149
150 def authenticate_user
150 def authenticate_user
151 if Setting.openid? && using_open_id?
151 if Setting.openid? && using_open_id?
152 open_id_authenticate(params[:openid_url])
152 open_id_authenticate(params[:openid_url])
153 else
153 else
154 password_authentication
154 password_authentication
155 end
155 end
156 end
156 end
157
157
158 def password_authentication
158 def password_authentication
159 user = User.try_to_login(params[:username], params[:password])
159 user = User.try_to_login(params[:username], params[:password])
160
160
161 if user.nil?
161 if user.nil?
162 invalid_credentials
162 invalid_credentials
163 elsif user.new_record?
163 elsif user.new_record?
164 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 })
165 else
165 else
166 # Valid user
166 # Valid user
167 successful_authentication(user)
167 successful_authentication(user)
168 end
168 end
169 end
169 end
170
170
171 def open_id_authenticate(openid_url)
171 def open_id_authenticate(openid_url)
172 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|
173 if result.successful?
173 if result.successful?
174 user = User.find_or_initialize_by_identity_url(identity_url)
174 user = User.find_or_initialize_by_identity_url(identity_url)
175 if user.new_record?
175 if user.new_record?
176 # Self-registration off
176 # Self-registration off
177 redirect_to(home_url) && return unless Setting.self_registration?
177 redirect_to(home_url) && return unless Setting.self_registration?
178
178
179 # Create on the fly
179 # Create on the fly
180 user.login = registration['nickname'] unless registration['nickname'].nil?
180 user.login = registration['nickname'] unless registration['nickname'].nil?
181 user.mail = registration['email'] unless registration['email'].nil?
181 user.mail = registration['email'] unless registration['email'].nil?
182 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
182 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
183 user.random_password
183 user.random_password
184 user.register
184 user.register
185
185
186 case Setting.self_registration
186 case Setting.self_registration
187 when '1'
187 when '1'
188 register_by_email_activation(user) do
188 register_by_email_activation(user) do
189 onthefly_creation_failed(user)
189 onthefly_creation_failed(user)
190 end
190 end
191 when '3'
191 when '3'
192 register_automatically(user) do
192 register_automatically(user) do
193 onthefly_creation_failed(user)
193 onthefly_creation_failed(user)
194 end
194 end
195 else
195 else
196 register_manually_by_administrator(user) do
196 register_manually_by_administrator(user) do
197 onthefly_creation_failed(user)
197 onthefly_creation_failed(user)
198 end
198 end
199 end
199 end
200 else
200 else
201 # Existing record
201 # Existing record
202 if user.active?
202 if user.active?
203 successful_authentication(user)
203 successful_authentication(user)
204 else
204 else
205 account_pending
205 account_pending
206 end
206 end
207 end
207 end
208 end
208 end
209 end
209 end
210 end
210 end
211
211
212 def successful_authentication(user)
212 def successful_authentication(user)
213 logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
213 logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
214 # Valid user
214 # Valid user
215 self.logged_user = user
215 self.logged_user = user
216 # generate a key and set cookie if autologin
216 # generate a key and set cookie if autologin
217 if params[:autologin] && Setting.autologin?
217 if params[:autologin] && Setting.autologin?
218 set_autologin_cookie(user)
218 set_autologin_cookie(user)
219 end
219 end
220 call_hook(:controller_account_success_authentication_after, {:user => user })
220 call_hook(:controller_account_success_authentication_after, {:user => user })
221 redirect_back_or_default my_page_path
221 redirect_back_or_default my_page_path
222 end
222 end
223
223
224 def set_autologin_cookie(user)
224 def set_autologin_cookie(user)
225 token = Token.create(:user => user, :action => 'autologin')
225 token = Token.create(:user => user, :action => 'autologin')
226 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
226 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
227 cookie_options = {
227 cookie_options = {
228 :value => token.value,
228 :value => token.value,
229 :expires => 1.year.from_now,
229 :expires => 1.year.from_now,
230 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
230 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
231 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
231 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
232 :httponly => true
232 :httponly => true
233 }
233 }
234 cookies[cookie_name] = cookie_options
234 cookies[cookie_name] = cookie_options
235 end
235 end
236
236
237 # Onthefly creation failed, display the registration form to fill/fix attributes
237 # Onthefly creation failed, display the registration form to fill/fix attributes
238 def onthefly_creation_failed(user, auth_source_options = { })
238 def onthefly_creation_failed(user, auth_source_options = { })
239 @user = user
239 @user = user
240 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
240 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
241 render :action => 'register'
241 render :action => 'register'
242 end
242 end
243
243
244 def invalid_credentials
244 def invalid_credentials
245 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
245 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
246 flash.now[:error] = l(:notice_account_invalid_creditentials)
246 flash.now[:error] = l(:notice_account_invalid_creditentials)
247 end
247 end
248
248
249 # Register a user for email activation.
249 # Register a user for email activation.
250 #
250 #
251 # Pass a block for behavior when a user fails to save
251 # Pass a block for behavior when a user fails to save
252 def register_by_email_activation(user, &block)
252 def register_by_email_activation(user, &block)
253 token = Token.new(:user => user, :action => "register")
253 token = Token.new(:user => user, :action => "register")
254 if user.save and token.save
254 if user.save and token.save
255 Mailer.register(token).deliver
255 Mailer.register(token).deliver
256 flash[:notice] = l(:notice_account_register_done)
256 flash[:notice] = l(:notice_account_register_done)
257 redirect_to signin_path
257 redirect_to signin_path
258 else
258 else
259 yield if block_given?
259 yield if block_given?
260 end
260 end
261 end
261 end
262
262
263 # Automatically register a user
263 # Automatically register a user
264 #
264 #
265 # Pass a block for behavior when a user fails to save
265 # Pass a block for behavior when a user fails to save
266 def register_automatically(user, &block)
266 def register_automatically(user, &block)
267 # Automatic activation
267 # Automatic activation
268 user.activate
268 user.activate
269 user.last_login_on = Time.now
269 user.last_login_on = Time.now
270 if user.save
270 if user.save
271 self.logged_user = user
271 self.logged_user = user
272 flash[:notice] = l(:notice_account_activated)
272 flash[:notice] = l(:notice_account_activated)
273 redirect_to my_account_path
273 redirect_to my_account_path
274 else
274 else
275 yield if block_given?
275 yield if block_given?
276 end
276 end
277 end
277 end
278
278
279 # Manual activation by the administrator
279 # Manual activation by the administrator
280 #
280 #
281 # Pass a block for behavior when a user fails to save
281 # Pass a block for behavior when a user fails to save
282 def register_manually_by_administrator(user, &block)
282 def register_manually_by_administrator(user, &block)
283 if user.save
283 if user.save
284 # Sends an email to the administrators
284 # Sends an email to the administrators
285 Mailer.account_activation_request(user).deliver
285 Mailer.account_activation_request(user).deliver
286 account_pending
286 account_pending
287 else
287 else
288 yield if block_given?
288 yield if block_given?
289 end
289 end
290 end
290 end
291
291
292 def account_pending
292 def account_pending
293 flash[:notice] = l(:notice_account_pending)
293 flash[:notice] = l(:notice_account_pending)
294 redirect_to signin_path
294 redirect_to signin_path
295 end
295 end
296 end
296 end
@@ -1,247 +1,260
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
19
20 class AccountControllerTest < ActionController::TestCase
20 class AccountControllerTest < ActionController::TestCase
21 fixtures :users, :roles
21 fixtures :users, :roles
22
22
23 def setup
23 def setup
24 User.current = nil
24 User.current = nil
25 end
25 end
26
26
27 def test_get_login
27 def test_get_login
28 get :login
28 get :login
29 assert_response :success
29 assert_response :success
30 assert_template 'login'
30 assert_template 'login'
31
31
32 assert_select 'input[name=username]'
32 assert_select 'input[name=username]'
33 assert_select 'input[name=password]'
33 assert_select 'input[name=password]'
34 end
34 end
35
35
36 def test_login_should_redirect_to_back_url_param
36 def test_login_should_redirect_to_back_url_param
37 # request.uri is "test.host" in test environment
37 # request.uri is "test.host" in test environment
38 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1'
38 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1'
39 assert_redirected_to '/issues/show/1'
39 assert_redirected_to '/issues/show/1'
40 end
40 end
41
41
42 def test_login_should_not_redirect_to_another_host
42 def test_login_should_not_redirect_to_another_host
43 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake'
43 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake'
44 assert_redirected_to '/my/page'
44 assert_redirected_to '/my/page'
45 end
45 end
46
46
47 def test_login_with_wrong_password
47 def test_login_with_wrong_password
48 post :login, :username => 'admin', :password => 'bad'
48 post :login, :username => 'admin', :password => 'bad'
49 assert_response :success
49 assert_response :success
50 assert_template 'login'
50 assert_template 'login'
51
51
52 assert_select 'div.flash.error', :text => /Invalid user or password/
52 assert_select 'div.flash.error', :text => /Invalid user or password/
53 assert_select 'input[name=username][value=admin]'
53 assert_select 'input[name=username][value=admin]'
54 assert_select 'input[name=password]'
54 assert_select 'input[name=password]'
55 assert_select 'input[name=password][value]', 0
55 assert_select 'input[name=password][value]', 0
56 end
56 end
57
57
58 def test_login_should_rescue_auth_source_exception
58 def test_login_should_rescue_auth_source_exception
59 source = AuthSource.create!(:name => 'Test')
59 source = AuthSource.create!(:name => 'Test')
60 User.find(2).update_attribute :auth_source_id, source.id
60 User.find(2).update_attribute :auth_source_id, source.id
61 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
61 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong"))
62
62
63 post :login, :username => 'jsmith', :password => 'jsmith'
63 post :login, :username => 'jsmith', :password => 'jsmith'
64 assert_response 500
64 assert_response 500
65 assert_error_tag :content => /Something wrong/
65 assert_error_tag :content => /Something wrong/
66 end
66 end
67
67
68 def test_login_should_reset_session
68 def test_login_should_reset_session
69 @controller.expects(:reset_session).once
69 @controller.expects(:reset_session).once
70
70
71 post :login, :username => 'jsmith', :password => 'jsmith'
71 post :login, :username => 'jsmith', :password => 'jsmith'
72 assert_response 302
72 assert_response 302
73 end
73 end
74
74
75 def test_logout
75 def test_logout
76 @request.session[:user_id] = 2
76 @request.session[:user_id] = 2
77 get :logout
77 get :logout
78 assert_redirected_to '/'
78 assert_redirected_to '/'
79 assert_nil @request.session[:user_id]
79 assert_nil @request.session[:user_id]
80 end
80 end
81
81
82 def test_logout_should_reset_session
82 def test_logout_should_reset_session
83 @controller.expects(:reset_session).once
83 @controller.expects(:reset_session).once
84
84
85 @request.session[:user_id] = 2
85 @request.session[:user_id] = 2
86 get :logout
86 get :logout
87 assert_response 302
87 assert_response 302
88 end
88 end
89
89
90 def test_get_register_with_registration_on
90 def test_get_register_with_registration_on
91 with_settings :self_registration => '3' do
91 with_settings :self_registration => '3' do
92 get :register
92 get :register
93 assert_response :success
93 assert_response :success
94 assert_template 'register'
94 assert_template 'register'
95 assert_not_nil assigns(:user)
95 assert_not_nil assigns(:user)
96
96
97 assert_tag 'input', :attributes => {:name => 'user[password]'}
97 assert_tag 'input', :attributes => {:name => 'user[password]'}
98 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
98 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
99 end
99 end
100 end
100 end
101
101
102 def test_get_register_should_detect_user_language
103 with_settings :self_registration => '3' do
104 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
105 get :register
106 assert_response :success
107 assert_not_nil assigns(:user)
108 assert_equal 'fr', assigns(:user).language
109 assert_select 'select[name=?]', 'user[language]' do
110 assert_select 'option[value=fr][selected=selected]'
111 end
112 end
113 end
114
102 def test_get_register_with_registration_off_should_redirect
115 def test_get_register_with_registration_off_should_redirect
103 with_settings :self_registration => '0' do
116 with_settings :self_registration => '0' do
104 get :register
117 get :register
105 assert_redirected_to '/'
118 assert_redirected_to '/'
106 end
119 end
107 end
120 end
108
121
109 # See integration/account_test.rb for the full test
122 # See integration/account_test.rb for the full test
110 def test_post_register_with_registration_on
123 def test_post_register_with_registration_on
111 with_settings :self_registration => '3' do
124 with_settings :self_registration => '3' do
112 assert_difference 'User.count' do
125 assert_difference 'User.count' do
113 post :register, :user => {
126 post :register, :user => {
114 :login => 'register',
127 :login => 'register',
115 :password => 'secret123',
128 :password => 'secret123',
116 :password_confirmation => 'secret123',
129 :password_confirmation => 'secret123',
117 :firstname => 'John',
130 :firstname => 'John',
118 :lastname => 'Doe',
131 :lastname => 'Doe',
119 :mail => 'register@example.com'
132 :mail => 'register@example.com'
120 }
133 }
121 assert_redirected_to '/my/account'
134 assert_redirected_to '/my/account'
122 end
135 end
123 user = User.first(:order => 'id DESC')
136 user = User.first(:order => 'id DESC')
124 assert_equal 'register', user.login
137 assert_equal 'register', user.login
125 assert_equal 'John', user.firstname
138 assert_equal 'John', user.firstname
126 assert_equal 'Doe', user.lastname
139 assert_equal 'Doe', user.lastname
127 assert_equal 'register@example.com', user.mail
140 assert_equal 'register@example.com', user.mail
128 assert user.check_password?('secret123')
141 assert user.check_password?('secret123')
129 assert user.active?
142 assert user.active?
130 end
143 end
131 end
144 end
132
145
133 def test_post_register_with_registration_off_should_redirect
146 def test_post_register_with_registration_off_should_redirect
134 with_settings :self_registration => '0' do
147 with_settings :self_registration => '0' do
135 assert_no_difference 'User.count' do
148 assert_no_difference 'User.count' do
136 post :register, :user => {
149 post :register, :user => {
137 :login => 'register',
150 :login => 'register',
138 :password => 'test',
151 :password => 'test',
139 :password_confirmation => 'test',
152 :password_confirmation => 'test',
140 :firstname => 'John',
153 :firstname => 'John',
141 :lastname => 'Doe',
154 :lastname => 'Doe',
142 :mail => 'register@example.com'
155 :mail => 'register@example.com'
143 }
156 }
144 assert_redirected_to '/'
157 assert_redirected_to '/'
145 end
158 end
146 end
159 end
147 end
160 end
148
161
149 def test_get_lost_password_should_display_lost_password_form
162 def test_get_lost_password_should_display_lost_password_form
150 get :lost_password
163 get :lost_password
151 assert_response :success
164 assert_response :success
152 assert_select 'input[name=mail]'
165 assert_select 'input[name=mail]'
153 end
166 end
154
167
155 def test_lost_password_for_active_user_should_create_a_token
168 def test_lost_password_for_active_user_should_create_a_token
156 Token.delete_all
169 Token.delete_all
157 ActionMailer::Base.deliveries.clear
170 ActionMailer::Base.deliveries.clear
158 assert_difference 'ActionMailer::Base.deliveries.size' do
171 assert_difference 'ActionMailer::Base.deliveries.size' do
159 assert_difference 'Token.count' do
172 assert_difference 'Token.count' do
160 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
173 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
161 post :lost_password, :mail => 'JSmith@somenet.foo'
174 post :lost_password, :mail => 'JSmith@somenet.foo'
162 assert_redirected_to '/login'
175 assert_redirected_to '/login'
163 end
176 end
164 end
177 end
165 end
178 end
166
179
167 token = Token.order('id DESC').first
180 token = Token.order('id DESC').first
168 assert_equal User.find(2), token.user
181 assert_equal User.find(2), token.user
169 assert_equal 'recovery', token.action
182 assert_equal 'recovery', token.action
170
183
171 assert_select_email do
184 assert_select_email do
172 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
185 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
173 end
186 end
174 end
187 end
175
188
176 def test_lost_password_for_unknown_user_should_fail
189 def test_lost_password_for_unknown_user_should_fail
177 Token.delete_all
190 Token.delete_all
178 assert_no_difference 'Token.count' do
191 assert_no_difference 'Token.count' do
179 post :lost_password, :mail => 'invalid@somenet.foo'
192 post :lost_password, :mail => 'invalid@somenet.foo'
180 assert_response :success
193 assert_response :success
181 end
194 end
182 end
195 end
183
196
184 def test_lost_password_for_non_active_user_should_fail
197 def test_lost_password_for_non_active_user_should_fail
185 Token.delete_all
198 Token.delete_all
186 assert User.find(2).lock!
199 assert User.find(2).lock!
187
200
188 assert_no_difference 'Token.count' do
201 assert_no_difference 'Token.count' do
189 post :lost_password, :mail => 'JSmith@somenet.foo'
202 post :lost_password, :mail => 'JSmith@somenet.foo'
190 assert_response :success
203 assert_response :success
191 end
204 end
192 end
205 end
193
206
194 def test_get_lost_password_with_token_should_display_the_password_recovery_form
207 def test_get_lost_password_with_token_should_display_the_password_recovery_form
195 user = User.find(2)
208 user = User.find(2)
196 token = Token.create!(:action => 'recovery', :user => user)
209 token = Token.create!(:action => 'recovery', :user => user)
197
210
198 get :lost_password, :token => token.value
211 get :lost_password, :token => token.value
199 assert_response :success
212 assert_response :success
200 assert_template 'password_recovery'
213 assert_template 'password_recovery'
201
214
202 assert_select 'input[type=hidden][name=token][value=?]', token.value
215 assert_select 'input[type=hidden][name=token][value=?]', token.value
203 end
216 end
204
217
205 def test_get_lost_password_with_invalid_token_should_redirect
218 def test_get_lost_password_with_invalid_token_should_redirect
206 get :lost_password, :token => "abcdef"
219 get :lost_password, :token => "abcdef"
207 assert_redirected_to '/'
220 assert_redirected_to '/'
208 end
221 end
209
222
210 def test_post_lost_password_with_token_should_change_the_user_password
223 def test_post_lost_password_with_token_should_change_the_user_password
211 user = User.find(2)
224 user = User.find(2)
212 token = Token.create!(:action => 'recovery', :user => user)
225 token = Token.create!(:action => 'recovery', :user => user)
213
226
214 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
227 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
215 assert_redirected_to '/login'
228 assert_redirected_to '/login'
216 user.reload
229 user.reload
217 assert user.check_password?('newpass123')
230 assert user.check_password?('newpass123')
218 assert_nil Token.find_by_id(token.id), "Token was not deleted"
231 assert_nil Token.find_by_id(token.id), "Token was not deleted"
219 end
232 end
220
233
221 def test_post_lost_password_with_token_for_non_active_user_should_fail
234 def test_post_lost_password_with_token_for_non_active_user_should_fail
222 user = User.find(2)
235 user = User.find(2)
223 token = Token.create!(:action => 'recovery', :user => user)
236 token = Token.create!(:action => 'recovery', :user => user)
224 user.lock!
237 user.lock!
225
238
226 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
239 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123'
227 assert_redirected_to '/'
240 assert_redirected_to '/'
228 assert ! user.check_password?('newpass123')
241 assert ! user.check_password?('newpass123')
229 end
242 end
230
243
231 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
244 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form
232 user = User.find(2)
245 user = User.find(2)
233 token = Token.create!(:action => 'recovery', :user => user)
246 token = Token.create!(:action => 'recovery', :user => user)
234
247
235 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
248 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass'
236 assert_response :success
249 assert_response :success
237 assert_template 'password_recovery'
250 assert_template 'password_recovery'
238 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
251 assert_not_nil Token.find_by_id(token.id), "Token was deleted"
239
252
240 assert_select 'input[type=hidden][name=token][value=?]', token.value
253 assert_select 'input[type=hidden][name=token][value=?]', token.value
241 end
254 end
242
255
243 def test_post_lost_password_with_invalid_token_should_redirect
256 def test_post_lost_password_with_invalid_token_should_redirect
244 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
257 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass'
245 assert_redirected_to '/'
258 assert_redirected_to '/'
246 end
259 end
247 end
260 end
General Comments 0
You need to be logged in to leave comments. Login now