##// END OF EJS Templates
Test failure....
Jean-Philippe Lang -
r9761:c02594af96cd
parent child
Show More
@@ -1,185 +1,188
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
144
145 def test_get_lost_password_should_display_lost_password_form
145 def test_get_lost_password_should_display_lost_password_form
146 get :lost_password
146 get :lost_password
147 assert_response :success
147 assert_response :success
148 assert_select 'input[name=mail]'
148 assert_select 'input[name=mail]'
149 end
149 end
150
150
151 def test_lost_password_for_active_user_should_create_a_token
151 def test_lost_password_for_active_user_should_create_a_token
152 Token.delete_all
152 assert_difference 'ActionMailer::Base.deliveries.size' do
153 assert_difference 'ActionMailer::Base.deliveries.size' do
153 assert_difference 'Token.count' do
154 assert_difference 'Token.count' do
154 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
155 with_settings :host_name => 'mydomain.foo', :protocol => 'http' do
155 post :lost_password, :mail => 'JSmith@somenet.foo'
156 post :lost_password, :mail => 'JSmith@somenet.foo'
156 assert_redirected_to '/login'
157 assert_redirected_to '/login'
157 end
158 end
158 end
159 end
159 end
160 end
160
161
161 token = Token.order('id DESC').first
162 token = Token.order('id DESC').first
162 assert_equal User.find(2), token.user
163 assert_equal User.find(2), token.user
163 assert_equal 'recovery', token.action
164 assert_equal 'recovery', token.action
164
165
165 assert_select_email do
166 assert_select_email do
166 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
167 assert_select "a[href=?]", "http://mydomain.foo/account/lost_password?token=#{token.value}"
167 end
168 end
168 end
169 end
169
170
170 def test_lost_password_for_unknown_user_should_fail
171 def test_lost_password_for_unknown_user_should_fail
172 Token.delete_all
171 assert_no_difference 'Token.count' do
173 assert_no_difference 'Token.count' do
172 post :lost_password, :mail => 'invalid@somenet.foo'
174 post :lost_password, :mail => 'invalid@somenet.foo'
173 assert_response :success
175 assert_response :success
174 end
176 end
175 end
177 end
176
178
177 def test_lost_password_for_non_active_user_should_fail
179 def test_lost_password_for_non_active_user_should_fail
180 Token.delete_all
178 assert User.find(2).lock!
181 assert User.find(2).lock!
179
182
180 assert_no_difference 'Token.count' do
183 assert_no_difference 'Token.count' do
181 post :lost_password, :mail => 'JSmith@somenet.foo'
184 post :lost_password, :mail => 'JSmith@somenet.foo'
182 assert_response :success
185 assert_response :success
183 end
186 end
184 end
187 end
185 end
188 end
General Comments 0
You need to be logged in to leave comments. Login now