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