]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/mda.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / mda.t
diff --git a/t/mda.t b/t/mda.t
index 4556e3234e9c7adfc6682aa7cff6bde9ba8f7563..d20cdb92be30fb8b962dac42585b1e7204c627ee 100644 (file)
--- a/t/mda.t
+++ b/t/mda.t
@@ -1,13 +1,14 @@
-# Copyright (C) 2014-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
 use warnings;
 use Test::More;
-use Email::MIME;
 use Cwd qw(getcwd);
 use PublicInbox::MID qw(mid2path);
 use PublicInbox::Git;
+use PublicInbox::InboxWritable;
 use PublicInbox::TestCommon;
+use PublicInbox::Import;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $home = "$tmpdir/pi-home";
 my $pi_home = "$home/.public-inbox";
@@ -20,7 +21,6 @@ my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
 my $addr = 'test-public@example.com';
 my $cfgpfx = "publicinbox.test";
 my $faildir = "$home/faildir/";
-my $mime;
 my $git = PublicInbox::Git->new($maindir);
 
 my $fail_bad_header = sub ($$$) {
@@ -34,7 +34,7 @@ my $fail_bad_header = sub ($$$) {
                "no error on undeliverable ($msg)");
        my $rev = $git->qx(qw(rev-list HEAD));
        chomp $rev;
-       is($rev, $good_rev, "bad revision not commited ($msg)");
+       is($rev, $good_rev, "bad revision not committed ($msg)");
        @f = glob("$faildir/*/*");
        is(scalar @f, 1, "faildir written to");
        [ $in, $out, $err ];
@@ -47,28 +47,22 @@ my $fail_bad_header = sub ($$$) {
                "spamc mock found (run in top of source tree");
        is(1, mkdir($home, 0755), "setup ~/ for testing");
        is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
-       is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
+       PublicInbox::Import::init_bare($maindir);
 
-       my %cfg = (
-               "$cfgpfx.address" => $addr,
-               "$cfgpfx.inboxdir" => $maindir,
-       );
-       while (my ($k,$v) = each %cfg) {
-               is(0, system(qw(git config --file), $pi_config, $k, $v),
-                       "setup $k");
-       }
+       open my $fh, '>>', $pi_config or die;
+       print $fh <<EOF or die;
+[publicinbox "test"]
+       address = $addr
+       inboxdir = $maindir
+EOF
+       close $fh or die;
 }
 
 local $ENV{GIT_COMMITTER_NAME} = eval {
        use PublicInbox::MDA;
        use PublicInbox::Address;
        use Encode qw/encode/;
-       my $mbox = 't/utf8.mbox';
-       open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
-       my $str = eval { local $/; <$fh> };
-       close $fh;
-       my $msg = Email::MIME->new($str);
-
+       my $msg = eml_load 't/utf8.eml';
        my $from = $msg->header('From');
        my ($author) = PublicInbox::Address::names($from);
        my ($email) = PublicInbox::Address::emails($from);
@@ -89,7 +83,7 @@ die $@ if $@;
        local $ENV{PI_EMERGENCY} = $faildir;
        local $ENV{HOME} = $home;
        local $ENV{ORIGINAL_RECIPIENT} = $addr;
-       my $simple = Email::Simple->new(<<EOF);
+       my $in = <<EOF;
 From: Me <me\@example.com>
 To: You <you\@example.com>
 Cc: $addr
@@ -98,8 +92,6 @@ Subject: hihi
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 
 EOF
-       my $in = $simple->as_string;
-
        # ensure successful message delivery
        {
                local $ENV{PATH} = $main_path;
@@ -179,7 +171,7 @@ Date: deadbeef
        local $ENV{ORIGINAL_RECIPIENT} = $addr;
        local $ENV{PATH} = $main_path;
        my $mid = 'spam-train@example.com';
-       my $simple = Email::Simple->new(<<EOF);
+       my $in = <<EOF;
 From: Spammer <spammer\@example.com>
 To: You <you\@example.com>
 Cc: $addr
@@ -188,8 +180,6 @@ Subject: this message will be trained as spam
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 
 EOF
-       my $in = $simple->as_string;
-
        {
                # deliver the spam message, first
                ok(run_script(['-mda'], undef, { 0 => \$in }));
@@ -216,7 +206,7 @@ EOF
        local $ENV{ORIGINAL_RECIPIENT} = $addr;
        local $ENV{PATH} = $main_path;
        my $mid = 'ham-train@example.com';
-       my $simple = Email::Simple->new(<<EOF);
+       my $in = <<EOF;
 From: False-positive <hammer\@example.com>
 To: You <you\@example.com>
 Cc: $addr
@@ -225,8 +215,6 @@ Subject: this message will be trained as spam
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 
 EOF
-       my $in = $simple->as_string;
-
        # now train it
        # these should be overridden
        local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
@@ -241,35 +229,8 @@ EOF
                "learned ham idempotently ");
 
        # ensure trained email is filtered, too
-       my $html_body = "<html><body>hi</body></html>";
-       my $parts = [
-               Email::MIME->create(
-                       attributes => {
-                               content_type => 'text/html; charset=UTF-8',
-                               encoding => 'base64',
-                       },
-                       body => $html_body,
-               ),
-               Email::MIME->create(
-                       attributes => {
-                               content_type => 'text/plain',
-                               encoding => 'quoted-printable',
-                       },
-                       body => 'hi = "bye"',
-               )
-       ];
-       $mid = 'multipart-html-sucks@11';
-       $mime = Email::MIME->create(
-               header_str => [
-                 From => 'a@example.com',
-                 Subject => 'blah',
-                 Cc => $addr,
-                 'Message-ID' => "<$mid>",
-                 'Content-Type' => 'multipart/alternative',
-               ],
-               parts => $parts,
-       );
-
+       my $mime = eml_load 't/mda-mime.eml';
+       ($mid) = ($mime->header_raw('message-id') =~ /<([^>]+)>/);
        {
                $in = $mime->as_string;
                ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
@@ -290,7 +251,7 @@ EOF
        local $ENV{PATH} = $main_path;
        my $list_id = 'foo.example.com';
        my $mid = 'list-id-delivery@example.com';
-       my $simple = Email::Simple->new(<<EOF);
+       my $in = <<EOF;
 From: user <user\@example.com>
 To: You <you\@example.com>
 Cc: $addr
@@ -300,9 +261,8 @@ Subject: this message will be trained as spam
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 
 EOF
-       system(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
+       xsys(qw(git config --file), $pi_config, "$cfgpfx.listid", uc $list_id);
        $? == 0 or die "failed to set listid $?";
-       my $in = $simple->as_string;
        ok(run_script(['-mda'], undef, { 0 => \$in }),
                'mda OK with List-Id match');
        my $path = mid2path($mid);
@@ -310,13 +270,12 @@ EOF
        like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
 
        # try a message w/o precheck
-       $simple = Email::Simple->new(<<EOF);
+       $in = <<EOF;
 To: You <you\@example.com>
 List-Id: <$list_id>
 
 this message would not be accepted without --no-precheck
 EOF
-       $in = $simple->as_string;
        my ($out, $err) = ('', '');
        my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
        ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
@@ -340,9 +299,17 @@ EOF
        ok(run_script(['-mda'], undef, $rdr),
                'mda OK with multiple List-Id matches');
        $cur = $git->qx(qw(diff HEAD~1..HEAD));
-       like($cur, qr/Message-ID: <2lids\@example>/,
+       like($cur, qr/^\+Message-ID: <2lids\@example>/sm,
                'multi List-ID match delivered');
        like($err, qr/multiple List-ID/, 'warned about multiple List-ID');
+
+       # ensure -learn rm works after inbox address is updated
+       ($out, $err) = ('', '');
+       xsys(qw(git config --file), $pi_config, "$cfgpfx.address",
+               'updated-address@example.com');
+       ok(run_script(['-learn', 'rm'], undef, $rdr), 'rm-ed via -learn');
+       $cur = $git->qx(qw(diff HEAD~1..HEAD));
+       like($cur, qr/^-Message-ID: <2lids\@example>/sm, 'changed in git');
 }
 
 done_testing();