]> Sergey Matveev's repositories - public-inbox.git/blob - xt/net_writer-imap.t
4832245add2f024b445cf0859c202bacb7a4b70b
[public-inbox.git] / xt / net_writer-imap.t
1 #!perl -w
2 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
5 use Sys::Hostname qw(hostname);
6 use POSIX qw(strftime);
7 use PublicInbox::OnDestroy;
8 use PublicInbox::URIimap;
9 use PublicInbox::Config;
10 use Fcntl qw(O_EXCL O_WRONLY O_CREAT);
11 my $imap_url = $ENV{TEST_IMAP_WRITE_URL} or
12         plan skip_all => 'TEST_IMAP_WRITE_URL unset';
13 my $uri = PublicInbox::URIimap->new($imap_url);
14 defined($uri->path) and
15         plan skip_all => "$imap_url should not be a mailbox (just host:port)";
16 require_mods('Mail::IMAPClient');
17 require_ok 'PublicInbox::NetWriter';
18 my $host = (split(/\./, hostname))[0];
19 my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
20 my $folder = "INBOX.$base-$host-".strftime('%Y%m%d%H%M%S', gmtime(time)).
21                 "-$$-".sprintf('%x', int(rand(0xffffffff)));
22 my $nwr = PublicInbox::NetWriter->new;
23 chop($imap_url) if substr($imap_url, -1) eq '/';
24 my $folder_uri = PublicInbox::URIimap->new("$imap_url/$folder");
25 is($folder_uri->mailbox, $folder, 'folder correct') or
26                 BAIL_OUT "BUG: bad $$uri";
27 $nwr->add_url($$folder_uri);
28 is($nwr->errors, undef, 'no errors');
29 $nwr->{pi_cfg} = bless {}, 'PublicInbox::Config';
30
31 my $set_cred_helper = sub {
32         my ($f, $cred_set) = @_;
33         sysopen(my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) or BAIL_OUT "open $f: $!";
34         print $fh <<EOF or BAIL_OUT "print $f: $!";
35 [credential]
36         helper = $cred_set
37 EOF
38         close $fh or BAIL_OUT "close $f: $!";
39 };
40
41 # allow testers with git-credential-store configured to reuse
42 # stored credentials inside test_lei(sub {...}) when $ENV{HOME}
43 # is overridden and localized.
44 my ($cred_set, @cred_link, $tmpdir, $for_destroy);
45 chomp(my $cred_helper = `git config credential.helper 2>/dev/null`);
46 if ($cred_helper eq 'store') {
47         my $config = $ENV{XDG_CONFIG_HOME} // "$ENV{HOME}/.config";
48         for my $f ("$ENV{HOME}/.git-credentials", "$config/git/credentials") {
49                 next unless -f $f;
50                 @cred_link = ($f, '/.git-credentials');
51                 last;
52         }
53         $cred_set = qq("$cred_helper");
54 } elsif ($cred_helper =~ /\Acache(?:[ \t]|\z)/) {
55         my $cache = $ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache";
56         for my $d ("$ENV{HOME}/.git-credential-cache",
57                         "$cache/git/credential") {
58                 next unless -d $d;
59                 @cred_link = ($d, '/.git-credential-cache');
60                 $cred_set = qq("$cred_helper");
61                 last;
62         }
63 } elsif (!$cred_helper) { # make the test less painful if no creds configured
64         ($tmpdir, $for_destroy) = tmpdir;
65         my $d = "$tmpdir/.git-credential-cache";
66         mkdir($d, 0700) or BAIL_OUT $!;
67         $cred_set = "cache --timeout=60";
68         @cred_link = ($d, '/.git-credential-cache');
69 } else {
70         diag "credential.helper=$cred_helper will not be used for this test";
71 }
72
73 my $mics = do {
74         local $ENV{HOME} = $tmpdir // $ENV{HOME};
75         if ($tmpdir && $cred_set) {
76                 $set_cred_helper->("$ENV{HOME}/.gitconfig", $cred_set)
77         }
78         $nwr->imap_common_init;
79 };
80 my $mic = (values %$mics)[0];
81 my $cleanup = PublicInbox::OnDestroy->new($$, sub {
82         my $mic = $nwr->mic_get($imap_url);
83         $mic->delete($folder) or fail "delete $folder <$folder_uri>: $@";
84         if ($tmpdir && -f "$tmpdir/.gitconfig") {
85                 local $ENV{HOME} = $tmpdir;
86                 system(qw(git credential-cache exit));
87         }
88 });
89 my $imap_append = $nwr->can('imap_append');
90 my $smsg = bless { kw => [ 'seen' ] }, 'PublicInbox::Smsg';
91 $imap_append->($mic, $folder, undef, $smsg, eml_load('t/plack-qp.eml'));
92 $nwr->{quiet} = 1;
93 my $imap_slurp_all = sub {
94         my ($u, $uid, $kw, $eml, $res) = @_;
95         push @$res, [ $kw, $eml ];
96 };
97 $nwr->imap_each($$folder_uri, $imap_slurp_all, my $res = []);
98 is(scalar(@$res), 1, 'got appended message');
99 my $plack_qp_eml = eml_load('t/plack-qp.eml');
100 is_deeply($res, [ [ [ 'seen' ], $plack_qp_eml ] ],
101         'uploaded message read back');
102 $res = $mic = $mics = undef;
103
104 test_lei(sub {
105         my ($ro_home, $cfg_path) = setup_public_inboxes;
106         my $cfg = PublicInbox::Config->new($cfg_path);
107         $cfg->each_inbox(sub {
108                 my ($ibx) = @_;
109                 lei_ok qw(add-external -q), $ibx->{inboxdir} or BAIL_OUT;
110         });
111
112         # cred_link[0] may be on a different (hopefully encrypted) FS,
113         # we only symlink to it here, so we don't copy any sensitive data
114         # into the temporary directory
115         if (@cred_link && !symlink($cred_link[0], $ENV{HOME}.$cred_link[1])) {
116                 diag "symlink @cred_link: $! (non-fatal)";
117                 $cred_set = undef;
118         }
119         $set_cred_helper->("$ENV{HOME}/.gitconfig", $cred_set) if $cred_set;
120
121         lei_ok qw(q f:qp@example.com -o), $$folder_uri;
122         $nwr->imap_each($$folder_uri, $imap_slurp_all, my $res = []);
123         is(scalar(@$res), 1, 'got one deduped result') or diag explain($res);
124         is_deeply($res->[0]->[1], $plack_qp_eml,
125                         'lei q wrote expected result');
126
127         lei_ok qw(q f:matz -a -o), $$folder_uri;
128         $nwr->imap_each($$folder_uri, $imap_slurp_all, my $aug = []);
129         is(scalar(@$aug), 2, '2 results after augment') or diag explain($aug);
130         my $exp = $res->[0]->[1]->as_string;
131         is(scalar(grep { $_->[1]->as_string eq $exp } @$aug), 1,
132                         'original remains after augment');
133         $exp = eml_load('t/iso-2202-jp.eml')->as_string;
134         is(scalar(grep { $_->[1]->as_string eq $exp } @$aug), 1,
135                         'new result shown after augment');
136
137         lei_ok qw(q s:thisbetternotgiveanyresult -o), $folder_uri->as_string;
138         $nwr->imap_each($$folder_uri, $imap_slurp_all, my $empty = []);
139         is(scalar(@$empty), 0, 'no results w/o augment');
140
141 });
142
143 undef $cleanup; # remove temporary folder
144 done_testing;