comparison test/unit/lib/redmine/safe_attributes_test.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 8661b858af72
children 433d4f72a19b
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2010 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
8 # 8 #
9 # This program is distributed in the hope that it will be useful, 9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details. 12 # GNU General Public License for more details.
13 # 13 #
14 # You should have received a copy of the GNU General Public License 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 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. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 require File.expand_path('../../../../test_helper', __FILE__) 18 require File.expand_path('../../../../test_helper', __FILE__)
19 19
20 class Redmine::SafeAttributesTest < ActiveSupport::TestCase 20 class Redmine::SafeAttributesTest < ActiveSupport::TestCase
21 21
22 class Base 22 class Base
23 def attributes=(attrs) 23 def attributes=(attrs)
24 attrs.each do |key, value| 24 attrs.each do |key, value|
25 send("#{key}=", value) 25 send("#{key}=", value)
26 end 26 end
27 end 27 end
28 end 28 end
29 29
30 class Person < Base 30 class Person < Base
31 attr_accessor :firstname, :lastname, :login 31 attr_accessor :firstname, :lastname, :login
32 include Redmine::SafeAttributes 32 include Redmine::SafeAttributes
33 safe_attributes :firstname, :lastname 33 safe_attributes :firstname, :lastname
34 safe_attributes :login, :if => lambda {|person, user| user.admin?} 34 safe_attributes :login, :if => lambda {|person, user| user.admin?}
35 end 35 end
36 36
37 class Book < Base 37 class Book < Base
38 attr_accessor :title 38 attr_accessor :title
39 include Redmine::SafeAttributes 39 include Redmine::SafeAttributes
40 safe_attributes :title 40 safe_attributes :title
41 end 41 end
42 42
43 def test_safe_attribute_names 43 def test_safe_attribute_names
44 p = Person.new 44 p = Person.new
45 assert_equal ['firstname', 'lastname'], p.safe_attribute_names(User.anonymous) 45 assert_equal ['firstname', 'lastname'], p.safe_attribute_names(User.anonymous)
46 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(User.find(1)) 46 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(User.find(1))
47 end 47 end
48 48
49 def test_safe_attribute_names_without_user 49 def test_safe_attribute_names_without_user
50 p = Person.new 50 p = Person.new
51 User.current = nil 51 User.current = nil
52 assert_equal ['firstname', 'lastname'], p.safe_attribute_names 52 assert_equal ['firstname', 'lastname'], p.safe_attribute_names
53 User.current = User.find(1) 53 User.current = User.find(1)
54 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names 54 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names
55 end 55 end
56 56
57 def test_set_safe_attributes 57 def test_set_safe_attributes
58 p = Person.new 58 p = Person.new
59 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous) 59 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous)
60 assert_equal 'John', p.firstname 60 assert_equal 'John', p.firstname
61 assert_equal 'Smith', p.lastname 61 assert_equal 'Smith', p.lastname
66 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1)) 66 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1))
67 assert_equal 'John', p.firstname 67 assert_equal 'John', p.firstname
68 assert_equal 'Smith', p.lastname 68 assert_equal 'Smith', p.lastname
69 assert_equal 'jsmith', p.login 69 assert_equal 'jsmith', p.login
70 end 70 end
71 71
72 def test_set_safe_attributes_without_user 72 def test_set_safe_attributes_without_user
73 p = Person.new 73 p = Person.new
74 User.current = nil 74 User.current = nil
75 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'} 75 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
76 assert_equal 'John', p.firstname 76 assert_equal 'John', p.firstname