summaryrefslogtreecommitdiff
path: root/rt/t/approval/admincc.t
blob: 7915b6a783fe3bebfca91885fd922938ae024212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

use strict;
use warnings;
use Test::More;
BEGIN {
    eval { require Email::Abstract; require Test::Email; 1 }
        or plan skip_all => 'require Email::Abstract and Test::Email';
}


use RT;
use RT::Test tests => 58;
use RT::Test::Email;

RT->Config->Set( LogToScreen => 'debug' );
RT->Config->Set( UseTransactionBatch => 1 );
my ($baseurl, $m) = RT::Test->started_ok;

my $q = RT::Queue->new($RT::SystemUser);
$q->Load('___Approvals');
$q->SetDisabled(0);

my %users;
# minion is the requestor, cto is the approval owner, coo and ceo are approval
# adminccs
for my $user_name (qw(minion cto coo ceo )) {
    my $user = $users{$user_name} = RT::User->new($RT::SystemUser);
    $user->Create( Name => uc($user_name),
                   Privileged => 1,
                   EmailAddress => $user_name.'@company.com',
                   Password => 'password',
                   );
    my ($val, $msg);
    ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
        for qw(ModifyTicket OwnTicket ShowTicket);
}

# XXX: we need to make the first approval ticket open so notification is sent.
my $approvals = 
'===Create-Ticket: for-CTO
Queue: ___Approvals
Type: approval
Owner: CTO
AdminCCs: COO, CEO
Requestors: {$Tickets{"TOP"}->Requestors}
DependedOnBy: TOP
Subject: CTO Approval for PO: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
Due: {time + 86400}
Content-Type: text/plain
Content: Your approval is requested for the PO ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
Blah
Blah
ENDOFCONTENT
';

my $apptemp = RT::Template->new($RT::SystemUser);
$apptemp->Create( Content => $approvals, Name => "PO Approvals", Queue => "0");

ok($apptemp->Id);

$q = RT::Queue->new($RT::SystemUser);
$q->Create(Name => 'PO');
ok ($q->Id, "Created PO queue");

my $scrip = RT::Scrip->new($RT::SystemUser);
my ($sval, $smsg) =$scrip->Create( ScripCondition => 'On Create',
                ScripAction => 'Create Tickets',
                Template => 'PO Approvals',
                Queue => $q->Id,
                Description => 'Create Approval Tickets');
ok ($sval, $smsg);
ok ($scrip->Id, "Created the scrip");
ok ($scrip->TemplateObj->Id, "Created the scrip template");
ok ($scrip->ConditionObj->Id, "Created the scrip condition");
ok ($scrip->ActionObj->Id, "Created the scrip action");

my $t = RT::Ticket->new($RT::SystemUser);
my ($tid, $ttrans, $tmsg);

mail_ok {
    ( $tid, $ttrans, $tmsg ) = $t->Create(
        Subject   => "PO for stationary",
        Owner     => "root",
        Requestor => 'minion',
        Queue     => $q->Id,
    );
} { from => qr/RT System/,
    bcc => qr/ceo.*coo|coo.*ceo/i,
    subject => qr/PO for stationary/i,
},
{ from => qr/RT System/,
    to => 'cto@company.com',
    subject => qr/New Pending Approval: CTO Approval/,
    body => qr/pending your approval.*Your approval is requested.*Blah/s
},
{ from => qr/PO via RT/,
    to => 'minion@company.com',
    subject => qr/PO for stationary/,
    body => qr/automatically generated in response/
};

ok ($tid,$tmsg);

is ($t->DependsOn->Count, 1, "depends on one ticket");
my $t_cto = RT::Ticket->new( $RT::SystemUser );
$t_cto->Load( $t->DependsOn->First->TargetObj->id );

is_deeply(
    [ $t->Status, $t_cto->Status ],
    [ 'new',      'open' ],
    'tickets in correct state'
);

mail_ok {
    my $cto = RT::CurrentUser->new;
    $cto->Load( $users{cto} );

    $t_cto->CurrentUser($cto);
    my $notes = MIME::Entity->build(
        Data => [ 'Resources exist to be consumed.' ]
    );
    RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8

    my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
    ok($notesval, $notesmsg);

    my ($ok, $msg) = $t_cto->SetStatus( Status => 'resolved' );
    ok($ok, "cto can approve - $msg");
    } 
{
    from => qr/CTO/,
    bcc => qr/ceo.*coo|coo.*ceo/i,
    body => qr/Resources exist to be consumed/,
},
{
    from => qr/RT System/,
    to => 'minion@company.com',
    subject => qr/Ticket Approved:/,
    body => qr/approved by CTO.*notes: Resources exist to be consumed/s
},
{
    from => qr/CTO/,
    to => 'root@localhost',
    subject => qr/Ticket Approved:/,
    body => qr/The ticket has been approved, you may now start to act on it/,
};

is_deeply(
    [ $t->Status, $t_cto->Status ],
    [ 'new', 'resolved' ],
    'ticket state after coo approval'
);

for my $admin (qw/coo ceo/) {
    $t_cto->_Set(
        Field => 'Status',
        Value => 'open',
    );

    RT::Test->clean_caught_mails;

    mail_ok {
        my $user = RT::CurrentUser->new;
        $user->Load( $users{$admin} );

        $t_cto->CurrentUser($user);
        my $notes =
          MIME::Entity->build( Data => ['Resources exist to be consumed.'] );
        RT::I18N::SetMIMEEntityToUTF8($notes);   # convert text parts into utf-8

        my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
        ok( $notesval, $notesmsg );

        my ( $ok, $msg ) = $t_cto->SetStatus( Status => 'resolved' );
        ok( $ok, "cto can approve - $msg" );
    }
    {
        from => qr/\U$admin/,
        bcc  => $admin eq 'coo' ? qr/ceo/i : qr/coo/,
        body => qr/Resources exist to be consumed/,
    },
      {
        from    => qr/RT System/,
        to      => 'minion@company.com',
        subject => qr/Ticket Approved:/,
        body    => qr/approved by \U$admin\E.*notes: Resources exist to be consumed/s
      },
      {
        from    => qr/\U$admin/,
        to      => 'root@localhost',
        subject => qr/Ticket Approved:/,
        body =>
          qr/The ticket has been approved, you may now start to act on it/,
      };
}

# now we test the web
my $approval_link = $baseurl . '/Approvals/';

$t = RT::Ticket->new($RT::SystemUser);
( $tid, $ttrans, $tmsg ) = $t->Create(
    Subject   => "first approval",
    Owner     => "root",
    Requestor => 'minion',
    Queue     => $q->Id,
);
ok( $tid, $tmsg );

my $first_ticket = RT::Ticket->new( $RT::SystemUser );
$first_ticket->Load( $tid );
my $first_approval = $first_ticket->DependsOn->First->TargetObj;

$t = RT::Ticket->new($RT::SystemUser);
( $tid, $ttrans, $tmsg ) = $t->Create(
    Subject   => "second approval",
    Owner     => "root",
    Requestor => 'minion',
    Queue     => $q->Id,
);

my $second_ticket = RT::Ticket->new( $RT::SystemUser );
$second_ticket->Load( $tid );
my $second_approval = $second_ticket->DependsOn->First->TargetObj;


ok( $m->login( 'cto', 'password' ), 'logged in as coo' );

my $m_coo = RT::Test::Web->new;
ok( $m_coo->login( 'coo', 'password' ), 'logged in as coo' );

my $m_ceo = RT::Test::Web->new;
ok( $m_ceo->login( 'ceo', 'password' ), 'logged in as coo' );


$m->get_ok( $approval_link );
$m_coo->get_ok( $approval_link );
$m_ceo->get_ok( $approval_link );

$m->content_like( qr/first approval.*second approval/s, 'cto: see both approvals' );
$m_coo->content_like( qr/first approval.*second approval/s, 'coo: see both approvals' );
$m_ceo->content_like( qr/first approval.*second approval/s, 'ceo: see both approvals' );

# now let's approve the first one via cto
$m->content_like( qr/first approval.*second approval/s, 'cto can see both approvals' );
$m->submit_form(
    form_number => 3,
    fields      => { 'Approval-' . $first_approval->id . '-Action' => 'approve', },
);

$m->content_lacks( 'first approval', 'cto: first approval is gone' );
$m->content_contains( 'second approval', 'cto: second approval is still here' );
$m_coo->get_ok( $approval_link );
$m_ceo->get_ok( $approval_link );
$m_coo->content_lacks( 'first approval', 'coo: first approval is gone' );
$m_coo->content_contains( 'second approval', 'coo: second approval is still here' );
$m_ceo->content_lacks( 'first approval', 'ceo: first approval is gone' );
$m_ceo->content_contains( 'second approval', 'ceo: second approval is still here' );

$m_coo->submit_form(
    form_number => 3,
    fields      => { 'Approval-' . $second_approval->id . '-Action' => 'approve', },
);

$m->get_ok( $approval_link );
$m_ceo->get_ok( $approval_link );

$m->content_lacks( 'second approval', 'cto: second approval is gone too' );
$m_coo->content_lacks( 'second approval', 'coo: second approval is gone too' );
$m_ceo->content_lacks( 'second approval', 'ceo: second approval is gone too' );

RT::Test->clean_caught_mails;