annotate .svn/pristine/e2/e2217f43c0e991a1cb8790cb4bc4f4cf7be6b81c.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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