annotate test/unit/member_test.rb @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 433d4f72a19b
children 622f24f53b42
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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 MemberTest < ActiveSupport::TestCase
Chris@909 21 fixtures :projects, :trackers, :issue_statuses, :issues,
Chris@909 22 :enumerations, :users, :issue_categories,
Chris@909 23 :projects_trackers,
Chris@909 24 :roles,
Chris@909 25 :member_roles,
Chris@909 26 :members,
Chris@909 27 :enabled_modules,
Chris@909 28 :workflows,
Chris@909 29 :groups_users,
Chris@909 30 :watchers,
Chris@909 31 :journals, :journal_details,
Chris@909 32 :messages,
Chris@909 33 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
Chris@909 34 :boards
Chris@0 35
Chris@1115 36 include Redmine::I18n
Chris@1115 37
Chris@0 38 def setup
Chris@0 39 @jsmith = Member.find(1)
Chris@0 40 end
Chris@909 41
Chris@0 42 def test_create
Chris@0 43 member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2])
Chris@0 44 assert member.save
Chris@0 45 member.reload
Chris@909 46
Chris@0 47 assert_equal 2, member.roles.size
Chris@0 48 assert_equal Role.find(1), member.roles.sort.first
Chris@0 49 end
Chris@0 50
Chris@909 51 def test_update
Chris@0 52 assert_equal "eCookbook", @jsmith.project.name
Chris@0 53 assert_equal "Manager", @jsmith.roles.first.name
Chris@0 54 assert_equal "jsmith", @jsmith.user.login
Chris@909 55
Chris@0 56 @jsmith.mail_notification = !@jsmith.mail_notification
Chris@0 57 assert @jsmith.save
Chris@0 58 end
Chris@0 59
Chris@0 60 def test_update_roles
Chris@0 61 assert_equal 1, @jsmith.roles.size
Chris@0 62 @jsmith.role_ids = [1, 2]
Chris@0 63 assert @jsmith.save
Chris@0 64 assert_equal 2, @jsmith.reload.roles.size
Chris@0 65 end
Chris@909 66
Chris@0 67 def test_validate
Chris@0 68 member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [2])
Chris@0 69 # same use can't have more than one membership for a project
Chris@0 70 assert !member.save
Chris@909 71
Chris@0 72 # must have one role at least
Chris@1115 73 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
Chris@1115 74 user.login = "test_validate"
Chris@1115 75 user.password, user.password_confirmation = "password", "password"
Chris@1115 76 assert user.save
Chris@1115 77
Chris@1115 78 set_language_if_valid 'fr'
Chris@1115 79 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [])
Chris@1115 80 assert !member.save
Chris@1115 81 assert_include I18n.translate('activerecord.errors.messages.empty'), member.errors[:role]
Chris@1115 82 str = "R\xc3\xb4le doit \xc3\xaatre renseign\xc3\xa9(e)"
Chris@1115 83 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
Chris@1115 84 assert_equal str, [member.errors.full_messages].flatten.join
Chris@1115 85 end
Chris@1115 86
Chris@1115 87 def test_validate_member_role
Chris@1115 88 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
Chris@1115 89 user.login = "test_validate_member_role"
Chris@1115 90 user.password, user.password_confirmation = "password", "password"
Chris@1115 91 assert user.save
Chris@1115 92 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [5])
Chris@0 93 assert !member.save
Chris@0 94 end
Chris@909 95
Chris@0 96 def test_destroy
Chris@1115 97 category1 = IssueCategory.find(1)
Chris@1115 98 assert_equal @jsmith.user.id, category1.assigned_to_id
Chris@0 99 assert_difference 'Member.count', -1 do
Chris@0 100 assert_difference 'MemberRole.count', -1 do
Chris@0 101 @jsmith.destroy
Chris@0 102 end
Chris@0 103 end
Chris@0 104 assert_raise(ActiveRecord::RecordNotFound) { Member.find(@jsmith.id) }
Chris@1115 105 category1.reload
Chris@1115 106 assert_nil category1.assigned_to_id
Chris@0 107 end
Chris@909 108
Chris@929 109 def test_sort_without_roles
Chris@929 110 a = Member.new(:roles => [Role.first])
Chris@929 111 b = Member.new
Chris@929 112
Chris@929 113 assert_equal -1, a <=> b
Chris@929 114 assert_equal 1, b <=> a
Chris@929 115 end
Chris@929 116
Chris@929 117 def test_sort_without_principal
Chris@929 118 role = Role.first
Chris@929 119 a = Member.new(:roles => [role], :principal => User.first)
Chris@929 120 b = Member.new(:roles => [role])
Chris@929 121
Chris@929 122 assert_equal -1, a <=> b
Chris@929 123 assert_equal 1, b <=> a
Chris@929 124 end
Chris@929 125
Chris@0 126 context "removing permissions" do
Chris@0 127 setup do
Chris@0 128 Watcher.delete_all("user_id = 9")
Chris@0 129 user = User.find(9)
Chris@0 130 # public
Chris@0 131 Watcher.create!(:watchable => Issue.find(1), :user => user)
Chris@0 132 # private
Chris@0 133 Watcher.create!(:watchable => Issue.find(4), :user => user)
Chris@0 134 Watcher.create!(:watchable => Message.find(7), :user => user)
Chris@0 135 Watcher.create!(:watchable => Wiki.find(2), :user => user)
Chris@0 136 Watcher.create!(:watchable => WikiPage.find(3), :user => user)
Chris@0 137 end
Chris@909 138
Chris@0 139 context "of user" do
Chris@0 140 setup do
Chris@0 141 @member = Member.create!(:project => Project.find(2), :principal => User.find(9), :role_ids => [1, 2])
Chris@0 142 end
Chris@909 143
Chris@0 144 context "by deleting membership" do
Chris@0 145 should "prune watchers" do
Chris@0 146 assert_difference 'Watcher.count', -4 do
Chris@0 147 @member.destroy
Chris@0 148 end
Chris@0 149 end
Chris@0 150 end
Chris@909 151
Chris@0 152 context "by updating roles" do
Chris@0 153 should "prune watchers" do
Chris@0 154 Role.find(2).remove_permission! :view_wiki_pages
Chris@0 155 member = Member.first(:order => 'id desc')
Chris@0 156 assert_difference 'Watcher.count', -2 do
Chris@0 157 member.role_ids = [2]
Chris@0 158 member.save
Chris@0 159 end
Chris@0 160 assert !Message.find(7).watched_by?(@user)
Chris@0 161 end
Chris@0 162 end
Chris@0 163 end
Chris@909 164
Chris@0 165 context "of group" do
Chris@0 166 setup do
Chris@0 167 group = Group.find(10)
Chris@0 168 @member = Member.create!(:project => Project.find(2), :principal => group, :role_ids => [1, 2])
Chris@0 169 group.users << User.find(9)
Chris@0 170 end
Chris@0 171
Chris@0 172 context "by deleting membership" do
Chris@0 173 should "prune watchers" do
Chris@0 174 assert_difference 'Watcher.count', -4 do
Chris@0 175 @member.destroy
Chris@0 176 end
Chris@0 177 end
Chris@909 178 end
Chris@0 179
Chris@0 180 context "by updating roles" do
Chris@0 181 should "prune watchers" do
Chris@0 182 Role.find(2).remove_permission! :view_wiki_pages
Chris@0 183 assert_difference 'Watcher.count', -2 do
Chris@0 184 @member.role_ids = [2]
Chris@0 185 @member.save
Chris@0 186 end
Chris@0 187 end
Chris@0 188 end
Chris@0 189 end
Chris@0 190 end
Chris@0 191 end