]> Sergey Matveev's repositories - public-inbox.git/blob - xt/net_writer-imap.t
URIimap: overload "" to ->as_string
[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 my $imap_url = $ENV{TEST_IMAP_WRITE_URL} or
11         plan skip_all => 'TEST_IMAP_WRITE_URL unset';
12 my $uri = PublicInbox::URIimap->new($imap_url);
13 defined($uri->path) and
14         plan skip_all => "$imap_url should not be a mailbox (just host:port)";
15 require_mods('Mail::IMAPClient');
16 require_ok 'PublicInbox::NetWriter';
17 my $host = (split(/\./, hostname))[0];
18 my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
19 my $folder = "INBOX.$base-$host-".strftime('%Y%m%d%H%M%S', gmtime(time)).
20                 "-$$-".sprintf('%x', int(rand(0xffffffff)));
21 my $nwr = PublicInbox::NetWriter->new;
22 $imap_url .= '/' unless substr($imap_url, -1) eq '/';
23 my $folder_uri = PublicInbox::URIimap->new("$imap_url/$folder");
24 is($folder_uri->mailbox, $folder, 'folder correct') or
25                 BAIL_OUT "BUG: bad $$uri";
26 $nwr->add_url($$folder_uri);
27 is($nwr->errors, undef, 'no errors');
28 $nwr->{pi_cfg} = bless {}, 'PublicInbox::Config';
29 my $mics = $nwr->imap_common_init;
30 my $mic = (values %$mics)[0];
31 my $cleanup = PublicInbox::OnDestroy->new(sub {
32         $mic->delete($folder) or fail "delete $folder <$folder_uri>: $@";
33 });
34 my $imap_append = $nwr->can('imap_append');
35 my $smsg = bless { kw => [ 'seen' ] }, 'PublicInbox::Smsg';
36 $imap_append->($mic, $folder, undef, $smsg, eml_load('t/plack-qp.eml'));
37 my @res;
38 $nwr->{quiet} = 1;
39 $nwr->imap_each($$folder_uri, sub {
40         my ($u, $uid, $kw, $eml, $arg) = @_;
41         push @res, [ $kw, $eml ];
42 });
43 is(scalar(@res), 1, 'got appended message');
44 is_deeply(\@res, [ [ [ 'seen' ], eml_load('t/plack-qp.eml') ] ],
45         'uploaded message read back');
46
47 undef $cleanup;
48 done_testing;