import rt 3.8.7
[freeside.git] / rt / t / api / rights.t
1 #!/usr/bin/perl -w
2 # BEGIN BPS TAGGED BLOCK {{{
3
4 # COPYRIGHT:
5 #  
6 # This software is Copyright (c) 1996-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/copyleft/gpl.html.
29
30
31 # CONTRIBUTION SUBMISSION POLICY:
32
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
38
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
47
48 # END BPS TAGGED BLOCK }}}
49
50 use RT;
51 use RT::Test tests => 26;
52
53 use RT::I18N;
54 use strict;
55 no warnings 'once';
56
57 use RT::Queue;
58 use RT::ACE;
59 use RT::User;
60 use RT::Group;
61 use RT::Ticket;
62
63
64 # clear all global right
65 my $acl = RT::ACL->new($RT::SystemUser);
66 $acl->Limit( FIELD => 'RightName', OPERATOR => '!=', VALUE => 'SuperUser' );
67 $acl->LimitToObject( $RT::System );
68 while( my $ace = $acl->Next ) {
69         $ace->Delete;
70 }
71
72 my $rand_name = "rights". int rand($$);
73 # create new queue to be shure we don't mess with rights
74 my $queue = RT::Queue->new($RT::SystemUser);
75 my ($queue_id) = $queue->Create( Name => $rand_name);
76 ok( $queue_id, 'queue created for rights tests' );
77
78 # new privileged user to check rights
79 my $user = RT::User->new( $RT::SystemUser );
80 my ($user_id) = $user->Create( Name => $rand_name,
81                            EmailAddress => $rand_name .'@localhost',
82                            Privileged => 1,
83                            Password => 'qwe123',
84                          );
85 ok( !$user->HasRight( Right => 'OwnTicket', Object => $queue ), "user can't own ticket" );
86 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $queue ), "user can't reply to ticket" );
87
88 my $group = RT::Group->new( $RT::SystemUser );
89 ok( $group->LoadQueueRoleGroup( Queue => $queue_id, Type=> 'Owner' ), "load queue owners role group" );
90 my $ace = RT::ACE->new( $RT::SystemUser );
91 my ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ReplyToTicket', Object => $queue );
92 ok( $ace_id, "Granted queue owners role group with ReplyToTicket right: $msg" );
93 ok( $group->PrincipalObj->HasRight( Right => 'ReplyToTicket', Object => $queue ), "role group can reply to ticket" );
94 ok( !$user->HasRight( Right => 'ReplyToTicket', Object => $queue ), "user can't reply to ticket" );
95
96 # new ticket
97 my $ticket = RT::Ticket->new($RT::SystemUser);
98 my ($ticket_id) = $ticket->Create( Queue => $queue_id, Subject => 'test');
99 ok( $ticket_id, 'new ticket created' );
100 is( $ticket->Owner, $RT::Nobody->Id, 'owner of the new ticket is nobody' );
101
102 my $status;
103 ($status, $msg) = $user->PrincipalObj->GrantRight( Object => $queue, Right => 'OwnTicket' );
104 ok( $status, "successfuly granted right: $msg" );
105 ok( $user->HasRight( Right => 'OwnTicket', Object => $queue ), "user can own ticket" );
106
107 ($status, $msg) = $ticket->SetOwner( $user_id );
108 ok( $status, "successfuly set owner: $msg" );
109 is( $ticket->Owner, $user_id, "set correct owner" );
110
111 ok( $user->HasRight( Right => 'ReplyToTicket', Object => $ticket ), "user is owner and can reply to ticket" );
112
113 # Testing of EquivObjects
114 $group = RT::Group->new( $RT::SystemUser );
115 ok( $group->LoadQueueRoleGroup( Queue => $queue_id, Type=> 'AdminCc' ), "load queue AdminCc role group" );
116 $ace = RT::ACE->new( $RT::SystemUser );
117 ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ModifyTicket', Object => $queue );
118 ok( $ace_id, "Granted queue AdminCc role group with ModifyTicket right: $msg" );
119 ok( $group->PrincipalObj->HasRight( Right => 'ModifyTicket', Object => $queue ), "role group can modify ticket" );
120 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket ), "user is not AdminCc and can't modify ticket" );
121 ($status, $msg) = $ticket->AddWatcher(Type => 'AdminCc', PrincipalId => $user->PrincipalId);
122 ok( $status, "successfuly added user as AdminCc");
123 ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket ), "user is AdminCc and can modify ticket" );
124
125 my $ticket2 = RT::Ticket->new($RT::SystemUser);
126 my ($ticket2_id) = $ticket2->Create( Queue => $queue_id, Subject => 'test2');
127 ok( $ticket2_id, 'new ticket created' );
128 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2 ), "user is not AdminCc and can't modify ticket2" );
129
130 # now we can finally test EquivObjects
131 my $equiv = [ $ticket ];
132 ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv ), 
133     "user is not AdminCc but can modify ticket2 because of EquivObjects" );
134
135 # the first a third test below are the same, so they should both pass
136 my $equiv2 = [];
137 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv2 ), 
138     "user is not AdminCc and can't modify ticket2" );
139 ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket, EquivObjects => $equiv2 ), 
140     "user is AdminCc and can modify ticket" );
141 ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv2 ), 
142     "user is not AdminCc and can't modify ticket2 (same question different answer)" );