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.

safe_attributes_test.rb 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 Redmine::SafeAttributesTest < ActiveSupport::TestCase
  20. fixtures :users
  21. class Base
  22. def attributes=(attrs)
  23. attrs.each do |key, value|
  24. send("#{key}=", value)
  25. end
  26. end
  27. end
  28. class Person < Base
  29. attr_accessor :firstname, :lastname, :login
  30. include Redmine::SafeAttributes
  31. safe_attributes :firstname, :lastname
  32. safe_attributes :login, :if => lambda {|person, user| user.admin?}
  33. end
  34. class Book < Base
  35. attr_accessor :title
  36. include Redmine::SafeAttributes
  37. safe_attributes :title
  38. end
  39. def test_safe_attribute_names
  40. p = Person.new
  41. user = User.anonymous
  42. assert_equal ['firstname', 'lastname'], p.safe_attribute_names(user)
  43. assert p.safe_attribute?('firstname', user)
  44. assert !p.safe_attribute?('login', user)
  45. p = Person.new
  46. user = User.find(1)
  47. assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(user)
  48. assert p.safe_attribute?('firstname', user)
  49. assert p.safe_attribute?('login', user)
  50. end
  51. def test_safe_attribute_names_without_user
  52. p = Person.new
  53. User.current = nil
  54. assert_equal ['firstname', 'lastname'], p.safe_attribute_names
  55. assert p.safe_attribute?('firstname')
  56. assert !p.safe_attribute?('login')
  57. p = Person.new
  58. User.current = User.find(1)
  59. assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names
  60. assert p.safe_attribute?('firstname')
  61. assert p.safe_attribute?('login')
  62. end
  63. def test_set_safe_attributes
  64. p = Person.new
  65. p.send(:safe_attributes=, {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous)
  66. assert_equal 'John', p.firstname
  67. assert_equal 'Smith', p.lastname
  68. assert_nil p.login
  69. p = Person.new
  70. User.current = User.find(1)
  71. p.send(:safe_attributes=, {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1))
  72. assert_equal 'John', p.firstname
  73. assert_equal 'Smith', p.lastname
  74. assert_equal 'jsmith', p.login
  75. end
  76. def test_set_safe_attributes_without_user
  77. p = Person.new
  78. User.current = nil
  79. p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
  80. assert_equal 'John', p.firstname
  81. assert_equal 'Smith', p.lastname
  82. assert_nil p.login
  83. p = Person.new
  84. User.current = User.find(1)
  85. p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
  86. assert_equal 'John', p.firstname
  87. assert_equal 'Smith', p.lastname
  88. assert_equal 'jsmith', p.login
  89. end
  90. end