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