To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / test / unit / lib / redmine / safe_attributes_test.rb @ 441:cbce1fd3b1b7
History | View | Annotate | Download (2.92 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2010 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 |
|
| 18 |
require File.expand_path('../../../../test_helper', __FILE__) |
| 19 |
|
| 20 |
class Redmine::SafeAttributesTest < ActiveSupport::TestCase |
| 21 |
|
| 22 |
class Base |
| 23 |
def attributes=(attrs) |
| 24 |
attrs.each do |key, value|
|
| 25 |
send("#{key}=", value)
|
| 26 |
end
|
| 27 |
end
|
| 28 |
end
|
| 29 |
|
| 30 |
class Person < Base |
| 31 |
attr_accessor :firstname, :lastname, :login |
| 32 |
include Redmine::SafeAttributes |
| 33 |
safe_attributes :firstname, :lastname |
| 34 |
safe_attributes :login, :if => lambda {|person, user| user.admin?} |
| 35 |
end
|
| 36 |
|
| 37 |
class Book < Base |
| 38 |
attr_accessor :title
|
| 39 |
include Redmine::SafeAttributes |
| 40 |
safe_attributes :title
|
| 41 |
end
|
| 42 |
|
| 43 |
def test_safe_attribute_names |
| 44 |
p = Person.new
|
| 45 |
assert_equal ['firstname', 'lastname'], p.safe_attribute_names(User.anonymous) |
| 46 |
assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(User.find(1)) |
| 47 |
end
|
| 48 |
|
| 49 |
def test_safe_attribute_names_without_user |
| 50 |
p = Person.new
|
| 51 |
User.current = nil |
| 52 |
assert_equal ['firstname', 'lastname'], p.safe_attribute_names |
| 53 |
User.current = User.find(1) |
| 54 |
assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names |
| 55 |
end
|
| 56 |
|
| 57 |
def test_set_safe_attributes |
| 58 |
p = Person.new
|
| 59 |
p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous) |
| 60 |
assert_equal 'John', p.firstname
|
| 61 |
assert_equal 'Smith', p.lastname
|
| 62 |
assert_nil p.login |
| 63 |
|
| 64 |
p = Person.new
|
| 65 |
User.current = User.find(1) |
| 66 |
p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1)) |
| 67 |
assert_equal 'John', p.firstname
|
| 68 |
assert_equal 'Smith', p.lastname
|
| 69 |
assert_equal 'jsmith', p.login
|
| 70 |
end
|
| 71 |
|
| 72 |
def test_set_safe_attributes_without_user |
| 73 |
p = Person.new
|
| 74 |
User.current = nil |
| 75 |
p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
|
| 76 |
assert_equal 'John', p.firstname
|
| 77 |
assert_equal 'Smith', p.lastname
|
| 78 |
assert_nil p.login |
| 79 |
|
| 80 |
p = Person.new
|
| 81 |
User.current = User.find(1) |
| 82 |
p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
|
| 83 |
assert_equal 'John', p.firstname
|
| 84 |
assert_equal 'Smith', p.lastname
|
| 85 |
assert_equal 'jsmith', p.login
|
| 86 |
end
|
| 87 |
end
|