]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
remove XML::Atom::SimpleFeed dependency
[public-inbox.git] / t / feed.t
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
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(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                 unlike($feed, qr/drop me/, "long quoted text dropped");
80                 like($feed, qr!/f/\d%40example\.com\.html\b!,
81                         "/f/ url generated for long quoted text");
82                 like($feed, qr/inline me here/, "short quoted text kept");
83                 like($feed, qr/keep me/, "unquoted text saved");
84         }
85
86         # add a new spam message
87         my $spam;
88         {
89                 my $pid = open(my $pipe, "|-");
90                 defined $pid or die "fork/pipe failed: $!\n";
91                 if ($pid == 0) {
92                         exec("ssoma-mda", $git_dir);
93                 }
94
95                 $spam = Email::Simple->new(<<EOF);
96 From: SPAMMER <spammer\@example.com>
97 To: U <u\@example.com>
98 Message-Id: <this-is-spam\@example.com>
99 Subject: SPAM!!!!!!!!
100 Date: Thu, 01 Jan 1970 00:00:00 +0000
101
102 EOF
103                 print $pipe $spam->as_string or die "print failed: $!\n";
104                 close $pipe or die "close pipe failed: $!\n";
105         }
106
107         # check spam shows up
108         {
109                 my $spammy_feed = string_feed({
110                         git_dir => $git_dir,
111                         max => 3
112                 });
113                 SKIP: {
114                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
115                         my $p = XML::Feed->parse(\$spammy_feed);
116                         is($p->format, "Atom", "parsed atom feed");
117                         is(scalar $p->entries, 3, "parsed three entries");
118                 }
119                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
120         }
121
122         # nuke spam
123         {
124                 my $spam_str = $spam->as_string;
125                 run(["ssoma-rm", $git_dir], \$spam_str) or
126                                 die "ssoma-rm failed: $?\n";
127         }
128
129         # spam no longer shows up
130         {
131                 my $feed = string_feed({
132                         git_dir => $git_dir,
133                         max => 3
134                 });
135                 SKIP: {
136                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
137                         my $p = XML::Feed->parse(\$feed);
138                         is($p->format, "Atom", "parsed atom feed");
139                         is(scalar $p->entries, 3, "parsed three entries");
140                 }
141                 unlike($feed, qr/SPAM/, "spam gone :>");
142         }
143 }
144
145 # check pi_config
146 {
147         foreach my $addr (('a@example.com'), ['a@example.com','b@localhost']) {
148                 my $feed = string_feed({
149                         git_dir => $git_dir,
150                         max => 3,
151                         listname => 'asdf',
152                         pi_config => bless({
153                                 'publicinbox.asdf.address' => $addr,
154                         }, 'PublicInbox::Config'),
155                 });
156                 SKIP: {
157                         skip 'XML::Feed missing', 3 unless $have_xml_feed;
158                         my $p = XML::Feed->parse(\$feed);
159                         is($p->id, 'mailto:a@example.com',
160                                 "ID is set correctly");
161                         is($p->format, "Atom", "parsed atom feed");
162                         is(scalar $p->entries, 3, "parsed three entries");
163                 }
164         }
165 }
166
167 done_testing();