]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
b2a944875c4acfbd6f676301b98fac1f0aa9220f
[public-inbox.git] / t / feed.t
1 # Copyright (C) 2014-2019 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::Git;
9 use PublicInbox::Import;
10 use PublicInbox::Config;
11 use PublicInbox::Inbox;
12 my $have_xml_feed = eval { require XML::Feed; 1 };
13 use PublicInbox::TestCommon;
14
15 sub string_feed {
16         my $res = PublicInbox::Feed::generate($_[0]);
17         my $body = $res->[2];
18         my $str = '';
19         while (defined(my $chunk = $body->getline)) {
20                 $str .= $chunk;
21         }
22         $body->close;
23         $str;
24 }
25
26 my ($tmpdir, $for_destroy) = tmpdir();
27 my $git_dir = "$tmpdir/gittest";
28 my $ibx = PublicInbox::Inbox->new({
29         address => 'test@example',
30         name => 'testbox',
31         inboxdir => $git_dir,
32         url => [ 'http://example.com/test' ],
33         feedmax => 3,
34 });
35 my $git = $ibx->git;
36 my $im = PublicInbox::Import->new($git, $ibx->{name}, 'test@example');
37
38 {
39         is(0, system(qw(git init -q --bare), $git_dir), "git init");
40         local $ENV{GIT_DIR} = $git_dir;
41
42         foreach my $i (1..6) {
43                 my $mime = Email::MIME->new(<<EOF);
44 From: ME <me\@example.com>
45 To: U <u\@example.com>
46 Message-Id: <$i\@example.com>
47 Subject: zzz #$i
48 Date: Thu, 01 Jan 1970 00:00:00 +0000
49
50 > This is a long multi line quote so it should not be allowed to
51 > show up in its entirty in the Atom feed.  drop me
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 > I quote to much
64 > I quote to much
65
66 msg $i
67
68 > inline me here
69 > this is a short quote
70
71 keep me
72 EOF
73                 like($im->add($mime), qr/\A:\d+/, 'added');
74         }
75         $im->done;
76 }
77
78 # spam check
79 {
80         # check initial feed
81         {
82                 my $feed = string_feed({ -inbox => $ibx });
83                 SKIP: {
84                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
85                         my $p = XML::Feed->parse(\$feed);
86                         is($p->format, "Atom", "parsed atom feed");
87                         is(scalar $p->entries, 3, "parsed three entries");
88                         is($p->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::Feed missing', 2 unless $have_xml_feed;
117                         my $p = XML::Feed->parse(\$spammy_feed);
118                         is($p->format, "Atom", "parsed atom feed");
119                         is(scalar $p->entries, 3, "parsed three entries");
120                 }
121                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
122         }
123
124         # nuke spam
125         $im->remove($spam);
126         $im->done;
127
128         # spam no longer shows up
129         {
130                 my $feed = string_feed({ -inbox => $ibx });
131                 SKIP: {
132                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
133                         my $p = XML::Feed->parse(\$feed);
134                         is($p->format, "Atom", "parsed atom feed");
135                         is(scalar $p->entries, 3, "parsed three entries");
136                 }
137                 unlike($feed, qr/SPAM/, "spam gone :>");
138         }
139 }
140
141 done_testing();