You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

account_test.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2015 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. require File.expand_path('../../test_helper', __FILE__)
  18. class AccountTest < Redmine::IntegrationTest
  19. fixtures :users, :email_addresses, :roles
  20. def test_login
  21. get "/my/page"
  22. assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
  23. log_user('jsmith', 'jsmith')
  24. get "/my/account"
  25. assert_response :success
  26. assert_template "my/account"
  27. end
  28. def test_login_should_set_session_token
  29. assert_difference 'Token.count' do
  30. log_user('jsmith', 'jsmith')
  31. assert_equal 2, session[:user_id]
  32. assert_not_nil session[:tk]
  33. end
  34. end
  35. def test_autologin
  36. user = User.find(1)
  37. Token.delete_all
  38. with_settings :autologin => '7' do
  39. assert_difference 'Token.count', 2 do
  40. # User logs in with 'autologin' checked
  41. post '/login', :username => user.login, :password => 'admin', :autologin => 1
  42. assert_redirected_to '/my/page'
  43. end
  44. token = Token.where(:action => 'autologin').order(:id => :desc).first
  45. assert_not_nil token
  46. assert_equal user, token.user
  47. assert_equal 'autologin', token.action
  48. assert_equal user.id, session[:user_id]
  49. assert_equal token.value, cookies['autologin']
  50. # Session is cleared
  51. reset!
  52. User.current = nil
  53. # Clears user's last login timestamp
  54. user.update_attribute :last_login_on, nil
  55. assert_nil user.reload.last_login_on
  56. # User comes back with user's autologin cookie
  57. cookies[:autologin] = token.value
  58. get '/my/page'
  59. assert_response :success
  60. assert_template 'my/page'
  61. assert_equal user.id, session[:user_id]
  62. assert_not_nil user.reload.last_login_on
  63. end
  64. end
  65. def test_autologin_should_use_autologin_cookie_name
  66. Token.delete_all
  67. Redmine::Configuration.stubs(:[]).with('autologin_cookie_name').returns('custom_autologin')
  68. Redmine::Configuration.stubs(:[]).with('autologin_cookie_path').returns('/')
  69. Redmine::Configuration.stubs(:[]).with('autologin_cookie_secure').returns(false)
  70. Redmine::Configuration.stubs(:[]).with('sudo_mode_timeout').returns(15)
  71. with_settings :autologin => '7' do
  72. assert_difference 'Token.count', 2 do
  73. post '/login', :username => 'admin', :password => 'admin', :autologin => 1
  74. assert_response 302
  75. end
  76. assert cookies['custom_autologin'].present?
  77. token = cookies['custom_autologin']
  78. # Session is cleared
  79. reset!
  80. cookies['custom_autologin'] = token
  81. get '/my/page'
  82. assert_response :success
  83. assert_difference 'Token.count', -2 do
  84. post '/logout'
  85. end
  86. assert cookies['custom_autologin'].blank?
  87. end
  88. end
  89. def test_lost_password
  90. Token.delete_all
  91. get "/account/lost_password"
  92. assert_response :success
  93. assert_template "account/lost_password"
  94. assert_select 'input[name=mail]'
  95. post "/account/lost_password", :mail => 'jSmith@somenet.foo'
  96. assert_redirected_to "/login"
  97. token = Token.first
  98. assert_equal 'recovery', token.action
  99. assert_equal 'jsmith@somenet.foo', token.user.mail
  100. assert !token.expired?
  101. get "/account/lost_password", :token => token.value
  102. assert_response :success
  103. assert_template "account/password_recovery"
  104. assert_select 'input[type=hidden][name=token][value=?]', token.value
  105. assert_select 'input[name=new_password]'
  106. assert_select 'input[name=new_password_confirmation]'
  107. post "/account/lost_password",
  108. :token => token.value, :new_password => 'newpass123',
  109. :new_password_confirmation => 'newpass123'
  110. assert_redirected_to "/login"
  111. assert_equal 'Password was successfully updated.', flash[:notice]
  112. log_user('jsmith', 'newpass123')
  113. assert_equal false, Token.exists?(token.id), "Password recovery token was not deleted"
  114. end
  115. def test_user_with_must_change_passwd_should_be_forced_to_change_its_password
  116. User.find_by_login('jsmith').update_attribute :must_change_passwd, true
  117. post '/login', :username => 'jsmith', :password => 'jsmith'
  118. assert_redirected_to '/my/page'
  119. follow_redirect!
  120. assert_redirected_to '/my/password'
  121. get '/issues'
  122. assert_redirected_to '/my/password'
  123. end
  124. def test_user_with_must_change_passwd_should_be_able_to_change_its_password
  125. User.find_by_login('jsmith').update_attribute :must_change_passwd, true
  126. post '/login', :username => 'jsmith', :password => 'jsmith'
  127. assert_redirected_to '/my/page'
  128. follow_redirect!
  129. assert_redirected_to '/my/password'
  130. follow_redirect!
  131. assert_response :success
  132. post '/my/password', :password => 'jsmith', :new_password => 'newpassword', :new_password_confirmation => 'newpassword'
  133. assert_redirected_to '/my/account'
  134. follow_redirect!
  135. assert_response :success
  136. assert_equal false, User.find_by_login('jsmith').must_change_passwd?
  137. end
  138. def test_user_with_expired_password_should_be_forced_to_change_its_password
  139. User.find_by_login('jsmith').update_attribute :passwd_changed_on, 14.days.ago
  140. with_settings :password_max_age => 7 do
  141. post '/login', :username => 'jsmith', :password => 'jsmith'
  142. assert_redirected_to '/my/page'
  143. follow_redirect!
  144. assert_redirected_to '/my/password'
  145. get '/issues'
  146. assert_redirected_to '/my/password'
  147. end
  148. end
  149. def test_user_with_expired_password_should_be_able_to_change_its_password
  150. User.find_by_login('jsmith').update_attribute :passwd_changed_on, 14.days.ago
  151. with_settings :password_max_age => 7 do
  152. post '/login', :username => 'jsmith', :password => 'jsmith'
  153. assert_redirected_to '/my/page'
  154. follow_redirect!
  155. assert_redirected_to '/my/password'
  156. follow_redirect!
  157. assert_response :success
  158. post '/my/password', :password => 'jsmith', :new_password => 'newpassword', :new_password_confirmation => 'newpassword'
  159. assert_redirected_to '/my/account'
  160. follow_redirect!
  161. assert_response :success
  162. assert_equal false, User.find_by_login('jsmith').must_change_passwd?
  163. end
  164. end
  165. def test_register_with_automatic_activation
  166. Setting.self_registration = '3'
  167. get '/account/register'
  168. assert_response :success
  169. assert_template 'account/register'
  170. post '/account/register',
  171. :user => {:login => "newuser", :language => "en",
  172. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  173. :password => "newpass123", :password_confirmation => "newpass123"}
  174. assert_redirected_to '/my/account'
  175. follow_redirect!
  176. assert_response :success
  177. assert_template 'my/account'
  178. user = User.find_by_login('newuser')
  179. assert_not_nil user
  180. assert user.active?
  181. assert_not_nil user.last_login_on
  182. end
  183. def test_register_with_manual_activation
  184. Setting.self_registration = '2'
  185. post '/account/register',
  186. :user => {:login => "newuser", :language => "en",
  187. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  188. :password => "newpass123", :password_confirmation => "newpass123"}
  189. assert_redirected_to '/login'
  190. assert !User.find_by_login('newuser').active?
  191. end
  192. def test_register_with_email_activation
  193. Setting.self_registration = '1'
  194. Token.delete_all
  195. post '/account/register',
  196. :user => {:login => "newuser", :language => "en",
  197. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  198. :password => "newpass123", :password_confirmation => "newpass123"}
  199. assert_redirected_to '/login'
  200. assert !User.find_by_login('newuser').active?
  201. token = Token.first
  202. assert_equal 'register', token.action
  203. assert_equal 'newuser@foo.bar', token.user.mail
  204. assert !token.expired?
  205. get '/account/activate', :token => token.value
  206. assert_redirected_to '/login'
  207. log_user('newuser', 'newpass123')
  208. end
  209. def test_onthefly_registration
  210. # disable registration
  211. Setting.self_registration = '0'
  212. AuthSource.expects(:authenticate).returns(
  213. {:login => 'foo', :firstname => 'Foo', :lastname => 'Smith',
  214. :mail => 'foo@bar.com', :auth_source_id => 66})
  215. post '/login', :username => 'foo', :password => 'bar'
  216. assert_redirected_to '/my/page'
  217. user = User.find_by_login('foo')
  218. assert user.is_a?(User)
  219. assert_equal 66, user.auth_source_id
  220. assert user.hashed_password.blank?
  221. end
  222. def test_onthefly_registration_with_invalid_attributes
  223. # disable registration
  224. Setting.self_registration = '0'
  225. AuthSource.expects(:authenticate).returns(
  226. {:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
  227. post '/login', :username => 'foo', :password => 'bar'
  228. assert_response :success
  229. assert_template 'account/register'
  230. assert_select 'input[name=?][value=""]', 'user[firstname]'
  231. assert_select 'input[name=?][value=Smith]', 'user[lastname]'
  232. assert_select 'input[name=?]', 'user[login]', 0
  233. assert_select 'input[name=?]', 'user[password]', 0
  234. post '/account/register',
  235. :user => {:firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'}
  236. assert_redirected_to '/my/account'
  237. user = User.find_by_login('foo')
  238. assert user.is_a?(User)
  239. assert_equal 66, user.auth_source_id
  240. assert user.hashed_password.blank?
  241. end
  242. def test_registered_user_should_be_able_to_get_a_new_activation_email
  243. Token.delete_all
  244. with_settings :self_registration => '1', :default_language => 'en' do
  245. # register a new account
  246. assert_difference 'User.count' do
  247. assert_difference 'Token.count' do
  248. post '/account/register',
  249. :user => {:login => "newuser", :language => "en",
  250. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  251. :password => "newpass123", :password_confirmation => "newpass123"}
  252. end
  253. end
  254. user = User.order('id desc').first
  255. assert_equal User::STATUS_REGISTERED, user.status
  256. reset!
  257. # try to use "lost password"
  258. assert_no_difference 'ActionMailer::Base.deliveries.size' do
  259. post '/account/lost_password', :mail => 'newuser@foo.bar'
  260. end
  261. assert_redirected_to '/account/lost_password'
  262. follow_redirect!
  263. assert_response :success
  264. assert_select 'div.flash', :text => /new activation email/
  265. assert_select 'div.flash a[href="/account/activation_email"]'
  266. # request a new action activation email
  267. assert_difference 'ActionMailer::Base.deliveries.size' do
  268. get '/account/activation_email'
  269. end
  270. assert_redirected_to '/login'
  271. token = Token.order('id desc').first
  272. activation_path = "/account/activate?token=#{token.value}"
  273. assert_include activation_path, mail_body(ActionMailer::Base.deliveries.last)
  274. # activate the account
  275. get activation_path
  276. assert_redirected_to '/login'
  277. post '/login', :username => 'newuser', :password => 'newpass123'
  278. assert_redirected_to '/my/page'
  279. end
  280. end
  281. end