##// END OF EJS Templates
Moved tests about session reset to functional tests....
Jean-Philippe Lang -
r8894:39aa4cac86be
parent child
Show More
@@ -1,232 +1,247
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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
65 @controller.expects(:reset_session).once
66
67 post :login, :username => 'jsmith', :password => 'jsmith'
68 assert_response 302
69 end
70
64 if Object.const_defined?(:OpenID)
71 if Object.const_defined?(:OpenID)
65
72
66 def test_login_with_openid_for_existing_user
73 def test_login_with_openid_for_existing_user
67 Setting.self_registration = '3'
74 Setting.self_registration = '3'
68 Setting.openid = '1'
75 Setting.openid = '1'
69 existing_user = User.new(:firstname => 'Cool',
76 existing_user = User.new(:firstname => 'Cool',
70 :lastname => 'User',
77 :lastname => 'User',
71 :mail => 'user@somedomain.com',
78 :mail => 'user@somedomain.com',
72 :identity_url => 'http://openid.example.com/good_user')
79 :identity_url => 'http://openid.example.com/good_user')
73 existing_user.login = 'cool_user'
80 existing_user.login = 'cool_user'
74 assert existing_user.save!
81 assert existing_user.save!
75
82
76 post :login, :openid_url => existing_user.identity_url
83 post :login, :openid_url => existing_user.identity_url
77 assert_redirected_to '/my/page'
84 assert_redirected_to '/my/page'
78 end
85 end
79
86
80 def test_login_with_invalid_openid_provider
87 def test_login_with_invalid_openid_provider
81 Setting.self_registration = '0'
88 Setting.self_registration = '0'
82 Setting.openid = '1'
89 Setting.openid = '1'
83 post :login, :openid_url => 'http;//openid.example.com/good_user'
90 post :login, :openid_url => 'http;//openid.example.com/good_user'
84 assert_redirected_to home_url
91 assert_redirected_to home_url
85 end
92 end
86
93
87 def test_login_with_openid_for_existing_non_active_user
94 def test_login_with_openid_for_existing_non_active_user
88 Setting.self_registration = '2'
95 Setting.self_registration = '2'
89 Setting.openid = '1'
96 Setting.openid = '1'
90 existing_user = User.new(:firstname => 'Cool',
97 existing_user = User.new(:firstname => 'Cool',
91 :lastname => 'User',
98 :lastname => 'User',
92 :mail => 'user@somedomain.com',
99 :mail => 'user@somedomain.com',
93 :identity_url => 'http://openid.example.com/good_user',
100 :identity_url => 'http://openid.example.com/good_user',
94 :status => User::STATUS_REGISTERED)
101 :status => User::STATUS_REGISTERED)
95 existing_user.login = 'cool_user'
102 existing_user.login = 'cool_user'
96 assert existing_user.save!
103 assert existing_user.save!
97
104
98 post :login, :openid_url => existing_user.identity_url
105 post :login, :openid_url => existing_user.identity_url
99 assert_redirected_to '/login'
106 assert_redirected_to '/login'
100 end
107 end
101
108
102 def test_login_with_openid_with_new_user_created
109 def test_login_with_openid_with_new_user_created
103 Setting.self_registration = '3'
110 Setting.self_registration = '3'
104 Setting.openid = '1'
111 Setting.openid = '1'
105 post :login, :openid_url => 'http://openid.example.com/good_user'
112 post :login, :openid_url => 'http://openid.example.com/good_user'
106 assert_redirected_to '/my/account'
113 assert_redirected_to '/my/account'
107 user = User.find_by_login('cool_user')
114 user = User.find_by_login('cool_user')
108 assert user
115 assert user
109 assert_equal 'Cool', user.firstname
116 assert_equal 'Cool', user.firstname
110 assert_equal 'User', user.lastname
117 assert_equal 'User', user.lastname
111 end
118 end
112
119
113 def test_login_with_openid_with_new_user_and_self_registration_off
120 def test_login_with_openid_with_new_user_and_self_registration_off
114 Setting.self_registration = '0'
121 Setting.self_registration = '0'
115 Setting.openid = '1'
122 Setting.openid = '1'
116 post :login, :openid_url => 'http://openid.example.com/good_user'
123 post :login, :openid_url => 'http://openid.example.com/good_user'
117 assert_redirected_to home_url
124 assert_redirected_to home_url
118 user = User.find_by_login('cool_user')
125 user = User.find_by_login('cool_user')
119 assert ! user
126 assert ! user
120 end
127 end
121
128
122 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
129 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
123 Setting.self_registration = '1'
130 Setting.self_registration = '1'
124 Setting.openid = '1'
131 Setting.openid = '1'
125 post :login, :openid_url => 'http://openid.example.com/good_user'
132 post :login, :openid_url => 'http://openid.example.com/good_user'
126 assert_redirected_to '/login'
133 assert_redirected_to '/login'
127 user = User.find_by_login('cool_user')
134 user = User.find_by_login('cool_user')
128 assert user
135 assert user
129
136
130 token = Token.find_by_user_id_and_action(user.id, 'register')
137 token = Token.find_by_user_id_and_action(user.id, 'register')
131 assert token
138 assert token
132 end
139 end
133
140
134 def test_login_with_openid_with_new_user_created_with_manual_activation
141 def test_login_with_openid_with_new_user_created_with_manual_activation
135 Setting.self_registration = '2'
142 Setting.self_registration = '2'
136 Setting.openid = '1'
143 Setting.openid = '1'
137 post :login, :openid_url => 'http://openid.example.com/good_user'
144 post :login, :openid_url => 'http://openid.example.com/good_user'
138 assert_redirected_to '/login'
145 assert_redirected_to '/login'
139 user = User.find_by_login('cool_user')
146 user = User.find_by_login('cool_user')
140 assert user
147 assert user
141 assert_equal User::STATUS_REGISTERED, user.status
148 assert_equal User::STATUS_REGISTERED, user.status
142 end
149 end
143
150
144 def test_login_with_openid_with_new_user_with_conflict_should_register
151 def test_login_with_openid_with_new_user_with_conflict_should_register
145 Setting.self_registration = '3'
152 Setting.self_registration = '3'
146 Setting.openid = '1'
153 Setting.openid = '1'
147 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
154 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
148 existing_user.login = 'cool_user'
155 existing_user.login = 'cool_user'
149 assert existing_user.save!
156 assert existing_user.save!
150
157
151 post :login, :openid_url => 'http://openid.example.com/good_user'
158 post :login, :openid_url => 'http://openid.example.com/good_user'
152 assert_response :success
159 assert_response :success
153 assert_template 'register'
160 assert_template 'register'
154 assert assigns(:user)
161 assert assigns(:user)
155 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
162 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
156 end
163 end
157
164
158 def test_setting_openid_should_return_true_when_set_to_true
165 def test_setting_openid_should_return_true_when_set_to_true
159 Setting.openid = '1'
166 Setting.openid = '1'
160 assert_equal true, Setting.openid?
167 assert_equal true, Setting.openid?
161 end
168 end
162
169
163 else
170 else
164 puts "Skipping openid tests."
171 puts "Skipping openid tests."
165 end
172 end
166
173
167 def test_logout
174 def test_logout
168 @request.session[:user_id] = 2
175 @request.session[:user_id] = 2
169 get :logout
176 get :logout
170 assert_redirected_to '/'
177 assert_redirected_to '/'
171 assert_nil @request.session[:user_id]
178 assert_nil @request.session[:user_id]
172 end
179 end
173
180
181 def test_logout_should_reset_session
182 @controller.expects(:reset_session).once
183
184 @request.session[:user_id] = 2
185 get :logout
186 assert_response 302
187 end
188
174 def test_get_register_with_registration_on
189 def test_get_register_with_registration_on
175 with_settings :self_registration => '3' do
190 with_settings :self_registration => '3' do
176 get :register
191 get :register
177 assert_response :success
192 assert_response :success
178 assert_template 'register'
193 assert_template 'register'
179 assert_not_nil assigns(:user)
194 assert_not_nil assigns(:user)
180
195
181 assert_tag 'input', :attributes => {:name => 'user[password]'}
196 assert_tag 'input', :attributes => {:name => 'user[password]'}
182 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
197 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
183 end
198 end
184 end
199 end
185
200
186 def test_get_register_with_registration_off_should_redirect
201 def test_get_register_with_registration_off_should_redirect
187 with_settings :self_registration => '0' do
202 with_settings :self_registration => '0' do
188 get :register
203 get :register
189 assert_redirected_to '/'
204 assert_redirected_to '/'
190 end
205 end
191 end
206 end
192
207
193 # See integration/account_test.rb for the full test
208 # See integration/account_test.rb for the full test
194 def test_post_register_with_registration_on
209 def test_post_register_with_registration_on
195 with_settings :self_registration => '3' do
210 with_settings :self_registration => '3' do
196 assert_difference 'User.count' do
211 assert_difference 'User.count' do
197 post :register, :user => {
212 post :register, :user => {
198 :login => 'register',
213 :login => 'register',
199 :password => 'test',
214 :password => 'test',
200 :password_confirmation => 'test',
215 :password_confirmation => 'test',
201 :firstname => 'John',
216 :firstname => 'John',
202 :lastname => 'Doe',
217 :lastname => 'Doe',
203 :mail => 'register@example.com'
218 :mail => 'register@example.com'
204 }
219 }
205 assert_redirected_to '/my/account'
220 assert_redirected_to '/my/account'
206 end
221 end
207 user = User.first(:order => 'id DESC')
222 user = User.first(:order => 'id DESC')
208 assert_equal 'register', user.login
223 assert_equal 'register', user.login
209 assert_equal 'John', user.firstname
224 assert_equal 'John', user.firstname
210 assert_equal 'Doe', user.lastname
225 assert_equal 'Doe', user.lastname
211 assert_equal 'register@example.com', user.mail
226 assert_equal 'register@example.com', user.mail
212 assert user.check_password?('test')
227 assert user.check_password?('test')
213 assert user.active?
228 assert user.active?
214 end
229 end
215 end
230 end
216
231
217 def test_post_register_with_registration_off_should_redirect
232 def test_post_register_with_registration_off_should_redirect
218 with_settings :self_registration => '0' do
233 with_settings :self_registration => '0' do
219 assert_no_difference 'User.count' do
234 assert_no_difference 'User.count' do
220 post :register, :user => {
235 post :register, :user => {
221 :login => 'register',
236 :login => 'register',
222 :password => 'test',
237 :password => 'test',
223 :password_confirmation => 'test',
238 :password_confirmation => 'test',
224 :firstname => 'John',
239 :firstname => 'John',
225 :lastname => 'Doe',
240 :lastname => 'Doe',
226 :mail => 'register@example.com'
241 :mail => 'register@example.com'
227 }
242 }
228 assert_redirected_to '/'
243 assert_redirected_to '/'
229 end
244 end
230 end
245 end
231 end
246 end
232 end
247 end
@@ -1,200 +1,182
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 begin
20 begin
21 require 'mocha'
21 require 'mocha'
22 rescue
22 rescue
23 # Won't run some tests
23 # Won't run some tests
24 end
24 end
25
25
26 class AccountTest < ActionController::IntegrationTest
26 class AccountTest < ActionController::IntegrationTest
27 fixtures :users, :roles
27 fixtures :users, :roles
28
28
29 # Replace this with your real tests.
29 # Replace this with your real tests.
30 def test_login
30 def test_login
31 get "my/page"
31 get "my/page"
32 assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
32 assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
33 log_user('jsmith', 'jsmith')
33 log_user('jsmith', 'jsmith')
34
34
35 get "my/account"
35 get "my/account"
36 assert_response :success
36 assert_response :success
37 assert_template "my/account"
37 assert_template "my/account"
38 end
38 end
39
39
40 def test_autologin
40 def test_autologin
41 user = User.find(1)
41 user = User.find(1)
42 Setting.autologin = "7"
42 Setting.autologin = "7"
43 Token.delete_all
43 Token.delete_all
44
44
45 # User logs in with 'autologin' checked
45 # User logs in with 'autologin' checked
46 post '/login', :username => user.login, :password => 'admin', :autologin => 1
46 post '/login', :username => user.login, :password => 'admin', :autologin => 1
47 assert_redirected_to '/my/page'
47 assert_redirected_to '/my/page'
48 token = Token.find :first
48 token = Token.find :first
49 assert_not_nil token
49 assert_not_nil token
50 assert_equal user, token.user
50 assert_equal user, token.user
51 assert_equal 'autologin', token.action
51 assert_equal 'autologin', token.action
52 assert_equal user.id, session[:user_id]
52 assert_equal user.id, session[:user_id]
53 assert_equal token.value, cookies['autologin']
53 assert_equal token.value, cookies['autologin']
54
54
55 # Session is cleared
55 # Session is cleared
56 reset!
56 reset!
57 User.current = nil
57 User.current = nil
58 # Clears user's last login timestamp
58 # Clears user's last login timestamp
59 user.update_attribute :last_login_on, nil
59 user.update_attribute :last_login_on, nil
60 assert_nil user.reload.last_login_on
60 assert_nil user.reload.last_login_on
61
61
62 # User comes back with his autologin cookie
62 # User comes back with his autologin cookie
63 cookies[:autologin] = token.value
63 cookies[:autologin] = token.value
64 get '/my/page'
64 get '/my/page'
65 assert_response :success
65 assert_response :success
66 assert_template 'my/page'
66 assert_template 'my/page'
67 assert_equal user.id, session[:user_id]
67 assert_equal user.id, session[:user_id]
68 assert_not_nil user.reload.last_login_on
68 assert_not_nil user.reload.last_login_on
69 assert user.last_login_on.utc > 10.second.ago.utc
69 assert user.last_login_on.utc > 10.second.ago.utc
70 end
70 end
71
71
72 def test_lost_password
72 def test_lost_password
73 Token.delete_all
73 Token.delete_all
74
74
75 get "account/lost_password"
75 get "account/lost_password"
76 assert_response :success
76 assert_response :success
77 assert_template "account/lost_password"
77 assert_template "account/lost_password"
78
78
79 post "account/lost_password", :mail => 'jSmith@somenet.foo'
79 post "account/lost_password", :mail => 'jSmith@somenet.foo'
80 assert_redirected_to "/login"
80 assert_redirected_to "/login"
81
81
82 token = Token.find(:first)
82 token = Token.find(:first)
83 assert_equal 'recovery', token.action
83 assert_equal 'recovery', token.action
84 assert_equal 'jsmith@somenet.foo', token.user.mail
84 assert_equal 'jsmith@somenet.foo', token.user.mail
85 assert !token.expired?
85 assert !token.expired?
86
86
87 get "account/lost_password", :token => token.value
87 get "account/lost_password", :token => token.value
88 assert_response :success
88 assert_response :success
89 assert_template "account/password_recovery"
89 assert_template "account/password_recovery"
90
90
91 post "account/lost_password", :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
91 post "account/lost_password", :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
92 assert_redirected_to "/login"
92 assert_redirected_to "/login"
93 assert_equal 'Password was successfully updated.', flash[:notice]
93 assert_equal 'Password was successfully updated.', flash[:notice]
94
94
95 log_user('jsmith', 'newpass')
95 log_user('jsmith', 'newpass')
96 assert_equal 0, Token.count
96 assert_equal 0, Token.count
97 end
97 end
98
98
99 def test_register_with_automatic_activation
99 def test_register_with_automatic_activation
100 Setting.self_registration = '3'
100 Setting.self_registration = '3'
101
101
102 get 'account/register'
102 get 'account/register'
103 assert_response :success
103 assert_response :success
104 assert_template 'account/register'
104 assert_template 'account/register'
105
105
106 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
106 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
107 :password => "newpass", :password_confirmation => "newpass"}
107 :password => "newpass", :password_confirmation => "newpass"}
108 assert_redirected_to '/my/account'
108 assert_redirected_to '/my/account'
109 follow_redirect!
109 follow_redirect!
110 assert_response :success
110 assert_response :success
111 assert_template 'my/account'
111 assert_template 'my/account'
112
112
113 user = User.find_by_login('newuser')
113 user = User.find_by_login('newuser')
114 assert_not_nil user
114 assert_not_nil user
115 assert user.active?
115 assert user.active?
116 assert_not_nil user.last_login_on
116 assert_not_nil user.last_login_on
117 end
117 end
118
118
119 def test_register_with_manual_activation
119 def test_register_with_manual_activation
120 Setting.self_registration = '2'
120 Setting.self_registration = '2'
121
121
122 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
122 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
123 :password => "newpass", :password_confirmation => "newpass"}
123 :password => "newpass", :password_confirmation => "newpass"}
124 assert_redirected_to '/login'
124 assert_redirected_to '/login'
125 assert !User.find_by_login('newuser').active?
125 assert !User.find_by_login('newuser').active?
126 end
126 end
127
127
128 def test_register_with_email_activation
128 def test_register_with_email_activation
129 Setting.self_registration = '1'
129 Setting.self_registration = '1'
130 Token.delete_all
130 Token.delete_all
131
131
132 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
132 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
133 :password => "newpass", :password_confirmation => "newpass"}
133 :password => "newpass", :password_confirmation => "newpass"}
134 assert_redirected_to '/login'
134 assert_redirected_to '/login'
135 assert !User.find_by_login('newuser').active?
135 assert !User.find_by_login('newuser').active?
136
136
137 token = Token.find(:first)
137 token = Token.find(:first)
138 assert_equal 'register', token.action
138 assert_equal 'register', token.action
139 assert_equal 'newuser@foo.bar', token.user.mail
139 assert_equal 'newuser@foo.bar', token.user.mail
140 assert !token.expired?
140 assert !token.expired?
141
141
142 get 'account/activate', :token => token.value
142 get 'account/activate', :token => token.value
143 assert_redirected_to '/login'
143 assert_redirected_to '/login'
144 log_user('newuser', 'newpass')
144 log_user('newuser', 'newpass')
145 end
145 end
146
146
147 def test_onthefly_registration
147 def test_onthefly_registration
148 # disable registration
148 # disable registration
149 Setting.self_registration = '0'
149 Setting.self_registration = '0'
150 AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
150 AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
151
151
152 post '/login', :username => 'foo', :password => 'bar'
152 post '/login', :username => 'foo', :password => 'bar'
153 assert_redirected_to '/my/page'
153 assert_redirected_to '/my/page'
154
154
155 user = User.find_by_login('foo')
155 user = User.find_by_login('foo')
156 assert user.is_a?(User)
156 assert user.is_a?(User)
157 assert_equal 66, user.auth_source_id
157 assert_equal 66, user.auth_source_id
158 assert user.hashed_password.blank?
158 assert user.hashed_password.blank?
159 end
159 end
160
160
161 def test_onthefly_registration_with_invalid_attributes
161 def test_onthefly_registration_with_invalid_attributes
162 # disable registration
162 # disable registration
163 Setting.self_registration = '0'
163 Setting.self_registration = '0'
164 AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
164 AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
165
165
166 post '/login', :username => 'foo', :password => 'bar'
166 post '/login', :username => 'foo', :password => 'bar'
167 assert_response :success
167 assert_response :success
168 assert_template 'account/register'
168 assert_template 'account/register'
169 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
169 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
170 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
170 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
171 assert_no_tag :input, :attributes => { :name => 'user[login]' }
171 assert_no_tag :input, :attributes => { :name => 'user[login]' }
172 assert_no_tag :input, :attributes => { :name => 'user[password]' }
172 assert_no_tag :input, :attributes => { :name => 'user[password]' }
173
173
174 post 'account/register', :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
174 post 'account/register', :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
175 assert_redirected_to '/my/account'
175 assert_redirected_to '/my/account'
176
176
177 user = User.find_by_login('foo')
177 user = User.find_by_login('foo')
178 assert user.is_a?(User)
178 assert user.is_a?(User)
179 assert_equal 66, user.auth_source_id
179 assert_equal 66, user.auth_source_id
180 assert user.hashed_password.blank?
180 assert user.hashed_password.blank?
181 end
181 end
182
183 def test_login_and_logout_should_clear_session
184 get '/login'
185 sid = session[:session_id]
186
187 post '/login', :username => 'admin', :password => 'admin'
188 assert_redirected_to '/my/page'
189 assert_not_equal sid, session[:session_id], "login should reset session"
190 assert_equal 1, session[:user_id]
191 sid = session[:session_id]
192
193 get '/'
194 assert_equal sid, session[:session_id]
195
196 get '/logout'
197 assert_not_equal sid, session[:session_id], "logout should reset session"
198 assert_nil session[:user_id]
199 end
200 end
182 end
General Comments 0
You need to be logged in to leave comments. Login now