@@ -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 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | require 'account_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class AccountController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class AccountControllerTest < ActionController::TestCase |
|
25 | 25 | fixtures :users, :roles |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | @controller = AccountController.new |
|
29 | 29 | @request = ActionController::TestRequest.new |
|
30 | 30 | @response = ActionController::TestResponse.new |
|
31 | 31 | User.current = nil |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_login_should_redirect_to_back_url_param |
|
35 | 35 | # request.uri is "test.host" in test environment |
|
36 | 36 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1' |
|
37 | 37 | assert_redirected_to '/issues/show/1' |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | def test_login_should_not_redirect_to_another_host |
|
41 | 41 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' |
|
42 | 42 | assert_redirected_to '/my/page' |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def test_login_with_wrong_password |
|
46 | 46 | post :login, :username => 'admin', :password => 'bad' |
|
47 | 47 | assert_response :success |
|
48 | 48 | assert_template 'login' |
|
49 | 49 | assert_tag 'div', |
|
50 | 50 | :attributes => { :class => "flash error" }, |
|
51 | 51 | :content => /Invalid user or password/ |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def test_login_should_rescue_auth_source_exception |
|
55 | 55 | source = AuthSource.create!(:name => 'Test') |
|
56 | 56 | User.find(2).update_attribute :auth_source_id, source.id |
|
57 | 57 | AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) |
|
58 | 58 | |
|
59 | 59 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
60 | 60 | assert_response 500 |
|
61 | 61 | assert_error_tag :content => /Something wrong/ |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def test_login_should_reset_session |
|
65 | 65 | @controller.expects(:reset_session).once |
|
66 | 66 | |
|
67 | 67 | post :login, :username => 'jsmith', :password => 'jsmith' |
|
68 | 68 | assert_response 302 |
|
69 | 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 | 71 | def test_logout |
|
175 | 72 | @request.session[:user_id] = 2 |
|
176 | 73 | get :logout |
|
177 | 74 | assert_redirected_to '/' |
|
178 | 75 | assert_nil @request.session[:user_id] |
|
179 | 76 | end |
|
180 | 77 | |
|
181 | 78 | def test_logout_should_reset_session |
|
182 | 79 | @controller.expects(:reset_session).once |
|
183 | 80 | |
|
184 | 81 | @request.session[:user_id] = 2 |
|
185 | 82 | get :logout |
|
186 | 83 | assert_response 302 |
|
187 | 84 | end |
|
188 | 85 | |
|
189 | 86 | def test_get_register_with_registration_on |
|
190 | 87 | with_settings :self_registration => '3' do |
|
191 | 88 | get :register |
|
192 | 89 | assert_response :success |
|
193 | 90 | assert_template 'register' |
|
194 | 91 | assert_not_nil assigns(:user) |
|
195 | 92 | |
|
196 | 93 | assert_tag 'input', :attributes => {:name => 'user[password]'} |
|
197 | 94 | assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'} |
|
198 | 95 | end |
|
199 | 96 | end |
|
200 | 97 | |
|
201 | 98 | def test_get_register_with_registration_off_should_redirect |
|
202 | 99 | with_settings :self_registration => '0' do |
|
203 | 100 | get :register |
|
204 | 101 | assert_redirected_to '/' |
|
205 | 102 | end |
|
206 | 103 | end |
|
207 | 104 | |
|
208 | 105 | # See integration/account_test.rb for the full test |
|
209 | 106 | def test_post_register_with_registration_on |
|
210 | 107 | with_settings :self_registration => '3' do |
|
211 | 108 | assert_difference 'User.count' do |
|
212 | 109 | post :register, :user => { |
|
213 | 110 | :login => 'register', |
|
214 | 111 | :password => 'test', |
|
215 | 112 | :password_confirmation => 'test', |
|
216 | 113 | :firstname => 'John', |
|
217 | 114 | :lastname => 'Doe', |
|
218 | 115 | :mail => 'register@example.com' |
|
219 | 116 | } |
|
220 | 117 | assert_redirected_to '/my/account' |
|
221 | 118 | end |
|
222 | 119 | user = User.first(:order => 'id DESC') |
|
223 | 120 | assert_equal 'register', user.login |
|
224 | 121 | assert_equal 'John', user.firstname |
|
225 | 122 | assert_equal 'Doe', user.lastname |
|
226 | 123 | assert_equal 'register@example.com', user.mail |
|
227 | 124 | assert user.check_password?('test') |
|
228 | 125 | assert user.active? |
|
229 | 126 | end |
|
230 | 127 | end |
|
231 | 128 | |
|
232 | 129 | def test_post_register_with_registration_off_should_redirect |
|
233 | 130 | with_settings :self_registration => '0' do |
|
234 | 131 | assert_no_difference 'User.count' do |
|
235 | 132 | post :register, :user => { |
|
236 | 133 | :login => 'register', |
|
237 | 134 | :password => 'test', |
|
238 | 135 | :password_confirmation => 'test', |
|
239 | 136 | :firstname => 'John', |
|
240 | 137 | :lastname => 'Doe', |
|
241 | 138 | :mail => 'register@example.com' |
|
242 | 139 | } |
|
243 | 140 | assert_redirected_to '/' |
|
244 | 141 | end |
|
245 | 142 | end |
|
246 | 143 | end |
|
247 | 144 | end |
General Comments 0
You need to be logged in to leave comments.
Login now