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