]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
www: stop generating /$MESSAGE_ID/f/ links
[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::Simple;
7 use PublicInbox::Feed;
8 use PublicInbox::Config;
9 use IPC::Run qw/run/;
10 use File::Temp qw/tempdir/;
11 my $have_xml_feed = eval { require XML::Feed; 1 };
12 require 't/common.perl';
13
14 sub string_feed {
15         stream_to_string(PublicInbox::Feed::generate($_[0]));
16 }
17
18 my $tmpdir = tempdir('pi-feed-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 my $git_dir = "$tmpdir/gittest";
20
21 {
22         is(0, system(qw(git init -q --bare), $git_dir), "git init");
23         local $ENV{GIT_DIR} = $git_dir;
24
25         foreach my $i (1..6) {
26                 my $simple = Email::Simple->new(<<EOF);
27 From: ME <me\@example.com>
28 To: U <u\@example.com>
29 Message-Id: <$i\@example.com>
30 Subject: zzz #$i
31 Date: Thu, 01 Jan 1970 00:00:00 +0000
32
33 > This is a long multi line quote so it should not be allowed to
34 > show up in its entirty in the Atom feed.  drop me
35 > I quote to much
36 > I quote to much
37 > I quote to much
38 > I quote to much
39 > I quote to much
40 > I quote to much
41 > I quote to much
42 > I quote to much
43 > I quote to much
44 > I quote to much
45 > I quote to much
46 > I quote to much
47 > I quote to much
48
49 msg $i
50
51 > inline me here
52 > this is a short quote
53
54 keep me
55 EOF
56                 my $str = $simple->as_string;
57                 run(['ssoma-mda', $git_dir], \$str) or
58                         die "mda failed: $?\n";
59         }
60 }
61
62 # spam check
63 {
64         # check initial feed
65         {
66                 my $feed = string_feed({
67                         git_dir => $git_dir,
68                         max => 3
69                 });
70                 SKIP: {
71                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
72                         my $p = XML::Feed->parse(\$feed);
73                         is($p->format, "Atom", "parsed atom feed");
74                         is(scalar $p->entries, 3, "parsed three entries");
75                         is($p->id, 'mailto:public-inbox@example.com',
76                                 "id is set to default");
77                 }
78
79                 like($feed, qr/drop me/, "long quoted text kept");
80                 like($feed, qr/inline me here/, "short quoted text kept");
81                 like($feed, qr/keep me/, "unquoted text saved");
82         }
83
84         # add a new spam message
85         my $spam;
86         {
87                 my $pid = open(my $pipe, "|-");
88                 defined $pid or die "fork/pipe failed: $!\n";
89                 if ($pid == 0) {
90                         exec("ssoma-mda", $git_dir);
91                 }
92
93                 $spam = Email::Simple->new(<<EOF);
94 From: SPAMMER <spammer\@example.com>
95 To: U <u\@example.com>
96 Message-Id: <this-is-spam\@example.com>
97 Subject: SPAM!!!!!!!!
98 Date: Thu, 01 Jan 1970 00:00:00 +0000
99
100 EOF
101                 print $pipe $spam->as_string or die "print failed: $!\n";
102                 close $pipe or die "close pipe failed: $!\n";
103         }
104
105         # check spam shows up
106         {
107                 my $spammy_feed = string_feed({
108                         git_dir => $git_dir,
109                         max => 3
110                 });
111                 SKIP: {
112                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
113                         my $p = XML::Feed->parse(\$spammy_feed);
114                         is($p->format, "Atom", "parsed atom feed");
115                         is(scalar $p->entries, 3, "parsed three entries");
116                 }
117                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
118         }
119
120         # nuke spam
121         {
122                 my $spam_str = $spam->as_string;
123                 run(["ssoma-rm", $git_dir], \$spam_str) or
124                                 die "ssoma-rm failed: $?\n";
125         }
126
127         # spam no longer shows up
128         {
129                 my $feed = string_feed({
130                         git_dir => $git_dir,
131                         max => 3
132                 });
133                 SKIP: {
134                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
135                         my $p = XML::Feed->parse(\$feed);
136                         is($p->format, "Atom", "parsed atom feed");
137                         is(scalar $p->entries, 3, "parsed three entries");
138                 }
139                 unlike($feed, qr/SPAM/, "spam gone :>");
140         }
141 }
142
143 # check pi_config
144 {
145         foreach my $addr (('a@example.com'), ['a@example.com','b@localhost']) {
146                 my $feed = string_feed({
147                         git_dir => $git_dir,
148                         max => 3,
149                         listname => 'asdf',
150                         pi_config => bless({
151                                 'publicinbox.asdf.address' => $addr,
152                         }, 'PublicInbox::Config'),
153                 });
154                 SKIP: {
155                         skip 'XML::Feed missing', 3 unless $have_xml_feed;
156                         my $p = XML::Feed->parse(\$feed);
157                         is($p->id, 'mailto:a@example.com',
158                                 "ID is set correctly");
159                         is($p->format, "Atom", "parsed atom feed");
160                         is(scalar $p->entries, 3, "parsed three entries");
161                 }
162         }
163 }
164
165 done_testing();