##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r9760:193b571e67c1
parent child
Show More
@@ -1,285 +1,291
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 if request.post?
53 if request.post?
54 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
54 @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
55 if @user.save
55 if @user.save
56 @token.destroy
56 @token.destroy
57 flash[:notice] = l(:notice_account_password_updated)
57 flash[:notice] = l(:notice_account_password_updated)
58 redirect_to signin_path
58 redirect_to signin_path
59 return
59 return
60 end
60 end
61 end
61 end
62 render :template => "account/password_recovery"
62 render :template => "account/password_recovery"
63 return
63 return
64 else
64 else
65 if request.post?
65 if request.post?
66 user = User.find_by_mail(params[:mail])
66 user = User.find_by_mail(params[:mail].to_s)
67 # user not found in db
67 # user not found or not active
68 (flash.now[:error] = l(:notice_account_unknown_email); return) unless user
68 unless user && user.active?
69 # user uses an external authentification
69 flash.now[:error] = l(:notice_account_unknown_email)
70 (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id
70 return
71 end
72 # user cannot change its password
73 unless user.change_password_allowed?
74 flash.now[:error] = l(:notice_can_t_change_password)
75 return
76 end
71 # create a new token for password recovery
77 # create a new token for password recovery
72 token = Token.new(:user => user, :action => "recovery")
78 token = Token.new(:user => user, :action => "recovery")
73 if token.save
79 if token.save
74 Mailer.lost_password(token).deliver
80 Mailer.lost_password(token).deliver
75 flash[:notice] = l(:notice_account_lost_email_sent)
81 flash[:notice] = l(:notice_account_lost_email_sent)
76 redirect_to signin_path
82 redirect_to signin_path
77 return
83 return
78 end
84 end
79 end
85 end
80 end
86 end
81 end
87 end
82
88
83 # User self-registration
89 # User self-registration
84 def register
90 def register
85 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
91 redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
86 if request.get?
92 if request.get?
87 session[:auth_source_registration] = nil
93 session[:auth_source_registration] = nil
88 @user = User.new(:language => Setting.default_language)
94 @user = User.new(:language => Setting.default_language)
89 else
95 else
90 user_params = params[:user] || {}
96 user_params = params[:user] || {}
91 @user = User.new
97 @user = User.new
92 @user.safe_attributes = user_params
98 @user.safe_attributes = user_params
93 @user.admin = false
99 @user.admin = false
94 @user.register
100 @user.register
95 if session[:auth_source_registration]
101 if session[:auth_source_registration]
96 @user.activate
102 @user.activate
97 @user.login = session[:auth_source_registration][:login]
103 @user.login = session[:auth_source_registration][:login]
98 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
104 @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
99 if @user.save
105 if @user.save
100 session[:auth_source_registration] = nil
106 session[:auth_source_registration] = nil
101 self.logged_user = @user
107 self.logged_user = @user
102 flash[:notice] = l(:notice_account_activated)
108 flash[:notice] = l(:notice_account_activated)
103 redirect_to :controller => 'my', :action => 'account'
109 redirect_to :controller => 'my', :action => 'account'
104 end
110 end
105 else
111 else
106 @user.login = params[:user][:login]
112 @user.login = params[:user][:login]
107 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
113 unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
108 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
114 @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
109 end
115 end
110
116
111 case Setting.self_registration
117 case Setting.self_registration
112 when '1'
118 when '1'
113 register_by_email_activation(@user)
119 register_by_email_activation(@user)
114 when '3'
120 when '3'
115 register_automatically(@user)
121 register_automatically(@user)
116 else
122 else
117 register_manually_by_administrator(@user)
123 register_manually_by_administrator(@user)
118 end
124 end
119 end
125 end
120 end
126 end
121 end
127 end
122
128
123 # Token based account activation
129 # Token based account activation
124 def activate
130 def activate
125 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
131 redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
126 token = Token.find_by_action_and_value('register', params[:token])
132 token = Token.find_by_action_and_value('register', params[:token])
127 redirect_to(home_url) && return unless token and !token.expired?
133 redirect_to(home_url) && return unless token and !token.expired?
128 user = token.user
134 user = token.user
129 redirect_to(home_url) && return unless user.registered?
135 redirect_to(home_url) && return unless user.registered?
130 user.activate
136 user.activate
131 if user.save
137 if user.save
132 token.destroy
138 token.destroy
133 flash[:notice] = l(:notice_account_activated)
139 flash[:notice] = l(:notice_account_activated)
134 end
140 end
135 redirect_to signin_path
141 redirect_to signin_path
136 end
142 end
137
143
138 private
144 private
139
145
140 def authenticate_user
146 def authenticate_user
141 if Setting.openid? && using_open_id?
147 if Setting.openid? && using_open_id?
142 open_id_authenticate(params[:openid_url])
148 open_id_authenticate(params[:openid_url])
143 else
149 else
144 password_authentication
150 password_authentication
145 end
151 end
146 end
152 end
147
153
148 def password_authentication
154 def password_authentication
149 user = User.try_to_login(params[:username], params[:password])
155 user = User.try_to_login(params[:username], params[:password])
150
156
151 if user.nil?
157 if user.nil?
152 invalid_credentials
158 invalid_credentials
153 elsif user.new_record?
159 elsif user.new_record?
154 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
160 onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
155 else
161 else
156 # Valid user
162 # Valid user
157 successful_authentication(user)
163 successful_authentication(user)
158 end
164 end
159 end
165 end
160
166
161 def open_id_authenticate(openid_url)
167 def open_id_authenticate(openid_url)
162 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
168 authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
163 if result.successful?
169 if result.successful?
164 user = User.find_or_initialize_by_identity_url(identity_url)
170 user = User.find_or_initialize_by_identity_url(identity_url)
165 if user.new_record?
171 if user.new_record?
166 # Self-registration off
172 # Self-registration off
167 redirect_to(home_url) && return unless Setting.self_registration?
173 redirect_to(home_url) && return unless Setting.self_registration?
168
174
169 # Create on the fly
175 # Create on the fly
170 user.login = registration['nickname'] unless registration['nickname'].nil?
176 user.login = registration['nickname'] unless registration['nickname'].nil?
171 user.mail = registration['email'] unless registration['email'].nil?
177 user.mail = registration['email'] unless registration['email'].nil?
172 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
178 user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
173 user.random_password
179 user.random_password
174 user.register
180 user.register
175
181
176 case Setting.self_registration
182 case Setting.self_registration
177 when '1'
183 when '1'
178 register_by_email_activation(user) do
184 register_by_email_activation(user) do
179 onthefly_creation_failed(user)
185 onthefly_creation_failed(user)
180 end
186 end
181 when '3'
187 when '3'
182 register_automatically(user) do
188 register_automatically(user) do
183 onthefly_creation_failed(user)
189 onthefly_creation_failed(user)
184 end
190 end
185 else
191 else
186 register_manually_by_administrator(user) do
192 register_manually_by_administrator(user) do
187 onthefly_creation_failed(user)
193 onthefly_creation_failed(user)
188 end
194 end
189 end
195 end
190 else
196 else
191 # Existing record
197 # Existing record
192 if user.active?
198 if user.active?
193 successful_authentication(user)
199 successful_authentication(user)
194 else
200 else
195 account_pending
201 account_pending
196 end
202 end
197 end
203 end
198 end
204 end
199 end
205 end
200 end
206 end
201
207
202 def successful_authentication(user)
208 def successful_authentication(user)
203 # Valid user
209 # Valid user
204 self.logged_user = user
210 self.logged_user = user
205 # generate a key and set cookie if autologin
211 # generate a key and set cookie if autologin
206 if params[:autologin] && Setting.autologin?
212 if params[:autologin] && Setting.autologin?
207 set_autologin_cookie(user)
213 set_autologin_cookie(user)
208 end
214 end
209 call_hook(:controller_account_success_authentication_after, {:user => user })
215 call_hook(:controller_account_success_authentication_after, {:user => user })
210 redirect_back_or_default :controller => 'my', :action => 'page'
216 redirect_back_or_default :controller => 'my', :action => 'page'
211 end
217 end
212
218
213 def set_autologin_cookie(user)
219 def set_autologin_cookie(user)
214 token = Token.create(:user => user, :action => 'autologin')
220 token = Token.create(:user => user, :action => 'autologin')
215 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
221 cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
216 cookie_options = {
222 cookie_options = {
217 :value => token.value,
223 :value => token.value,
218 :expires => 1.year.from_now,
224 :expires => 1.year.from_now,
219 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
225 :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
220 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
226 :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
221 :httponly => true
227 :httponly => true
222 }
228 }
223 cookies[cookie_name] = cookie_options
229 cookies[cookie_name] = cookie_options
224 end
230 end
225
231
226 # Onthefly creation failed, display the registration form to fill/fix attributes
232 # Onthefly creation failed, display the registration form to fill/fix attributes
227 def onthefly_creation_failed(user, auth_source_options = { })
233 def onthefly_creation_failed(user, auth_source_options = { })
228 @user = user
234 @user = user
229 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
235 session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
230 render register_path
236 render register_path
231 end
237 end
232
238
233 def invalid_credentials
239 def invalid_credentials
234 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
240 logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
235 flash.now[:error] = l(:notice_account_invalid_creditentials)
241 flash.now[:error] = l(:notice_account_invalid_creditentials)
236 end
242 end
237
243
238 # Register a user for email activation.
244 # Register a user for email activation.
239 #
245 #
240 # Pass a block for behavior when a user fails to save
246 # Pass a block for behavior when a user fails to save
241 def register_by_email_activation(user, &block)
247 def register_by_email_activation(user, &block)
242 token = Token.new(:user => user, :action => "register")
248 token = Token.new(:user => user, :action => "register")
243 if user.save and token.save
249 if user.save and token.save
244 Mailer.register(token).deliver
250 Mailer.register(token).deliver
245 flash[:notice] = l(:notice_account_register_done)
251 flash[:notice] = l(:notice_account_register_done)
246 redirect_to signin_path
252 redirect_to signin_path
247 else
253 else
248 yield if block_given?
254 yield if block_given?
249 end
255 end
250 end
256 end
251
257
252 # Automatically register a user
258 # Automatically register a user
253 #
259 #
254 # Pass a block for behavior when a user fails to save
260 # Pass a block for behavior when a user fails to save
255 def register_automatically(user, &block)
261 def register_automatically(user, &block)
256 # Automatic activation
262 # Automatic activation
257 user.activate
263 user.activate
258 user.last_login_on = Time.now
264 user.last_login_on = Time.now
259 if user.save
265 if user.save
260 self.logged_user = user
266 self.logged_user = user
261 flash[:notice] = l(:notice_account_activated)
267 flash[:notice] = l(:notice_account_activated)
262 redirect_to :controller => 'my', :action => 'account'
268 redirect_to :controller => 'my', :action => 'account'
263 else
269 else
264 yield if block_given?
270 yield if block_given?
265 end
271 end
266 end
272 end
267
273
268 # Manual activation by the administrator
274 # Manual activation by the administrator
269 #
275 #
270 # Pass a block for behavior when a user fails to save
276 # Pass a block for behavior when a user fails to save
271 def register_manually_by_administrator(user, &block)
277 def register_manually_by_administrator(user, &block)
272 if user.save
278 if user.save
273 # Sends an email to the administrators
279 # Sends an email to the administrators
274 Mailer.account_activation_request(user).deliver
280 Mailer.account_activation_request(user).deliver
275 account_pending
281 account_pending
276 else
282 else
277 yield if block_given?
283 yield if block_given?
278 end
284 end
279 end
285 end
280
286
281 def account_pending
287 def account_pending
282 flash[:notice] = l(:notice_account_pending)
288 flash[:notice] = l(:notice_account_pending)
283 redirect_to signin_path
289 redirect_to signin_path
284 end
290 end
285 end
291 end
@@ -1,144 +1,185
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
145 def test_get_lost_password_should_display_lost_password_form
146 get :lost_password
147 assert_response :success
148 assert_select 'input[name=mail]'
149 end
150
151 def test_lost_password_for_active_user_should_create_a_token
152 assert_difference 'ActionMailer::Base.deliveries.size' do
153 assert_difference 'Token.count' do
154 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
155 post :lost_password, :mail => 'JSmith@somenet.foo'
156 assert_redirected_to '/login'
157 end
158 end
159 end
160
161 token = Token.order('id DESC').first
162 assert_equal User.find(2), token.user
163 assert_equal 'recovery', token.action
164
165 assert_select_email do
166 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
167 end
168 end
169
170 def test_lost_password_for_unknown_user_should_fail
171 assert_no_difference 'Token.count' do
172 post :lost_password, :mail => 'invalid@somenet.foo'
173 assert_response :success
174 end
175 end
176
177 def test_lost_password_for_non_active_user_should_fail
178 assert User.find(2).lock!
179
180 assert_no_difference 'Token.count' do
181 post :lost_password, :mail => 'JSmith@somenet.foo'
182 assert_response :success
183 end
184 end
144 end
185 end
General Comments 0
You need to be logged in to leave comments. Login now