]> Sergey Matveev's repositories - public-inbox.git/blob - t/feed.t
t/feed: remove useless $ENV{GIT_DIR} assignment
[public-inbox.git] / t / feed.t
1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use PublicInbox::Feed;
8 use PublicInbox::Import;
9 use PublicInbox::Inbox;
10 my $have_xml_treepp = eval { require XML::TreePP; 1 };
11 use PublicInbox::TestCommon;
12
13 sub string_feed {
14         my $res = PublicInbox::Feed::generate($_[0]);
15         my $body = $res->[2];
16         my $str = '';
17         while (defined(my $chunk = $body->getline)) {
18                 $str .= $chunk;
19         }
20         $body->close;
21         $str;
22 }
23
24 my ($tmpdir, $for_destroy) = tmpdir();
25 my $git_dir = "$tmpdir/gittest";
26 my $ibx = PublicInbox::Inbox->new({
27         address => 'test@example',
28         name => 'testbox',
29         inboxdir => $git_dir,
30         url => [ 'http://example.com/test' ],
31         feedmax => 3,
32 });
33 my $git = $ibx->git;
34 my $im = PublicInbox::Import->new($git, $ibx->{name}, 'test@example');
35
36 {
37         $im->init_bare;
38         foreach my $i (1..6) {
39                 my $mime = Email::MIME->new(<<EOF);
40 From: ME <me\@example.com>
41 To: U <u\@example.com>
42 Message-Id: <$i\@example.com>
43 Subject: zzz #$i
44 Date: Thu, 01 Jan 1970 00:00:00 +0000
45
46 > This is a long multi line quote so it should not be allowed to
47 > show up in its entirty in the Atom feed.  drop me
48 > I quote to much
49 > I quote to much
50 > I quote to much
51 > I quote to much
52 > I quote to much
53 > I quote to much
54 > I quote to much
55 > I quote to much
56 > I quote to much
57 > I quote to much
58 > I quote to much
59 > I quote to much
60 > I quote to much
61
62 msg $i
63
64 > inline me here
65 > this is a short quote
66
67 keep me
68 EOF
69                 like($im->add($mime), qr/\A:\d+/, 'added');
70         }
71         $im->done;
72 }
73
74 # spam check
75 {
76         # check initial feed
77         {
78                 my $feed = string_feed({ -inbox => $ibx });
79                 SKIP: {
80                         skip 'XML::TreePP missing', 3 unless $have_xml_treepp;
81                         my $t = XML::TreePP->new->parse($feed);
82                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
83                                 'looks like an an Atom feed');
84                         is(scalar @{$t->{feed}->{entry}}, 3,
85                                 'parsed three entries');
86                         is($t->{feed}->{id}, 'mailto:test@example',
87                                 'id is set to default');
88                 }
89
90                 like($feed, qr/drop me/, "long quoted text kept");
91                 like($feed, qr/inline me here/, "short quoted text kept");
92                 like($feed, qr/keep me/, "unquoted text saved");
93         }
94
95         # add a new spam message
96         my $spam;
97         {
98                 $spam = Email::MIME->new(<<EOF);
99 From: SPAMMER <spammer\@example.com>
100 To: U <u\@example.com>
101 Message-Id: <this-is-spam\@example.com>
102 Subject: SPAM!!!!!!!!
103 Date: Thu, 01 Jan 1970 00:00:00 +0000
104
105 EOF
106                 $im->add($spam);
107                 $im->done;
108         }
109
110         # check spam shows up
111         {
112                 my $spammy_feed = string_feed({ -inbox => $ibx });
113                 SKIP: {
114                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
115                         my $t = XML::TreePP->new->parse($spammy_feed);
116                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
117                                 'looks like an an Atom feed');
118                         is(scalar @{$t->{feed}->{entry}}, 3,
119                                 'parsed three entries');
120                 }
121                 like($spammy_feed, qr/SPAM/s, "spam showed up :<");
122         }
123
124         # nuke spam
125         $im->remove($spam);
126         $im->done;
127
128         # spam no longer shows up
129         {
130                 my $feed = string_feed({ -inbox => $ibx });
131                 SKIP: {
132                         skip 'XML::TreePP missing', 2 unless $have_xml_treepp;
133                         my $t = XML::TreePP->new->parse($feed);
134                         like($t->{feed}->{-xmlns}, qr/\bAtom\b/,
135                                 'looks like an an Atom feed');
136                         is(scalar @{$t->{feed}->{entry}}, 3,
137                                 'parsed three entries');
138                 }
139                 unlike($feed, qr/SPAM/, "spam gone :>");
140         }
141 }
142
143 done_testing();