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