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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class AccountTest < Redmine::IntegrationTest
  20. fixtures :users, :email_addresses, :roles
  21. def test_login
  22. get "/my/page"
  23. assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fmy%2Fpage"
  24. log_user('jsmith', 'jsmith')
  25. get "/my/account"
  26. assert_response :success
  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', :params => {
  42. :username => user.login,
  43. :password => 'admin',
  44. :autologin => 1
  45. }
  46. assert_redirected_to '/my/page'
  47. end
  48. token = Token.where(:action => 'autologin').order(:id => :desc).first
  49. assert_not_nil token
  50. assert_equal user, token.user
  51. assert_equal 'autologin', token.action
  52. assert_equal user.id, session[:user_id]
  53. assert_equal token.value, cookies['autologin']
  54. # Session is cleared
  55. reset!
  56. User.current = nil
  57. # Clears user's last login timestamp
  58. user.update_attribute :last_login_on, nil
  59. assert_nil user.reload.last_login_on
  60. # User comes back with user's autologin cookie
  61. cookies[:autologin] = token.value
  62. get '/my/page'
  63. assert_response :success
  64. assert_equal user.id, session[:user_id]
  65. assert_not_nil user.reload.last_login_on
  66. end
  67. end
  68. def test_autologin_should_use_autologin_cookie_name
  69. Token.delete_all
  70. Redmine::Configuration.stubs(:[]).with('autologin_cookie_name').returns('custom_autologin')
  71. Redmine::Configuration.stubs(:[]).with('autologin_cookie_path').returns('/')
  72. Redmine::Configuration.stubs(:[]).with('autologin_cookie_secure').returns(false)
  73. Redmine::Configuration.stubs(:[]).with('sudo_mode_timeout').returns(15)
  74. with_settings :autologin => '7' do
  75. assert_difference 'Token.count', 2 do
  76. post '/login', :params => {
  77. :username => 'admin',
  78. :password => 'admin',
  79. :autologin => 1
  80. }
  81. assert_response 302
  82. end
  83. assert cookies['custom_autologin'].present?
  84. token = cookies['custom_autologin']
  85. # Session is cleared
  86. reset!
  87. cookies['custom_autologin'] = token
  88. get '/my/page'
  89. assert_response :success
  90. assert_difference 'Token.count', -2 do
  91. post '/logout'
  92. end
  93. assert cookies['custom_autologin'].blank?
  94. end
  95. end
  96. def test_lost_password
  97. Token.delete_all
  98. get "/account/lost_password"
  99. assert_response :success
  100. assert_select 'input[name=mail]'
  101. post "/account/lost_password", :params => {
  102. :mail => 'jSmith@somenet.foo'
  103. }
  104. assert_redirected_to "/login"
  105. token = Token.first
  106. assert_equal 'recovery', token.action
  107. assert_equal 'jsmith@somenet.foo', token.user.mail
  108. assert !token.expired?
  109. get "/account/lost_password", :params => {
  110. :token => token.value
  111. }
  112. assert_redirected_to '/account/lost_password'
  113. follow_redirect!
  114. assert_response :success
  115. assert_select 'input[type=hidden][name=token][value=?]', token.value
  116. assert_select 'input[name=new_password]'
  117. assert_select 'input[name=new_password_confirmation]'
  118. post "/account/lost_password", :params => {
  119. :token => token.value, :new_password => 'newpass123',
  120. :new_password_confirmation => 'newpass123'
  121. }
  122. assert_redirected_to "/login"
  123. assert_equal 'Password was successfully updated.', flash[:notice]
  124. log_user('jsmith', 'newpass123')
  125. assert_equal false, Token.exists?(token.id), "Password recovery token was not deleted"
  126. end
  127. def test_lost_password_expired_token
  128. Token.delete_all
  129. get "/account/lost_password"
  130. assert_response :success
  131. assert_select 'input[name=mail]'
  132. post "/account/lost_password", :params => {
  133. :mail => 'jSmith@somenet.foo'
  134. }
  135. assert_redirected_to "/login"
  136. token = Token.first
  137. assert_equal 'recovery', token.action
  138. assert_equal 'jsmith@somenet.foo', token.user.mail
  139. refute token.expired?
  140. get "/account/lost_password", :params => {
  141. :token => token.value
  142. }
  143. assert_redirected_to '/account/lost_password'
  144. follow_redirect!
  145. assert_response :success
  146. # suppose the user forgets to continue the process and the token expires.
  147. token.update_column :created_on, 1.week.ago
  148. assert token.expired?
  149. assert_select 'input[type=hidden][name=token][value=?]', token.value
  150. assert_select 'input[name=new_password]'
  151. assert_select 'input[name=new_password_confirmation]'
  152. post "/account/lost_password", :params => {
  153. :token => token.value, :new_password => 'newpass123',
  154. :new_password_confirmation => 'newpass123'
  155. }
  156. assert_redirected_to "/account/lost_password"
  157. assert_equal 'This password recovery link has expired, please try again.', flash[:error]
  158. follow_redirect!
  159. assert_response :success
  160. post "/account/lost_password", :params => {
  161. :mail => 'jSmith@somenet.foo'
  162. }
  163. assert_redirected_to "/login"
  164. # should have a new token now
  165. token = Token.last
  166. assert_equal 'recovery', token.action
  167. assert_equal 'jsmith@somenet.foo', token.user.mail
  168. refute token.expired?
  169. end
  170. def test_user_with_must_change_passwd_should_be_forced_to_change_its_password
  171. User.find_by_login('jsmith').update_attribute :must_change_passwd, true
  172. post '/login', :params => {
  173. :username => 'jsmith',
  174. :password => 'jsmith'
  175. }
  176. assert_redirected_to '/my/page'
  177. follow_redirect!
  178. assert_redirected_to '/my/password'
  179. get '/issues'
  180. assert_redirected_to '/my/password'
  181. end
  182. def test_flash_message_should_use_user_language_when_redirecting_user_for_password_change
  183. user = User.find_by_login('jsmith')
  184. user.must_change_passwd = true
  185. user.language = 'it'
  186. user.save!
  187. post '/login', :params => {
  188. :username => 'jsmith',
  189. :password => 'jsmith'
  190. }
  191. assert_redirected_to '/my/page'
  192. follow_redirect!
  193. assert_redirected_to '/my/password'
  194. follow_redirect!
  195. assert_select 'div.error', :text => /richiesto che sia cambiata/
  196. end
  197. def test_user_with_must_change_passwd_should_be_able_to_change_its_password
  198. User.find_by_login('jsmith').update_attribute :must_change_passwd, true
  199. post '/login', :params => {
  200. :username => 'jsmith',
  201. :password => 'jsmith'
  202. }
  203. assert_redirected_to '/my/page'
  204. follow_redirect!
  205. assert_redirected_to '/my/password'
  206. follow_redirect!
  207. assert_response :success
  208. post '/my/password', :params => {
  209. :password => 'jsmith',
  210. :new_password => 'newpassword',
  211. :new_password_confirmation => 'newpassword'
  212. }
  213. assert_redirected_to '/my/account'
  214. follow_redirect!
  215. assert_response :success
  216. assert_equal false, User.find_by_login('jsmith').must_change_passwd?
  217. end
  218. def test_user_with_expired_password_should_be_forced_to_change_its_password
  219. User.find_by_login('jsmith').update_attribute :passwd_changed_on, 14.days.ago
  220. with_settings :password_max_age => 7 do
  221. post '/login', :params => {
  222. :username => 'jsmith',
  223. :password => 'jsmith'
  224. }
  225. assert_redirected_to '/my/page'
  226. follow_redirect!
  227. assert_redirected_to '/my/password'
  228. get '/issues'
  229. assert_redirected_to '/my/password'
  230. end
  231. end
  232. def test_user_with_expired_password_should_be_able_to_change_its_password
  233. User.find_by_login('jsmith').update_attribute :passwd_changed_on, 14.days.ago
  234. with_settings :password_max_age => 7 do
  235. post '/login', :params => {
  236. :username => 'jsmith',
  237. :password => 'jsmith'
  238. }
  239. assert_redirected_to '/my/page'
  240. follow_redirect!
  241. assert_redirected_to '/my/password'
  242. follow_redirect!
  243. assert_response :success
  244. post '/my/password', :params => {
  245. :password => 'jsmith',
  246. :new_password => 'newpassword',
  247. :new_password_confirmation => 'newpassword'
  248. }
  249. assert_redirected_to '/my/account'
  250. follow_redirect!
  251. assert_response :success
  252. assert_equal false, User.find_by_login('jsmith').must_change_passwd?
  253. end
  254. end
  255. def test_register_with_automatic_activation
  256. Setting.self_registration = '3'
  257. get '/account/register'
  258. assert_response :success
  259. post '/account/register', :params => {
  260. :user => {
  261. :login => "newuser", :language => "en",
  262. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  263. :password => "newpass123", :password_confirmation => "newpass123"
  264. }
  265. }
  266. assert_redirected_to '/my/account'
  267. follow_redirect!
  268. assert_response :success
  269. user = User.find_by_login('newuser')
  270. assert_not_nil user
  271. assert user.active?
  272. assert_not_nil user.last_login_on
  273. end
  274. def test_register_with_manual_activation
  275. Setting.self_registration = '2'
  276. post '/account/register', :params => {
  277. :user => {
  278. :login => "newuser", :language => "en",
  279. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  280. :password => "newpass123", :password_confirmation => "newpass123"
  281. }
  282. }
  283. assert_redirected_to '/login'
  284. assert !User.find_by_login('newuser').active?
  285. end
  286. def test_register_with_email_activation
  287. Setting.self_registration = '1'
  288. Token.delete_all
  289. post '/account/register', :params => {
  290. :user => {
  291. :login => "newuser", :language => "en",
  292. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  293. :password => "newpass123", :password_confirmation => "newpass123"
  294. }
  295. }
  296. assert_redirected_to '/login'
  297. assert !User.find_by_login('newuser').active?
  298. token = Token.first
  299. assert_equal 'register', token.action
  300. assert_equal 'newuser@foo.bar', token.user.mail
  301. assert !token.expired?
  302. get '/account/activate', :params => {
  303. :token => token.value
  304. }
  305. assert_redirected_to '/login'
  306. log_user('newuser', 'newpass123')
  307. end
  308. def test_onthefly_registration
  309. # disable registration
  310. Setting.self_registration = '0'
  311. AuthSource.expects(:authenticate).returns(
  312. {:login => 'foo', :firstname => 'Foo', :lastname => 'Smith',
  313. :mail => 'foo@bar.com', :auth_source_id => 66})
  314. post '/login', :params => {
  315. :username => 'foo',
  316. :password => 'bar'
  317. }
  318. assert_redirected_to '/my/page'
  319. user = User.find_by_login('foo')
  320. assert user.is_a?(User)
  321. assert_equal 66, user.auth_source_id
  322. assert user.hashed_password.blank?
  323. end
  324. def test_onthefly_registration_with_invalid_attributes
  325. # disable registration
  326. Setting.self_registration = '0'
  327. AuthSource.expects(:authenticate).returns(
  328. {:login => 'foo', :lastname => 'Smith', :auth_source_id => 66})
  329. post '/login', :params => {
  330. :username => 'foo',
  331. :password => 'bar'
  332. }
  333. assert_response :success
  334. assert_select 'input[name=?][value=""]', 'user[firstname]'
  335. assert_select 'input[name=?][value=Smith]', 'user[lastname]'
  336. assert_select 'input[name=?]', 'user[login]', 0
  337. assert_select 'input[name=?]', 'user[password]', 0
  338. post '/account/register', :params => {
  339. :user => {
  340. :firstname => 'Foo', :lastname => 'Smith', :mail => 'foo@bar.com'
  341. }
  342. }
  343. assert_redirected_to '/my/account'
  344. user = User.find_by_login('foo')
  345. assert user.is_a?(User)
  346. assert_equal 66, user.auth_source_id
  347. assert user.hashed_password.blank?
  348. end
  349. def test_registered_user_should_be_able_to_get_a_new_activation_email
  350. Token.delete_all
  351. with_settings :self_registration => '1', :default_language => 'en' do
  352. # register a new account
  353. assert_difference 'User.count' do
  354. assert_difference 'Token.count' do
  355. post '/account/register', :params => {
  356. :user => {
  357. :login => "newuser", :language => "en",
  358. :firstname => "New", :lastname => "User", :mail => "newuser@foo.bar",
  359. :password => "newpass123", :password_confirmation => "newpass123"
  360. }
  361. }
  362. end
  363. end
  364. user = User.order('id desc').first
  365. assert_equal User::STATUS_REGISTERED, user.status
  366. reset!
  367. # try to use "lost password"
  368. assert_no_difference 'ActionMailer::Base.deliveries.size' do
  369. post '/account/lost_password', :params => {
  370. :mail => 'newuser@foo.bar'
  371. }
  372. end
  373. assert_redirected_to '/account/lost_password'
  374. follow_redirect!
  375. assert_response :success
  376. assert_select 'div.flash', :text => /new activation email/
  377. assert_select 'div.flash a[href="/account/activation_email"]'
  378. # request a new action activation email
  379. assert_difference 'ActionMailer::Base.deliveries.size' do
  380. get '/account/activation_email'
  381. end
  382. assert_redirected_to '/login'
  383. token = Token.order('id desc').first
  384. activation_path = "/account/activate?token=#{token.value}"
  385. assert_include activation_path, mail_body(ActionMailer::Base.deliveries.last)
  386. # activate the account
  387. get activation_path
  388. assert_redirected_to '/login'
  389. post '/login', :params => {
  390. :username => 'newuser',
  391. :password => 'newpass123'
  392. }
  393. assert_redirected_to '/my/page'
  394. end
  395. end
  396. end