]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
t/common: inline stream_to_string into t/feed.t
[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 use File::Temp qw/tempdir/;
13 my $have_xml_feed = eval { require XML::Feed; 1 };
14 require './t/common.perl';
15
16 sub string_feed {
17         my $res = PublicInbox::Feed::generate($_[0]);
18         my $body = $res->[2];
19         my $str = '';
20         while (defined(my $chunk = $body->getline)) {
21                 $str .= $chunk;
22         }
23         $body->close;
24         $str;
25 }
26
27 my $tmpdir = tempdir('pi-feed-XXXXXX', TMPDIR => 1, CLEANUP => 1);
28 my $git_dir = "$tmpdir/gittest";
29 my $ibx = PublicInbox::Inbox->new({
30         address => 'test@example',
31         name => 'testbox',
32         inboxdir => $git_dir,
33         url => 'http://example.com/test',
34         feedmax => 3,
35 });
36 my $git = $ibx->git;
37 my $im = PublicInbox::Import->new($git, $ibx->{name}, 'test@example');
38
39 {
40         is(0, system(qw(git init -q --bare), $git_dir), "git init");
41         local $ENV{GIT_DIR} = $git_dir;
42
43         foreach my $i (1..6) {
44                 my $mime = Email::MIME->new(<<EOF);
45 From: ME <me\@example.com>
46 To: U <u\@example.com>
47 Message-Id: <$i\@example.com>
48 Subject: zzz #$i
49 Date: Thu, 01 Jan 1970 00:00:00 +0000
50
51 > This is a long multi line quote so it should not be allowed to
52 > show up in its entirty in the Atom feed.  drop me
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 > I quote to much
66
67 msg $i
68
69 > inline me here
70 > this is a short quote
71
72 keep me
73 EOF
74                 like($im->add($mime), qr/\A:\d+/, 'added');
75         }
76         $im->done;
77 }
78
79 # spam check
80 {
81         # check initial feed
82         {
83                 my $feed = string_feed({ -inbox => $ibx });
84                 SKIP: {
85                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
86                         my $p = XML::Feed->parse(\$feed);
87                         is($p->format, "Atom", "parsed atom feed");
88                         is(scalar $p->entries, 3, "parsed three entries");
89                         is($p->id, 'mailto:test@example',
90                                 "id is set to default");
91                 }
92
93                 like($feed, qr/drop me/, "long quoted text kept");
94                 like($feed, qr/inline me here/, "short quoted text kept");
95                 like($feed, qr/keep me/, "unquoted text saved");
96         }
97
98         # add a new spam message
99         my $spam;
100         {
101                 $spam = Email::MIME->new(<<EOF);
102 From: SPAMMER <spammer\@example.com>
103 To: U <u\@example.com>
104 Message-Id: <this-is-spam\@example.com>
105 Subject: SPAM!!!!!!!!
106 Date: Thu, 01 Jan 1970 00:00:00 +0000
107
108 EOF
109                 $im->add($spam);
110                 $im->done;
111         }
112
113         # check spam shows up
114         {
115                 my $spammy_feed = string_feed({ -inbox => $ibx });
116                 SKIP: {
117                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
118                         my $p = XML::Feed->parse(\$spammy_feed);
119                         is($p->format, "Atom", "parsed atom feed");
120                         is(scalar $p->entries, 3, "parsed three entries");
121                 }
122                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
123         }
124
125         # nuke spam
126         $im->remove($spam);
127         $im->done;
128
129         # spam no longer shows up
130         {
131                 my $feed = string_feed({ -inbox => $ibx });
132                 SKIP: {
133                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
134                         my $p = XML::Feed->parse(\$feed);
135                         is($p->format, "Atom", "parsed atom feed");
136                         is(scalar $p->entries, 3, "parsed three entries");
137                 }
138                 unlike($feed, qr/SPAM/, "spam gone :>");
139         }
140 }
141
142 done_testing();