| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-2023 FastMail Pty Ltd. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. The name "Fastmail Pty Ltd" must not be used to |
| 18 |
# endorse or promote products derived from this software without |
| 19 |
# prior written permission. For permission or any legal |
| 20 |
# details, please contact |
| 21 |
# FastMail Pty Ltd |
| 22 |
# PO Box 234 |
| 23 |
# Collins St West 8007 |
| 24 |
# Victoria |
| 25 |
# Australia |
| 26 |
# |
| 27 |
# 4. Redistributions of any form whatsoever must retain the following |
| 28 |
# acknowledgment: |
| 29 |
# "This product includes software developed by Fastmail Pty. Ltd." |
| 30 |
# |
| 31 |
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 32 |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 33 |
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT |
| 34 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 35 |
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 36 |
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
# OF THIS SOFTWARE. |
| 38 |
# |
| 39 |
|
| 40 |
package Cassandane::Cyrus::JMAPVacationResponse; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use DateTime; |
| 44 |
use JSON; |
| 45 |
use JSON::XS; |
| 46 |
use Mail::JMAPTalk 0.13; |
| 47 |
use Data::Dumper; |
| 48 |
use Storable 'dclone'; |
| 49 |
use File::Basename; |
| 50 |
use IO::File; |
| 51 |
|
| 52 |
use lib '.'; |
| 53 |
use base qw(Cassandane::Cyrus::TestCase); |
| 54 |
use Cassandane::Util::Log; |
| 55 |
|
| 56 |
use charnames ':full'; |
| 57 |
|
| 58 |
sub new |
| 59 |
{ |
| 60 |
my ($class, @args) = @_; |
| 61 |
|
| 62 |
my $config = Cassandane::Config->default()->clone(); |
| 63 |
|
| 64 |
my ($maj, $min) = Cassandane::Instance->get_version(); |
| 65 |
if ($maj == 3 && $min == 0) { |
| 66 |
# need to explicitly add 'body' to sieve_extensions for 3.0 |
| 67 |
$config->set(sieve_extensions => |
| 68 |
"fileinto reject vacation vacation-seconds imap4flags notify " . |
| 69 |
"envelope relational regex subaddress copy date index " . |
| 70 |
"imap4flags mailbox mboxmetadata servermetadata variables " . |
| 71 |
"body"); |
| 72 |
} |
| 73 |
elsif ($maj < 3) { |
| 74 |
# also for 2.5 (the earliest Cyrus that Cassandane can test) |
| 75 |
$config->set(sieve_extensions => |
| 76 |
"fileinto reject vacation vacation-seconds imap4flags notify " . |
| 77 |
"envelope relational regex subaddress copy date index " . |
| 78 |
"imap4flags body"); |
| 79 |
} |
| 80 |
|
| 81 |
$config->set(caldav_realm => 'Cassandane', |
| 82 |
conversations => 'yes', |
| 83 |
httpmodules => 'jmap', |
| 84 |
httpallowcompress => 'no'); |
| 85 |
|
| 86 |
my $self = $class->SUPER::new({ |
| 87 |
config => $config, |
| 88 |
jmap => 1, |
| 89 |
deliver => 1, |
| 90 |
adminstore => 1, |
| 91 |
services => [ 'imap', 'sieve', 'http' ] |
| 92 |
}, @args); |
| 93 |
|
| 94 |
$self->needs('component', 'jmap'); |
| 95 |
return $self; |
| 96 |
} |
| 97 |
|
| 98 |
sub set_up |
| 99 |
{ |
| 100 |
my ($self) = @_; |
| 101 |
$self->SUPER::set_up(); |
| 102 |
$self->{jmap}->DefaultUsing([ |
| 103 |
'urn:ietf:params:jmap:core', |
| 104 |
'urn:ietf:params:jmap:vacationresponse' |
| 105 |
]); |
| 106 |
} |
| 107 |
|
| 108 |
sub tear_down |
| 109 |
{ |
| 110 |
my ($self) = @_; |
| 111 |
$self->SUPER::tear_down(); |
| 112 |
} |
| 113 |
|
| 114 |
sub test_vacation_get_none |
| 115 |
:min_version_3_9 |
| 116 |
{ |
| 117 |
my ($self) = @_; |
| 118 |
|
| 119 |
my $jmap = $self->{jmap}; |
| 120 |
|
| 121 |
xlog "get vacation"; |
| 122 |
my $res = $jmap->CallMethods([ |
| 123 |
['VacationResponse/get', { |
| 124 |
properties => ['isEnabled'] |
| 125 |
}, "R1"]]); |
| 126 |
$self->assert_not_null($res); |
| 127 |
$self->assert_str_equals('VacationResponse/get', $res->[0][0]); |
| 128 |
$self->assert_str_equals('R1', $res->[0][2]); |
| 129 |
$self->assert_num_equals(1, scalar @{$res->[0][1]{list}}); |
| 130 |
$self->assert_str_equals('singleton', $res->[0][1]{list}[0]{id}); |
| 131 |
$self->assert_equals(JSON::false, $res->[0][1]{list}[0]{isEnabled}); |
| 132 |
$self->assert(not exists $res->[0][1]{list}[0]{subject}); |
| 133 |
} |
| 134 |
|
| 135 |
sub test_vacation_set |
| 136 |
:min_version_3_9 |
| 137 |
{ |
| 138 |
my ($self) = @_; |
| 139 |
|
| 140 |
my $jmap = $self->{jmap}; |
| 141 |
|
| 142 |
xlog "attempt to create a new vacation response"; |
| 143 |
my $res = $jmap->CallMethods([ |
| 144 |
['VacationResponse/set', { |
| 145 |
create => { |
| 146 |
"1" => { |
| 147 |
textBody => "Gone fishing" |
| 148 |
} |
| 149 |
} |
| 150 |
}, "R1"]]); |
| 151 |
$self->assert_not_null($res); |
| 152 |
$self->assert_str_equals('VacationResponse/set', $res->[0][0]); |
| 153 |
$self->assert_str_equals('R1', $res->[0][2]); |
| 154 |
$self->assert_str_equals('singleton', $res->[0][1]{notCreated}{1}{type}); |
| 155 |
|
| 156 |
xlog "enable the vacation response"; |
| 157 |
$res = $jmap->CallMethods([ |
| 158 |
['VacationResponse/set', { |
| 159 |
update => { |
| 160 |
"singleton" => { |
| 161 |
isEnabled=> JSON::true, |
| 162 |
textBody => "Gone fishing" |
| 163 |
} |
| 164 |
} |
| 165 |
}, "R1"], |
| 166 |
['VacationResponse/get', { |
| 167 |
}, "R2"] |
| 168 |
]); |
| 169 |
$self->assert_not_null($res); |
| 170 |
$self->assert_str_equals('VacationResponse/set', $res->[0][0]); |
| 171 |
$self->assert_str_equals('R1', $res->[0][2]); |
| 172 |
$self->assert(exists $res->[0][1]{updated}{singleton}); |
| 173 |
$self->assert_str_equals('VacationResponse/get', $res->[1][0]); |
| 174 |
$self->assert_str_equals('R2', $res->[1][2]); |
| 175 |
$self->assert_num_equals(1, scalar @{$res->[1][1]{list}}); |
| 176 |
$self->assert_str_equals('singleton', $res->[1][1]{list}[0]{id}); |
| 177 |
$self->assert_equals(JSON::true, $res->[1][1]{list}[0]{isEnabled}); |
| 178 |
$self->assert_str_equals('Gone fishing', $res->[1][1]{list}[0]{textBody}); |
| 179 |
|
| 180 |
xlog "disable the vacation response"; |
| 181 |
$res = $jmap->CallMethods([ |
| 182 |
['VacationResponse/set', { |
| 183 |
update => { |
| 184 |
"singleton" => { |
| 185 |
isEnabled=> JSON::false |
| 186 |
} |
| 187 |
} |
| 188 |
}, "R1"], |
| 189 |
['VacationResponse/get', { |
| 190 |
}, "R2"] |
| 191 |
]); |
| 192 |
$self->assert_not_null($res); |
| 193 |
$self->assert_str_equals('VacationResponse/set', $res->[0][0]); |
| 194 |
$self->assert_str_equals('R1', $res->[0][2]); |
| 195 |
$self->assert(exists $res->[0][1]{updated}{singleton}); |
| 196 |
$self->assert_str_equals('VacationResponse/get', $res->[1][0]); |
| 197 |
$self->assert_str_equals('R2', $res->[1][2]); |
| 198 |
$self->assert_num_equals(1, scalar @{$res->[1][1]{list}}); |
| 199 |
$self->assert_str_equals('singleton', $res->[1][1]{list}[0]{id}); |
| 200 |
$self->assert_equals(JSON::false, $res->[1][1]{list}[0]{isEnabled}); |
| 201 |
$self->assert_str_equals('Gone fishing', $res->[1][1]{list}[0]{textBody}); |
| 202 |
|
| 203 |
xlog "attempt to destroy the vacation response"; |
| 204 |
$res = $jmap->CallMethods([ |
| 205 |
['VacationResponse/set', { |
| 206 |
destroy => ["singleton"] |
| 207 |
}, "R1"]]); |
| 208 |
$self->assert_not_null($res); |
| 209 |
$self->assert_str_equals('VacationResponse/set', $res->[0][0]); |
| 210 |
$self->assert_str_equals('R1', $res->[0][2]); |
| 211 |
$self->assert_str_equals('singleton', |
| 212 |
$res->[0][1]{notDestroyed}{singleton}{type}); |
| 213 |
} |
| 214 |
|
| 215 |
1; |