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