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