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