4 use RT::Test tests => 181;
6 use RT::Dashboard::Mailer;
8 my ($baseurl, $m) = RT::Test->started_ok;
9 ok($m->login, 'logged in');
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!');
20 $m->follow_link_ok({text => 'Content'});
21 $m->title_is('Modify the content of dashboard Testing!');
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');
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>});
39 sub create_subscription {
40 my ($baseurl, $m, %fields) = @_;
41 local $Test::Builder::Level = $Test::Builder::Level + 1;
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!");
52 sub get_dash_sub_ids {
53 my $user = RT::User->new(RT->SystemUser);
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');
63 return ($dashboard_id, $subscription_id);
66 # first, create and populate a dashboard
67 create_dashboard($baseurl, $m);
71 # without a subscription..
72 RT::Dashboard::Mailer->MailDashboards();
74 my @mails = RT::Test->fetch_caught_mails;
75 is @mails, 0, 'no mail yet';
77 RT::Dashboard::Mailer->MailDashboards(
81 @mails = RT::Test->fetch_caught_mails;
82 is @mails, 0, "no mail yet since there's no subscription";
84 create_subscription($baseurl, $m,
89 my ($dashboard_id, $subscription_id) = get_dash_sub_ids();
91 sub produces_dashboard_mail_ok { # {{{
93 my $subject = delete $args{Subject};
95 local $Test::Builder::Level = $Test::Builder::Level + 1;
97 RT::Dashboard::Mailer->MailDashboards(%args);
99 my @mails = RT::Test->fetch_caught_mails;
100 is @mails, 1, "got a dashboard mail";
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");
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>});
116 sub produces_no_dashboard_mail_ok { # {{{
118 my $name = delete $args{Name};
120 local $Test::Builder::Level = $Test::Builder::Level + 1;
122 RT::Dashboard::Mailer->MailDashboards(%args);
124 @mails = RT::Test->fetch_caught_mails;
128 sub delete_dashboard { # {{{
129 my $dashboard_id = shift;
130 # delete the dashboard and make sure we get exactly one subscription failure
132 my $dashboard = RT::Dashboard->new(RT::CurrentUser->new('root'));
133 my ($ok, $msg) = $dashboard->LoadById($dashboard_id);
136 ($ok, $msg) = $dashboard->Delete;
140 my $good_time = 1290423660; # 6:01 EST on a monday
141 my $bad_time = 1290427260; # 7:01 EST on a monday
143 my $expected_subject = "[example.com] Daily Dashboard: Testing!\n";
145 produces_dashboard_mail_ok(
147 Subject => $expected_subject,
150 produces_dashboard_mail_ok(
152 Subject => $expected_subject,
155 produces_dashboard_mail_ok(
158 Subject => $expected_subject,
161 produces_dashboard_mail_ok(
164 Subject => $expected_subject,
168 produces_no_dashboard_mail_ok(
169 Name => "no dashboard mail it's a dry run",
174 produces_no_dashboard_mail_ok(
175 Name => "no dashboard mail it's a dry run",
180 produces_no_dashboard_mail_ok(
181 Name => "no mail because it's the wrong time",
185 @mails = RT::Test->fetch_caught_mails;
186 is(@mails, 0, "no mail leftover");
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;
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");
206 skip 'Weird MIME failure', 2;
207 my $body = $mail->stringify_body;
208 unlike($body, qr{My dashboards});
209 unlike($body, qr{Testing!});
212 delete_dashboard($dashboard_id);
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");
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;
224 create_dashboard($baseurl, $m);
225 create_subscription($baseurl, $m,
226 Frequency => 'weekly',
230 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
232 # bump $bad_time to Tuesday
233 $bad_time = $good_time + 86400;
235 produces_dashboard_mail_ok(
237 Subject => "[example.com] a Weekly b Testing! c\n",
240 produces_no_dashboard_mail_ok(
241 Name => "no mail because it's the wrong time",
245 @mails = RT::Test->fetch_caught_mails;
246 is(@mails, 0, "no mail leftover");
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;
255 delete_dashboard($dashboard_id);
257 RT::Test->clean_caught_mails;
259 RT::Test->stop_server;
261 RT->Config->Set('EmailDashboardRemove' => ());
262 RT->Config->Set('DashboardAddress' => 'root');
263 ($baseurl, $m) = RT::Test->started_ok;
265 create_dashboard($baseurl, $m);
266 create_subscription($baseurl, $m,
271 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
273 # bump $bad_time back to Sunday
274 $bad_time = $good_time - 86400;
276 produces_dashboard_mail_ok(
278 Subject => "[example.com] a Weekday b Testing! c\n",
281 produces_no_dashboard_mail_ok(
282 Name => "no mail because it's the wrong time",
286 produces_no_dashboard_mail_ok(
287 Name => "no mail because it's the wrong time",
288 Time => $bad_time - 86400, # saturday
291 produces_dashboard_mail_ok(
292 Time => $bad_time - 86400 * 2, # friday
293 Subject => "[example.com] a Weekday b Testing! c\n",
297 @mails = RT::Test->fetch_caught_mails;
298 is(@mails, 0, "no mail leftover");
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;
307 delete_dashboard($dashboard_id);
309 RT::Test->clean_caught_mails;
311 RT::Test->stop_server;
313 RT->Config->Set('EmailDashboardRemove' => ());
314 RT->Config->Set('DashboardAddress' => 'root');
315 ($baseurl, $m) = RT::Test->started_ok;
317 create_dashboard($baseurl, $m);
318 create_subscription($baseurl, $m,
319 Frequency => 'monthly',
323 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
325 $good_time = 1291201200; # dec 1
326 $bad_time = $good_time - 86400; # day before (i.e. different month)
328 produces_dashboard_mail_ok(
330 Subject => "[example.com] a Monthly b Testing! c\n",
333 produces_no_dashboard_mail_ok(
334 Name => "no mail because it's the wrong time",
339 @mails = RT::Test->fetch_caught_mails;
340 is(@mails, 0, "no mail leftover");
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;
349 delete_dashboard($dashboard_id);
351 RT::Test->clean_caught_mails;
353 RT::Test->stop_server;
355 RT->Config->Set('EmailDashboardRemove' => ());
356 RT->Config->Set('DashboardAddress' => 'root');
357 ($baseurl, $m) = RT::Test->started_ok;
359 create_dashboard($baseurl, $m);
360 create_subscription($baseurl, $m,
361 Frequency => 'never',
364 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
366 produces_no_dashboard_mail_ok(
367 Name => "mail should never get sent",