import rt 3.8.9
[freeside.git] / rt / lib / t / regression / 24-watchers.t
1 #!/usr/bin/perl -w
2 # BEGIN BPS TAGGED BLOCK {{{
3
4 # COPYRIGHT:
5 #  
6 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
7 #                                          <jesse.com>
8
9 # (Except where explicitly superseded by other copyright notices)
10
11
12 # LICENSE:
13
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
17 # from www.gnu.org.
18
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # General Public License for more details.
23
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28
29 # CONTRIBUTION SUBMISSION POLICY:
30
31 # (The following paragraph is not intended to limit the rights granted
32 # to you to modify and distribute this software under the terms of
33 # the GNU General Public License and is only of importance to you if
34 # you choose to contribute your changes and enhancements to the
35 # community by submitting them to Best Practical Solutions, LLC.)
36
37 # By intentionally submitting any modifications, corrections or
38 # derivatives to this work, or any other work intended for use with
39 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
40 # you are the copyright holder for those contributions and you grant
41 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
42 # royalty-free, perpetual, license to use, copy, create derivative
43 # works based on those contributions, and sublicense and distribute
44 # those contributions and any derivatives thereof.
45
46 # END BPS TAGGED BLOCK }}}
47
48 use Test::More tests => 28;
49 use RT;
50 RT::LoadConfig();
51 RT::Init();
52 use strict;
53 no warnings 'once';
54
55 use RT::Queue;
56 use RT::User;
57 use RT::Group;
58 use RT::Ticket;
59 use RT::CurrentUser;
60
61
62 # clear all global right
63 my $acl = RT::ACL->new($RT::SystemUser);
64 $acl->Limit( FIELD => 'RightName', OPERATOR => '!=', VALUE => 'SuperUser' );
65 $acl->LimitToObject( $RT::System );
66 while( my $ace = $acl->Next ) {
67         $ace->Delete;
68 }
69
70 # create new queue to be sure we do not mess with rights
71 my $queue = RT::Queue->new($RT::SystemUser);
72 my ($queue_id) = $queue->Create( Name => 'watcher tests '.$$);
73 ok( $queue_id, 'queue created for watcher tests' );
74
75 # new privileged user to check rights
76 my $user = RT::User->new( $RT::SystemUser );
77 my ($user_id) = $user->Create( Name => 'watcher'.$$,
78                            EmailAddress => "watcher$$".'@localhost',
79                            Privileged => 1,
80                            Password => 'qwe123',
81                          );
82 my $cu= RT::CurrentUser->new($user);
83
84 # make sure user can see tickets in the queue
85 my $principal = $user->PrincipalObj;
86 ok( $principal, "principal loaded" );
87 $principal->GrantRight( Right => 'ShowTicket', Object => $queue );
88 $principal->GrantRight( Right => 'SeeQueue'  , Object => $queue );
89
90 ok(  $user->HasRight( Right => 'SeeQueue',     Object => $queue ), "user can see queue" );
91 ok(  $user->HasRight( Right => 'ShowTicket',   Object => $queue ), "user can show queue tickets" );
92 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $queue ), "user can't modify queue tickets" );
93 ok( !$user->HasRight( Right => 'Watch',        Object => $queue ), "user can't watch queue tickets" );
94
95 my $ticket = RT::Ticket->new( $RT::SystemUser );
96 my ($rv, $msg) = $ticket->Create( Subject => 'watcher tests', Queue => $queue->Name );
97 ok( $ticket->id, "ticket created" );
98
99 my $ticket2 = RT::Ticket->new( $cu );
100 $ticket2->Load( $ticket->id );
101 ok( $ticket2->Subject, "ticket load by user" );
102
103 # user can add self to ticket only after getting Watch right
104 ($rv, $msg) = $ticket2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
105 ok( !$rv, "user can't add self as Cc" );
106 ($rv, $msg) = $ticket2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
107 ok( !$rv, "user can't add self as Requestor" );
108 $principal->GrantRight( Right => 'Watch'  , Object => $queue );
109 ok(  $user->HasRight( Right => 'Watch',        Object => $queue ), "user can watch queue tickets" );
110 ($rv, $msg) = $ticket2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
111 ok(  $rv, "user can add self as Cc by PrincipalId" );
112 ($rv, $msg) = $ticket2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
113 ok(  $rv, "user can add self as Requestor by PrincipalId" );
114
115 # remove user and try adding with Email address
116 ($rv, $msg) = $ticket->DeleteWatcher( Type => 'Cc',        PrincipalId => $user->PrincipalId );
117 ok( $rv, "watcher removed by PrincipalId" );
118 ($rv, $msg) = $ticket->DeleteWatcher( Type => 'Requestor', Email => $user->EmailAddress );
119 ok( $rv, "watcher removed by Email" );
120
121 ($rv, $msg) = $ticket2->AddWatcher( Type => 'Cc', Email => $user->EmailAddress );
122 ok(  $rv, "user can add self as Cc by Email" );
123 ($rv, $msg) = $ticket2->AddWatcher( Type => 'Requestor', Email => $user->EmailAddress );
124 ok(  $rv, "user can add self as Requestor by Email" );
125
126 # Queue watcher tests
127 $principal->RevokeRight( Right => 'Watch'  , Object => $queue );
128 ok( !$user->HasRight( Right => 'Watch',        Object => $queue ), "user queue watch right revoked" );
129
130 my $queue2 = RT::Queue->new( $cu );
131 ($rv, $msg) = $queue2->Load( $queue->id );
132 ok( $rv, "user loaded queue" );
133
134 # user can add self to queue only after getting Watch right
135 ($rv, $msg) = $queue2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
136 ok( !$rv, "user can't add self as Cc" );
137 ($rv, $msg) = $queue2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
138 ok( !$rv, "user can't add self as Requestor" );
139 $principal->GrantRight( Right => 'Watch'  , Object => $queue );
140 ok(  $user->HasRight( Right => 'Watch',        Object => $queue ), "user can watch queue queues" );
141 ($rv, $msg) = $queue2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
142 ok(  $rv, "user can add self as Cc by PrincipalId" );
143 ($rv, $msg) = $queue2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
144 ok(  $rv, "user can add self as Requestor by PrincipalId" );
145
146 # remove user and try adding with Email address
147 ($rv, $msg) = $queue->DeleteWatcher( Type => 'Cc',        PrincipalId => $user->PrincipalId );
148 ok( $rv, "watcher removed by PrincipalId" );
149 ($rv, $msg) = $queue->DeleteWatcher( Type => 'Requestor', Email => $user->EmailAddress );
150 ok( $rv, "watcher removed by Email" );
151
152 ($rv, $msg) = $queue2->AddWatcher( Type => 'Cc', Email => $user->EmailAddress );
153 ok(  $rv, "user can add self as Cc by Email" );
154 ($rv, $msg) = $queue2->AddWatcher( Type => 'Requestor', Email => $user->EmailAddress );
155 ok(  $rv, "user can add self as Requestor by Email" );
156
157