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