5 use RT::Test tests => 181;
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 my $good_time = 1290423660; # 6:01 EST on a monday
142 my $bad_time = 1290427260; # 7:01 EST on a monday
144 my $expected_subject = "[example.com] Daily Dashboard: Testing!\n";
146 produces_dashboard_mail_ok(
148 Subject => $expected_subject,
151 produces_dashboard_mail_ok(
153 Subject => $expected_subject,
156 produces_dashboard_mail_ok(
159 Subject => $expected_subject,
162 produces_dashboard_mail_ok(
165 Subject => $expected_subject,
169 produces_no_dashboard_mail_ok(
170 Name => "no dashboard mail it's a dry run",
175 produces_no_dashboard_mail_ok(
176 Name => "no dashboard mail it's a dry run",
181 produces_no_dashboard_mail_ok(
182 Name => "no mail because it's the wrong time",
186 @mails = RT::Test->fetch_caught_mails;
187 is(@mails, 0, "no mail leftover");
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;
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");
207 skip 'Weird MIME failure', 2;
208 my $body = $mail->stringify_body;
209 unlike($body, qr{My dashboards});
210 unlike($body, qr{Testing!});
213 delete_dashboard($dashboard_id);
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");
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;
225 create_dashboard($baseurl, $m);
226 create_subscription($baseurl, $m,
227 Frequency => 'weekly',
231 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
233 # bump $bad_time to Tuesday
234 $bad_time = $good_time + 86400;
236 produces_dashboard_mail_ok(
238 Subject => "[example.com] a Weekly b Testing! c\n",
241 produces_no_dashboard_mail_ok(
242 Name => "no mail because it's the wrong time",
246 @mails = RT::Test->fetch_caught_mails;
247 is(@mails, 0, "no mail leftover");
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;
256 delete_dashboard($dashboard_id);
258 RT::Test->clean_caught_mails;
260 RT::Test->stop_server;
262 RT->Config->Set('EmailDashboardRemove' => ());
263 RT->Config->Set('DashboardAddress' => 'root');
264 ($baseurl, $m) = RT::Test->started_ok;
266 create_dashboard($baseurl, $m);
267 create_subscription($baseurl, $m,
272 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
274 # bump $bad_time back to Sunday
275 $bad_time = $good_time - 86400;
277 produces_dashboard_mail_ok(
279 Subject => "[example.com] a Weekday b Testing! c\n",
282 produces_no_dashboard_mail_ok(
283 Name => "no mail because it's the wrong time",
287 produces_no_dashboard_mail_ok(
288 Name => "no mail because it's the wrong time",
289 Time => $bad_time - 86400, # saturday
292 produces_dashboard_mail_ok(
293 Time => $bad_time - 86400 * 2, # friday
294 Subject => "[example.com] a Weekday b Testing! c\n",
298 @mails = RT::Test->fetch_caught_mails;
299 is(@mails, 0, "no mail leftover");
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;
308 delete_dashboard($dashboard_id);
310 RT::Test->clean_caught_mails;
312 RT::Test->stop_server;
314 RT->Config->Set('EmailDashboardRemove' => ());
315 RT->Config->Set('DashboardAddress' => 'root');
316 ($baseurl, $m) = RT::Test->started_ok;
318 create_dashboard($baseurl, $m);
319 create_subscription($baseurl, $m,
320 Frequency => 'monthly',
324 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
326 $good_time = 1291201200; # dec 1
327 $bad_time = $good_time - 86400; # day before (i.e. different month)
329 produces_dashboard_mail_ok(
331 Subject => "[example.com] a Monthly b Testing! c\n",
334 produces_no_dashboard_mail_ok(
335 Name => "no mail because it's the wrong time",
340 @mails = RT::Test->fetch_caught_mails;
341 is(@mails, 0, "no mail leftover");
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;
350 delete_dashboard($dashboard_id);
352 RT::Test->clean_caught_mails;
354 RT::Test->stop_server;
356 RT->Config->Set('EmailDashboardRemove' => ());
357 RT->Config->Set('DashboardAddress' => 'root');
358 ($baseurl, $m) = RT::Test->started_ok;
360 create_dashboard($baseurl, $m);
361 create_subscription($baseurl, $m,
362 Frequency => 'never',
365 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
367 produces_no_dashboard_mail_ok(
368 Name => "mail should never get sent",