]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
feed: generate takes a hashref for args
[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
35 msg $i
36
37 > inline me here, short quote
38
39 keep me
40 EOF
41                 print $pipe $simple->as_string or die "print failed: $!\n";
42                 close $pipe or die "close pipe failed: $!\n";
43         }
44
45         # check initial feed
46         {
47                 my $feed = PublicInbox::Feed->generate({
48                         git_dir => $git_dir,
49                         max => 3
50                 });
51                 if ($have_xml_feed) {
52                         my $p = XML::Feed->parse(\$feed);
53                         is($p->format, "Atom", "parsed atom feed");
54                         is(scalar $p->entries, 3, "parsed three entries");
55                 }
56                 unlike($feed, qr/drop me/, "long quoted text dropped");
57                 like($feed, qr/inline me here/, "short quoted text kept");
58                 like($feed, qr/keep me/, "unquoted text saved");
59         }
60
61         # add a new spam message
62         my $spam;
63         {
64                 my $pid = open(my $pipe, "|-");
65                 defined $pid or die "fork/pipe failed: $!\n";
66                 if ($pid == 0) {
67                         exec("ssoma-mda", $git_dir);
68                 }
69
70                 $spam = Email::Simple->new(<<EOF);
71 From: SPAMMER <spammer\@example.com>
72 To: U <u\@example.com>
73 Message-Id: <this-is-spam\@example.com>
74 Subject: SPAM!!!!!!!!
75 Date: Thu, 01 Jan 1970 00:00:00 +0000
76
77 EOF
78                 print $pipe $spam->as_string or die "print failed: $!\n";
79                 close $pipe or die "close pipe failed: $!\n";
80         }
81
82         # check spam shows up
83         {
84                 my $spammy_feed = PublicInbox::Feed->generate({
85                         git_dir => $git_dir,
86                         max => 3
87                 });
88                 if ($have_xml_feed) {
89                         my $p = XML::Feed->parse(\$spammy_feed);
90                         is($p->format, "Atom", "parsed atom feed");
91                         is(scalar $p->entries, 3, "parsed three entries");
92                 }
93                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
94         }
95
96         # nuke spam
97         {
98                 my $pid = open(my $pipe, "|-");
99                 defined $pid or die "fork/pipe failed: $!\n";
100                 if ($pid == 0) {
101                         exec("ssoma-rm", $git_dir);
102                 }
103                 print $pipe $spam->as_string or die "print failed: $!\n";
104                 close $pipe or die "close pipe failed: $!\n";
105         }
106
107         # spam no longer shows up
108         {
109                 my $feed = PublicInbox::Feed->generate({
110                         git_dir => $git_dir,
111                         max => 3
112                 });
113                 if ($have_xml_feed) {
114                         my $p = XML::Feed->parse(\$feed);
115                         is($p->format, "Atom", "parsed atom feed");
116                         is(scalar $p->entries, 3, "parsed three entries");
117                 }
118                 unlike($feed, qr/SPAM/, "spam gone :>");
119         }
120 }
121
122 done_testing();