Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / t / mail / dashboards.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 181;
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 my $good_time = 1290423660; # 6:01 EST on a monday
142 my $bad_time  = 1290427260; # 7:01 EST on a monday
143
144 my $expected_subject = "[example.com] Daily Dashboard: Testing!\n";
145
146 produces_dashboard_mail_ok(
147     Time => $good_time,
148     Subject => $expected_subject,
149 );
150
151 produces_dashboard_mail_ok(
152     All => 1,
153     Subject => $expected_subject,
154 );
155
156 produces_dashboard_mail_ok(
157     All  => 1,
158     Time => $good_time,
159     Subject => $expected_subject,
160 );
161
162 produces_dashboard_mail_ok(
163     All  => 1,
164     Time => $bad_time,
165     Subject => $expected_subject,
166 );
167
168
169 produces_no_dashboard_mail_ok(
170     Name   => "no dashboard mail it's a dry run",
171     All    => 1,
172     DryRun => 1,
173 );
174
175 produces_no_dashboard_mail_ok(
176     Name   => "no dashboard mail it's a dry run",
177     Time   => $good_time,
178     DryRun => 1,
179 );
180
181 produces_no_dashboard_mail_ok(
182     Name => "no mail because it's the wrong time",
183     Time => $bad_time,
184 );
185
186 @mails = RT::Test->fetch_caught_mails;
187 is(@mails, 0, "no mail leftover");
188
189
190 $m->no_warnings_ok;
191 RT::Test->stop_server;
192 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
193 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
194 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
195 ($baseurl, $m) = RT::Test->started_ok;
196
197 RT::Dashboard::Mailer->MailDashboards(All => 1);
198 @mails = RT::Test->fetch_caught_mails;
199 is(@mails, 1, "one mail");
200 my $mail = parse_mail($mails[0]);
201 is($mail->head->get('Subject'), "[example.com] a Daily b Testing! c\n");
202 is($mail->head->get('From'), "dashboard\@example.com\n");
203 is($mail->head->get('X-RT-Dashboard-Id'), "$dashboard_id\n");
204 is($mail->head->get('X-RT-Dashboard-Subscription-Id'), "$subscription_id\n");
205
206 SKIP: {
207     skip 'Weird MIME failure', 2;
208     my $body = $mail->stringify_body;
209     unlike($body, qr{My dashboards});
210     unlike($body, qr{Testing!});
211 };
212
213 delete_dashboard($dashboard_id);
214
215 RT::Dashboard::Mailer->MailDashboards(All => 1);
216 @mails = RT::Test->fetch_caught_mails;
217 is(@mails, 0, "no mail because the subscription is deleted");
218
219 RT::Test->stop_server;
220 RT::Test->clean_caught_mails;
221 RT->Config->Set('EmailDashboardRemove' => ());
222 RT->Config->Set('DashboardAddress' => 'root');
223 ($baseurl, $m) = RT::Test->started_ok;
224 $m->login;
225 create_dashboard($baseurl, $m);
226 create_subscription($baseurl, $m,
227     Frequency => 'weekly',
228     Hour => '06:00',
229 );
230
231 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
232
233 # bump $bad_time to Tuesday
234 $bad_time = $good_time + 86400;
235
236 produces_dashboard_mail_ok(
237     Time    => $good_time,
238     Subject =>  "[example.com] a Weekly b Testing! c\n",
239 );
240
241 produces_no_dashboard_mail_ok(
242     Name    => "no mail because it's the wrong time",
243     Time    => $bad_time,
244 );
245
246 @mails = RT::Test->fetch_caught_mails;
247 is(@mails, 0, "no mail leftover");
248
249 $m->no_warnings_ok;
250 RT::Test->stop_server;
251 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
252 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
253 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
254 ($baseurl, $m) = RT::Test->started_ok;
255
256 delete_dashboard($dashboard_id);
257
258 RT::Test->clean_caught_mails;
259
260 RT::Test->stop_server;
261
262 RT->Config->Set('EmailDashboardRemove' => ());
263 RT->Config->Set('DashboardAddress' => 'root');
264 ($baseurl, $m) = RT::Test->started_ok;
265 $m->login;
266 create_dashboard($baseurl, $m);
267 create_subscription($baseurl, $m,
268     Frequency => 'm-f',
269     Hour => '06:00',
270 );
271
272 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
273
274 # bump $bad_time back to Sunday
275 $bad_time = $good_time - 86400;
276
277 produces_dashboard_mail_ok(
278     Time    => $good_time,
279     Subject =>  "[example.com] a Weekday b Testing! c\n",
280 );
281
282 produces_no_dashboard_mail_ok(
283     Name    => "no mail because it's the wrong time",
284     Time    => $bad_time,
285 );
286
287 produces_no_dashboard_mail_ok(
288     Name    => "no mail because it's the wrong time",
289     Time    => $bad_time - 86400, # saturday
290 );
291
292 produces_dashboard_mail_ok(
293     Time    => $bad_time - 86400 * 2, # friday
294     Subject =>  "[example.com] a Weekday b Testing! c\n",
295 );
296
297
298 @mails = RT::Test->fetch_caught_mails;
299 is(@mails, 0, "no mail leftover");
300
301 $m->no_warnings_ok;
302 RT::Test->stop_server;
303 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
304 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
305 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
306 ($baseurl, $m) = RT::Test->started_ok;
307
308 delete_dashboard($dashboard_id);
309
310 RT::Test->clean_caught_mails;
311
312 RT::Test->stop_server;
313
314 RT->Config->Set('EmailDashboardRemove' => ());
315 RT->Config->Set('DashboardAddress' => 'root');
316 ($baseurl, $m) = RT::Test->started_ok;
317 $m->login;
318 create_dashboard($baseurl, $m);
319 create_subscription($baseurl, $m,
320     Frequency => 'monthly',
321     Hour => '06:00',
322 );
323
324 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
325
326 $good_time = 1291201200;        # dec 1
327 $bad_time = $good_time - 86400; # day before (i.e. different month)
328
329 produces_dashboard_mail_ok(
330     Time    => $good_time,
331     Subject =>  "[example.com] a Monthly b Testing! c\n",
332 );
333
334 produces_no_dashboard_mail_ok(
335     Name    => "no mail because it's the wrong time",
336     Time    => $bad_time,
337 );
338
339
340 @mails = RT::Test->fetch_caught_mails;
341 is(@mails, 0, "no mail leftover");
342
343 $m->no_warnings_ok;
344 RT::Test->stop_server;
345 RT->Config->Set('DashboardSubject' => 'a %s b %s c');
346 RT->Config->Set('DashboardAddress' => 'dashboard@example.com');
347 RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
348 ($baseurl, $m) = RT::Test->started_ok;
349
350 delete_dashboard($dashboard_id);
351
352 RT::Test->clean_caught_mails;
353
354 RT::Test->stop_server;
355
356 RT->Config->Set('EmailDashboardRemove' => ());
357 RT->Config->Set('DashboardAddress' => 'root');
358 ($baseurl, $m) = RT::Test->started_ok;
359 $m->login;
360 create_dashboard($baseurl, $m);
361 create_subscription($baseurl, $m,
362     Frequency => 'never',
363 );
364
365 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
366
367 produces_no_dashboard_mail_ok(
368     Name    => "mail should never get sent",
369     Time    => $bad_time,
370 );
371