5 use RT::Test tests => 187;
7 use RT::Dashboard::Mailer;
9 my ($baseurl, $m) = RT::Test->started_ok;
10 ok($m->login, 'logged in');
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!');
21 $m->follow_link_ok({text => 'Content'});
22 $m->title_is('Modify the content of dashboard Testing!');
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');
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>});
40 sub create_subscription {
41 my ($baseurl, $m, %fields) = @_;
42 local $Test::Builder::Level = $Test::Builder::Level + 1;
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!");
53 sub get_dash_sub_ids {
54 my $user = RT::User->new(RT->SystemUser);
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');
64 return ($dashboard_id, $subscription_id);
67 # first, create and populate a dashboard
68 create_dashboard($baseurl, $m);
72 # without a subscription..
73 RT::Dashboard::Mailer->MailDashboards();
75 my @mails = RT::Test->fetch_caught_mails;
76 is @mails, 0, 'no mail yet';
78 RT::Dashboard::Mailer->MailDashboards(
82 @mails = RT::Test->fetch_caught_mails;
83 is @mails, 0, "no mail yet since there's no subscription";
85 create_subscription($baseurl, $m,
90 my ($dashboard_id, $subscription_id) = get_dash_sub_ids();
92 sub produces_dashboard_mail_ok { # {{{
94 my $subject = delete $args{Subject};
96 local $Test::Builder::Level = $Test::Builder::Level + 1;
98 RT::Dashboard::Mailer->MailDashboards(%args);
100 my @mails = RT::Test->fetch_caught_mails;
101 is @mails, 1, "got a dashboard mail";
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");
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>});
117 sub produces_no_dashboard_mail_ok { # {{{
119 my $name = delete $args{Name};
121 local $Test::Builder::Level = $Test::Builder::Level + 1;
123 RT::Dashboard::Mailer->MailDashboards(%args);
125 @mails = RT::Test->fetch_caught_mails;
129 sub delete_dashboard { # {{{
130 my $dashboard_id = shift;
131 # delete the dashboard and make sure we get exactly one subscription failure
133 my $dashboard = RT::Dashboard->new(RT::CurrentUser->new('root'));
134 my ($ok, $msg) = $dashboard->LoadById($dashboard_id);
137 ($ok, $msg) = $dashboard->Delete;
141 sub delete_subscriptions { # {{{
142 my $subscription_id = shift;
143 # delete the dashboard and make sure we get exactly one subscription failure
145 my $user = RT::User->new(RT->SystemUser);
147 for my $subscription ($user->Attributes->Named('Subscription')) {
148 $subscription->Delete;
152 my $good_time = 1290423660; # 6:01 EST on a monday
153 my $bad_time = 1290427260; # 7:01 EST on a monday
155 my $expected_subject = "[example.com] Daily Dashboard: Testing!\n";
157 produces_dashboard_mail_ok(
159 Subject => $expected_subject,
162 produces_dashboard_mail_ok(
164 Subject => $expected_subject,
167 produces_dashboard_mail_ok(
170 Subject => $expected_subject,
173 produces_dashboard_mail_ok(
176 Subject => $expected_subject,
180 produces_no_dashboard_mail_ok(
181 Name => "no dashboard mail it's a dry run",
186 produces_no_dashboard_mail_ok(
187 Name => "no dashboard mail it's a dry run",
192 produces_no_dashboard_mail_ok(
193 Name => "no mail because it's the wrong time",
197 @mails = RT::Test->fetch_caught_mails;
198 is(@mails, 0, "no mail leftover");
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;
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");
218 skip 'Weird MIME failure', 2;
219 my $body = $mail->stringify_body;
220 unlike($body, qr{My dashboards});
221 unlike($body, qr{Testing!});
224 delete_dashboard($dashboard_id);
227 RT::Dashboard::Mailer->MailDashboards(All => 1);
228 } qr/Unable to load dashboard $dashboard_id of subscription $subscription_id for user root/;
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");
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");
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;
248 create_dashboard($baseurl, $m);
249 create_subscription($baseurl, $m,
250 Frequency => 'weekly',
254 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
256 # bump $bad_time to Tuesday
257 $bad_time = $good_time + 86400;
259 produces_dashboard_mail_ok(
261 Subject => "[example.com] a Weekly b Testing! c\n",
264 produces_no_dashboard_mail_ok(
265 Name => "no mail because it's the wrong time",
269 @mails = RT::Test->fetch_caught_mails;
270 is(@mails, 0, "no mail leftover");
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;
279 delete_dashboard($dashboard_id);
280 delete_subscriptions();
282 RT::Test->clean_caught_mails;
284 RT::Test->stop_server;
286 RT->Config->Set('EmailDashboardRemove' => ());
287 RT->Config->Set('DashboardAddress' => 'root');
288 ($baseurl, $m) = RT::Test->started_ok;
290 create_dashboard($baseurl, $m);
291 create_subscription($baseurl, $m,
296 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
298 # bump $bad_time back to Sunday
299 $bad_time = $good_time - 86400;
301 produces_dashboard_mail_ok(
303 Subject => "[example.com] a Weekday b Testing! c\n",
306 produces_no_dashboard_mail_ok(
307 Name => "no mail because it's the wrong time",
311 produces_no_dashboard_mail_ok(
312 Name => "no mail because it's the wrong time",
313 Time => $bad_time - 86400, # saturday
316 produces_dashboard_mail_ok(
317 Time => $bad_time - 86400 * 2, # friday
318 Subject => "[example.com] a Weekday b Testing! c\n",
322 @mails = RT::Test->fetch_caught_mails;
323 is(@mails, 0, "no mail leftover");
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;
332 delete_dashboard($dashboard_id);
333 delete_subscriptions();
335 RT::Test->clean_caught_mails;
337 RT::Test->stop_server;
339 RT->Config->Set('EmailDashboardRemove' => ());
340 RT->Config->Set('DashboardAddress' => 'root');
341 ($baseurl, $m) = RT::Test->started_ok;
343 create_dashboard($baseurl, $m);
344 create_subscription($baseurl, $m,
345 Frequency => 'monthly',
349 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
351 $good_time = 1291201200; # dec 1
352 $bad_time = $good_time - 86400; # day before (i.e. different month)
354 produces_dashboard_mail_ok(
356 Subject => "[example.com] a Monthly b Testing! c\n",
359 produces_no_dashboard_mail_ok(
360 Name => "no mail because it's the wrong time",
365 @mails = RT::Test->fetch_caught_mails;
366 is(@mails, 0, "no mail leftover");
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;
375 delete_dashboard($dashboard_id);
376 delete_subscriptions();
378 RT::Test->clean_caught_mails;
380 RT::Test->stop_server;
382 RT->Config->Set('EmailDashboardRemove' => ());
383 RT->Config->Set('DashboardAddress' => 'root');
384 ($baseurl, $m) = RT::Test->started_ok;
386 create_dashboard($baseurl, $m);
387 create_subscription($baseurl, $m,
388 Frequency => 'never',
391 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
393 produces_no_dashboard_mail_ok(
394 Name => "mail should never get sent",