]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
treewide: "require" + "use" cleanup and docs
[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::Import;
9 use PublicInbox::Inbox;
10 my $have_xml_feed = eval { require XML::Feed; 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         is(0, system(qw(git init -q --bare), $git_dir), "git init");
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::Feed missing', 2 unless $have_xml_feed;
83                         my $p = XML::Feed->parse(\$feed);
84                         is($p->format, "Atom", "parsed atom feed");
85                         is(scalar $p->entries, 3, "parsed three entries");
86                         is($p->id, 'mailto:test@example',
87                                 "id is set to default");
88                 }
89
90                 like($feed, qr/drop me/, "long quoted text kept");
91                 like($feed, qr/inline me here/, "short quoted text kept");
92                 like($feed, qr/keep me/, "unquoted text saved");
93         }
94
95         # add a new spam message
96         my $spam;
97         {
98                 $spam = Email::MIME->new(<<EOF);
99 From: SPAMMER <spammer\@example.com>
100 To: U <u\@example.com>
101 Message-Id: <this-is-spam\@example.com>
102 Subject: SPAM!!!!!!!!
103 Date: Thu, 01 Jan 1970 00:00:00 +0000
104
105 EOF
106                 $im->add($spam);
107                 $im->done;
108         }
109
110         # check spam shows up
111         {
112                 my $spammy_feed = string_feed({ -inbox => $ibx });
113                 SKIP: {
114                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
115                         my $p = XML::Feed->parse(\$spammy_feed);
116                         is($p->format, "Atom", "parsed atom feed");
117                         is(scalar $p->entries, 3, "parsed three entries");
118                 }
119                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
120         }
121
122         # nuke spam
123         $im->remove($spam);
124         $im->done;
125
126         # spam no longer shows up
127         {
128                 my $feed = string_feed({ -inbox => $ibx });
129                 SKIP: {
130                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
131                         my $p = XML::Feed->parse(\$feed);
132                         is($p->format, "Atom", "parsed atom feed");
133                         is(scalar $p->entries, 3, "parsed three entries");
134                 }
135                 unlike($feed, qr/SPAM/, "spam gone :>");
136         }
137 }
138
139 done_testing();