]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
ffd5ca7e71f86dc9a37ba5d1865dc38e11619f5a
[public-inbox.git] / t / feed.t
1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use PublicInbox::Feed;
8 use PublicInbox::Import;
9 use PublicInbox::Inbox;
10 my $have_xml_treepp = eval { require XML::TreePP; 1 };
11 use PublicInbox::TestCommon;
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 ($tmpdir, $for_destroy) = tmpdir();
25 my $git_dir = "$tmpdir/gittest";
26 my $ibx = PublicInbox::Inbox->new({
27         address => 'test@example',
28         name => 'testbox',
29         inboxdir => $git_dir,
30         url => [ 'http://example.com/test' ],
31         feedmax => 3,
32 });
33 my $git = $ibx->git;
34 my $im = PublicInbox::Import->new($git, $ibx->{name}, 'test@example');
35
36 {
37         $im->init_bare;
38         local $ENV{GIT_DIR} = $git_dir;
39
40         foreach my $i (1..6) {
41                 my $mime = Email::MIME->new(<<EOF);
42 From: ME <me\@example.com>
43 To: U <u\@example.com>
44 Message-Id: <$i\@example.com>
45 Subject: zzz #$i
46 Date: Thu, 01 Jan 1970 00:00:00 +0000
47
48 > This is a long multi line quote so it should not be allowed to
49 > show up in its entirty in the Atom feed.  drop me
50 > I quote to much
51 > I quote to much
52 > I quote to much
53 > I quote to much
54 > I quote to much
55 > I quote to much
56 > I quote to much
57 > I quote to much
58 > I quote to much
59 > I quote to much
60 > I quote to much
61 > I quote to much
62 > I quote to much
63
64 msg $i
65
66 > inline me here
67 > this is a short quote
68
69 keep me
70 EOF
71                 like($im->add($mime), qr/\A:\d+/, 'added');
72         }
73         $im->done;
74 }
75
76 # spam check
77 {
78         # check initial feed
79         {
80                 my $feed = string_feed({ -inbox => $ibx });
81                 SKIP: {
82                         skip 'XML::TreePP missing', 3 unless $have_xml_treepp;
83                         my $t = XML::TreePP->new->parse($feed);
84                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
85                                 'looks like an an Atom feed');
86                         is(scalar @{$t->{feed}->{entry}}, 3,
87                                 'parsed three entries');
88                         is($t->{feed}->{id}, 'mailto:test@example',
89                                 'id is set to default');
90                 }
91
92                 like($feed, qr/drop me/, "long quoted text kept");
93                 like($feed, qr/inline me here/, "short quoted text kept");
94                 like($feed, qr/keep me/, "unquoted text saved");
95         }
96
97         # add a new spam message
98         my $spam;
99         {
100                 $spam = Email::MIME->new(<<EOF);
101 From: SPAMMER <spammer\@example.com>
102 To: U <u\@example.com>
103 Message-Id: <this-is-spam\@example.com>
104 Subject: SPAM!!!!!!!!
105 Date: Thu, 01 Jan 1970 00:00:00 +0000
106
107 EOF
108                 $im->add($spam);
109                 $im->done;
110         }
111
112         # check spam shows up
113         {
114                 my $spammy_feed = string_feed({ -inbox => $ibx });
115                 SKIP: {
116                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
117                         my $t = XML::TreePP->new->parse($spammy_feed);
118                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
119                                 'looks like an an Atom feed');
120                         is(scalar @{$t->{feed}->{entry}}, 3,
121                                 'parsed three entries');
122                 }
123                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
124         }
125
126         # nuke spam
127         $im->remove($spam);
128         $im->done;
129
130         # spam no longer shows up
131         {
132                 my $feed = string_feed({ -inbox => $ibx });
133                 SKIP: {
134                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
135                         my $t = XML::TreePP->new->parse($feed);
136                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
137                                 'looks like an an Atom feed');
138                         is(scalar @{$t->{feed}->{entry}}, 3,
139                                 'parsed three entries');
140                 }
141                 unlike($feed, qr/SPAM/, "spam gone :>");
142         }
143 }
144
145 done_testing();