Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require "#{File.dirname(__FILE__)}/../test_helper"
|
Chris@0
|
19
|
Chris@0
|
20 begin
|
Chris@0
|
21 require 'mocha'
|
Chris@0
|
22 rescue
|
Chris@0
|
23 # Won't run some tests
|
Chris@0
|
24 end
|
Chris@0
|
25
|
Chris@0
|
26 class AccountTest < ActionController::IntegrationTest
|
Chris@0
|
27 fixtures :users, :roles
|
Chris@0
|
28
|
Chris@0
|
29 # Replace this with your real tests.
|
Chris@0
|
30 def test_login
|
Chris@0
|
31 get "my/page"
|
Chris@0
|
32 assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
|
Chris@0
|
33 log_user('jsmith', 'jsmith')
|
Chris@0
|
34
|
Chris@0
|
35 get "my/account"
|
Chris@0
|
36 assert_response :success
|
Chris@0
|
37 assert_template "my/account"
|
Chris@0
|
38 end
|
Chris@0
|
39
|
Chris@0
|
40 def test_autologin
|
Chris@0
|
41 user = User.find(1)
|
Chris@0
|
42 Setting.autologin = "7"
|
Chris@0
|
43 Token.delete_all
|
Chris@0
|
44
|
Chris@0
|
45 # User logs in with 'autologin' checked
|
Chris@0
|
46 post '/login', :username => user.login, :password => 'admin', :autologin => 1
|
chris@37
|
47 assert_redirected_to '/my/page'
|
Chris@0
|
48 token = Token.find :first
|
Chris@0
|
49 assert_not_nil token
|
Chris@0
|
50 assert_equal user, token.user
|
Chris@0
|
51 assert_equal 'autologin', token.action
|
Chris@0
|
52 assert_equal user.id, session[:user_id]
|
Chris@0
|
53 assert_equal token.value, cookies['autologin']
|
Chris@0
|
54
|
Chris@0
|
55 # Session is cleared
|
Chris@0
|
56 reset!
|
Chris@0
|
57 User.current = nil
|
Chris@0
|
58 # Clears user's last login timestamp
|
Chris@0
|
59 user.update_attribute :last_login_on, nil
|
Chris@0
|
60 assert_nil user.reload.last_login_on
|
Chris@0
|
61
|
Chris@0
|
62 # User comes back with his autologin cookie
|
Chris@0
|
63 cookies[:autologin] = token.value
|
Chris@0
|
64 get '/my/page'
|
Chris@0
|
65 assert_response :success
|
Chris@0
|
66 assert_template 'my/page'
|
Chris@0
|
67 assert_equal user.id, session[:user_id]
|
Chris@0
|
68 assert_not_nil user.reload.last_login_on
|
Chris@0
|
69 assert user.last_login_on.utc > 10.second.ago.utc
|
Chris@0
|
70 end
|
Chris@0
|
71
|
Chris@0
|
72 def test_lost_password
|
Chris@0
|
73 Token.delete_all
|
Chris@0
|
74
|
Chris@0
|
75 get "account/lost_password"
|
Chris@0
|
76 assert_response :success
|
Chris@0
|
77 assert_template "account/lost_password"
|
Chris@0
|
78
|
Chris@0
|
79 post "account/lost_password", :mail => 'jSmith@somenet.foo'
|
Chris@0
|
80 assert_redirected_to "/login"
|
Chris@0
|
81
|
Chris@0
|
82 token = Token.find(:first)
|
Chris@0
|
83 assert_equal 'recovery', token.action
|
Chris@0
|
84 assert_equal 'jsmith@somenet.foo', token.user.mail
|
Chris@0
|
85 assert !token.expired?
|
Chris@0
|
86
|
Chris@0
|
87 get "account/lost_password", :token => token.value
|
Chris@0
|
88 assert_response :success
|
Chris@0
|
89 assert_template "account/password_recovery"
|
Chris@0
|
90
|
Chris@0
|
91 post "account/lost_password", :token => token.value, :new_password => 'newpass', :new_password_confirmation => 'newpass'
|
Chris@0
|
92 assert_redirected_to "/login"
|
Chris@0
|
93 assert_equal 'Password was successfully updated.', flash[:notice]
|
Chris@0
|
94
|
Chris@0
|
95 log_user('jsmith', 'newpass')
|
Chris@0
|
96 assert_equal 0, Token.count
|
Chris@0
|
97 end
|
Chris@0
|
98
|
Chris@0
|
99 def test_register_with_automatic_activation
|
Chris@0
|
100 Setting.self_registration = '3'
|
Chris@0
|
101
|
Chris@0
|
102 get 'account/register'
|
Chris@0
|
103 assert_response :success
|
Chris@0
|
104 assert_template 'account/register'
|
Chris@0
|
105
|
Chris@0
|
106 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar"},
|
Chris@0
|
107 :password => "newpass", :password_confirmation => "newpass"
|
chris@37
|
108 assert_redirected_to '/my/account'
|
Chris@0
|
109 follow_redirect!
|
Chris@0
|
110 assert_response :success
|
Chris@0
|
111 assert_template 'my/account'
|
Chris@0
|
112
|
Chris@0
|
113 user = User.find_by_login('newuser')
|
Chris@0
|
114 assert_not_nil user
|
Chris@0
|
115 assert user.active?
|
Chris@0
|
116 assert_not_nil user.last_login_on
|
Chris@0
|
117 end
|
Chris@0
|
118
|
Chris@0
|
119 def test_register_with_manual_activation
|
Chris@0
|
120 Setting.self_registration = '2'
|
Chris@0
|
121
|
Chris@0
|
122 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar"},
|
Chris@0
|
123 :password => "newpass", :password_confirmation => "newpass"
|
Chris@0
|
124 assert_redirected_to '/login'
|
Chris@0
|
125 assert !User.find_by_login('newuser').active?
|
Chris@0
|
126 end
|
Chris@0
|
127
|
Chris@0
|
128 def test_register_with_email_activation
|
Chris@0
|
129 Setting.self_registration = '1'
|
Chris@0
|
130 Token.delete_all
|
Chris@0
|
131
|
Chris@0
|
132 post 'account/register', :user => {:login => "newuser", :language => "en", :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar"},
|
Chris@0
|
133 :password => "newpass", :password_confirmation => "newpass"
|
Chris@0
|
134 assert_redirected_to '/login'
|
Chris@0
|
135 assert !User.find_by_login('newuser').active?
|
Chris@0
|
136
|
Chris@0
|
137 token = Token.find(:first)
|
Chris@0
|
138 assert_equal 'register', token.action
|
Chris@0
|
139 assert_equal 'newuser@foo.bar', token.user.mail
|
Chris@0
|
140 assert !token.expired?
|
Chris@0
|
141
|
Chris@0
|
142 get 'account/activate', :token => token.value
|
Chris@0
|
143 assert_redirected_to '/login'
|
Chris@0
|
144 log_user('newuser', 'newpass')
|
Chris@0
|
145 end
|
Chris@0
|
146
|
Chris@0
|
147 if Object.const_defined?(:Mocha)
|
Chris@0
|
148
|
Chris@0
|
149 def test_onthefly_registration
|
Chris@0
|
150 # disable registration
|
Chris@0
|
151 Setting.self_registration = '0'
|
Chris@0
|
152 AuthSource.expects(:authenticate).returns({:login => 'foo', :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com', :auth_source_id => 66})
|
Chris@0
|
153
|
Chris@0
|
154 post 'account/login', :username => 'foo', :password => 'bar'
|
chris@37
|
155 assert_redirected_to '/my/page'
|
Chris@0
|
156
|
Chris@0
|
157 user = User.find_by_login('foo')
|
Chris@0
|
158 assert user.is_a?(User)
|
Chris@0
|
159 assert_equal 66, user.auth_source_id
|
Chris@0
|
160 assert user.hashed_password.blank?
|
Chris@0
|
161 end
|
Chris@0
|
162
|
Chris@0
|
163 def test_onthefly_registration_with_invalid_attributes
|
Chris@0
|
164 # disable registration
|
Chris@0
|
165 Setting.self_registration = '0'
|
Chris@0
|
166 AuthSource.expects(:authenticate).returns({:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
|
Chris@0
|
167
|
Chris@0
|
168 post 'account/login', :username => 'foo', :password => 'bar'
|
Chris@0
|
169 assert_response :success
|
Chris@0
|
170 assert_template 'account/register'
|
Chris@0
|
171 assert_tag :input, :attributes => { :name => 'user[firstname]', :value => '' }
|
Chris@0
|
172 assert_tag :input, :attributes => { :name => 'user[lastname]', :value => 'Smith' }
|
Chris@0
|
173 assert_no_tag :input, :attributes => { :name => 'user[login]' }
|
Chris@0
|
174 assert_no_tag :input, :attributes => { :name => 'user[password]' }
|
Chris@0
|
175
|
Chris@0
|
176 post 'account/register', :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
|
Chris@0
|
177 assert_redirected_to '/my/account'
|
Chris@0
|
178
|
Chris@0
|
179 user = User.find_by_login('foo')
|
Chris@0
|
180 assert user.is_a?(User)
|
Chris@0
|
181 assert_equal 66, user.auth_source_id
|
Chris@0
|
182 assert user.hashed_password.blank?
|
Chris@0
|
183 end
|
Chris@0
|
184
|
Chris@0
|
185 def test_login_and_logout_should_clear_session
|
Chris@0
|
186 get '/login'
|
Chris@0
|
187 sid = session[:session_id]
|
Chris@0
|
188
|
Chris@0
|
189 post '/login', :username => 'admin', :password => 'admin'
|
chris@37
|
190 assert_redirected_to '/my/page'
|
Chris@0
|
191 assert_not_equal sid, session[:session_id], "login should reset session"
|
Chris@0
|
192 assert_equal 1, session[:user_id]
|
Chris@0
|
193 sid = session[:session_id]
|
Chris@0
|
194
|
Chris@0
|
195 get '/'
|
Chris@0
|
196 assert_equal sid, session[:session_id]
|
Chris@0
|
197
|
Chris@0
|
198 get '/logout'
|
Chris@0
|
199 assert_not_equal sid, session[:session_id], "logout should reset session"
|
Chris@0
|
200 assert_nil session[:user_id]
|
Chris@0
|
201 end
|
Chris@0
|
202
|
Chris@0
|
203 else
|
Chris@0
|
204 puts 'Mocha is missing. Skipping tests.'
|
Chris@0
|
205 end
|
Chris@0
|
206 end
|