fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / ticket / deferred_owner.t
1 use strict;
2 use warnings;
3
4 use RT::Test nodata => 1, tests => undef;
5 use Test::Warn;
6
7
8 my $tester = RT::Test->load_or_create_user(
9     EmailAddress => 'tester@localhost',
10 );
11 ok $tester && $tester->id, 'loaded or created user';
12
13 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
14 ok $queue && $queue->id, 'loaded or created queue';
15
16 my $owner_role_group = $queue->RoleGroup( 'Owner' );
17 ok $owner_role_group->id, 'loaded owners role group of the queue';
18
19 diag "check that deffering owner doesn't regress";
20 {
21     RT::Test->set_rights(
22         { Principal => $tester->PrincipalObj,
23           Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket)],
24         },
25         { Principal => $owner_role_group->PrincipalObj,
26           Object => $queue,
27           Right => [qw(ModifyTicket)],
28         },
29     );
30     my $ticket = RT::Ticket->new( $tester );
31     # tester is owner, owner has right to modify owned tickets,
32     # this right is required to set somebody as AdminCc
33     my ($tid, $txn_id, $msg) = $ticket->Create(
34         Queue   => $queue->id,
35         Owner   => $tester->id,
36         AdminCc => 'root@localhost',
37     );
38     diag $msg if $msg;
39     ok $tid, "created a ticket";
40     is $ticket->Owner, $tester->id, 'correct owner';
41     like $ticket->AdminCcAddresses, qr/root\@localhost/, 'root is there';
42 }
43
44 diag "check that previous trick doesn't work without sufficient rights";
45 {
46     RT::Test->set_rights(
47         { Principal => $tester->PrincipalObj,
48           Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket)],
49         },
50     );
51     my $ticket = RT::Ticket->new( $tester );
52     # tester is owner, owner has right to modify owned tickets,
53     # this right is required to set somebody as AdminCc
54     my ($tid, $txn_id, $msg) = $ticket->Create(
55         Queue   => $queue->id,
56         Owner   => $tester->id,
57         AdminCc => 'root@localhost',
58     );
59     diag $msg if $msg;
60     ok $tid, "created a ticket";
61     is $ticket->Owner, $tester->id, 'correct owner';
62     unlike $ticket->AdminCcAddresses, qr/root\@localhost/, 'root is not there';
63 }
64
65 diag "check that deffering owner really works";
66 {
67     RT::Test->set_rights(
68         { Principal => $tester->PrincipalObj,
69           Right => [qw(SeeQueue ShowTicket CreateTicket)],
70         },
71         { Principal => $queue->Cc->PrincipalObj,
72           Object => $queue,
73           Right => [qw(OwnTicket TakeTicket)],
74         },
75     );
76     my $ticket = RT::Ticket->new( $tester );
77     # set tester as Cc, Cc role group has right to own and take tickets
78     my ($tid, $txn_id, $msg) = $ticket->Create(
79         Queue => $queue->id,
80         Owner => $tester->id,
81         Cc    => 'tester@localhost',
82     );
83     diag $msg if $msg;
84     ok $tid, "created a ticket";
85     like $ticket->CcAddresses, qr/tester\@localhost/, 'tester is in the cc list';
86     is $ticket->Owner, $tester->id, 'tester is also owner';
87     my $owners = $ticket->OwnerGroup->MembersObj;
88     is $owners->Count, 1, 'one record in owner group';
89     is $owners->First->MemberObj->Id, $tester->id, 'and it is tester';
90 }
91
92 diag "check that deffering doesn't work without correct rights";
93 {
94     RT::Test->set_rights(
95         { Principal => $tester->PrincipalObj,
96           Right => [qw(SeeQueue ShowTicket CreateTicket)],
97         },
98     );
99
100     my $ticket = RT::Ticket->new( $tester );
101     # set tester as Cc, Cc role group has right to own and take tickets
102     my ($tid, $txn_id, $msg);
103     warning_like {
104         ($tid, $txn_id, $msg) = $ticket->Create(
105             Queue => $queue->id,
106             Owner => $tester->id,
107             Cc    => 'tester@localhost',
108         );
109     } qr/User .* was proposed as a ticket owner but has no rights to own tickets in General/;
110
111     diag $msg if $msg;
112     ok $tid, "created a ticket";
113     like $ticket->CcAddresses, qr/tester\@localhost/, 'tester is in the cc list';
114     is $ticket->Owner, RT->Nobody->id, 'nobody is the owner';
115     my $owners = $ticket->OwnerGroup->MembersObj;
116     is $owners->Count, 1, 'one record in owner group';
117     is $owners->First->MemberObj->Id, RT->Nobody->id, 'and it is nobody';
118 }
119
120 done_testing;