first pass RT4 merge, RT#13852
[freeside.git] / rt / t / mail / dashboards.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 187;
6 use Test::Warn;
7 use RT::Dashboard::Mailer;
8
9 my ($baseurl, $m) = RT::Test->started_ok;
10 ok($m->login, 'logged in');
11
12 sub create_dashboard {
13     my ($baseurl, $m) = @_;
14     local $Test::Builder::Level = $Test::Builder::Level + 1;
15     $m->get_ok($baseurl . '/Dashboards/Modify.html?Create=1');
16     $m->form_name('ModifyDashboard');
17     $m->field('Name' => 'Testing!');
18     $m->click_button(value => 'Create');
19     $m->title_is('Modify the dashboard Testing!');
20
21     $m->follow_link_ok({text => 'Content'});
22     $m->title_is('Modify the content of dashboard Testing!');
23
24     my $form = $m->form_name('Dashboard-Searches-body');
25     my @input = $form->find_input('Searches-body-Available');
26     my ($dashboards_component) =
27         map { ( $_->possible_values )[1] }
28         grep { ( $_->value_names )[1] =~ /Dashboards/ } @input;
29     $form->value('Searches-body-Available' => $dashboards_component );
30     $m->click_button(name => 'add');
31     $m->content_contains('Dashboard updated');
32
33     $m->follow_link_ok({text => 'Show'});
34     $m->title_is('Testing! Dashboard');
35     $m->content_contains('My dashboards');
36     $m->content_like(qr{<a href="/Dashboards/\d+/Testing!">Testing!</a>});
37
38 }
39
40 sub create_subscription {
41     my ($baseurl, $m, %fields) = @_;
42     local $Test::Builder::Level = $Test::Builder::Level + 1;
43
44     # create a subscription
45     $m->follow_link_ok({text => 'Subscription'});
46     $m->title_is('Subscribe to dashboard Testing!');
47     $m->form_name('SubscribeDashboard');
48     $m->set_fields(%fields);
49     $m->click_button(name => 'Save');
50     $m->content_contains("Subscribed to dashboard Testing!");
51 }
52
53 sub get_dash_sub_ids {
54     my $user = RT::User->new(RT->SystemUser);
55     $user->Load('root');
56     ok($user->Id, 'loaded user');
57     my ($subscription) = $user->Attributes->Named('Subscription');
58     my $subscription_id = $subscription->Id;
59     ok($subscription_id, 'loaded subscription');
60     my $dashboard_id = $subscription->SubValue('DashboardId');
61     ok($dashboard_id, 'got dashboard id');
62
63
64     return ($dashboard_id, $subscription_id);
65 }
66
67 # first, create and populate a dashboard
68 create_dashboard($baseurl, $m);
69
70 # now test the mailer
71
72 # without a subscription..
73 RT::Dashboard::Mailer->MailDashboards();
74
75 my @mails = RT::Test->fetch_caught_mails;
76 is @mails, 0, 'no mail yet';
77
78 RT::Dashboard::Mailer->MailDashboards(
79     All => 1,
80 );
81
82 @mails = RT::Test->fetch_caught_mails;
83 is @mails, 0, "no mail yet since there's no subscription";
84
85 create_subscription($baseurl, $m,
86     Frequency => 'daily',
87     Hour      => '06:00',
88 );
89
90 my ($dashboard_id, $subscription_id) = get_dash_sub_ids();
91
92 sub produces_dashboard_mail_ok { # {{{
93     my %args = @_;
94     my $subject = delete $args{Subject};
95
96     local $Test::Builder::Level = $Test::Builder::Level + 1;
97
98     RT::Dashboard::Mailer->MailDashboards(%args);
99
100     my @mails = RT::Test->fetch_caught_mails;
101     is @mails, 1, "got a dashboard mail";
102
103     my $mail = parse_mail( $mails[0] );
104     is($mail->head->get('Subject'), $subject);
105     is($mail->head->get('From'), "root\n");
106     is($mail->head->get('X-RT-Dashboard-Id'), "$dashboard_id\n");
107     is($mail->head->get('X-RT-Dashboard-Subscription-Id'), "$subscription_id\n");
108
109     SKIP: {
110         skip 'Weird MIME failure', 2;
111         my $body = $mail->stringify_body;
112         like($body, qr{My dashboards});
113         like($body, qr{<a href="http://[^/]+/Dashboards/\d+/Testing!">Testing!</a>});
114     };
115 } # }}}
116
117 sub produces_no_dashboard_mail_ok { # {{{
118     my %args = @_;
119     my $name = delete $args{Name};
120
121     local $Test::Builder::Level = $Test::Builder::Level + 1;
122
123     RT::Dashboard::Mailer->MailDashboards(%args);
124
125     @mails = RT::Test->fetch_caught_mails;
126     is @mails, 0, $name;
127 } # }}}
128
129 sub delete_dashboard { # {{{
130     my $dashboard_id = shift;
131     # delete the dashboard and make sure we get exactly one subscription failure
132     # notice
133     my $dashboard = RT::Dashboard->new(RT::CurrentUser->new('root'));
134     my ($ok, $msg) = $dashboard->LoadById($dashboard_id);
135     ok($ok, $msg);
136
137     ($ok, $msg) = $dashboard->Delete;
138     ok($ok, $msg);
139 } # }}}
140
141 sub delete_subscriptions { # {{{
142     my $subscription_id = shift;
143     # delete the dashboard and make sure we get exactly one subscription failure
144     # notice
145     my $user = RT::User->new(RT->SystemUser);
146     $user->Load('root');
147     for my $subscription ($user->Attributes->Named('Subscription')) {
148         $subscription->Delete;
149     }
150 } # }}}
151
152 my $good_time = 1290423660; # 6:01 EST on a monday
153 my $bad_time  = 1290427260; # 7:01 EST on a monday
154
155 my $expected_subject = "[example.com] Daily Dashboard: Testing!\n";
156
157 produces_dashboard_mail_ok(
158     Time => $good_time,
159     Subject => $expected_subject,
160 );
161
162 produces_dashboard_mail_ok(
163     All => 1,
164     Subject => $expected_subject,
165 );
166
167 produces_dashboard_mail_ok(
168     All  => 1,
169     Time => $good_time,
170     Subject => $expected_subject,
171 );
172
173 produces_dashboard_mail_ok(
174     All  => 1,
175     Time => $bad_time,
176     Subject => $expected_subject,
177 );
178
179
180 produces_no_dashboard_mail_ok(
181     Name   => "no dashboard mail it's a dry run",
182     All    => 1,
183     DryRun => 1,
184 );
185
186 produces_no_dashboard_mail_ok(
187     Name   => "no dashboard mail it's a dry run",
188     Time   => $good_time,
189     DryRun => 1,
190 );
191
192 produces_no_dashboard_mail_ok(
193     Name => "no mail because it's the wrong time",
194     Time => $bad_time,
195 );
196
197 @mails = RT::Test->fetch_caught_mails;
198 is(@mails, 0, "no mail leftover");
199
200
201 $m->no_warnings_ok;
202 RT::Test->stop_server;
203 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
204 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
205 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
206 ($baseurl, $m) = RT::Test->started_ok;
207
208 RT::Dashboard::Mailer->MailDashboards(All => 1);
209 @mails = RT::Test->fetch_caught_mails;
210 is(@mails, 1, "one mail");
211 my $mail = parse_mail($mails[0]);
212 is($mail->head->get('Subject'), "[example.com] a Daily b Testing! c\n");
213 is($mail->head->get('From'), "dashboard\@example.com\n");
214 is($mail->head->get('X-RT-Dashboard-Id'), "$dashboard_id\n");
215 is($mail->head->get('X-RT-Dashboard-Subscription-Id'), "$subscription_id\n");
216
217 SKIP: {
218     skip 'Weird MIME failure', 2;
219     my $body = $mail->stringify_body;
220     unlike($body, qr{My dashboards});
221     unlike($body, qr{Testing!});
222 };
223
224 delete_dashboard($dashboard_id);
225
226 warning_like {
227     RT::Dashboard::Mailer->MailDashboards(All => 1);
228 } qr/Unable to load dashboard $dashboard_id of subscription $subscription_id for user root/;
229
230 @mails = RT::Test->fetch_caught_mails;
231 is(@mails, 1, "one mail for subscription failure");
232 $mail = parse_mail($mails[0]);
233 is($mail->head->get('Subject'), "[example.com] Missing dashboard!\n");
234 is($mail->head->get('From'), "dashboard\@example.com\n");
235 is($mail->head->get('X-RT-Dashboard-Id'), "$dashboard_id\n");
236 is($mail->head->get('X-RT-Dashboard-Subscription-Id'), "$subscription_id\n");
237
238 RT::Dashboard::Mailer->MailDashboards(All => 1);
239 @mails = RT::Test->fetch_caught_mails;
240 is(@mails, 0, "no mail because the subscription notice happens only once");
241
242 RT::Test->stop_server;
243 RT::Test->clean_caught_mails;
244 RT->Config->Set('EmailDashboardRemove' => ());
245 RT->Config->Set('DashboardAddress' => 'root');
246 ($baseurl, $m) = RT::Test->started_ok;
247 $m->login;
248 create_dashboard($baseurl, $m);
249 create_subscription($baseurl, $m,
250     Frequency => 'weekly',
251     Hour => '06:00',
252 );
253
254 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
255
256 # bump $bad_time to Tuesday
257 $bad_time = $good_time + 86400;
258
259 produces_dashboard_mail_ok(
260     Time    => $good_time,
261     Subject =>  "[example.com] a Weekly b Testing! c\n",
262 );
263
264 produces_no_dashboard_mail_ok(
265     Name    => "no mail because it's the wrong time",
266     Time    => $bad_time,
267 );
268
269 @mails = RT::Test->fetch_caught_mails;
270 is(@mails, 0, "no mail leftover");
271
272 $m->no_warnings_ok;
273 RT::Test->stop_server;
274 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
275 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
276 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
277 ($baseurl, $m) = RT::Test->started_ok;
278
279 delete_dashboard($dashboard_id);
280 delete_subscriptions();
281
282 RT::Test->clean_caught_mails;
283
284 RT::Test->stop_server;
285
286 RT->Config->Set('EmailDashboardRemove' => ());
287 RT->Config->Set('DashboardAddress' => 'root');
288 ($baseurl, $m) = RT::Test->started_ok;
289 $m->login;
290 create_dashboard($baseurl, $m);
291 create_subscription($baseurl, $m,
292     Frequency => 'm-f',
293     Hour => '06:00',
294 );
295
296 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
297
298 # bump $bad_time back to Sunday
299 $bad_time = $good_time - 86400;
300
301 produces_dashboard_mail_ok(
302     Time    => $good_time,
303     Subject =>  "[example.com] a Weekday b Testing! c\n",
304 );
305
306 produces_no_dashboard_mail_ok(
307     Name    => "no mail because it's the wrong time",
308     Time    => $bad_time,
309 );
310
311 produces_no_dashboard_mail_ok(
312     Name    => "no mail because it's the wrong time",
313     Time    => $bad_time - 86400, # saturday
314 );
315
316 produces_dashboard_mail_ok(
317     Time    => $bad_time - 86400 * 2, # friday
318     Subject =>  "[example.com] a Weekday b Testing! c\n",
319 );
320
321
322 @mails = RT::Test->fetch_caught_mails;
323 is(@mails, 0, "no mail leftover");
324
325 $m->no_warnings_ok;
326 RT::Test->stop_server;
327 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
328 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
329 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
330 ($baseurl, $m) = RT::Test->started_ok;
331
332 delete_dashboard($dashboard_id);
333 delete_subscriptions();
334
335 RT::Test->clean_caught_mails;
336
337 RT::Test->stop_server;
338
339 RT->Config->Set('EmailDashboardRemove' => ());
340 RT->Config->Set('DashboardAddress' => 'root');
341 ($baseurl, $m) = RT::Test->started_ok;
342 $m->login;
343 create_dashboard($baseurl, $m);
344 create_subscription($baseurl, $m,
345     Frequency => 'monthly',
346     Hour => '06:00',
347 );
348
349 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
350
351 $good_time = 1291201200;        # dec 1
352 $bad_time = $good_time - 86400; # day before (i.e. different month)
353
354 produces_dashboard_mail_ok(
355     Time    => $good_time,
356     Subject =>  "[example.com] a Monthly b Testing! c\n",
357 );
358
359 produces_no_dashboard_mail_ok(
360     Name    => "no mail because it's the wrong time",
361     Time    => $bad_time,
362 );
363
364
365 @mails = RT::Test->fetch_caught_mails;
366 is(@mails, 0, "no mail leftover");
367
368 $m->no_warnings_ok;
369 RT::Test->stop_server;
370 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
371 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
372 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
373 ($baseurl, $m) = RT::Test->started_ok;
374
375 delete_dashboard($dashboard_id);
376 delete_subscriptions();
377
378 RT::Test->clean_caught_mails;
379
380 RT::Test->stop_server;
381
382 RT->Config->Set('EmailDashboardRemove' => ());
383 RT->Config->Set('DashboardAddress' => 'root');
384 ($baseurl, $m) = RT::Test->started_ok;
385 $m->login;
386 create_dashboard($baseurl, $m);
387 create_subscription($baseurl, $m,
388     Frequency => 'never',
389 );
390
391 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
392
393 produces_no_dashboard_mail_ok(
394     Name    => "mail should never get sent",
395     Time    => $bad_time,
396 );
397