To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / test / unit / member_test.rb @ 442:753f1380d6bc

History | View | Annotate | Download (4.08 KB)

1
# redMine - project management software
2
# Copyright (C) 2006  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
require File.expand_path('../../test_helper', __FILE__)
19

    
20
class MemberTest < ActiveSupport::TestCase
21
  fixtures :all
22

    
23
  def setup
24
    @jsmith = Member.find(1)
25
  end
26
  
27
  def test_create
28
    member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2])
29
    assert member.save
30
    member.reload
31
    
32
    assert_equal 2, member.roles.size
33
    assert_equal Role.find(1), member.roles.sort.first
34
  end
35

    
36
  def test_update    
37
    assert_equal "eCookbook", @jsmith.project.name
38
    assert_equal "Manager", @jsmith.roles.first.name
39
    assert_equal "jsmith", @jsmith.user.login
40
    
41
    @jsmith.mail_notification = !@jsmith.mail_notification
42
    assert @jsmith.save
43
  end
44

    
45
  def test_update_roles
46
    assert_equal 1, @jsmith.roles.size
47
    @jsmith.role_ids = [1, 2]
48
    assert @jsmith.save
49
    assert_equal 2, @jsmith.reload.roles.size
50
  end
51
  
52
  def test_validate
53
    member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [2])
54
    # same use can't have more than one membership for a project
55
    assert !member.save
56
    
57
    member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [])
58
    # must have one role at least
59
    assert !member.save
60
  end
61
  
62
  def test_destroy
63
    assert_difference 'Member.count', -1 do
64
      assert_difference 'MemberRole.count', -1 do
65
        @jsmith.destroy
66
      end
67
    end
68
    
69
    assert_raise(ActiveRecord::RecordNotFound) { Member.find(@jsmith.id) }
70
  end
71
  
72
  context "removing permissions" do
73
    setup do
74
      Watcher.delete_all("user_id = 9")
75
      user = User.find(9)
76
      # public
77
      Watcher.create!(:watchable => Issue.find(1), :user => user)
78
      # private
79
      Watcher.create!(:watchable => Issue.find(4), :user => user)
80
      Watcher.create!(:watchable => Message.find(7), :user => user)
81
      Watcher.create!(:watchable => Wiki.find(2), :user => user)
82
      Watcher.create!(:watchable => WikiPage.find(3), :user => user)
83
    end
84
    
85
    context "of user" do
86
      setup do
87
        @member = Member.create!(:project => Project.find(2), :principal => User.find(9), :role_ids => [1, 2])
88
      end
89
      
90
      context "by deleting membership" do
91
        should "prune watchers" do
92
          assert_difference 'Watcher.count', -4 do
93
            @member.destroy
94
          end
95
        end
96
      end
97
      
98
      context "by updating roles" do
99
        should "prune watchers" do
100
          Role.find(2).remove_permission! :view_wiki_pages
101
          member = Member.first(:order => 'id desc')
102
          assert_difference 'Watcher.count', -2 do
103
            member.role_ids = [2]
104
            member.save
105
          end
106
          assert !Message.find(7).watched_by?(@user)
107
        end
108
      end
109
    end
110
    
111
    context "of group" do
112
      setup do
113
        group = Group.find(10)
114
        @member = Member.create!(:project => Project.find(2), :principal => group, :role_ids => [1, 2])
115
        group.users << User.find(9)
116
      end
117

    
118
      context "by deleting membership" do
119
        should "prune watchers" do
120
          assert_difference 'Watcher.count', -4 do
121
            @member.destroy
122
          end
123
        end
124
      end  
125

    
126
      context "by updating roles" do
127
        should "prune watchers" do
128
          Role.find(2).remove_permission! :view_wiki_pages
129
          assert_difference 'Watcher.count', -2 do
130
            @member.role_ids = [2]
131
            @member.save
132
          end
133
        end
134
      end
135
    end
136
  end
137
end