]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
t/feed: notify user of missing XML::Feed
[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 File::Temp qw/tempdir/;
9 my $have_xml_feed = eval { require XML::Feed; 1 };
10
11 my $tmpdir = tempdir(CLEANUP => 1);
12 my $git_dir = "$tmpdir/gittest";
13
14 {
15         is(0, system(qw(git init -q --bare), $git_dir), "git init");
16         local $ENV{GIT_DIR} = $git_dir;
17
18         foreach my $i (1..6) {
19                 my $pid = open(my $pipe, "|-");
20                 defined $pid or die "fork/pipe failed: $!\n";
21                 if ($pid == 0) {
22                         exec("ssoma-mda", $git_dir);
23                 }
24
25                 my $simple = Email::Simple->new(<<EOF);
26 From: ME <me\@example.com>
27 To: U <u\@example.com>
28 Message-Id: <$i\@example.com>
29 Subject: zzz #$i
30 Date: Thu, 01 Jan 1970 00:00:00 +0000
31
32 > This is a long multi line quote so it should not be allowed to
33 > show up in its entirty in the Atom feed.  drop me
34 > I quote to much
35 > I quote to much
36 > I quote to much
37 > I quote to much
38
39 msg $i
40
41 > inline me here
42 > this is a short quote
43
44 keep me
45 EOF
46                 print $pipe $simple->as_string or die "print failed: $!\n";
47                 close $pipe or die "close pipe failed: $!\n";
48         }
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                 }
62                 unlike($feed, qr/drop me/, "long quoted text dropped");
63                 like($feed, qr/inline me here/, "short quoted text kept");
64                 like($feed, qr/keep me/, "unquoted text saved");
65         }
66
67         # add a new spam message
68         my $spam;
69         {
70                 my $pid = open(my $pipe, "|-");
71                 defined $pid or die "fork/pipe failed: $!\n";
72                 if ($pid == 0) {
73                         exec("ssoma-mda", $git_dir);
74                 }
75
76                 $spam = Email::Simple->new(<<EOF);
77 From: SPAMMER <spammer\@example.com>
78 To: U <u\@example.com>
79 Message-Id: <this-is-spam\@example.com>
80 Subject: SPAM!!!!!!!!
81 Date: Thu, 01 Jan 1970 00:00:00 +0000
82
83 EOF
84                 print $pipe $spam->as_string or die "print failed: $!\n";
85                 close $pipe or die "close pipe failed: $!\n";
86         }
87
88         # check spam shows up
89         {
90                 my $spammy_feed = PublicInbox::Feed->generate({
91                         git_dir => $git_dir,
92                         max => 3
93                 });
94                 SKIP: {
95                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
96                         my $p = XML::Feed->parse(\$spammy_feed);
97                         is($p->format, "Atom", "parsed atom feed");
98                         is(scalar $p->entries, 3, "parsed three entries");
99                 }
100                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
101         }
102
103         # nuke spam
104         {
105                 my $pid = open(my $pipe, "|-");
106                 defined $pid or die "fork/pipe failed: $!\n";
107                 if ($pid == 0) {
108                         exec("ssoma-rm", $git_dir);
109                 }
110                 print $pipe $spam->as_string or die "print failed: $!\n";
111                 close $pipe or die "close pipe failed: $!\n";
112         }
113
114         # spam no longer shows up
115         {
116                 my $feed = PublicInbox::Feed->generate({
117                         git_dir => $git_dir,
118                         max => 3
119                 });
120                 SKIP: {
121                         skip 'XML::Feed missing', 2 unless $have_xml_feed;
122                         my $p = XML::Feed->parse(\$feed);
123                         is($p->format, "Atom", "parsed atom feed");
124                         is(scalar $p->entries, 3, "parsed three entries");
125                 }
126                 unlike($feed, qr/SPAM/, "spam gone :>");
127         }
128 }
129
130 done_testing();