#!perl
use Cassandane::Tiny;

sub test_squatter_attachextract_cachedir_noperm
    :min_version_3_9 :SearchAttachmentExtractor :NoCheckSyslog
{
    my ($self) = @_;
    my $instance = $self->{instance};
    my $imap = $self->{store}->get_client();

    my $tracedir = tempdir(DIR => $instance->{basedir} . "/tmp");
    $self->start_echo_extractor(tracedir => $tracedir);

    xlog "Run squatter with read-only cache directory";
    my $cachedir = tempdir(DIR => $instance->{basedir} . "/tmp");
    chmod 0400, $cachedir || die;
    $self->squatter_attachextract_cache_run($cachedir, "--allow-partials");

    xlog "Assert text bodies of both messages are indexed";
    my $uids = $imap->search('fuzzy', 'body', 'bodyterm');
    $self->assert_deep_equals([1,2], $uids);

    xlog "Assert attachments of both messages are not indexed";
    $uids = $imap->search('fuzzy', 'xattachmentbody', 'attachterm');
    $self->assert_deep_equals([], $uids);

    xlog "Assert extractor got called twice with attachment uploads";
    my @tracefiles = glob($tracedir."/*_PUT_*");
    $self->assert_num_equals(2, scalar @tracefiles);

    xlog "Assert cache contains no file";
    chmod 0700, $cachedir || die;
    my @files = glob($cachedir."/*");
    $self->assert_num_equals(0, scalar @files);
}
