Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jmap_quota.c: Implement JMAP quotas per RFC9425 #4459

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,7 @@ imap_httpd_SOURCES += \
imap/jmap_notif.h \
imap/jmap_push.c \
imap/jmap_push.h \
imap/jmap_quota.c \
imap/jmap_util.c \
imap/jmap_util.h

Expand Down
120 changes: 120 additions & 0 deletions cassandane/Cassandane/Cyrus/JMAPQuota.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/perl
#
# Copyright (c) 2011-2024 FastMail Pty Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. The name "Fastmail Pty Ltd" must not be used to
# endorse or promote products derived from this software without
# prior written permission. For permission or any legal
# details, please contact
# FastMail Pty Ltd
# PO Box 234
# Collins St West 8007
# Victoria
# Australia
#
# 4. Redistributions of any form whatsoever must retain the following
# acknowledgment:
# "This product includes software developed by Fastmail Pty. Ltd."
#
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.
#

package Cassandane::Cyrus::JMAPQuota;
use strict;
use warnings;
use JSON;
use JSON::XS;
use Mail::JMAPTalk 0.13;
use Data::Dumper;

use lib '.';
use base qw(Cassandane::Cyrus::TestCase);
use Cassandane::Util::Log;

use charnames ':full';

sub new
{
my ($class, @args) = @_;

my $config = Cassandane::Config->default()->clone();

$config->set(caldav_realm => 'Cassandane',
conversations => 'yes',
httpmodules => 'jmap caldav carddav',
httpallowcompress => 'no');

return $class->SUPER::new({
config => $config,
jmap => 1,
deliver => 1,
adminstore => 1,
services => [ 'imap', 'sieve', 'http' ]
}, @args);
}

sub set_up
{
my ($self) = @_;
$self->SUPER::set_up();
$self->{jmap}->DefaultUsing([
'urn:ietf:params:jmap:core',
'urn:ietf:params:jmap:quota',
'urn:ietf:params:jmap:mail',
'urn:ietf:params:jmap:calendars',
'urn:ietf:params:jmap:sieve',
]);
}

sub _set_quotaroot
{
my ($self, $quotaroot) = @_;
$self->{quotaroot} = $quotaroot;
}

# Utility function to set quota limits and check that it stuck
sub _set_limits
{
my ($self, %resources) = @_;
my $admintalk = $self->{adminstore}->get_client();

my $quotaroot = delete $resources{quotaroot} || $self->{quotaroot};
my @quotalist;
foreach my $resource (keys %resources)
{
my $limit = $resources{$resource}
or die "No limit specified for $resource";
push(@quotalist, uc($resource), $limit);
}
$self->{limits}->{$quotaroot} = { @quotalist };
$admintalk->setquota($quotaroot, \@quotalist);
$self->assert_str_equals('ok', $admintalk->get_last_completion_response());
}

sub tear_down
{
my ($self) = @_;
$self->SUPER::tear_down();
}

use Cassandane::Tiny::Loader 'tiny-tests/JMAPQuota';

1;
129 changes: 129 additions & 0 deletions cassandane/tiny-tests/JMAPQuota/quota-changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!perl
use Cassandane::Tiny;

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

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

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

xlog "Get all quotas";
my $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(3, scalar @{$res->[0][1]{list}});

my $state = $res->[0][1]{'state'};

xlog "Get quota changes";
$res = $jmap->CallMethods([
['Quota/changes', {
"sinceState" => $state,
}, "R2"]]);
$self->assert_not_null($res);
$self->assert_str_equals('Quota/changes', $res->[0][0]);
$self->assert_str_equals('R2', $res->[0][2]);
$self->assert_num_equals(0, scalar @{$res->[0][1]{created}});
$self->assert_num_equals(0, scalar @{$res->[0][1]{updated}});
$self->assert_num_equals(0, scalar @{$res->[0][1]{destroyed}});

$state = $res->[0][1]{'newState'};

xlog "Update limit";
$self->_set_quotaroot('user.cassandane');
$self->_set_limits(
storage => 200000,
message => 50000,
mailbox => 100,
);

xlog "Get quota changes";
$res = $jmap->CallMethods([
['Quota/changes', {
"sinceState" => $state,
}, "R2"]]);
$self->assert_not_null($res);
$self->assert_str_equals('Quota/changes', $res->[0][0]);
$self->assert_str_equals('R2', $res->[0][2]);
$self->assert_num_equals(0, scalar @{$res->[0][1]{created}});
$self->assert_num_equals(3, scalar @{$res->[0][1]{updated}});
$self->assert_num_equals(0, scalar @{$res->[0][1]{destroyed}});

$state = $res->[0][1]{'newState'};

xlog "Get all calendars (to create #calendars mailbox) ";
my $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}});

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

xlog "Get quota changes";
$res = $jmap->CallMethods([
['Quota/changes', {
"sinceState" => $state,
}, "R2"]]);
$self->assert_not_null($res);
$self->assert_str_equals('Quota/changes', $res->[0][0]);
$self->assert_str_equals('R2', $res->[0][2]);
$self->assert_num_equals(0, scalar @{$res->[0][1]{created}});
$self->assert_num_equals(6, scalar @{$res->[0][1]{updated}});
$self->assert_num_equals(0, scalar @{$res->[0][1]{destroyed}});

$state = $res->[0][1]{'newState'};

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

xlog "Get quota changes";
$res = $jmap->CallMethods([
['Quota/changes', {
"sinceState" => $state,
}, "R2"]]);
$self->assert_not_null($res);
$self->assert_str_equals('Quota/changes', $res->[0][0]);
$self->assert_str_equals('R2', $res->[0][2]);
$self->assert_num_equals(0, scalar @{$res->[0][1]{created}});
$self->assert_num_equals(3, scalar @{$res->[0][1]{updated}});
$self->assert_num_equals(0, scalar @{$res->[0][1]{destroyed}});

$state = $res->[0][1]{'newState'};

xlog "Get quota changes";
$res = $jmap->CallMethods([
['Quota/changes', {
"sinceState" => $state,
}, "R2"]]);
$self->assert_not_null($res);
$self->assert_str_equals('Quota/changes', $res->[0][0]);
$self->assert_str_equals('R2', $res->[0][2]);
$self->assert_num_equals(0, scalar @{$res->[0][1]{created}});
$self->assert_num_equals(0, scalar @{$res->[0][1]{updated}});
$self->assert_num_equals(0, scalar @{$res->[0][1]{destroyed}});
}
72 changes: 72 additions & 0 deletions cassandane/tiny-tests/JMAPQuota/quota-get
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!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) ";
my $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";
my $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});
}
Loading