##// END OF EJS Templates
Removed assertion that is susceptible to fail if test runs slowly....
Jean-Philippe Lang -
r9828:570e1b1d6265
parent child
Show More
@@ -1,187 +1,185
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
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 seconds_ago = 10.second.ago.utc
70 assert user.last_login_on.utc > 10.second.ago.utc, "#{user.last_login_on.utc} was not > #{seconds_ago}"
71 end
69 end
72
70
73 def test_lost_password
71 def test_lost_password
74 Token.delete_all
72 Token.delete_all
75
73
76 get "account/lost_password"
74 get "account/lost_password"
77 assert_response :success
75 assert_response :success
78 assert_template "account/lost_password"
76 assert_template "account/lost_password"
79 assert_select 'input[name=mail]'
77 assert_select 'input[name=mail]'
80
78
81 post "account/lost_password", :mail => 'jSmith@somenet.foo'
79 post "account/lost_password", :mail => 'jSmith@somenet.foo'
82 assert_redirected_to "/login"
80 assert_redirected_to "/login"
83
81
84 token = Token.find(:first)
82 token = Token.find(:first)
85 assert_equal 'recovery', token.action
83 assert_equal 'recovery', token.action
86 assert_equal 'jsmith@somenet.foo', token.user.mail
84 assert_equal 'jsmith@somenet.foo', token.user.mail
87 assert !token.expired?
85 assert !token.expired?
88
86
89 get "account/lost_password", :token => token.value
87 get "account/lost_password", :token => token.value
90 assert_response :success
88 assert_response :success
91 assert_template "account/password_recovery"
89 assert_template "account/password_recovery"
92 assert_select 'input[type=hidden][name=token][value=?]', token.value
90 assert_select 'input[type=hidden][name=token][value=?]', token.value
93 assert_select 'input[name=new_password]'
91 assert_select 'input[name=new_password]'
94 assert_select 'input[name=new_password_confirmation]'
92 assert_select 'input[name=new_password_confirmation]'
95
93
96 post "account/lost_password", :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
94 post "account/lost_password", :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
97 assert_redirected_to "/login"
95 assert_redirected_to "/login"
98 assert_equal 'Password was successfully updated.', flash[:notice]
96 assert_equal 'Password was successfully updated.', flash[:notice]
99
97
100 log_user('jsmith', 'newpass')
98 log_user('jsmith', 'newpass')
101 assert_equal 0, Token.count
99 assert_equal 0, Token.count
102 end
100 end
103
101
104 def test_register_with_automatic_activation
102 def test_register_with_automatic_activation
105 Setting.self_registration = '3'
103 Setting.self_registration = '3'
106
104
107 get 'account/register'
105 get 'account/register'
108 assert_response :success
106 assert_response :success
109 assert_template 'account/register'
107 assert_template 'account/register'
110
108
111 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
109 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
112 :password => "newpass", :password_confirmation => "newpass"}
110 :password => "newpass", :password_confirmation => "newpass"}
113 assert_redirected_to '/my/account'
111 assert_redirected_to '/my/account'
114 follow_redirect!
112 follow_redirect!
115 assert_response :success
113 assert_response :success
116 assert_template 'my/account'
114 assert_template 'my/account'
117
115
118 user = User.find_by_login('newuser')
116 user = User.find_by_login('newuser')
119 assert_not_nil user
117 assert_not_nil user
120 assert user.active?
118 assert user.active?
121 assert_not_nil user.last_login_on
119 assert_not_nil user.last_login_on
122 end
120 end
123
121
124 def test_register_with_manual_activation
122 def test_register_with_manual_activation
125 Setting.self_registration = '2'
123 Setting.self_registration = '2'
126
124
127 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
125 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
128 :password => "newpass", :password_confirmation => "newpass"}
126 :password => "newpass", :password_confirmation => "newpass"}
129 assert_redirected_to '/login'
127 assert_redirected_to '/login'
130 assert !User.find_by_login('newuser').active?
128 assert !User.find_by_login('newuser').active?
131 end
129 end
132
130
133 def test_register_with_email_activation
131 def test_register_with_email_activation
134 Setting.self_registration = '1'
132 Setting.self_registration = '1'
135 Token.delete_all
133 Token.delete_all
136
134
137 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
135 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
138 :password => "newpass", :password_confirmation => "newpass"}
136 :password => "newpass", :password_confirmation => "newpass"}
139 assert_redirected_to '/login'
137 assert_redirected_to '/login'
140 assert !User.find_by_login('newuser').active?
138 assert !User.find_by_login('newuser').active?
141
139
142 token = Token.find(:first)
140 token = Token.find(:first)
143 assert_equal 'register', token.action
141 assert_equal 'register', token.action
144 assert_equal 'newuser@foo.bar', token.user.mail
142 assert_equal 'newuser@foo.bar', token.user.mail
145 assert !token.expired?
143 assert !token.expired?
146
144
147 get 'account/activate', :token => token.value
145 get 'account/activate', :token => token.value
148 assert_redirected_to '/login'
146 assert_redirected_to '/login'
149 log_user('newuser', 'newpass')
147 log_user('newuser', 'newpass')
150 end
148 end
151
149
152 def test_onthefly_registration
150 def test_onthefly_registration
153 # disable registration
151 # disable registration
154 Setting.self_registration = '0'
152 Setting.self_registration = '0'
155 AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
153 AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
156
154
157 post '/login', :username => 'foo', :password => 'bar'
155 post '/login', :username => 'foo', :password => 'bar'
158 assert_redirected_to '/my/page'
156 assert_redirected_to '/my/page'
159
157
160 user = User.find_by_login('foo')
158 user = User.find_by_login('foo')
161 assert user.is_a?(User)
159 assert user.is_a?(User)
162 assert_equal 66, user.auth_source_id
160 assert_equal 66, user.auth_source_id
163 assert user.hashed_password.blank?
161 assert user.hashed_password.blank?
164 end
162 end
165
163
166 def test_onthefly_registration_with_invalid_attributes
164 def test_onthefly_registration_with_invalid_attributes
167 # disable registration
165 # disable registration
168 Setting.self_registration = '0'
166 Setting.self_registration = '0'
169 AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
167 AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
170
168
171 post '/login', :username => 'foo', :password => 'bar'
169 post '/login', :username => 'foo', :password => 'bar'
172 assert_response :success
170 assert_response :success
173 assert_template 'account/register'
171 assert_template 'account/register'
174 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
172 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
175 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
173 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
176 assert_no_tag :input, :attributes => { :name => 'user[login]' }
174 assert_no_tag :input, :attributes => { :name => 'user[login]' }
177 assert_no_tag :input, :attributes => { :name => 'user[password]' }
175 assert_no_tag :input, :attributes => { :name => 'user[password]' }
178
176
179 post 'account/register', :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
177 post 'account/register', :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
180 assert_redirected_to '/my/account'
178 assert_redirected_to '/my/account'
181
179
182 user = User.find_by_login('foo')
180 user = User.find_by_login('foo')
183 assert user.is_a?(User)
181 assert user.is_a?(User)
184 assert_equal 66, user.auth_source_id
182 assert_equal 66, user.auth_source_id
185 assert user.hashed_password.blank?
183 assert user.hashed_password.blank?
186 end
184 end
187 end
185 end
General Comments 0
You need to be logged in to leave comments. Login now