##// END OF EJS Templates
Moved openid functional tests for their own test case....
Jean-Philippe Lang -
r9743:6351631ec618
parent child
Show More
@@ -0,0 +1,133
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.expand_path('../../test_helper', __FILE__)
19
20 class AccountControllerTest < ActionController::TestCase
21 tests AccountController
22 fixtures :users, :roles
23
24 def setup
25 User.current = nil
26 end
27
28 if Object.const_defined?(:OpenID)
29
30 def test_login_with_openid_for_existing_user
31 Setting.self_registration = '3'
32 Setting.openid = '1'
33 existing_user = User.new(:firstname => 'Cool',
34 :lastname => 'User',
35 :mail => 'user@somedomain.com',
36 :identity_url => 'http://openid.example.com/good_user')
37 existing_user.login = 'cool_user'
38 assert existing_user.save!
39
40 post :login, :openid_url => existing_user.identity_url
41 assert_redirected_to '/my/page'
42 end
43
44 def test_login_with_invalid_openid_provider
45 Setting.self_registration = '0'
46 Setting.openid = '1'
47 post :login, :openid_url => 'http;//openid.example.com/good_user'
48 assert_redirected_to home_url
49 end
50
51 def test_login_with_openid_for_existing_non_active_user
52 Setting.self_registration = '2'
53 Setting.openid = '1'
54 existing_user = User.new(:firstname => 'Cool',
55 :lastname => 'User',
56 :mail => 'user@somedomain.com',
57 :identity_url => 'http://openid.example.com/good_user',
58 :status => User::STATUS_REGISTERED)
59 existing_user.login = 'cool_user'
60 assert existing_user.save!
61
62 post :login, :openid_url => existing_user.identity_url
63 assert_redirected_to '/login'
64 end
65
66 def test_login_with_openid_with_new_user_created
67 Setting.self_registration = '3'
68 Setting.openid = '1'
69 post :login, :openid_url => 'http://openid.example.com/good_user'
70 assert_redirected_to '/my/account'
71 user = User.find_by_login('cool_user')
72 assert user
73 assert_equal 'Cool', user.firstname
74 assert_equal 'User', user.lastname
75 end
76
77 def test_login_with_openid_with_new_user_and_self_registration_off
78 Setting.self_registration = '0'
79 Setting.openid = '1'
80 post :login, :openid_url => 'http://openid.example.com/good_user'
81 assert_redirected_to home_url
82 user = User.find_by_login('cool_user')
83 assert ! user
84 end
85
86 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
87 Setting.self_registration = '1'
88 Setting.openid = '1'
89 post :login, :openid_url => 'http://openid.example.com/good_user'
90 assert_redirected_to '/login'
91 user = User.find_by_login('cool_user')
92 assert user
93
94 token = Token.find_by_user_id_and_action(user.id, 'register')
95 assert token
96 end
97
98 def test_login_with_openid_with_new_user_created_with_manual_activation
99 Setting.self_registration = '2'
100 Setting.openid = '1'
101 post :login, :openid_url => 'http://openid.example.com/good_user'
102 assert_redirected_to '/login'
103 user = User.find_by_login('cool_user')
104 assert user
105 assert_equal User::STATUS_REGISTERED, user.status
106 end
107
108 def test_login_with_openid_with_new_user_with_conflict_should_register
109 Setting.self_registration = '3'
110 Setting.openid = '1'
111 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
112 existing_user.login = 'cool_user'
113 assert existing_user.save!
114
115 post :login, :openid_url => 'http://openid.example.com/good_user'
116 assert_response :success
117 assert_template 'register'
118 assert assigns(:user)
119 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
120 end
121
122 def test_setting_openid_should_return_true_when_set_to_true
123 Setting.openid = '1'
124 assert_equal true, Setting.openid?
125 end
126
127 else
128 puts "Skipping openid tests."
129
130 def test_dummy
131 end
132 end
133 end
@@ -1,247 +1,144
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 if Object.const_defined?(:OpenID)
72
73 def test_login_with_openid_for_existing_user
74 Setting.self_registration = '3'
75 Setting.openid = '1'
76 existing_user = User.new(:firstname => 'Cool',
77 :lastname => 'User',
78 :mail => 'user@somedomain.com',
79 :identity_url => 'http://openid.example.com/good_user')
80 existing_user.login = 'cool_user'
81 assert existing_user.save!
82
83 post :login, :openid_url => existing_user.identity_url
84 assert_redirected_to '/my/page'
85 end
86
87 def test_login_with_invalid_openid_provider
88 Setting.self_registration = '0'
89 Setting.openid = '1'
90 post :login, :openid_url => 'http;//openid.example.com/good_user'
91 assert_redirected_to home_url
92 end
93
94 def test_login_with_openid_for_existing_non_active_user
95 Setting.self_registration = '2'
96 Setting.openid = '1'
97 existing_user = User.new(:firstname => 'Cool',
98 :lastname => 'User',
99 :mail => 'user@somedomain.com',
100 :identity_url => 'http://openid.example.com/good_user',
101 :status => User::STATUS_REGISTERED)
102 existing_user.login = 'cool_user'
103 assert existing_user.save!
104
105 post :login, :openid_url => existing_user.identity_url
106 assert_redirected_to '/login'
107 end
108
109 def test_login_with_openid_with_new_user_created
110 Setting.self_registration = '3'
111 Setting.openid = '1'
112 post :login, :openid_url => 'http://openid.example.com/good_user'
113 assert_redirected_to '/my/account'
114 user = User.find_by_login('cool_user')
115 assert user
116 assert_equal 'Cool', user.firstname
117 assert_equal 'User', user.lastname
118 end
119
120 def test_login_with_openid_with_new_user_and_self_registration_off
121 Setting.self_registration = '0'
122 Setting.openid = '1'
123 post :login, :openid_url => 'http://openid.example.com/good_user'
124 assert_redirected_to home_url
125 user = User.find_by_login('cool_user')
126 assert ! user
127 end
128
129 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
130 Setting.self_registration = '1'
131 Setting.openid = '1'
132 post :login, :openid_url => 'http://openid.example.com/good_user'
133 assert_redirected_to '/login'
134 user = User.find_by_login('cool_user')
135 assert user
136
137 token = Token.find_by_user_id_and_action(user.id, 'register')
138 assert token
139 end
140
141 def test_login_with_openid_with_new_user_created_with_manual_activation
142 Setting.self_registration = '2'
143 Setting.openid = '1'
144 post :login, :openid_url => 'http://openid.example.com/good_user'
145 assert_redirected_to '/login'
146 user = User.find_by_login('cool_user')
147 assert user
148 assert_equal User::STATUS_REGISTERED, user.status
149 end
150
151 def test_login_with_openid_with_new_user_with_conflict_should_register
152 Setting.self_registration = '3'
153 Setting.openid = '1'
154 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
155 existing_user.login = 'cool_user'
156 assert existing_user.save!
157
158 post :login, :openid_url => 'http://openid.example.com/good_user'
159 assert_response :success
160 assert_template 'register'
161 assert assigns(:user)
162 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
163 end
164
165 def test_setting_openid_should_return_true_when_set_to_true
166 Setting.openid = '1'
167 assert_equal true, Setting.openid?
168 end
169
170 else
171 puts "Skipping openid tests."
172 end
173
174 def test_logout
71 def test_logout
175 @request.session[:user_id] = 2
72 @request.session[:user_id] = 2
176 get :logout
73 get :logout
177 assert_redirected_to '/'
74 assert_redirected_to '/'
178 assert_nil @request.session[:user_id]
75 assert_nil @request.session[:user_id]
179 end
76 end
180
77
181 def test_logout_should_reset_session
78 def test_logout_should_reset_session
182 @controller.expects(:reset_session).once
79 @controller.expects(:reset_session).once
183
80
184 @request.session[:user_id] = 2
81 @request.session[:user_id] = 2
185 get :logout
82 get :logout
186 assert_response 302
83 assert_response 302
187 end
84 end
188
85
189 def test_get_register_with_registration_on
86 def test_get_register_with_registration_on
190 with_settings :self_registration => '3' do
87 with_settings :self_registration => '3' do
191 get :register
88 get :register
192 assert_response :success
89 assert_response :success
193 assert_template 'register'
90 assert_template 'register'
194 assert_not_nil assigns(:user)
91 assert_not_nil assigns(:user)
195
92
196 assert_tag 'input', :attributes => {:name => 'user[password]'}
93 assert_tag 'input', :attributes => {:name => 'user[password]'}
197 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
94 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'}
198 end
95 end
199 end
96 end
200
97
201 def test_get_register_with_registration_off_should_redirect
98 def test_get_register_with_registration_off_should_redirect
202 with_settings :self_registration => '0' do
99 with_settings :self_registration => '0' do
203 get :register
100 get :register
204 assert_redirected_to '/'
101 assert_redirected_to '/'
205 end
102 end
206 end
103 end
207
104
208 # See integration/account_test.rb for the full test
105 # See integration/account_test.rb for the full test
209 def test_post_register_with_registration_on
106 def test_post_register_with_registration_on
210 with_settings :self_registration => '3' do
107 with_settings :self_registration => '3' do
211 assert_difference 'User.count' do
108 assert_difference 'User.count' do
212 post :register, :user => {
109 post :register, :user => {
213 :login => 'register',
110 :login => 'register',
214 :password => 'test',
111 :password => 'test',
215 :password_confirmation => 'test',
112 :password_confirmation => 'test',
216 :firstname => 'John',
113 :firstname => 'John',
217 :lastname => 'Doe',
114 :lastname => 'Doe',
218 :mail => 'register@example.com'
115 :mail => 'register@example.com'
219 }
116 }
220 assert_redirected_to '/my/account'
117 assert_redirected_to '/my/account'
221 end
118 end
222 user = User.first(:order => 'id DESC')
119 user = User.first(:order => 'id DESC')
223 assert_equal 'register', user.login
120 assert_equal 'register', user.login
224 assert_equal 'John', user.firstname
121 assert_equal 'John', user.firstname
225 assert_equal 'Doe', user.lastname
122 assert_equal 'Doe', user.lastname
226 assert_equal 'register@example.com', user.mail
123 assert_equal 'register@example.com', user.mail
227 assert user.check_password?('test')
124 assert user.check_password?('test')
228 assert user.active?
125 assert user.active?
229 end
126 end
230 end
127 end
231
128
232 def test_post_register_with_registration_off_should_redirect
129 def test_post_register_with_registration_off_should_redirect
233 with_settings :self_registration => '0' do
130 with_settings :self_registration => '0' do
234 assert_no_difference 'User.count' do
131 assert_no_difference 'User.count' do
235 post :register, :user => {
132 post :register, :user => {
236 :login => 'register',
133 :login => 'register',
237 :password => 'test',
134 :password => 'test',
238 :password_confirmation => 'test',
135 :password_confirmation => 'test',
239 :firstname => 'John',
136 :firstname => 'John',
240 :lastname => 'Doe',
137 :lastname => 'Doe',
241 :mail => 'register@example.com'
138 :mail => 'register@example.com'
242 }
139 }
243 assert_redirected_to '/'
140 assert_redirected_to '/'
244 end
141 end
245 end
142 end
246 end
143 end
247 end
144 end
General Comments 0
You need to be logged in to leave comments. Login now