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