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

SieveScript/test: Allow prepopulating variables so your sieve scripts can behave differently during test #5176

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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 cassandane/Cassandane/Cyrus/JMAPSieve.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ sub new
}

$config->set(caldav_realm => 'Cassandane',
conversations_counted_flags => "\\Draft \\Flagged \$IsMailingList \$IsNotification \$HasAttachment \$muted",
conversations => 'yes',
httpmodules => 'carddav caldav jmap',
httpallowcompress => 'no',
Expand Down
285 changes: 285 additions & 0 deletions cassandane/tiny-tests/JMAPSieve/sieve-test-extensive
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
#!perl
use Cassandane::Tiny;
use Sub::Install;

### basic variable support
add_sieve_tests(
"variables_set_get",
'set "foo" "bar"; log "foo: ${foo}";',
[
[ 'log', {}, [ 'foo: bar' ] ],
[ 'keep', {}, [] ],
],
);

### jmapquery
add_sieve_tests(
"jmapquery_matches",
'if jmapquery "{\"from\" : \"sam\"}" {
fileinto "Matched";
}',
[ [ 'fileinto', {}, [ 'Matched' ] ] ],

"jmapquery_does_not_match",
'if jmapquery "{\"from\" : \"bugs\"}" {
fileinto "Matched";
}',
[ [ 'keep', {}, [] ] ],
);

### fileinto
add_sieve_tests(
"fileinto_basic",
'fileinto "foo";',
[ ['fileinto', {}, [ 'foo' ] ] ],

"fileinto_specialuse",
'fileinto :specialuse "\\\\junk" "Trash";',
[ ['fileinto', { specialuse => '\junk' }, [ 'Trash' ] ] ],

"fileinto_mailboxid",
'fileinto :mailboxid "a-b-c-d" "Alphabet";',
[ ['fileinto', { mailboxid => 'a-b-c-d' }, [ 'Alphabet' ] ] ],

"fileinto_create",
'fileinto :create "Server";',
[ ['fileinto', { create => JSON::true }, [ 'Server' ] ] ],

"fileinto_copy",
'fileinto :copy "Paper";',
[
['fileinto', { copy => JSON::true }, [ 'Paper' ] ],
['keep', {}, [] ],
],

"fileinto_all",
# (Can't use mailboxid and specialuse, so I picked one...)
'fileinto :mailboxid "mbid" :create :copy "Woah";',
[
[
'fileinto',
{
mailboxid => 'mbid',
create => JSON::true,
copy => JSON::true,
},
[ 'Woah' ],
],
['keep', {}, [] ],
],
);

### exists
add_sieve_tests(
"exists",
'if exists ["From"] { fileinto "FromHeaderFound"; }',
[ ['fileinto', {}, [ 'FromHeaderFound' ] ] ],

"exists_not",
'if not exists ["From"] { fileinto "FromHeaderFound"; }',
[ ['keep', {}, [] ] ],
);

sub test_sieve_test_extensive_some_in_thread_have_keyword
:min_version_3_3 :JMAPExtensions
{
my ($self) = @_;

# Create two messages, where the first is muted and the second is in
# the same conversation
my $mid = '<[email protected]>';

my (undef, $email1) = $self->new_email_blob({
'keywords' => { '$muted' => JSON::true },
'header:Message-Id' => $mid,
});

my ($email2_blob_id, $email2) = $self->new_email_blob({
'header:inReplyTo' => $mid,
'header:references' => $mid,
});

$self->assert_str_equals($email1->{threadId}, $email2->{threadId});

my $sieve_blob_id = $self->new_sieve_blob(
'if jmapquery "{\"someInThreadHaveKeyword\":\"$muted\"}" {
fileinto "muted";
}',
);

$self->run_sieve_test(
$sieve_blob_id,
$email2_blob_id,
[ ['fileinto', {}, ['muted'] ] ],
);
}

sub add_sieve_tests {
my (@tests) = @_;

while (@tests) {
my ($name, $sieve, $expect) = (
shift @tests,
shift @tests,
shift @tests,
);

if (! $expect) {
die "Expected at least 3 arguments, but didn't get it. Bad test spec?! "
. Dumper({
name => $name,
sieve => $sieve,
});
}

my $opts;

if (@tests && ref $tests[0] eq 'HASH') {
$opts = shift @tests;
}

add_sieve_test($name, $sieve, $expect, $opts);
}
}

sub add_sieve_test {
my ($name, $sieve, $expect, $opts) = @_;

unless ($name =~ /^[a-zA-Z][a-zA-Z0-9_]+$/) {
die "Test name '$name' invalid. Must look like a perl subroutine name (start with a letter, a-zA-Z0-9_ only after that...\n";
}

Sub::Install::install_sub({
code => sub :min_version_3_3 :JMAPExtensions {
shift->_do_test($sieve, $expect, $opts);
},
as => "test_sieve_test_extensive_$name",
});
}

sub _do_test {
my ($self, $sieve, $expect, $opts) = @_;

my $variables = $opts->{variables};
my $email_set_params = $opts->{email_set_params};

my $sieve_blob_id = $self->new_sieve_blob($sieve);
my $email_blob_id = $self->new_email_blob($email_set_params);

$self->run_sieve_test(
$sieve_blob_id,
$email_blob_id,
$expect,
$variables,
);
}

sub new_sieve_blob {
my ($self, $sieve) = @_;

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

xlog "create script";
my $fullscript = <<EOF;
require ["fileinto", "imap4flags", "copy", "variables", "mailbox", "mailboxid", "special-use", "vnd.cyrus.log", "vnd.cyrus.jmapquery"];

$sieve
EOF

$fullscript =~ s/\r?\n/\r\n/gs;

my $res = $jmap->CallMethods([
['Blob/upload', {
create => {
"A" => { data => [{'data:asText' => $fullscript}] }
}
}, "R0"],
['SieveScript/set', {
create => {
"1" => {
name => "foo",
blobId => "#A"
}
}
}, "R1"]
]);
$self->assert_not_null($res);

my $script_blob_id = $res->[1][1]{created}{"1"}{blobId};
$self->assert_not_null($script_blob_id);

return $script_blob_id;
}

sub new_email_blob {
my ($self, $email_set_params) = @_;

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

xlog "create email";

my $res = $jmap->CallMethods(
[['Mailbox/get', { properties => ["id"] }, "R1"]]
);
my $inbox_id = $res->[0][1]{list}[0]{id};

$self->assert_not_null($inbox_id);

my $email = {
mailboxIds => { $inbox_id => JSON::true },
from => [ { name => "Yosemite Sam", email => "sam\@acme.local" } ] ,
to => [ { name => "Bugs Bunny", email => "bugs\@acme.local" }, ],
subject => "Memo",
textBody => [{ partId => '1' }],
bodyValues => { '1' => { value => "Whoa!" }},
( $email_set_params ? ( %$email_set_params ) : () ),
};

$res = $jmap->CallMethods([
['Email/set', { create => { "1" => $email }}, "R2"],
]);

my $email_blob_id = $res->[0][1]{created}{"1"}{blobId};
$self->assert_not_null($email_blob_id);

return wantarray ? ($email_blob_id, $res->[0][1]{created}{"1"}) : $email_blob_id;
}

sub run_sieve_test {
my ($self, $sieve_blob_id, $email_blob_id, $expect, $variables) = @_;

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

xlog "test script";
my $res = $jmap->CallMethods([
['SieveScript/test', {
scriptBlobId => "$sieve_blob_id",
emailBlobIds => [ "$email_blob_id" ],
envelope => JSON::null,
lastVacationResponse => JSON::null,
( $variables ? (variables => $variables) : () ),
}, "R3"]
]);
$self->assert_not_null($res);
$self->assert_not_null($res->[0][1]{completed});
$self->assert_null($res->[0][1]{notCompleted});

eval {
$self->assert_deep_equals(
$expect,
$res->[0][1]{completed}{$email_blob_id},
);
};

if ($@) {
my $err = $@;

warn "Wanted: " . Dumper($expect);
warn "Got: " . Dumper($res->[0][1]{completed}{$email_blob_id});

# Rethrow for Test::Unit's sake
die $@;
}

return $res;
}
103 changes: 103 additions & 0 deletions cassandane/tiny-tests/JMAPSieve/sieve-test-variables
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!perl
use Cassandane::Tiny;

sub test_sieve_test_variables
:min_version_3_3 :JMAPExtensions
{
my ($self) = @_;

my $script = <<'EOF';
require ["variables", "vnd.cyrus.log"];

log "string: '${string}'";
log "int: '${int}'";
log "empty: '${empty}'";
log "missing: '${missing}'";
EOF
$script =~ s/\r?\n/\r\n/gs;
$script =~ s/\\/\\\\/gs;

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

xlog "create script";
my $res = $jmap->CallMethods([
['Blob/upload', {
create => {
"A" => { data => [{'data:asText' => $script}] }
}
}, "R0"],
['SieveScript/set', {
create => {
"1" => {
name => "foo",
blobId => "#A"
}
}
}, "R1"]
]);
$self->assert_not_null($res);

my $scriptid = $res->[1][1]{created}{"1"}{blobId};

xlog "create email";
$res = $jmap->CallMethods([['Mailbox/get', { properties => ["id"] }, "R1"]]);
my $inboxid = $res->[0][1]{list}[0]{id};

my $email = {
mailboxIds => { $inboxid => JSON::true },
from => [ { name => "Yosemite Sam", email => "sam\@acme.local" } ] ,
to => [ { name => "Bugs Bunny", email => "bugs\@acme.local" }, ],
subject => "Memo",
textBody => [{ partId => '1' }],
bodyValues => { '1' => { value => "Whoa!" }}
};

$res = $jmap->CallMethods([
['Email/set', { create => { "1" => $email }}, "R2"],
]);

my $emailid = $res->[0][1]{created}{"1"}{blobId};

xlog "test script";
$res = $jmap->CallMethods([
['SieveScript/test', {
scriptBlobId => "$scriptid",
emailBlobIds => [ "$emailid" ],
envelope => JSON::null,
lastVacationResponse => JSON::null,
variables => {
string => 'hello there',
int => '012',
empty => '',
notused => 'foo',
},
}, "R3"]
]);
$self->assert_not_null($res);
$self->assert_not_null($res->[0][1]{completed});

$self->assert_str_equals('log',
$res->[0][1]{completed}{$emailid}[0][0]);
$self->assert_str_equals("string: 'hello there'",
$res->[0][1]{completed}{$emailid}[0][2][0]);

$self->assert_str_equals('log',
$res->[0][1]{completed}{$emailid}[1][0]);
$self->assert_str_equals("int: '012'",
$res->[0][1]{completed}{$emailid}[1][2][0]);

$self->assert_str_equals('log',
$res->[0][1]{completed}{$emailid}[2][0]);
$self->assert_str_equals("empty: ''",
$res->[0][1]{completed}{$emailid}[2][2][0]);

$self->assert_str_equals('log',
$res->[0][1]{completed}{$emailid}[3][0]);
$self->assert_str_equals("missing: ''",
$res->[0][1]{completed}{$emailid}[3][2][0]);

$self->assert_str_equals('keep',
$res->[0][1]{completed}{$emailid}[4][0]);

$self->assert_null($res->[0][1]{notCompleted});
}
Loading
Loading