import rt 3.4.6
[freeside.git] / rt / lib / t / regression / 07rights.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 => 26;
49 use RT;
50 RT::LoadConfig();
51 RT::Init();
52 use RT::I18N;
53 use strict;
54 no warnings 'once';
55
56 use RT::Queue;
57 use RT::ACE;
58 use RT::User;
59 use RT::Group;
60 use RT::Ticket;
61
62
63 # clear all global right
64 my $acl = RT::ACL->new($RT::SystemUser);
65 $acl->Limit( FIELD => 'RightName', OPERATOR => '!=', VALUE => 'SuperUser' );
66 $acl->LimitToObject( $RT::System );
67 while( my $ace = $acl->Next ) {
68         $ace->Delete;
69 }
70
71 # create new queue to be shure we don't mess with rights
72 my $queue = RT::Queue->new($RT::SystemUser);
73 my ($queue_id) = $queue->Create( Name => 'rights');
74 ok( $queue_id, 'queue created for rights tests' );
75
76 # new privileged user to check rights
77 my $user = RT::User->new( $RT::SystemUser );
78 my ($user_id) = $user->Create( Name => 'rights',
79                            EmailAddress => 'rights@localhost',
80                            Privileged => 1,
81                            Password => 'qwe123',
82                          );
83 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "user can't own ticket" );
84 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $queue ), "user can't reply to ticket" );
85
86 my $group = RT::Group->new( $RT::SystemUser );
87 ok( $group->LoadQueueRoleGroup( Queue => $queue_id, Type=> 'Owner' ), "load queue owners role group" );
88 my $ace = RT::ACE->new( $RT::SystemUser );
89 my ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
90 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right: $msg" );
91 ok( $group->PrincipalObj->HasRight( Right => 'ReplyToTicket', Object => $queue ), "role group can reply to ticket" );
92 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $queue ), "user can't reply to ticket" );
93
94 # new ticket
95 my $ticket = RT::Ticket->new($RT::SystemUser);
96 my ($ticket_id) = $ticket->Create( Queue => $queue_id, Subject => 'test');
97 ok( $ticket_id, 'new ticket created' );
98 is( $ticket->Owner, $RT::Nobody->Id, 'owner of the new ticket is nobody' );
99
100 my $status;
101 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
102 ok( $status, "successfuly granted right: $msg" );
103 ok( $user->HasRight( Right => 'OwnTicket', Object => $queue ), "user can own ticket" );
104
105 ($status, $msg) = $ticket->SetOwner( $user_id );
106 ok( $status, "successfuly set owner: $msg" );
107 is( $ticket->Owner, $user_id, "set correct owner" );
108
109 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $ticket ), "user is owner and can reply to ticket" );
110
111 # Testing of EquivObjects
112 $group = RT::Group->new( $RT::SystemUser );
113 ok( $group->LoadQueueRoleGroup( Queue => $queue_id, Type=> 'AdminCc' ), "load queue AdminCc role group" );
114 $ace = RT::ACE->new( $RT::SystemUser );
115 my ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ModifyTicket', Object => $queue );
116 ok( $ace_id, "Granted queue AdminCc role group with ModifyTicket right: $msg" );
117 ok( $group->PrincipalObj->HasRight( Right => 'ModifyTicket', Object => $queue ), "role group can modify ticket" );
118 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket ), "user is not AdminCc and can't modify ticket" );
119 ($status, $msg) = $ticket->AddWatcher(Type => 'AdminCc', PrincipalId => $user->PrincipalId);
120 ok( $status, "successfuly added user as AdminCc");
121 ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket ), "user is AdminCc and can modify ticket" );
122
123 my $ticket2 = RT::Ticket->new($RT::SystemUser);
124 my ($ticket2_id) = $ticket2->Create( Queue => $queue_id, Subject => 'test2');
125 ok( $ticket2_id, 'new ticket created' );
126 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2 ), "user is not AdminCc and can't modify ticket2" );
127
128 # now we can finally test EquivObjects
129 my $equiv = [ $ticket ];
130 ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv ), 
131     "user is not AdminCc but can modify ticket2 because of EquivObjects" );
132
133 # the first a third test below are the same, so they should both pass
134 my $equiv2 = [];
135 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv2 ), 
136     "user is not AdminCc and can't modify ticket2" );
137 ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket, EquivObjects => $equiv2 ), 
138     "user is AdminCc and can modify ticket" );
139 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv2 ), 
140     "user is not AdminCc and can't modify ticket2 (same question different answer)" );