]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
978e21563fa3efdd69df0ec4c36d90d56c562855
[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 > I quote to much
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
44 msg $i
45
46 > inline me here
47 > this is a short quote
48
49 keep me
50 EOF
51                 my $str = $simple->as_string;
52                 run(['ssoma-mda', $git_dir], \$str) or
53                         die "mda failed: $?\n";
54         }
55 }
56
57 # spam check
58 {
59         # check initial feed
60         {
61                 my $feed = PublicInbox::Feed->generate({
62                         git_dir => $git_dir,
63                         max => 3
64                 });
65                 SKIP: {
66                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
67                         my $p = XML::Feed->parse(\$feed);
68                         is($p->format, "Atom", "parsed atom feed");
69                         is(scalar $p->entries, 3, "parsed three entries");
70                         is($p->id, 'mailto:public-inbox@example.com',
71                                 "id is set to default");
72                 }
73
74                 unlike($feed, qr/drop me/, "long quoted text dropped");
75                 like($feed, qr!/f/\d%40example\.com\.html\b!,
76                         "/f/ url generated for long quoted text");
77                 like($feed, qr/inline me here/, "short quoted text kept");
78                 like($feed, qr/keep me/, "unquoted text saved");
79         }
80
81         # add a new spam message
82         my $spam;
83         {
84                 my $pid = open(my $pipe, "|-");
85                 defined $pid or die "fork/pipe failed: $!\n";
86                 if ($pid == 0) {
87                         exec("ssoma-mda", $git_dir);
88                 }
89
90                 $spam = Email::Simple->new(<<EOF);
91 From: SPAMMER <spammer\@example.com>
92 To: U <u\@example.com>
93 Message-Id: <this-is-spam\@example.com>
94 Subject: SPAM!!!!!!!!
95 Date: Thu, 01 Jan 1970 00:00:00 +0000
96
97 EOF
98                 print $pipe $spam->as_string or die "print failed: $!\n";
99                 close $pipe or die "close pipe failed: $!\n";
100         }
101
102         # check spam shows up
103         {
104                 my $spammy_feed = PublicInbox::Feed->generate({
105                         git_dir => $git_dir,
106                         max => 3
107                 });
108                 SKIP: {
109                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
110                         my $p = XML::Feed->parse(\$spammy_feed);
111                         is($p->format, "Atom", "parsed atom feed");
112                         is(scalar $p->entries, 3, "parsed three entries");
113                 }
114                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
115         }
116
117         # nuke spam
118         {
119                 my $spam_str = $spam->as_string;
120                 run(["ssoma-rm", $git_dir], \$spam_str) or
121                                 die "ssoma-rm failed: $?\n";
122         }
123
124         # spam no longer shows up
125         {
126                 my $feed = PublicInbox::Feed->generate({
127                         git_dir => $git_dir,
128                         max => 3
129                 });
130                 SKIP: {
131                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
132                         my $p = XML::Feed->parse(\$feed);
133                         is($p->format, "Atom", "parsed atom feed");
134                         is(scalar $p->entries, 3, "parsed three entries");
135                 }
136                 unlike($feed, qr/SPAM/, "spam gone :>");
137         }
138 }
139
140 # check pi_config
141 {
142         foreach my $addr (('a@example.com'), ['a@example.com','b@localhost']) {
143                 my $feed = PublicInbox::Feed->generate({
144                         git_dir => $git_dir,
145                         max => 3,
146                         listname => 'asdf',
147                         pi_config => bless({
148                                 'publicinbox.asdf.address' => $addr,
149                         }, 'PublicInbox::Config'),
150                 });
151                 SKIP: {
152                         skip 'XML::Feed missing', 3 unless $have_xml_feed;
153                         my $p = XML::Feed->parse(\$feed);
154                         is($p->id, 'mailto:a@example.com',
155                                 "ID is set correctly");
156                         is($p->format, "Atom", "parsed atom feed");
157                         is(scalar $p->entries, 3, "parsed three entries");
158                 }
159         }
160 }
161
162 done_testing();