#!perl
use Cassandane::Tiny;

# RFC 8620 Section 5.4: a fromAccountId that does not correspond to a valid
# account is fromAccountNotFound, checked before the method runs, whatever
# the payload.

sub test_email_copy_from_inaccessible_account
    ($self)
{
    my $jmap = $self->{jmap};

    xlog $self, "create user foo but do not share anything";
    $self->{instance}->create_user("foo");

    for my $from (qw(foo nosuchuser)) {
        xlog $self, "Email/copy with nothing to copy from $from";
        my $res = $jmap->CallMethods([['Email/copy', {
            fromAccountId => $from,
            accountId     => 'cassandane',
            create        => {},
        }, 'R1']]);
        $self->assert_cmp_deeply(
            [['error', superhashof({ type => 'fromAccountNotFound' }), 'R1']],
            $res,
        );

        xlog $self, "Blob/copy with nothing to copy from $from";
        $res = $jmap->CallMethods([['Blob/copy', {
            fromAccountId => $from,
            accountId     => 'cassandane',
            blobIds       => [],
        }, 'R2']]);
        $self->assert_cmp_deeply(
            [['error', superhashof({ type => 'fromAccountNotFound' }), 'R2']],
            $res,
        );
    }

    xlog $self, "a non-string fromAccountId is invalidArguments";
    my $res = $jmap->CallMethods([['Email/copy', {
        fromAccountId => ['foo'],
        accountId     => 'cassandane',
        create        => {},
    }, 'R3']]);
    $self->assert_cmp_deeply(
        [['error', superhashof({
            type      => 'invalidArguments',
            arguments => ['fromAccountId'],
        }), 'R3']],
        $res,
    );
}
