annotate test/unit/role_test.rb @ 1621:3a510bf6a9bc

Merge from live branch
author Chris Cannam
date Fri, 13 Jul 2018 10:44:33 +0100
parents e248c7af89ec
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class RoleTest < ActiveSupport::TestCase
Chris@1115 21 fixtures :roles, :workflows, :trackers
Chris@1115 22
Chris@1115 23 def test_sorted_scope
Chris@1115 24 assert_equal Role.all.sort, Role.sorted.all
Chris@1115 25 end
Chris@1115 26
Chris@1115 27 def test_givable_scope
Chris@1115 28 assert_equal Role.all.reject(&:builtin?).sort, Role.givable.all
Chris@1115 29 end
Chris@1115 30
Chris@1115 31 def test_builtin_scope
Chris@1115 32 assert_equal Role.all.select(&:builtin?).sort, Role.builtin(true).all.sort
Chris@1115 33 assert_equal Role.all.reject(&:builtin?).sort, Role.builtin(false).all.sort
Chris@1115 34 end
Chris@1115 35
Chris@1115 36 def test_copy_from
Chris@1115 37 role = Role.find(1)
Chris@1115 38 copy = Role.new.copy_from(role)
Chris@1115 39
Chris@1115 40 assert_nil copy.id
Chris@1115 41 assert_equal '', copy.name
Chris@1115 42 assert_equal role.permissions, copy.permissions
Chris@1115 43
Chris@1115 44 copy.name = 'Copy'
Chris@1115 45 assert copy.save
Chris@1115 46 end
Chris@0 47
Chris@0 48 def test_copy_workflows
Chris@0 49 source = Role.find(1)
Chris@1115 50 assert_equal 90, source.workflow_rules.size
Chris@909 51
Chris@0 52 target = Role.new(:name => 'Target')
Chris@0 53 assert target.save
Chris@1115 54 target.workflow_rules.copy(source)
Chris@0 55 target.reload
Chris@1115 56 assert_equal 90, target.workflow_rules.size
Chris@1115 57 end
Chris@1115 58
Chris@1115 59 def test_permissions_should_be_unserialized_with_its_coder
Chris@1115 60 Role::PermissionsAttributeCoder.expects(:load).once
Chris@1115 61 Role.find(1).permissions
Chris@0 62 end
Chris@0 63
Chris@0 64 def test_add_permission
Chris@0 65 role = Role.find(1)
Chris@0 66 size = role.permissions.size
Chris@0 67 role.add_permission!("apermission", "anotherpermission")
Chris@0 68 role.reload
Chris@0 69 assert role.permissions.include?(:anotherpermission)
Chris@0 70 assert_equal size + 2, role.permissions.size
Chris@0 71 end
Chris@0 72
Chris@0 73 def test_remove_permission
Chris@0 74 role = Role.find(1)
Chris@0 75 size = role.permissions.size
Chris@0 76 perm = role.permissions[0..1]
Chris@0 77 role.remove_permission!(*perm)
Chris@0 78 role.reload
Chris@0 79 assert ! role.permissions.include?(perm[0])
Chris@0 80 assert_equal size - 2, role.permissions.size
Chris@0 81 end
Chris@909 82
Chris@441 83 def test_name
Chris@441 84 I18n.locale = 'fr'
Chris@441 85 assert_equal 'Manager', Role.find(1).name
Chris@441 86 assert_equal 'Anonyme', Role.anonymous.name
Chris@441 87 assert_equal 'Non membre', Role.non_member.name
Chris@441 88 end
Chris@0 89
Chris@1115 90 def test_find_all_givable
Chris@1115 91 assert_equal Role.all.reject(&:builtin?).sort, Role.find_all_givable
Chris@1115 92 end
Chris@1115 93
Chris@1464 94 def test_anonymous_should_return_the_anonymous_role
Chris@1464 95 assert_no_difference('Role.count') do
Chris@0 96 role = Role.anonymous
Chris@0 97 assert role.builtin?
Chris@0 98 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
Chris@0 99 end
Chris@1464 100 end
Chris@0 101
Chris@1464 102 def test_anonymous_with_a_missing_anonymous_role_should_return_the_anonymous_role
Chris@1464 103 Role.where(:builtin => Role::BUILTIN_ANONYMOUS).delete_all
Chris@0 104
Chris@1464 105 assert_difference('Role.count') do
Chris@1464 106 role = Role.anonymous
Chris@1464 107 assert role.builtin?
Chris@1464 108 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
Chris@0 109 end
Chris@0 110 end
Chris@0 111
Chris@1464 112 def test_non_member_should_return_the_non_member_role
Chris@1464 113 assert_no_difference('Role.count') do
Chris@0 114 role = Role.non_member
Chris@0 115 assert role.builtin?
Chris@0 116 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
Chris@0 117 end
Chris@1464 118 end
Chris@0 119
Chris@1464 120 def test_non_member_with_a_missing_non_member_role_should_return_the_non_member_role
Chris@1464 121 Role.where(:builtin => Role::BUILTIN_NON_MEMBER).delete_all
Chris@0 122
Chris@1464 123 assert_difference('Role.count') do
Chris@1464 124 role = Role.non_member
Chris@1464 125 assert role.builtin?
Chris@1464 126 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
Chris@0 127 end
Chris@0 128 end
Chris@0 129 end