##// END OF EJS Templates
Merged r10255 from trunk to 1.4-stable...
Toshi MARUYAMA -
r10079:1974123562e2
parent child
Show More
@@ -1,101 +1,102
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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 fixtures :users
21
22
22 class Base
23 class Base
23 def attributes=(attrs)
24 def attributes=(attrs)
24 attrs.each do |key, value|
25 attrs.each do |key, value|
25 send("#{key}=", value)
26 send("#{key}=", value)
26 end
27 end
27 end
28 end
28 end
29 end
29
30
30 class Person < Base
31 class Person < Base
31 attr_accessor :firstname, :lastname, :login
32 attr_accessor :firstname, :lastname, :login
32 include Redmine::SafeAttributes
33 include Redmine::SafeAttributes
33 safe_attributes :firstname, :lastname
34 safe_attributes :firstname, :lastname
34 safe_attributes :login, :if => lambda {|person, user| user.admin?}
35 safe_attributes :login, :if => lambda {|person, user| user.admin?}
35 end
36 end
36
37
37 class Book < Base
38 class Book < Base
38 attr_accessor :title
39 attr_accessor :title
39 include Redmine::SafeAttributes
40 include Redmine::SafeAttributes
40 safe_attributes :title
41 safe_attributes :title
41 end
42 end
42
43
43 def test_safe_attribute_names
44 def test_safe_attribute_names
44 p = Person.new
45 p = Person.new
45 user = User.anonymous
46 user = User.anonymous
46 assert_equal ['firstname', 'lastname'], p.safe_attribute_names(user)
47 assert_equal ['firstname', 'lastname'], p.safe_attribute_names(user)
47 assert p.safe_attribute?('firstname', user)
48 assert p.safe_attribute?('firstname', user)
48 assert !p.safe_attribute?('login', user)
49 assert !p.safe_attribute?('login', user)
49
50
50 p = Person.new
51 p = Person.new
51 user = User.find(1)
52 user = User.find(1)
52 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(user)
53 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(user)
53 assert p.safe_attribute?('firstname', user)
54 assert p.safe_attribute?('firstname', user)
54 assert p.safe_attribute?('login', user)
55 assert p.safe_attribute?('login', user)
55 end
56 end
56
57
57 def test_safe_attribute_names_without_user
58 def test_safe_attribute_names_without_user
58 p = Person.new
59 p = Person.new
59 User.current = nil
60 User.current = nil
60 assert_equal ['firstname', 'lastname'], p.safe_attribute_names
61 assert_equal ['firstname', 'lastname'], p.safe_attribute_names
61 assert p.safe_attribute?('firstname')
62 assert p.safe_attribute?('firstname')
62 assert !p.safe_attribute?('login')
63 assert !p.safe_attribute?('login')
63
64
64 p = Person.new
65 p = Person.new
65 User.current = User.find(1)
66 User.current = User.find(1)
66 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names
67 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names
67 assert p.safe_attribute?('firstname')
68 assert p.safe_attribute?('firstname')
68 assert p.safe_attribute?('login')
69 assert p.safe_attribute?('login')
69 end
70 end
70
71
71 def test_set_safe_attributes
72 def test_set_safe_attributes
72 p = Person.new
73 p = Person.new
73 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous)
74 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous)
74 assert_equal 'John', p.firstname
75 assert_equal 'John', p.firstname
75 assert_equal 'Smith', p.lastname
76 assert_equal 'Smith', p.lastname
76 assert_nil p.login
77 assert_nil p.login
77
78
78 p = Person.new
79 p = Person.new
79 User.current = User.find(1)
80 User.current = User.find(1)
80 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1))
81 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1))
81 assert_equal 'John', p.firstname
82 assert_equal 'John', p.firstname
82 assert_equal 'Smith', p.lastname
83 assert_equal 'Smith', p.lastname
83 assert_equal 'jsmith', p.login
84 assert_equal 'jsmith', p.login
84 end
85 end
85
86
86 def test_set_safe_attributes_without_user
87 def test_set_safe_attributes_without_user
87 p = Person.new
88 p = Person.new
88 User.current = nil
89 User.current = nil
89 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
90 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
90 assert_equal 'John', p.firstname
91 assert_equal 'John', p.firstname
91 assert_equal 'Smith', p.lastname
92 assert_equal 'Smith', p.lastname
92 assert_nil p.login
93 assert_nil p.login
93
94
94 p = Person.new
95 p = Person.new
95 User.current = User.find(1)
96 User.current = User.find(1)
96 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
97 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
97 assert_equal 'John', p.firstname
98 assert_equal 'John', p.firstname
98 assert_equal 'Smith', p.lastname
99 assert_equal 'Smith', p.lastname
99 assert_equal 'jsmith', p.login
100 assert_equal 'jsmith', p.login
100 end
101 end
101 end
102 end
General Comments 0
You need to be logged in to leave comments. Login now