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