viewvcs: handle exceptions in on_destroy cb
[public-inbox.git] / t / feed.t
blob8e80b531e489b863a85a4159a15c33dd5c8f1858
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Eml;
8 use PublicInbox::Feed;
9 use PublicInbox::Inbox;
10 my $have_xml_treepp = eval { require XML::TreePP; 1 };
11 my ($tmpdir, $for_destroy) = tmpdir();
13 my $git_dir = "$tmpdir/gittest";
14 my $ibx = create_inbox 'v1', tmpdir => $git_dir, sub {
15         my ($im, $ibx) = @_;
16         foreach my $i (1..6) {
17                 $im->add(PublicInbox::Eml->new(<<EOF)) or BAIL_OUT;
18 From: ME <me\@example.com>
19 To: U <u\@example.com>
20 Message-Id: <$i\@example.com>
21 Subject: zzz #$i
22 Date: Thu, 01 Jan 1970 00:00:00 +0000
24 > drop me
26 msg $i
28 > inline me here
29 > this is a short quote
31 keep me
32 EOF
33         }
36 $ibx->{url} = [ 'http://example.com/test' ];
37 $ibx->{feedmax} = 3;
38 my $ctx = {
39         ibx => $ibx,
40         env => { HTTP_HOST => 'example.com', 'psgi.url_scheme' => 'http' },
42 my $string_feed = sub {
43         my $res = PublicInbox::Feed::generate($ctx);
44         my $body = $res->[2];
45         my $str = '';
46         while (defined(my $chunk = $body->getline)) {
47                 $str .= $chunk;
48         }
49         $body->close;
50         $str;
53 my $im = $ibx->importer(0);
55 # spam check
57         # check initial feed
58         {
59                 my $feed = $string_feed->();
60                 SKIP: {
61                         skip 'XML::TreePP missing', 3 unless $have_xml_treepp;
62                         my $t = XML::TreePP->new->parse($feed);
63                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
64                                 'looks like an an Atom feed');
65                         is(scalar @{$t->{feed}->{entry}}, 3,
66                                 'parsed three entries');
67                         is($t->{feed}->{id}, 'mailto:v1@example.com',
68                                 'id is set to default');
69                 }
71                 like($feed, qr/drop me/, "long quoted text kept");
72                 like($feed, qr/inline me here/, "short quoted text kept");
73                 like($feed, qr/keep me/, "unquoted text saved");
74         }
76         # add a new spam message
77         my $spam;
78         {
79                 $spam = PublicInbox::Eml->new(<<EOF);
80 From: SPAMMER <spammer\@example.com>
81 To: U <u\@example.com>
82 Message-Id: <this-is-spam\@example.com>
83 Subject: SPAM!!!!!!!!
84 Date: Thu, 01 Jan 1970 00:00:00 +0000
86 EOF
87                 $im->add($spam);
88                 $im->done;
89         }
91         # check spam shows up
92         {
93                 my $spammy_feed = $string_feed->();
94                 SKIP: {
95                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
96                         my $t = XML::TreePP->new->parse($spammy_feed);
97                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
98                                 'looks like an an Atom feed');
99                         is(scalar @{$t->{feed}->{entry}}, 3,
100                                 'parsed three entries');
101                 }
102                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
103         }
105         # nuke spam
106         $im->remove($spam);
107         $im->done;
109         # spam no longer shows up
110         {
111                 my $feed = $string_feed->();
112                 SKIP: {
113                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
114                         my $t = XML::TreePP->new->parse($feed);
115                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
116                                 'looks like an an Atom feed');
117                         is(scalar @{$t->{feed}->{entry}}, 3,
118                                 'parsed three entries');
119                 }
120                 unlike($feed, qr/SPAM/, "spam gone :>");
121         }
124 done_testing;