Chris@117
|
1 # Redmine - project management software
|
Chris@117
|
2 # Copyright (C) 2006-2010 Jean-Philippe Lang
|
Chris@117
|
3 #
|
Chris@117
|
4 # This program is free software; you can redistribute it and/or
|
Chris@117
|
5 # modify it under the terms of the GNU General Public License
|
Chris@117
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@117
|
7 # of the License, or (at your option) any later version.
|
Chris@117
|
8 #
|
Chris@117
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@117
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@117
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@117
|
12 # GNU General Public License for more details.
|
Chris@117
|
13 #
|
Chris@117
|
14 # You should have received a copy of the GNU General Public License
|
Chris@117
|
15 # along with this program; if not, write to the Free Software
|
Chris@117
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@117
|
17
|
Chris@117
|
18 require File.expand_path('../../../../test_helper', __FILE__)
|
Chris@117
|
19
|
Chris@117
|
20 class Redmine::SafeAttributesTest < ActiveSupport::TestCase
|
Chris@117
|
21
|
Chris@117
|
22 class Base
|
Chris@117
|
23 def attributes=(attrs)
|
Chris@117
|
24 attrs.each do |key, value|
|
Chris@117
|
25 send("#{key}=", value)
|
Chris@117
|
26 end
|
Chris@117
|
27 end
|
Chris@117
|
28 end
|
Chris@117
|
29
|
Chris@117
|
30 class Person < Base
|
Chris@117
|
31 attr_accessor :firstname, :lastname, :login
|
Chris@117
|
32 include Redmine::SafeAttributes
|
Chris@117
|
33 safe_attributes :firstname, :lastname
|
Chris@117
|
34 safe_attributes :login, :if => lambda {|person, user| user.admin?}
|
Chris@117
|
35 end
|
Chris@117
|
36
|
Chris@117
|
37 class Book < Base
|
Chris@117
|
38 attr_accessor :title
|
Chris@117
|
39 include Redmine::SafeAttributes
|
Chris@117
|
40 safe_attributes :title
|
Chris@117
|
41 end
|
Chris@117
|
42
|
Chris@117
|
43 def test_safe_attribute_names
|
Chris@117
|
44 p = Person.new
|
Chris@117
|
45 assert_equal ['firstname', 'lastname'], p.safe_attribute_names(User.anonymous)
|
Chris@117
|
46 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(User.find(1))
|
Chris@117
|
47 end
|
Chris@117
|
48
|
Chris@117
|
49 def test_safe_attribute_names_without_user
|
Chris@117
|
50 p = Person.new
|
Chris@117
|
51 User.current = nil
|
Chris@117
|
52 assert_equal ['firstname', 'lastname'], p.safe_attribute_names
|
Chris@117
|
53 User.current = User.find(1)
|
Chris@117
|
54 assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names
|
Chris@117
|
55 end
|
Chris@117
|
56
|
Chris@117
|
57 def test_set_safe_attributes
|
Chris@117
|
58 p = Person.new
|
Chris@117
|
59 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous)
|
Chris@117
|
60 assert_equal 'John', p.firstname
|
Chris@117
|
61 assert_equal 'Smith', p.lastname
|
Chris@117
|
62 assert_nil p.login
|
Chris@117
|
63
|
Chris@117
|
64 p = Person.new
|
Chris@117
|
65 User.current = User.find(1)
|
Chris@117
|
66 p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1))
|
Chris@117
|
67 assert_equal 'John', p.firstname
|
Chris@117
|
68 assert_equal 'Smith', p.lastname
|
Chris@117
|
69 assert_equal 'jsmith', p.login
|
Chris@117
|
70 end
|
Chris@117
|
71
|
Chris@117
|
72 def test_set_safe_attributes_without_user
|
Chris@117
|
73 p = Person.new
|
Chris@117
|
74 User.current = nil
|
Chris@117
|
75 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
|
Chris@117
|
76 assert_equal 'John', p.firstname
|
Chris@117
|
77 assert_equal 'Smith', p.lastname
|
Chris@117
|
78 assert_nil p.login
|
Chris@117
|
79
|
Chris@117
|
80 p = Person.new
|
Chris@117
|
81 User.current = User.find(1)
|
Chris@117
|
82 p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
|
Chris@117
|
83 assert_equal 'John', p.firstname
|
Chris@117
|
84 assert_equal 'Smith', p.lastname
|
Chris@117
|
85 assert_equal 'jsmith', p.login
|
Chris@117
|
86 end
|
Chris@117
|
87 end
|