fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / approval / admincc.t
1
2 use strict;
3 use warnings;
4 use Test::More;
5
6 use RT;
7 use RT::Test tests => "no_declare";
8 use RT::Test::Email;
9
10 RT->Config->Set( LogToSTDERR => 'debug' );
11 RT->Config->Set( UseTransactionBatch => 1 );
12 my ($baseurl, $m) = RT::Test->started_ok;
13
14 my $q = RT::Queue->new($RT::SystemUser);
15 $q->Load('___Approvals');
16 $q->SetDisabled(0);
17
18 my %users;
19 # minion is the requestor, cto is the approval owner, coo and ceo are approval
20 # adminccs
21 for my $user_name (qw(minion cto coo ceo )) {
22     my $user = $users{$user_name} = RT::User->new($RT::SystemUser);
23     $user->Create( Name => uc($user_name),
24                    Privileged => 1,
25                    EmailAddress => $user_name.'@company.com',
26                    Password => 'password',
27                    );
28     my ($val, $msg);
29     ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
30         for qw(ModifyTicket OwnTicket ShowTicket);
31 }
32
33 # XXX: we need to make the first approval ticket open so notification is sent.
34 my $approvals = 
35 '===Create-Ticket: for-CTO
36 Queue: ___Approvals
37 Type: approval
38 Owner: CTO
39 AdminCCs: COO, CEO
40 DependedOnBy: TOP
41 Subject: CTO Approval for PO: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
42 Due: {time + 86400}
43 Content-Type: text/plain
44 Content: Your approval is requested for the PO ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
45 Blah
46 Blah
47 ENDOFCONTENT
48 ';
49
50 my $apptemp = RT::Template->new($RT::SystemUser);
51 $apptemp->Create( Content => $approvals, Name => "PO Approvals", Queue => "0");
52
53 ok($apptemp->Id);
54
55 $q = RT::Queue->new($RT::SystemUser);
56 $q->Create(Name => 'PO');
57 ok ($q->Id, "Created PO queue");
58
59 my $scrip = RT::Scrip->new($RT::SystemUser);
60 my ($sval, $smsg) =$scrip->Create( ScripCondition => 'On Create',
61                 ScripAction => 'Create Tickets',
62                 Template => 'PO Approvals',
63                 Queue => $q->Id,
64                 Description => 'Create Approval Tickets');
65 ok ($sval, $smsg);
66 ok ($scrip->Id, "Created the scrip");
67 ok ($scrip->TemplateObj->Id, "Created the scrip template");
68 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
69 ok ($scrip->ActionObj->Id, "Created the scrip action");
70
71 my $t = RT::Ticket->new($RT::SystemUser);
72 my ($tid, $ttrans, $tmsg);
73
74 mail_ok {
75     ( $tid, $ttrans, $tmsg ) = $t->Create(
76         Subject   => "PO for stationary",
77         Owner     => "root",
78         Requestor => 'minion',
79         Queue     => $q->Id,
80     );
81 } { from => qr/PO via RT/,
82     to => 'minion@company.com',
83     subject => qr/PO for stationary/,
84     body => qr/automatically generated in response/
85 },{ from => qr/RT System/,
86     to => 'root@localhost',
87     subject => qr/PO for stationary/,
88 },{ from => qr/RT System/,
89     to => 'cto@company.com',
90     bcc => qr/ceo.*coo|coo.*ceo/i,
91     subject => qr/New Pending Approval: CTO Approval/,
92     body => qr/pending your approval.*Your approval is requested.*Blah/s
93 }
94 ;
95
96 ok ($tid,$tmsg);
97
98 is ($t->DependsOn->Count, 1, "depends on one ticket");
99 my $t_cto = RT::Ticket->new( $RT::SystemUser );
100 $t_cto->Load( $t->DependsOn->First->TargetObj->id );
101
102 is_deeply(
103     [ $t->Status, $t_cto->Status ],
104     [ 'new',      'open' ],
105     'tickets in correct state'
106 );
107
108 mail_ok {
109     my $cto = RT::CurrentUser->new;
110     $cto->Load( $users{cto} );
111
112     $t_cto->CurrentUser($cto);
113     my $notes = MIME::Entity->build(
114         Data => [ 'Resources exist to be consumed.' ]
115     );
116     RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
117
118     my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
119     ok($notesval, $notesmsg);
120
121     my ($ok, $msg) = $t_cto->SetStatus( Status => 'resolved' );
122     ok($ok, "cto can approve - $msg");
123     } 
124 {
125     from => qr/CTO/,
126     bcc => qr/ceo.*coo|coo.*ceo/i,
127     body => qr/Resources exist to be consumed/,
128 },
129 {
130     from => qr/RT System/,
131     to => 'root@localhost',
132     subject => qr/Ticket Approved:/,
133 },
134 {
135     from => qr/RT System/,
136     to => 'minion@company.com',
137     subject => qr/Ticket Approved:/,
138     body => qr/approved by CTO.*notes: Resources exist to be consumed/s
139 },
140 {
141     from => qr/CTO/,
142     to => 'root@localhost',
143     subject => qr/Ticket Approved:/,
144     body => qr/The ticket has been approved, you may now start to act on it/,
145 };
146
147 is_deeply(
148     [ $t->Status, $t_cto->Status ],
149     [ 'new', 'resolved' ],
150     'ticket state after coo approval'
151 );
152
153 for my $admin (qw/coo ceo/) {
154     $t_cto->_Set(
155         Field => 'Status',
156         Value => 'open',
157     );
158
159     RT::Test->clean_caught_mails;
160
161     mail_ok {
162         my $user = RT::CurrentUser->new;
163         $user->Load( $users{$admin} );
164
165         $t_cto->CurrentUser($user);
166         my $notes =
167           MIME::Entity->build( Data => ['Resources exist to be consumed.'] );
168         RT::I18N::SetMIMEEntityToUTF8($notes);   # convert text parts into utf-8
169
170         my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
171         ok( $notesval, $notesmsg );
172
173         my ( $ok, $msg ) = $t_cto->SetStatus( Status => 'resolved' );
174         ok( $ok, "cto can approve - $msg" );
175     }
176     {
177         from => qr/\U$admin/,
178         bcc  => $admin eq 'coo' ? qr/ceo/i : qr/coo/,
179         body => qr/Resources exist to be consumed/,
180     },
181       {
182          from => qr/RT System/,
183          to => 'root@localhost',
184          subject => qr/Ticket Approved:/,
185          body    => qr/approved by \U$admin\E.*notes: Resources exist to be consumed/s
186       },
187       {
188         from    => qr/RT System/,
189         to      => 'minion@company.com',
190         subject => qr/Ticket Approved:/,
191         body    => qr/approved by \U$admin\E.*notes: Resources exist to be consumed/s
192       },
193       {
194         from    => qr/\U$admin/,
195         to      => 'root@localhost',
196         subject => qr/Ticket Approved:/,
197         body =>
198           qr/The ticket has been approved, you may now start to act on it/,
199       };
200 }
201
202 # now we test the web
203 my $approval_link = $baseurl . '/Approvals/';
204
205 $t = RT::Ticket->new($RT::SystemUser);
206 ( $tid, $ttrans, $tmsg ) = $t->Create(
207     Subject   => "first approval",
208     Owner     => "root",
209     Requestor => 'minion',
210     Queue     => $q->Id,
211 );
212 ok( $tid, $tmsg );
213
214 my $first_ticket = RT::Ticket->new( $RT::SystemUser );
215 $first_ticket->Load( $tid );
216 my $first_approval = $first_ticket->DependsOn->First->TargetObj;
217
218 $t = RT::Ticket->new($RT::SystemUser);
219 ( $tid, $ttrans, $tmsg ) = $t->Create(
220     Subject   => "second approval",
221     Owner     => "root",
222     Requestor => 'minion',
223     Queue     => $q->Id,
224 );
225
226 my $second_ticket = RT::Ticket->new( $RT::SystemUser );
227 $second_ticket->Load( $tid );
228 my $second_approval = $second_ticket->DependsOn->First->TargetObj;
229
230
231 ok( $m->login( 'cto', 'password' ), 'logged in as coo' );
232
233 my $m_coo = RT::Test::Web->new;
234 ok( $m_coo->login( 'coo', 'password' ), 'logged in as coo' );
235
236 my $m_ceo = RT::Test::Web->new;
237 ok( $m_ceo->login( 'ceo', 'password' ), 'logged in as coo' );
238
239
240 $m->get_ok( $approval_link );
241 $m_coo->get_ok( $approval_link );
242 $m_ceo->get_ok( $approval_link );
243
244 $m->content_contains('first approval',  'cto: see both approvals' );
245 $m->content_contains('second approval', 'cto: see both approvals' );
246
247 $m_coo->content_contains('first approval',  'coo: see both approvals');
248 $m_coo->content_contains('second approval', 'coo: see both approvals');
249
250 $m_ceo->content_contains('first approval',  'ceo: see both approvals');
251 $m_ceo->content_contains('second approval', 'ceo: see both approvals');
252
253 # now let's approve the first one via cto
254 $m->submit_form(
255     form_name   => 'Approvals',
256     fields      => { 'Approval-' . $first_approval->id . '-Action' => 'approve', },
257 );
258
259 $m->content_lacks( 'first approval', 'cto: first approval is gone' );
260 $m->content_contains( 'second approval', 'cto: second approval is still here' );
261 $m_coo->get_ok( $approval_link );
262 $m_ceo->get_ok( $approval_link );
263 $m_coo->content_lacks( 'first approval', 'coo: first approval is gone' );
264 $m_coo->content_contains( 'second approval', 'coo: second approval is still here' );
265 $m_ceo->content_lacks( 'first approval', 'ceo: first approval is gone' );
266 $m_ceo->content_contains( 'second approval', 'ceo: second approval is still here' );
267
268 $m_coo->submit_form(
269     form_name => 'Approvals',
270     fields      => { 'Approval-' . $second_approval->id . '-Action' => 'approve', },
271 );
272
273 $m->get_ok( $approval_link );
274 $m_ceo->get_ok( $approval_link );
275
276 $m->content_lacks( 'second approval', 'cto: second approval is gone too' );
277 $m_coo->content_lacks( 'second approval', 'coo: second approval is gone too' );
278 $m_ceo->content_lacks( 'second approval', 'ceo: second approval is gone too' );
279
280 RT::Test->clean_caught_mails;
281
282 undef $m;
283 done_testing;