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.

token_test.rb 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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 TokenTest < ActiveSupport::TestCase
  19. fixtures :tokens
  20. def test_create
  21. token = Token.new
  22. token.save
  23. assert_equal 40, token.value.length
  24. assert !token.expired?
  25. end
  26. def test_create_should_remove_existing_tokens
  27. user = User.find(1)
  28. t1 = Token.create(:user => user, :action => 'autologin')
  29. t2 = Token.create(:user => user, :action => 'autologin')
  30. assert_not_equal t1.value, t2.value
  31. assert !Token.exists?(t1.id)
  32. assert Token.exists?(t2.id)
  33. end
  34. def test_destroy_expired_should_not_destroy_feeds_and_api_tokens
  35. Token.delete_all
  36. Token.create!(:user_id => 1, :action => 'api', :created_on => 7.days.ago)
  37. Token.create!(:user_id => 1, :action => 'feeds', :created_on => 7.days.ago)
  38. assert_no_difference 'Token.count' do
  39. assert_equal 0, Token.destroy_expired
  40. end
  41. end
  42. def test_destroy_expired_should_destroy_expired_tokens
  43. Token.delete_all
  44. Token.create!(:user_id => 1, :action => 'autologin', :created_on => 7.days.ago)
  45. Token.create!(:user_id => 2, :action => 'autologin', :created_on => 3.days.ago)
  46. Token.create!(:user_id => 3, :action => 'autologin', :created_on => 1.hour.ago)
  47. assert_difference 'Token.count', -2 do
  48. assert_equal 2, Token.destroy_expired
  49. end
  50. end
  51. def test_find_active_user_should_return_user
  52. token = Token.create!(:user_id => 1, :action => 'api')
  53. assert_equal User.find(1), Token.find_active_user('api', token.value)
  54. end
  55. def test_find_active_user_should_return_nil_for_locked_user
  56. token = Token.create!(:user_id => 1, :action => 'api')
  57. User.find(1).lock!
  58. assert_nil Token.find_active_user('api', token.value)
  59. end
  60. def test_find_user_should_return_user
  61. token = Token.create!(:user_id => 1, :action => 'api')
  62. assert_equal User.find(1), Token.find_user('api', token.value)
  63. end
  64. def test_find_user_should_return_locked_user
  65. token = Token.create!(:user_id => 1, :action => 'api')
  66. User.find(1).lock!
  67. assert_equal User.find(1), Token.find_user('api', token.value)
  68. end
  69. def test_find_token_should_return_the_token
  70. token = Token.create!(:user_id => 1, :action => 'api')
  71. assert_equal token, Token.find_token('api', token.value)
  72. end
  73. def test_find_token_should_return_the_token_with_validity
  74. token = Token.create!(:user_id => 1, :action => 'api', :created_on => 1.hour.ago)
  75. assert_equal token, Token.find_token('api', token.value, 1)
  76. end
  77. def test_find_token_should_return_nil_with_wrong_action
  78. token = Token.create!(:user_id => 1, :action => 'feeds')
  79. assert_nil Token.find_token('api', token.value)
  80. end
  81. def test_find_token_should_return_nil_with_wrong_action
  82. token = Token.create!(:user_id => 1, :action => 'feeds')
  83. assert_nil Token.find_token('api', Token.generate_token_value)
  84. end
  85. def test_find_token_should_return_nil_without_user
  86. token = Token.create!(:user_id => 999, :action => 'api')
  87. assert_nil Token.find_token('api', token.value)
  88. end
  89. def test_find_token_should_return_nil_with_validity_expired
  90. token = Token.create!(:user_id => 999, :action => 'api', :created_on => 2.days.ago)
  91. assert_nil Token.find_token('api', token.value, 1)
  92. end
  93. end