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