Mercurial > hg > soundsoftware-site
comparison test/functional/account_controller_test.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 261b3d9a4903 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 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. |
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_get_login | |
35 get :login | |
36 assert_response :success | |
37 assert_template 'login' | |
38 | |
39 assert_select 'input[name=username]' | |
40 assert_select 'input[name=password]' | |
41 end | |
42 | |
34 def test_login_should_redirect_to_back_url_param | 43 def test_login_should_redirect_to_back_url_param |
35 # request.uri is "test.host" in test environment | 44 # 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' | 45 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.host/issues/show/1' |
37 assert_redirected_to '/issues/show/1' | 46 assert_redirected_to '/issues/show/1' |
38 end | 47 end |
39 | 48 |
40 def test_login_should_not_redirect_to_another_host | 49 def test_login_should_not_redirect_to_another_host |
41 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' | 50 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http://test.foo/fake' |
42 assert_redirected_to '/my/page' | 51 assert_redirected_to '/my/page' |
43 end | 52 end |
44 | 53 |
45 def test_login_with_wrong_password | 54 def test_login_with_wrong_password |
46 post :login, :username => 'admin', :password => 'bad' | 55 post :login, :username => 'admin', :password => 'bad' |
47 assert_response :success | 56 assert_response :success |
48 assert_template 'login' | 57 assert_template 'login' |
49 assert_tag 'div', | 58 |
50 :attributes => { :class => "flash error" }, | 59 assert_select 'div.flash.error', :text => /Invalid user or password/ |
51 :content => /Invalid user or password/ | 60 assert_select 'input[name=username][value=admin]' |
52 end | 61 assert_select 'input[name=password]' |
53 | 62 assert_select 'input[name=password][value]', 0 |
54 if Object.const_defined?(:OpenID) | 63 end |
55 | 64 |
56 def test_login_with_openid_for_existing_user | 65 def test_login_should_rescue_auth_source_exception |
57 Setting.self_registration = '3' | 66 source = AuthSource.create!(:name => 'Test') |
58 Setting.openid = '1' | 67 User.find(2).update_attribute :auth_source_id, source.id |
59 existing_user = User.new(:firstname => 'Cool', | 68 AuthSource.any_instance.stubs(:authenticate).raises(AuthSourceException.new("Something wrong")) |
60 :lastname => 'User', | 69 |
61 :mail => 'user@somedomain.com', | 70 post :login, :username => 'jsmith', :password => 'jsmith' |
62 :identity_url => 'http://openid.example.com/good_user') | 71 assert_response 500 |
63 existing_user.login = 'cool_user' | 72 assert_error_tag :content => /Something wrong/ |
64 assert existing_user.save! | 73 end |
65 | 74 |
66 post :login, :openid_url => existing_user.identity_url | 75 def test_login_should_reset_session |
67 assert_redirected_to '/my/page' | 76 @controller.expects(:reset_session).once |
68 end | 77 |
69 | 78 post :login, :username => 'jsmith', :password => 'jsmith' |
70 def test_login_with_invalid_openid_provider | 79 assert_response 302 |
71 Setting.self_registration = '0' | |
72 Setting.openid = '1' | |
73 post :login, :openid_url => 'http;//openid.example.com/good_user' | |
74 assert_redirected_to home_url | |
75 end | |
76 | |
77 def test_login_with_openid_for_existing_non_active_user | |
78 Setting.self_registration = '2' | |
79 Setting.openid = '1' | |
80 existing_user = User.new(:firstname => 'Cool', | |
81 :lastname => 'User', | |
82 :mail => 'user@somedomain.com', | |
83 :identity_url => 'http://openid.example.com/good_user', | |
84 :status => User::STATUS_REGISTERED) | |
85 existing_user.login = 'cool_user' | |
86 assert existing_user.save! | |
87 | |
88 post :login, :openid_url => existing_user.identity_url | |
89 assert_redirected_to '/login' | |
90 end | |
91 | |
92 def test_login_with_openid_with_new_user_created | |
93 Setting.self_registration = '3' | |
94 Setting.openid = '1' | |
95 post :login, :openid_url => 'http://openid.example.com/good_user' | |
96 assert_redirected_to '/my/account' | |
97 user = User.find_by_login('cool_user') | |
98 assert user | |
99 assert_equal 'Cool', user.firstname | |
100 assert_equal 'User', user.lastname | |
101 end | |
102 | |
103 def test_login_with_openid_with_new_user_and_self_registration_off | |
104 Setting.self_registration = '0' | |
105 Setting.openid = '1' | |
106 post :login, :openid_url => 'http://openid.example.com/good_user' | |
107 assert_redirected_to home_url | |
108 user = User.find_by_login('cool_user') | |
109 assert ! user | |
110 end | |
111 | |
112 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token | |
113 Setting.self_registration = '1' | |
114 Setting.openid = '1' | |
115 post :login, :openid_url => 'http://openid.example.com/good_user' | |
116 assert_redirected_to '/login' | |
117 user = User.find_by_login('cool_user') | |
118 assert user | |
119 | |
120 token = Token.find_by_user_id_and_action(user.id, 'register') | |
121 assert token | |
122 end | |
123 | |
124 def test_login_with_openid_with_new_user_created_with_manual_activation | |
125 Setting.self_registration = '2' | |
126 Setting.openid = '1' | |
127 post :login, :openid_url => 'http://openid.example.com/good_user' | |
128 assert_redirected_to '/login' | |
129 user = User.find_by_login('cool_user') | |
130 assert user | |
131 assert_equal User::STATUS_REGISTERED, user.status | |
132 end | |
133 | |
134 def test_login_with_openid_with_new_user_with_conflict_should_register | |
135 Setting.self_registration = '3' | |
136 Setting.openid = '1' | |
137 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com') | |
138 existing_user.login = 'cool_user' | |
139 assert existing_user.save! | |
140 | |
141 post :login, :openid_url => 'http://openid.example.com/good_user' | |
142 assert_response :success | |
143 assert_template 'register' | |
144 assert assigns(:user) | |
145 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url] | |
146 end | |
147 | |
148 def test_setting_openid_should_return_true_when_set_to_true | |
149 Setting.openid = '1' | |
150 assert_equal true, Setting.openid? | |
151 end | |
152 | |
153 else | |
154 puts "Skipping openid tests." | |
155 end | 80 end |
156 | 81 |
157 def test_logout | 82 def test_logout |
158 @request.session[:user_id] = 2 | 83 @request.session[:user_id] = 2 |
159 get :logout | 84 get :logout |
160 assert_redirected_to '/' | 85 assert_redirected_to '/' |
161 assert_nil @request.session[:user_id] | 86 assert_nil @request.session[:user_id] |
162 end | 87 end |
163 | 88 |
164 context "GET #register" do | 89 def test_logout_should_reset_session |
165 context "with self registration on" do | 90 @controller.expects(:reset_session).once |
166 setup do | 91 |
167 Setting.self_registration = '3' | 92 @request.session[:user_id] = 2 |
168 get :register | 93 get :logout |
94 assert_response 302 | |
95 end | |
96 | |
97 def test_get_register_with_registration_on | |
98 with_settings :self_registration => '3' do | |
99 get :register | |
100 assert_response :success | |
101 assert_template 'register' | |
102 assert_not_nil assigns(:user) | |
103 | |
104 assert_tag 'input', :attributes => {:name => 'user[password]'} | |
105 assert_tag 'input', :attributes => {:name => 'user[password_confirmation]'} | |
106 end | |
107 end | |
108 | |
109 def test_get_register_with_registration_off_should_redirect | |
110 with_settings :self_registration => '0' do | |
111 get :register | |
112 assert_redirected_to '/' | |
113 end | |
114 end | |
115 | |
116 # See integration/account_test.rb for the full test | |
117 def test_post_register_with_registration_on | |
118 with_settings :self_registration => '3' do | |
119 assert_difference 'User.count' do | |
120 post :register, :user => { | |
121 :login => 'register', | |
122 :password => 'secret123', | |
123 :password_confirmation => 'secret123', | |
124 :firstname => 'John', | |
125 :lastname => 'Doe', | |
126 :mail => 'register@example.com' | |
127 } | |
128 assert_redirected_to '/my/account' | |
169 end | 129 end |
170 | 130 user = User.first(:order => 'id DESC') |
171 should_respond_with :success | 131 assert_equal 'register', user.login |
172 should_render_template :register | 132 assert_equal 'John', user.firstname |
173 should_assign_to :user | 133 assert_equal 'Doe', user.lastname |
174 end | 134 assert_equal 'register@example.com', user.mail |
175 | 135 assert user.check_password?('secret123') |
176 context "with self registration off" do | 136 assert user.active? |
177 setup do | 137 end |
178 Setting.self_registration = '0' | 138 end |
179 get :register | 139 |
180 end | 140 def test_post_register_with_registration_off_should_redirect |
181 | 141 with_settings :self_registration => '0' do |
182 should_redirect_to('/') { home_url } | 142 assert_no_difference 'User.count' do |
183 end | |
184 end | |
185 | |
186 # See integration/account_test.rb for the full test | |
187 context "POST #register" do | |
188 context "with self registration on automatic" do | |
189 setup do | |
190 Setting.self_registration = '3' | |
191 post :register, :user => { | 143 post :register, :user => { |
192 :login => 'register', | 144 :login => 'register', |
193 :password => 'test', | 145 :password => 'test', |
194 :password_confirmation => 'test', | 146 :password_confirmation => 'test', |
195 :firstname => 'John', | 147 :firstname => 'John', |
196 :lastname => 'Doe', | 148 :lastname => 'Doe', |
197 :mail => 'register@example.com' | 149 :mail => 'register@example.com' |
198 } | 150 } |
151 assert_redirected_to '/' | |
199 end | 152 end |
200 | 153 end |
201 should_respond_with :redirect | 154 end |
202 should_assign_to :user | 155 |
203 should_redirect_to('my page') { {:controller => 'my', :action => 'account'} } | 156 def test_get_lost_password_should_display_lost_password_form |
204 | 157 get :lost_password |
205 should_create_a_new_user { User.last(:conditions => {:login => 'register'}) } | 158 assert_response :success |
206 | 159 assert_select 'input[name=mail]' |
207 should 'set the user status to active' do | 160 end |
208 user = User.last(:conditions => {:login => 'register'}) | 161 |
209 assert user | 162 def test_lost_password_for_active_user_should_create_a_token |
210 assert_equal User::STATUS_ACTIVE, user.status | 163 Token.delete_all |
164 ActionMailer::Base.deliveries.clear | |
165 assert_difference 'ActionMailer::Base.deliveries.size' do | |
166 assert_difference 'Token.count' do | |
167 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do | |
168 post :lost_password, :mail => 'JSmith@somenet.foo' | |
169 assert_redirected_to '/login' | |
170 end | |
211 end | 171 end |
212 end | 172 end |
213 | 173 |
214 context "with self registration off" do | 174 token = Token.order('id DESC').first |
215 setup do | 175 assert_equal User.find(2), token.user |
216 Setting.self_registration = '0' | 176 assert_equal 'recovery', token.action |
217 post :register | 177 |
218 end | 178 assert_select_email do |
219 | 179 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}" |
220 should_redirect_to('/') { home_url } | 180 end |
221 end | 181 end |
182 | |
183 def test_lost_password_for_unknown_user_should_fail | |
184 Token.delete_all | |
185 assert_no_difference 'Token.count' do | |
186 post :lost_password, :mail => 'invalid@somenet.foo' | |
187 assert_response :success | |
188 end | |
189 end | |
190 | |
191 def test_lost_password_for_non_active_user_should_fail | |
192 Token.delete_all | |
193 assert User.find(2).lock! | |
194 | |
195 assert_no_difference 'Token.count' do | |
196 post :lost_password, :mail => 'JSmith@somenet.foo' | |
197 assert_response :success | |
198 end | |
199 end | |
200 | |
201 def test_get_lost_password_with_token_should_display_the_password_recovery_form | |
202 user = User.find(2) | |
203 token = Token.create!(:action => 'recovery', :user => user) | |
204 | |
205 get :lost_password, :token => token.value | |
206 assert_response :success | |
207 assert_template 'password_recovery' | |
208 | |
209 assert_select 'input[type=hidden][name=token][value=?]', token.value | |
210 end | |
211 | |
212 def test_get_lost_password_with_invalid_token_should_redirect | |
213 get :lost_password, :token => "abcdef" | |
214 assert_redirected_to '/' | |
215 end | |
216 | |
217 def test_post_lost_password_with_token_should_change_the_user_password | |
218 user = User.find(2) | |
219 token = Token.create!(:action => 'recovery', :user => user) | |
220 | |
221 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
222 assert_redirected_to '/login' | |
223 user.reload | |
224 assert user.check_password?('newpass123') | |
225 assert_nil Token.find_by_id(token.id), "Token was not deleted" | |
226 end | |
227 | |
228 def test_post_lost_password_with_token_for_non_active_user_should_fail | |
229 user = User.find(2) | |
230 token = Token.create!(:action => 'recovery', :user => user) | |
231 user.lock! | |
232 | |
233 post :lost_password, :token => token.value, :new_password => 'newpass123', :new_password_confirmation => 'newpass123' | |
234 assert_redirected_to '/' | |
235 assert ! user.check_password?('newpass123') | |
236 end | |
237 | |
238 def test_post_lost_password_with_token_and_password_confirmation_failure_should_redisplay_the_form | |
239 user = User.find(2) | |
240 token = Token.create!(:action => 'recovery', :user => user) | |
241 | |
242 post :lost_password, :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'wrongpass' | |
243 assert_response :success | |
244 assert_template 'password_recovery' | |
245 assert_not_nil Token.find_by_id(token.id), "Token was deleted" | |
246 | |
247 assert_select 'input[type=hidden][name=token][value=?]', token.value | |
248 end | |
249 | |
250 def test_post_lost_password_with_invalid_token_should_redirect | |
251 post :lost_password, :token => "abcdef", :new_password => 'newpass', :new_password_confirmation => 'newpass' | |
252 assert_redirected_to '/' | |
222 end | 253 end |
223 end | 254 end |