]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
t/feed.t: make IPC::Run usage optional
[public-inbox.git] / t / feed.t
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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 File::Temp qw/tempdir/;
12 my $have_xml_feed = eval { require XML::Feed; 1 };
13 require 't/common.perl';
14
15 sub string_feed {
16         stream_to_string(PublicInbox::Feed::generate($_[0]));
17 }
18
19 # ensure we are compatible with existing ssoma installations which
20 # do not use fast-import.  We can probably remove this in 2018
21 my %SSOMA;
22 sub rand_use ($) {
23         return 0 if $ENV{FAST};
24         eval { require IPC::Run };
25         return 0 if $@;
26         my $cmd = $_[0];
27         my $x = $SSOMA{$cmd};
28         unless ($x) {
29                 $x = -1;
30                 foreach my $p (split(':', $ENV{PATH})) {
31                         -x "$p/$cmd" or next;
32                         $x = 1;
33                         last;
34                 }
35                 $SSOMA{$cmd} = $x;
36         }
37         return if $x < 0;
38         ($x > 0 && (int(rand(10)) % 2) == 1);
39 }
40
41 my $tmpdir = tempdir('pi-feed-XXXXXX', TMPDIR => 1, CLEANUP => 1);
42 my $git_dir = "$tmpdir/gittest";
43 my $git = PublicInbox::Git->new($git_dir);
44 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
45
46 {
47         is(0, system(qw(git init -q --bare), $git_dir), "git init");
48         local $ENV{GIT_DIR} = $git_dir;
49
50         foreach my $i (1..6) {
51                 my $mime = Email::MIME->new(<<EOF);
52 From: ME <me\@example.com>
53 To: U <u\@example.com>
54 Message-Id: <$i\@example.com>
55 Subject: zzz #$i
56 Date: Thu, 01 Jan 1970 00:00:00 +0000
57
58 > This is a long multi line quote so it should not be allowed to
59 > show up in its entirty in the Atom feed.  drop me
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 > I quote to much
67 > I quote to much
68 > I quote to much
69 > I quote to much
70 > I quote to much
71 > I quote to much
72 > I quote to much
73
74 msg $i
75
76 > inline me here
77 > this is a short quote
78
79 keep me
80 EOF
81                 if (rand_use('ssoma-mda')) {
82                         $im->done;
83                         my $str = $mime->as_string;
84                         IPC::Run::run(['ssoma-mda', $git_dir], \$str) or
85                                 die "mda failed: $?\n";
86                 } else {
87                         like($im->add($mime), qr/\A:\d+/, 'added');
88                 }
89         }
90         $im->done;
91 }
92
93 # spam check
94 {
95         # check initial feed
96         {
97                 my $feed = string_feed({
98                         git_dir => $git_dir,
99                         max => 3
100                 });
101                 SKIP: {
102                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
103                         my $p = XML::Feed->parse(\$feed);
104                         is($p->format, "Atom", "parsed atom feed");
105                         is(scalar $p->entries, 3, "parsed three entries");
106                         is($p->id, 'mailto:public-inbox@example.com',
107                                 "id is set to default");
108                 }
109
110                 like($feed, qr/drop me/, "long quoted text kept");
111                 like($feed, qr/inline me here/, "short quoted text kept");
112                 like($feed, qr/keep me/, "unquoted text saved");
113         }
114
115         # add a new spam message
116         my $spam;
117         {
118                 $spam = Email::MIME->new(<<EOF);
119 From: SPAMMER <spammer\@example.com>
120 To: U <u\@example.com>
121 Message-Id: <this-is-spam\@example.com>
122 Subject: SPAM!!!!!!!!
123 Date: Thu, 01 Jan 1970 00:00:00 +0000
124
125 EOF
126                 if (rand_use('ssoma-mda')) {
127                         my $str = $spam->as_string;
128                         IPC::Run::run(['ssoma-mda', $git_dir], \$str) or
129                                 die "mda failed: $?\n";
130                 } else {
131                         $im->add($spam);
132                         $im->done;
133                 }
134         }
135
136         # check spam shows up
137         {
138                 my $spammy_feed = string_feed({
139                         git_dir => $git_dir,
140                         max => 3
141                 });
142                 SKIP: {
143                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
144                         my $p = XML::Feed->parse(\$spammy_feed);
145                         is($p->format, "Atom", "parsed atom feed");
146                         is(scalar $p->entries, 3, "parsed three entries");
147                 }
148                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
149         }
150
151         # nuke spam
152         if (rand_use('ssoma-rm')) {
153                 my $spam_str = $spam->as_string;
154                 IPC::Run::run(["ssoma-rm", $git_dir], \$spam_str) or
155                                 die "ssoma-rm failed: $?\n";
156         } else {
157                 $im->remove($spam);
158                 $im->done;
159         }
160
161         # spam no longer shows up
162         {
163                 my $feed = string_feed({
164                         git_dir => $git_dir,
165                         max => 3
166                 });
167                 SKIP: {
168                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
169                         my $p = XML::Feed->parse(\$feed);
170                         is($p->format, "Atom", "parsed atom feed");
171                         is(scalar $p->entries, 3, "parsed three entries");
172                 }
173                 unlike($feed, qr/SPAM/, "spam gone :>");
174         }
175 }
176
177 # check pi_config
178 {
179         foreach my $addr (('a@example.com'), ['a@example.com','b@localhost']) {
180                 my $feed = string_feed({
181                         git_dir => $git_dir,
182                         max => 3,
183                         inbox => 'asdf',
184                         pi_config => bless({
185                                 'publicinbox.asdf.address' => $addr,
186                         }, 'PublicInbox::Config'),
187                 });
188                 SKIP: {
189                         skip 'XML::Feed missing', 3 unless $have_xml_feed;
190                         my $p = XML::Feed->parse(\$feed);
191                         is($p->id, 'mailto:a@example.com',
192                                 "ID is set correctly");
193                         is($p->format, "Atom", "parsed atom feed");
194                         is(scalar $p->entries, 3, "parsed three entries");
195                 }
196         }
197 }
198
199 done_testing();