Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require File.expand_path('../../test_helper', __FILE__) Chris@1296: Chris@1296: class RoleTest < ActiveSupport::TestCase Chris@1296: fixtures :roles, :workflows, :trackers Chris@1296: Chris@1296: def test_sorted_scope Chris@1296: assert_equal Role.all.sort, Role.sorted.all Chris@1296: end Chris@1296: Chris@1296: def test_givable_scope Chris@1296: assert_equal Role.all.reject(&:builtin?).sort, Role.givable.all Chris@1296: end Chris@1296: Chris@1296: def test_builtin_scope Chris@1296: assert_equal Role.all.select(&:builtin?).sort, Role.builtin(true).all.sort Chris@1296: assert_equal Role.all.reject(&:builtin?).sort, Role.builtin(false).all.sort Chris@1296: end Chris@1296: Chris@1296: def test_copy_from Chris@1296: role = Role.find(1) Chris@1296: copy = Role.new.copy_from(role) Chris@1296: Chris@1296: assert_nil copy.id Chris@1296: assert_equal '', copy.name Chris@1296: assert_equal role.permissions, copy.permissions Chris@1296: Chris@1296: copy.name = 'Copy' Chris@1296: assert copy.save Chris@1296: end Chris@1296: Chris@1296: def test_copy_workflows Chris@1296: source = Role.find(1) Chris@1296: assert_equal 90, source.workflow_rules.size Chris@1296: Chris@1296: target = Role.new(:name => 'Target') Chris@1296: assert target.save Chris@1296: target.workflow_rules.copy(source) Chris@1296: target.reload Chris@1296: assert_equal 90, target.workflow_rules.size Chris@1296: end Chris@1296: Chris@1296: def test_permissions_should_be_unserialized_with_its_coder Chris@1296: Role::PermissionsAttributeCoder.expects(:load).once Chris@1296: Role.find(1).permissions Chris@1296: end Chris@1296: Chris@1296: def test_add_permission Chris@1296: role = Role.find(1) Chris@1296: size = role.permissions.size Chris@1296: role.add_permission!("apermission", "anotherpermission") Chris@1296: role.reload Chris@1296: assert role.permissions.include?(:anotherpermission) Chris@1296: assert_equal size + 2, role.permissions.size Chris@1296: end Chris@1296: Chris@1296: def test_remove_permission Chris@1296: role = Role.find(1) Chris@1296: size = role.permissions.size Chris@1296: perm = role.permissions[0..1] Chris@1296: role.remove_permission!(*perm) Chris@1296: role.reload Chris@1296: assert ! role.permissions.include?(perm[0]) Chris@1296: assert_equal size - 2, role.permissions.size Chris@1296: end Chris@1296: Chris@1296: def test_name Chris@1296: I18n.locale = 'fr' Chris@1296: assert_equal 'Manager', Role.find(1).name Chris@1296: assert_equal 'Anonyme', Role.anonymous.name Chris@1296: assert_equal 'Non membre', Role.non_member.name Chris@1296: end Chris@1296: Chris@1296: def test_find_all_givable Chris@1296: assert_equal Role.all.reject(&:builtin?).sort, Role.find_all_givable Chris@1296: end Chris@1296: Chris@1296: context "#anonymous" do Chris@1296: should "return the anonymous role" do Chris@1296: role = Role.anonymous Chris@1296: assert role.builtin? Chris@1296: assert_equal Role::BUILTIN_ANONYMOUS, role.builtin Chris@1296: end Chris@1296: Chris@1296: context "with a missing anonymous role" do Chris@1296: setup do Chris@1296: Role.delete_all("builtin = #{Role::BUILTIN_ANONYMOUS}") Chris@1296: end Chris@1296: Chris@1296: should "create a new anonymous role" do Chris@1296: assert_difference('Role.count') do Chris@1296: Role.anonymous Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: should "return the anonymous role" do Chris@1296: role = Role.anonymous Chris@1296: assert role.builtin? Chris@1296: assert_equal Role::BUILTIN_ANONYMOUS, role.builtin Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: context "#non_member" do Chris@1296: should "return the non-member role" do Chris@1296: role = Role.non_member Chris@1296: assert role.builtin? Chris@1296: assert_equal Role::BUILTIN_NON_MEMBER, role.builtin Chris@1296: end Chris@1296: Chris@1296: context "with a missing non-member role" do Chris@1296: setup do Chris@1296: Role.delete_all("builtin = #{Role::BUILTIN_NON_MEMBER}") Chris@1296: end Chris@1296: Chris@1296: should "create a new non-member role" do Chris@1296: assert_difference('Role.count') do Chris@1296: Role.non_member Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: should "return the non-member role" do Chris@1296: role = Role.non_member Chris@1296: assert role.builtin? Chris@1296: assert_equal Role::BUILTIN_NON_MEMBER, role.builtin Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end