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