]> Sergey Matveev's repositories - public-inbox.git/blob - t/precheck.t
precheck: require Message-ID to be set
[public-inbox.git] / t / precheck.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 Email::Filter;
8 use PublicInbox;
9
10 sub do_checks {
11         my ($s) = @_;
12
13         my $f = Email::Filter->new(data => $s->as_string);
14         local %ENV;
15         delete $ENV{ORIGINAL_RECIPIENT};
16
17         ok(PublicInbox->precheck($f),
18                 "ORIGINAL_RECIPIENT unset is OK");
19
20         $ENV{ORIGINAL_RECIPIENT} = 'foo@example.com';
21         ok(!PublicInbox->precheck($f),
22                 "wrong ORIGINAL_RECIPIENT rejected");
23
24         $ENV{ORIGINAL_RECIPIENT} = 'b@example.com';
25         ok(PublicInbox->precheck($f),
26                 "ORIGINAL_RECIPIENT in To: is OK");
27
28         $ENV{ORIGINAL_RECIPIENT} = 'c@example.com';
29         ok(PublicInbox->precheck($f),
30                 "ORIGINAL_RECIPIENT in Cc: is OK");
31 }
32
33 {
34         do_checks(Email::Simple->create(
35                 header => [
36                         From => 'a@example.com',
37                         To => 'b@example.com',
38                         Cc => 'c@example.com',
39                         'Content-Type' => 'text/plain',
40                         Subject => 'this is a subject',
41                         'Message-ID' => '<MID>',
42                 ],
43                 body => "hello world\n",
44         ));
45 }
46
47 {
48         do_checks(Email::Simple->create(
49                 header => [
50                         From => 'a@example.com',
51                         To => 'b+plus@example.com',
52                         Cc => 'John Doe <c@example.com>',
53                         'Content-Type' => 'text/plain',
54                         Subject => 'this is a subject',
55                         'Message-ID' => '<MID>',
56                 ],
57                 body => "hello world\n",
58         ));
59 }
60
61 {
62         $ENV{ORIGINAL_RECIPIENT} = 'b@example.com';
63         my $s = Email::Simple->create(
64                 header => [
65                         To => 'b@example.com',
66                         Cc => 'c@example.com',
67                         'Content-Type' => 'text/plain',
68                         Subject => 'this is a subject',
69                         'Message-ID' => '<MID>',
70                 ],
71                 body => "hello world\n",
72         );
73         my $f = Email::Filter->new(data => $s->as_string);
74         ok(!PublicInbox->precheck($f), "missing From: is rejected");
75 }
76
77 done_testing();