Debian Codegraph @

cassandane/Cassandane/Cyrus/JMAPContacts.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2017 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::JMAPContacts;
41 use strict;
42 use warnings;
43 use DateTime;
44 use JSON::XS;
45 use Net::CalDAVTalk 0.09;
46 use Net::CardDAVTalk 0.03;
47 use Mail::JMAPTalk 0.13;
48 use Data::Dumper;
49 use Storable 'dclone';
50 use File::Basename;
51 use File::Copy;
52 use Cwd qw(abs_path getcwd);
53
54 use lib '.';
55 use base qw(Cassandane::Cyrus::TestCase);
56 use Cassandane::Util::Log;
57 use Cassandane::Util::Slurp;
58
59 use charnames ':full';
60
61 sub new
62 {
63 my ($class, @args) = @_;
64
65 my $config = Cassandane::Config->default()->clone();
66 $config->set(carddav_realm => 'Cassandane',
67 conversations => 'yes',
68 httpmodules => 'carddav jmap',
69 httpallowcompress => 'no',
70 vcard_max_size => 100000,
71 jmap_nonstandard_extensions => 'yes');
72
73 my $self = $class->SUPER::new({
74 config => $config,
75 jmap => 1,
76 adminstore => 1,
77 services => [ 'imap', 'http' ]
78 }, @args);
79
80 $self->needs('component', 'jmap');
81 return $self;
82 }
83
84 sub set_up
85 {
86 my ($self) = @_;
87 $self->SUPER::set_up();
88 $self->{jmap}->DefaultUsing([
89 'urn:ietf:params:jmap:core',
90 'https://cyrusimap.org/ns/jmap/contacts',
91 'https://cyrusimap.org/ns/jmap/debug',
92 ]);
93
94 my $buildinfo = Cassandane::BuildInfo->new();
95 if ($buildinfo->get('dependency', 'icalvcard')) {
96 $self->{jmap}->AddUsing('urn:ietf:params:jmap:contacts');
97 }
98 }
99
100 sub normalize_jscard
101 {
102 my ($jscard) = @_;
103
104 if ($jscard->{vCardProps}) {
105 my @sorted = sort { $a->[0] cmp $b->[0] } @{$jscard->{vCardProps}};
106 $jscard->{vCardProps} = \@sorted;
107 }
108
109 if (not exists $jscard->{kind}) {
110 $jscard->{kind} = 'individual';
111 }
112
113 if (not exists $jscard->{'cyrusimap.org:importance'}) {
114 $jscard->{'cyrusimap.org:importance'} = '0';
115 }
116 }
117
118 sub _set_quotaroot
119 {
120 my ($self, $quotaroot) = @_;
121 $self->{quotaroot} = $quotaroot;
122 }
123
124 sub _set_quotalimits
125 {
126 my ($self, %resources) = @_;
127 my $admintalk = $self->{adminstore}->get_client();
128
129 my $quotaroot = delete $resources{quotaroot} || $self->{quotaroot};
130 my @quotalist;
131 foreach my $resource (keys %resources)
132 {
133 my $limit = $resources{$resource}
134 or die "No limit specified for $resource";
135 push(@quotalist, uc($resource), $limit);
136 }
137 $self->{limits}->{$quotaroot} = { @quotalist };
138 $admintalk->setquota($quotaroot, \@quotalist);
139 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
140 }
141
142 use Cassandane::Tiny::Loader 'tiny-tests/JMAPContacts';
143
144 1;