kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.1 KiB
128 lines
4.1 KiB
13 years ago
|
#-- encoding: UTF-8
|
||
14 years ago
|
#-- copyright
|
||
12 years ago
|
# OpenProject is a project management system.
|
||
10 years ago
|
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
|
||
14 years ago
|
#
|
||
14 years ago
|
# This program is free software; you can redistribute it and/or
|
||
12 years ago
|
# modify it under the terms of the GNU General Public License version 3.
|
||
14 years ago
|
#
|
||
11 years ago
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
#
|
||
14 years ago
|
# See doc/COPYRIGHT.rdoc for more details.
|
||
|
#++
|
||
10 years ago
|
require 'legacy_spec_helper'
|
||
14 years ago
|
|
||
11 years ago
|
describe Redmine::SafeAttributes do
|
||
14 years ago
|
class Base
|
||
|
def attributes=(attrs)
|
||
|
attrs.each do |key, value|
|
||
|
send("#{key}=", value)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
14 years ago
|
|
||
14 years ago
|
class Person < Base
|
||
|
attr_accessor :firstname, :lastname, :login
|
||
|
include Redmine::SafeAttributes
|
||
|
safe_attributes :firstname, :lastname
|
||
10 years ago
|
safe_attributes :login, if: lambda { |_person, user| user.admin? }
|
||
14 years ago
|
end
|
||
14 years ago
|
|
||
14 years ago
|
class Book < Base
|
||
14 years ago
|
attr_accessor :title, :isbn
|
||
14 years ago
|
include Redmine::SafeAttributes
|
||
|
safe_attributes :title
|
||
|
end
|
||
14 years ago
|
|
||
14 years ago
|
class PublishedBook < Book
|
||
|
safe_attributes :isbn
|
||
|
end
|
||
|
|
||
11 years ago
|
before do
|
||
10 years ago
|
@admin = User.find_by_login('admin') || FactoryGirl.create(:admin)
|
||
12 years ago
|
@anonymous = User.anonymous || FactoryGirl.create(:anonymous)
|
||
|
end
|
||
|
|
||
11 years ago
|
it 'should safe attribute names' do
|
||
14 years ago
|
p = Person.new
|
||
12 years ago
|
assert_equal ['firstname', 'lastname'], p.safe_attribute_names(@anonymous)
|
||
|
assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names(@admin)
|
||
14 years ago
|
end
|
||
14 years ago
|
|
||
11 years ago
|
it 'should safe attribute names without user' do
|
||
14 years ago
|
p = Person.new
|
||
|
User.current = nil
|
||
|
assert_equal ['firstname', 'lastname'], p.safe_attribute_names
|
||
12 years ago
|
User.current = @admin
|
||
14 years ago
|
assert_equal ['firstname', 'lastname', 'login'], p.safe_attribute_names
|
||
|
end
|
||
14 years ago
|
|
||
11 years ago
|
it 'should set safe attributes' do
|
||
14 years ago
|
p = Person.new
|
||
10 years ago
|
p.send('safe_attributes=', { 'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith' }, @anonymous)
|
||
14 years ago
|
assert_equal 'John', p.firstname
|
||
|
assert_equal 'Smith', p.lastname
|
||
|
assert_nil p.login
|
||
|
|
||
|
p = Person.new
|
||
12 years ago
|
User.current = @admin
|
||
10 years ago
|
p.send('safe_attributes=', { 'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith' }, @admin)
|
||
14 years ago
|
assert_equal 'John', p.firstname
|
||
|
assert_equal 'Smith', p.lastname
|
||
|
assert_equal 'jsmith', p.login
|
||
|
end
|
||
14 years ago
|
|
||
11 years ago
|
it 'should set safe attributes without user' do
|
||
14 years ago
|
p = Person.new
|
||
|
User.current = nil
|
||
10 years ago
|
p.safe_attributes = { 'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith' }
|
||
14 years ago
|
assert_equal 'John', p.firstname
|
||
|
assert_equal 'Smith', p.lastname
|
||
|
assert_nil p.login
|
||
|
|
||
|
p = Person.new
|
||
12 years ago
|
User.current = @admin
|
||
10 years ago
|
p.safe_attributes = { 'firstname' => 'John', 'lastname' => 'Smith', 'login' => 'jsmith' }
|
||
14 years ago
|
assert_equal 'John', p.firstname
|
||
|
assert_equal 'Smith', p.lastname
|
||
|
assert_equal 'jsmith', p.login
|
||
|
end
|
||
14 years ago
|
|
||
11 years ago
|
it 'should with indifferent access' do
|
||
14 years ago
|
p = Person.new
|
||
10 years ago
|
p.safe_attributes = { 'firstname' => 'Jack', lastname: 'Miller' }
|
||
14 years ago
|
assert_equal 'Jack', p.firstname
|
||
|
assert_equal 'Miller', p.lastname
|
||
|
end
|
||
|
|
||
11 years ago
|
it 'should use safe attributes in subclasses' do
|
||
14 years ago
|
b = Book.new
|
||
|
p = PublishedBook.new
|
||
|
|
||
10 years ago
|
b.safe_attributes = { 'title' => 'My awesome Ruby Book', 'isbn' => '1221132343' }
|
||
|
p.safe_attributes = { 'title' => 'The Pickaxe', 'isbn' => '1934356085' }
|
||
14 years ago
|
|
||
|
assert_equal 'My awesome Ruby Book', b.title
|
||
|
assert_nil b.isbn
|
||
|
|
||
|
assert_equal 'The Pickaxe', p.title
|
||
|
assert_equal '1934356085', p.isbn
|
||
|
end
|
||
14 years ago
|
end
|