]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
extindex: guard against false mismatch unrefs
[public-inbox.git] / t / feed.t
1 #!perl -w
2 # Copyright (C) 2014-2021 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();
12
13 sub string_feed {
14         my $res = PublicInbox::Feed::generate($_[0]);
15         my $body = $res->[2];
16         my $str = '';
17         while (defined(my $chunk = $body->getline)) {
18                 $str .= $chunk;
19         }
20         $body->close;
21         $str;
22 }
23
24 my $git_dir = "$tmpdir/gittest";
25 my $ibx = create_inbox 'v1', tmpdir => $git_dir, sub {
26         my ($im, $ibx) = @_;
27         foreach my $i (1..6) {
28                 $im->add(PublicInbox::Eml->new(<<EOF)) or BAIL_OUT;
29 From: ME <me\@example.com>
30 To: U <u\@example.com>
31 Message-Id: <$i\@example.com>
32 Subject: zzz #$i
33 Date: Thu, 01 Jan 1970 00:00:00 +0000
34
35 > drop me
36
37 msg $i
38
39 > inline me here
40 > this is a short quote
41
42 keep me
43 EOF
44         }
45 };
46
47 $ibx->{url} = [ 'http://example.com/test' ];
48 $ibx->{feedmax} = 3;
49 my $im = $ibx->importer(0);
50
51 # spam check
52 {
53         # check initial feed
54         {
55                 my $feed = string_feed({ ibx => $ibx });
56                 SKIP: {
57                         skip 'XML::TreePP missing', 3 unless $have_xml_treepp;
58                         my $t = XML::TreePP->new->parse($feed);
59                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
60                                 'looks like an an Atom feed');
61                         is(scalar @{$t->{feed}->{entry}}, 3,
62                                 'parsed three entries');
63                         is($t->{feed}->{id}, 'mailto:v1@example.com',
64                                 'id is set to default');
65                 }
66
67                 like($feed, qr/drop me/, "long quoted text kept");
68                 like($feed, qr/inline me here/, "short quoted text kept");
69                 like($feed, qr/keep me/, "unquoted text saved");
70         }
71
72         # add a new spam message
73         my $spam;
74         {
75                 $spam = PublicInbox::Eml->new(<<EOF);
76 From: SPAMMER <spammer\@example.com>
77 To: U <u\@example.com>
78 Message-Id: <this-is-spam\@example.com>
79 Subject: SPAM!!!!!!!!
80 Date: Thu, 01 Jan 1970 00:00:00 +0000
81
82 EOF
83                 $im->add($spam);
84                 $im->done;
85         }
86
87         # check spam shows up
88         {
89                 my $spammy_feed = string_feed({ ibx => $ibx });
90                 SKIP: {
91                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
92                         my $t = XML::TreePP->new->parse($spammy_feed);
93                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
94                                 'looks like an an Atom feed');
95                         is(scalar @{$t->{feed}->{entry}}, 3,
96                                 'parsed three entries');
97                 }
98                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
99         }
100
101         # nuke spam
102         $im->remove($spam);
103         $im->done;
104
105         # spam no longer shows up
106         {
107                 my $feed = string_feed({ ibx => $ibx });
108                 SKIP: {
109                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
110                         my $t = XML::TreePP->new->parse($feed);
111                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
112                                 'looks like an an Atom feed');
113                         is(scalar @{$t->{feed}->{entry}}, 3,
114                                 'parsed three entries');
115                 }
116                 unlike($feed, qr/SPAM/, "spam gone :>");
117         }
118 }
119
120 done_testing;