4 use RT::Test tests => undef;
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('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");
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>});
114 sub produces_no_dashboard_mail_ok { # {{{
116 my $name = delete $args{Name};
118 local $Test::Builder::Level = $Test::Builder::Level + 1;
120 RT::Dashboard::Mailer->MailDashboards(%args);
122 @mails = RT::Test->fetch_caught_mails;
126 sub delete_dashboard { # {{{
127 my $dashboard_id = shift;
128 # delete the dashboard and make sure we get exactly one subscription failure
130 my $dashboard = RT::Dashboard->new(RT::CurrentUser->new('root'));
131 my ($ok, $msg) = $dashboard->LoadById($dashboard_id);
134 ($ok, $msg) = $dashboard->Delete;
138 my $good_time = 1290423660; # 6:01 EST on a monday
139 my $bad_time = 1290427260; # 7:01 EST on a monday
141 my $expected_subject = "[example.com] Daily Dashboard: Testing!\n";
143 produces_dashboard_mail_ok(
145 Subject => $expected_subject,
148 produces_dashboard_mail_ok(
150 Subject => $expected_subject,
153 produces_dashboard_mail_ok(
156 Subject => $expected_subject,
159 produces_dashboard_mail_ok(
162 Subject => $expected_subject,
166 produces_no_dashboard_mail_ok(
167 Name => "no dashboard mail it's a dry run",
172 produces_no_dashboard_mail_ok(
173 Name => "no dashboard mail it's a dry run",
178 produces_no_dashboard_mail_ok(
179 Name => "no mail because it's the wrong time",
183 @mails = RT::Test->fetch_caught_mails;
184 is(@mails, 0, "no mail leftover");
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;
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");
203 my $body = $mail->bodyhandle->as_string;
204 unlike($body, qr{My dashboards});
205 unlike($body, qr{Testing!});
207 delete_dashboard($dashboard_id);
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");
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;
219 create_dashboard($baseurl, $m);
220 create_subscription($baseurl, $m,
221 Frequency => 'weekly',
225 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
227 # bump $bad_time to Tuesday
228 $bad_time = $good_time + 86400;
230 produces_dashboard_mail_ok(
232 Subject => "[example.com] a Weekly b Testing! c\n",
235 produces_no_dashboard_mail_ok(
236 Name => "no mail because it's the wrong time",
240 @mails = RT::Test->fetch_caught_mails;
241 is(@mails, 0, "no mail leftover");
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;
250 delete_dashboard($dashboard_id);
252 RT::Test->clean_caught_mails;
254 RT::Test->stop_server;
256 RT->Config->Set('EmailDashboardRemove' => ());
257 RT->Config->Set('DashboardAddress' => 'root');
258 ($baseurl, $m) = RT::Test->started_ok;
260 create_dashboard($baseurl, $m);
261 create_subscription($baseurl, $m,
266 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
268 # bump $bad_time back to Sunday
269 $bad_time = $good_time - 86400;
271 produces_dashboard_mail_ok(
273 Subject => "[example.com] a Weekday b Testing! c\n",
276 produces_no_dashboard_mail_ok(
277 Name => "no mail because it's the wrong time",
281 produces_no_dashboard_mail_ok(
282 Name => "no mail because it's the wrong time",
283 Time => $bad_time - 86400, # saturday
286 produces_dashboard_mail_ok(
287 Time => $bad_time - 86400 * 2, # friday
288 Subject => "[example.com] a Weekday b Testing! c\n",
292 @mails = RT::Test->fetch_caught_mails;
293 is(@mails, 0, "no mail leftover");
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;
302 delete_dashboard($dashboard_id);
304 RT::Test->clean_caught_mails;
306 RT::Test->stop_server;
308 RT->Config->Set('EmailDashboardRemove' => ());
309 RT->Config->Set('DashboardAddress' => 'root');
310 ($baseurl, $m) = RT::Test->started_ok;
312 create_dashboard($baseurl, $m);
313 create_subscription($baseurl, $m,
314 Frequency => 'monthly',
318 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
320 $good_time = 1291201200; # dec 1
321 $bad_time = $good_time - 86400; # day before (i.e. different month)
323 produces_dashboard_mail_ok(
325 Subject => "[example.com] a Monthly b Testing! c\n",
328 produces_no_dashboard_mail_ok(
329 Name => "no mail because it's the wrong time",
334 @mails = RT::Test->fetch_caught_mails;
335 is(@mails, 0, "no mail leftover");
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;
344 delete_dashboard($dashboard_id);
346 RT::Test->clean_caught_mails;
348 RT::Test->stop_server;
350 RT->Config->Set('EmailDashboardRemove' => ());
351 RT->Config->Set('DashboardAddress' => 'root');
352 ($baseurl, $m) = RT::Test->started_ok;
354 create_dashboard($baseurl, $m);
355 create_subscription($baseurl, $m,
356 Frequency => 'never',
359 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
361 produces_no_dashboard_mail_ok(
362 Name => "mail should never get sent",