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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. end