#!perl
use Cassandane::Tiny;

sub test_quota_get
    :needs_component_jmap
{
    my ($self) = @_;

    my $jmap = $self->{jmap};

    xlog $self, "Install a sieve script";
    my $script = <<EOF;
keep;\r
EOF
    $self->{instance}->install_sieve_script($script);

    xlog "Get all Sieve scripts (to create #sieve mailbox) ";
    my $res = $jmap->CallMethods([
        ['SieveScript/get', {
         }, "R1"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('SieveScript/get', $res->[0][0]);
    $self->assert_str_equals('R1', $res->[0][2]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});

    xlog "Get all calendars (to create #calendars mailbox) ";
    $res = $jmap->CallMethods([
        ['Calendar/get', {
         }, "R1"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Calendar/get', $res->[0][0]);
    $self->assert_str_equals('R1', $res->[0][2]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});

    # Right - let's set ourselves some basic usage quota
    $self->_set_quotaroot('user.cassandane');
    $self->_set_limits(
        storage => 100000,
        message => 50000,
        mailbox => 100,
    );

    $self->_set_quotaroot('user.cassandane.#calendars');
    $self->_set_limits(
        storage => 10000,
        message => 5000,
        mailbox => 10,
    );

    xlog "Get all quotas";
    $res = $jmap->CallMethods([
        ['Quota/get', {
         }, "R1"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Quota/get', $res->[0][0]);
    $self->assert_str_equals('R1', $res->[0][2]);
    $self->assert_num_equals(7, scalar @{$res->[0][1]{list}});

    my $id = $res->[0][1]{list}[0]{id};
    my $name = $res->[0][1]{list}[0]{name};

    xlog "Get quota by id";
    $res = $jmap->CallMethods([
        ['Quota/get', {
            ids => [$id],
         }, "R2"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Quota/get', $res->[0][0]);
    $self->assert_str_equals('R2', $res->[0][2]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});
    $self->assert_str_equals($name, $res->[0][1]{list}[0]{name});
}
