]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
b60273ed53b313112f9ad175659a2936c3c454dd
[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 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 # 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         eval { require IPC::Run };
26         return 0 if $@;
27         my $cmd = $_[0];
28         my $x = $SSOMA{$cmd};
29         unless ($x) {
30                 $x = -1;
31                 foreach my $p (split(':', $ENV{PATH})) {
32                         -x "$p/$cmd" or next;
33                         $x = 1;
34                         last;
35                 }
36                 $SSOMA{$cmd} = $x;
37         }
38         return if $x < 0;
39         ($x > 0 && (int(rand(10)) % 2) == 1);
40 }
41
42 my $tmpdir = tempdir('pi-feed-XXXXXX', TMPDIR => 1, CLEANUP => 1);
43 my $git_dir = "$tmpdir/gittest";
44 my $ibx = PublicInbox::Inbox->new({
45         address => 'test@example',
46         name => 'testbox',
47         mainrepo => $git_dir,
48         url => 'http://example.com/test',
49         feedmax => 3,
50 });
51 my $git = $ibx->git;
52 my $im = PublicInbox::Import->new($git, $ibx->{name}, 'test@example');
53
54 {
55         is(0, system(qw(git init -q --bare), $git_dir), "git init");
56         local $ENV{GIT_DIR} = $git_dir;
57
58         foreach my $i (1..6) {
59                 my $mime = Email::MIME->new(<<EOF);
60 From: ME <me\@example.com>
61 To: U <u\@example.com>
62 Message-Id: <$i\@example.com>
63 Subject: zzz #$i
64 Date: Thu, 01 Jan 1970 00:00:00 +0000
65
66 > This is a long multi line quote so it should not be allowed to
67 > show up in its entirty in the Atom feed.  drop me
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 > I quote to much
74 > I quote to much
75 > I quote to much
76 > I quote to much
77 > I quote to much
78 > I quote to much
79 > I quote to much
80 > I quote to much
81
82 msg $i
83
84 > inline me here
85 > this is a short quote
86
87 keep me
88 EOF
89                 if (rand_use('ssoma-mda')) {
90                         $im->done;
91                         my $str = $mime->as_string;
92                         IPC::Run::run(['ssoma-mda', $git_dir], \$str) or
93                                 die "mda failed: $?\n";
94                 } else {
95                         like($im->add($mime), qr/\A:\d+/, 'added');
96                 }
97         }
98         $im->done;
99 }
100
101 # spam check
102 {
103         # check initial feed
104         {
105                 my $feed = string_feed({ -inbox => $ibx });
106                 SKIP: {
107                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
108                         my $p = XML::Feed->parse(\$feed);
109                         is($p->format, "Atom", "parsed atom feed");
110                         is(scalar $p->entries, 3, "parsed three entries");
111                         is($p->id, 'mailto:test@example',
112                                 "id is set to default");
113                 }
114
115                 like($feed, qr/drop me/, "long quoted text kept");
116                 like($feed, qr/inline me here/, "short quoted text kept");
117                 like($feed, qr/keep me/, "unquoted text saved");
118         }
119
120         # add a new spam message
121         my $spam;
122         {
123                 $spam = Email::MIME->new(<<EOF);
124 From: SPAMMER <spammer\@example.com>
125 To: U <u\@example.com>
126 Message-Id: <this-is-spam\@example.com>
127 Subject: SPAM!!!!!!!!
128 Date: Thu, 01 Jan 1970 00:00:00 +0000
129
130 EOF
131                 if (rand_use('ssoma-mda')) {
132                         my $str = $spam->as_string;
133                         IPC::Run::run(['ssoma-mda', $git_dir], \$str) or
134                                 die "mda failed: $?\n";
135                 } else {
136                         $im->add($spam);
137                         $im->done;
138                 }
139         }
140
141         # check spam shows up
142         {
143                 my $spammy_feed = string_feed({ -inbox => $ibx });
144                 SKIP: {
145                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
146                         my $p = XML::Feed->parse(\$spammy_feed);
147                         is($p->format, "Atom", "parsed atom feed");
148                         is(scalar $p->entries, 3, "parsed three entries");
149                 }
150                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
151         }
152
153         # nuke spam
154         if (rand_use('ssoma-rm')) {
155                 my $spam_str = $spam->as_string;
156                 IPC::Run::run(["ssoma-rm", $git_dir], \$spam_str) or
157                                 die "ssoma-rm failed: $?\n";
158         } else {
159                 $im->remove($spam);
160                 $im->done;
161         }
162
163         # spam no longer shows up
164         {
165                 my $feed = string_feed({ -inbox => $ibx });
166                 SKIP: {
167                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
168                         my $p = XML::Feed->parse(\$feed);
169                         is($p->format, "Atom", "parsed atom feed");
170                         is(scalar $p->entries, 3, "parsed three entries");
171                 }
172                 unlike($feed, qr/SPAM/, "spam gone :>");
173         }
174 }
175
176 done_testing();