From bf367c9ac999e2e64a53ead083f68b171d9f6d35 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 19 Feb 2021 05:09:54 -0700 Subject: net_writer: start implementing IMAP write support Requiring TEST_IMAP_WRITE_URL to be set to a writable IMAP server URL isn't ideal, but it works for now until we have time to setup a mock dovecot/cyrus/etc... instance for testing. --- xt/net_writer-imap.t | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 xt/net_writer-imap.t (limited to 'xt') diff --git a/xt/net_writer-imap.t b/xt/net_writer-imap.t new file mode 100644 index 00000000..ea812f16 --- /dev/null +++ b/xt/net_writer-imap.t @@ -0,0 +1,48 @@ +#!perl -w +# Copyright (C) 2021 all contributors +# License: AGPL-3.0+ +use strict; use v5.10.1; use PublicInbox::TestCommon; +use Sys::Hostname qw(hostname); +use POSIX qw(strftime); +use PublicInbox::OnDestroy; +use PublicInbox::URIimap; +use PublicInbox::Config; +my $imap_url = $ENV{TEST_IMAP_WRITE_URL} or + plan skip_all => 'TEST_IMAP_WRITE_URL unset'; +my $uri = PublicInbox::URIimap->new($imap_url); +defined($uri->path) and + plan skip_all => "$imap_url should not be a mailbox (just host:port)"; +require_mods('Mail::IMAPClient'); +require_ok 'PublicInbox::NetWriter'; +my $host = (split(/\./, hostname))[0]; +my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!); +my $folder = "INBOX.$base-$host-".strftime('%Y%m%d%H%M%S', gmtime(time)). + "-$$-".sprintf('%x', int(rand(0xffffffff))); +my $nwr = PublicInbox::NetWriter->new; +$imap_url .= '/' unless substr($imap_url, -1) eq '/'; +my $folder_uri = PublicInbox::URIimap->new("$imap_url/$folder"); +is($folder_uri->mailbox, $folder, 'folder correct') or + BAIL_OUT "BUG: bad $$uri"; +$nwr->add_url($$folder_uri); +is($nwr->errors, undef, 'no errors'); +$nwr->{pi_cfg} = bless {}, 'PublicInbox::Config'; +my $mics = $nwr->imap_common_init; +my $mic = (values %$mics)[0]; +my $cleanup = PublicInbox::OnDestroy->new(sub { + $mic->delete($folder) or fail "delete $folder <$$folder_uri>: $@"; +}); +my $imap_append = $nwr->can('imap_append'); +my $smsg = bless { kw => [ 'seen' ] }, 'PublicInbox::Smsg'; +$imap_append->($mic, $folder, undef, $smsg, eml_load('t/plack-qp.eml')); +my @res; +$nwr->{quiet} = 1; +$nwr->imap_each($$folder_uri, sub { + my ($u, $uid, $kw, $eml, $arg) = @_; + push @res, [ $kw, $eml ]; +}); +is(scalar(@res), 1, 'got appended message'); +is_deeply(\@res, [ [ [ 'seen' ], eml_load('t/plack-qp.eml') ] ], + 'uploaded message read back'); + +undef $cleanup; +done_testing; -- cgit v1.2.3-24-ge0c7