To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 22 / 22ddddc8c878fc3e93d3136a28528f9e4f69f7e0.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (3.33 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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
  fixtures :users
22

    
23
  class Base
24
    def attributes=(attrs)
25
      attrs.each do |key, value|
26
        send("#{key}=", value)
27
      end
28
    end
29
  end
30

    
31
  class Person < Base
32
    attr_accessor :firstname, :lastname, :login
33
    include Redmine::SafeAttributes
34
    safe_attributes :firstname, :lastname
35
    safe_attributes :login, :if => lambda {|person, user| user.admin?}
36
  end
37

    
38
  class Book < Base
39
    attr_accessor :title
40
    include Redmine::SafeAttributes
41
    safe_attributes :title
42
  end
43

    
44
  def test_safe_attribute_names
45
    p = Person.new
46
    user = User.anonymous
47
    assert_equal ['firstname', 'lastname'], p.safe_attribute_names(user)
48
    assert p.safe_attribute?('firstname', user)
49
    assert !p.safe_attribute?('login', user)
50

    
51
    p = Person.new
52
    user = User.find(1)
53
    assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(user)
54
    assert p.safe_attribute?('firstname', user)
55
    assert p.safe_attribute?('login', user)
56
  end
57

    
58
  def test_safe_attribute_names_without_user
59
    p = Person.new
60
    User.current = nil
61
    assert_equal ['firstname', 'lastname'], p.safe_attribute_names
62
    assert p.safe_attribute?('firstname')
63
    assert !p.safe_attribute?('login')
64

    
65
    p = Person.new
66
    User.current = User.find(1)
67
    assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names
68
    assert p.safe_attribute?('firstname')
69
    assert p.safe_attribute?('login')
70
  end
71

    
72
  def test_set_safe_attributes
73
    p = Person.new
74
    p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.anonymous)
75
    assert_equal 'John', p.firstname
76
    assert_equal 'Smith', p.lastname
77
    assert_nil p.login
78

    
79
    p = Person.new
80
    User.current = User.find(1)
81
    p.send('safe_attributes=', {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}, User.find(1))
82
    assert_equal 'John', p.firstname
83
    assert_equal 'Smith', p.lastname
84
    assert_equal 'jsmith', p.login
85
  end
86

    
87
  def test_set_safe_attributes_without_user
88
    p = Person.new
89
    User.current = nil
90
    p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
91
    assert_equal 'John', p.firstname
92
    assert_equal 'Smith', p.lastname
93
    assert_nil p.login
94

    
95
    p = Person.new
96
    User.current = User.find(1)
97
    p.safe_attributes = {'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith'}
98
    assert_equal 'John', p.firstname
99
    assert_equal 'Smith', p.lastname
100
    assert_equal 'jsmith', p.login
101
  end
102
end