aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/dav/tests/Sabre/CalDAV/SharingPluginTest.php
blob: 57732cd06c34b9f3b12803253c66bd7543c59ccc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php

declare(strict_types=1);

namespace Sabre\CalDAV;

use Sabre\DAV;
use Sabre\DAV\Xml\Element\Sharee;
use Sabre\HTTP;

class SharingPluginTest extends \Sabre\DAVServerTest
{
    protected $setupCalDAV = true;
    protected $setupCalDAVSharing = true;
    protected $setupACL = true;
    protected $autoLogin = 'user1';

    public function setUp()
    {
        $this->caldavCalendars = [
            [
                'principaluri' => 'principals/user1',
                'id' => 1,
                'uri' => 'cal1',
            ],
            [
                'principaluri' => 'principals/user1',
                'id' => 2,
                'uri' => 'cal2',
                'share-access' => \Sabre\DAV\Sharing\Plugin::ACCESS_READWRITE,
            ],
            [
                'principaluri' => 'principals/user1',
                'id' => 3,
                'uri' => 'cal3',
            ],
        ];

        parent::setUp();

        // Making the logged in user an admin, for full access:
        $this->aclPlugin->adminPrincipals[] = 'principals/user2';
    }

    public function testSimple()
    {
        $this->assertInstanceOf('Sabre\\CalDAV\\SharingPlugin', $this->server->getPlugin('caldav-sharing'));
        $this->assertEquals(
            'caldav-sharing',
            $this->caldavSharingPlugin->getPluginInfo()['name']
        );
    }

    /**
     * @expectedException \LogicException
     */
    public function testSetupWithoutCoreSharingPlugin()
    {
        $server = new DAV\Server();
        $server->addPlugin(
            new SharingPlugin()
        );
    }

    public function testGetFeatures()
    {
        $this->assertEquals(['calendarserver-sharing'], $this->caldavSharingPlugin->getFeatures());
    }

    public function testBeforeGetShareableCalendar()
    {
        // Forcing the server to authenticate:
        $this->authPlugin->beforeMethod(new HTTP\Request('GET', '/'), new HTTP\Response());
        $props = $this->server->getProperties('calendars/user1/cal1', [
            '{'.Plugin::NS_CALENDARSERVER.'}invite',
            '{'.Plugin::NS_CALENDARSERVER.'}allowed-sharing-modes',
        ]);

        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{'.Plugin::NS_CALENDARSERVER.'}invite']);
        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\AllowedSharingModes', $props['{'.Plugin::NS_CALENDARSERVER.'}allowed-sharing-modes']);
    }

    public function testBeforeGetSharedCalendar()
    {
        $props = $this->server->getProperties('calendars/user1/cal2', [
            '{'.Plugin::NS_CALENDARSERVER.'}shared-url',
            '{'.Plugin::NS_CALENDARSERVER.'}invite',
        ]);

        $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{'.Plugin::NS_CALENDARSERVER.'}invite']);
        //$this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $props['{' . Plugin::NS_CALENDARSERVER . '}shared-url']);
    }

    public function testUpdateResourceType()
    {
        $this->caldavBackend->updateInvites(1,
            [
                new Sharee([
                    'href' => 'mailto:joe@example.org',
                ]),
            ]
        );
        $result = $this->server->updateProperties('calendars/user1/cal1', [
            '{DAV:}resourcetype' => new DAV\Xml\Property\ResourceType(['{DAV:}collection']),
        ]);

        $this->assertEquals([
            '{DAV:}resourcetype' => 200,
        ], $result);

        $this->assertEquals(0, count($this->caldavBackend->getInvites(1)));
    }

    public function testUpdatePropertiesPassThru()
    {
        $result = $this->server->updateProperties('calendars/user1/cal3', [
            '{DAV:}foo' => 'bar',
        ]);

        $this->assertEquals([
            '{DAV:}foo' => 200,
        ], $result);
    }

    public function testUnknownMethodNoPOST()
    {
        $request = HTTP\Sapi::createFromServerArray([
            'REQUEST_METHOD' => 'PATCH',
            'REQUEST_URI' => '/',
        ]);

        $response = $this->request($request);

        $this->assertEquals(501, $response->status, $response->body);
    }

    public function testUnknownMethodNoXML()
    {
        $request = HTTP\Sapi::createFromServerArray([
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI' => '/',
            'CONTENT_TYPE' => 'text/plain',
        ]);

        $response = $this->request($request);

        $this->assertEquals(501, $response->status, $response->body);
    }

    public function testUnknownMethodNoNode()
    {
        $request = HTTP\Sapi::createFromServerArray([
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI' => '/foo',
            'CONTENT_TYPE' => 'text/xml',
        ]);

        $response = $this->request($request);

        $this->assertEquals(501, $response->status, $response->body);
    }

    public function testShareRequest()
    {
        $request = new HTTP\Request('POST', '/calendars/user1/cal1', ['Content-Type' => 'text/xml']);

        $xml = <<<RRR
<?xml version="1.0"?>
<cs:share xmlns:cs="http://calendarserver.org/ns/" xmlns:d="DAV:">
    <cs:set>
        <d:href>mailto:joe@example.org</d:href>
        <cs:common-name>Joe Shmoe</cs:common-name>
        <cs:read-write />
    </cs:set>
    <cs:remove>
        <d:href>mailto:nancy@example.org</d:href>
    </cs:remove>
</cs:share>
RRR;

        $request->setBody($xml);

        $response = $this->request($request, 200);

        $this->assertEquals(
            [
                new Sharee([
                    'href' => 'mailto:joe@example.org',
                    'properties' => [
                        '{DAV:}displayname' => 'Joe Shmoe',
                    ],
                    'access' => \Sabre\DAV\Sharing\Plugin::ACCESS_READWRITE,
                    'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_NORESPONSE,
                    'comment' => '',
                ]),
            ],
            $this->caldavBackend->getInvites(1)
        );

        // Wiping out tree cache
        $this->server->tree->markDirty('');

        // Verifying that the calendar is now marked shared.
        $props = $this->server->getProperties('calendars/user1/cal1', ['{DAV:}resourcetype']);
        $this->assertTrue(
            $props['{DAV:}resourcetype']->is('{http://calendarserver.org/ns/}shared-owner')
        );
    }

    public function testShareRequestNoShareableCalendar()
    {
        $request = new HTTP\Request(
            'POST',
            '/calendars/user1/cal2',
            ['Content-Type' => 'text/xml']
        );

        $xml = '<?xml version="1.0"?>
<cs:share xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
    <cs:set>
        <d:href>mailto:joe@example.org</d:href>
        <cs:common-name>Joe Shmoe</cs:common-name>
        <cs:read-write />
    </cs:set>
    <cs:remove>
        <d:href>mailto:nancy@example.org</d:href>
    </cs:remove>
</cs:share>
';

        $request->setBody($xml);

        $response = $this->request($request, 403);
    }

    public function testInviteReply()
    {
        $request = HTTP\Sapi::createFromServerArray([
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI' => '/calendars/user1',
            'CONTENT_TYPE' => 'text/xml',
        ]);

        $xml = '<?xml version="1.0"?>
<cs:invite-reply xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
    <cs:hosturl><d:href>/principals/owner</d:href></cs:hosturl>
    <cs:invite-accepted />
</cs:invite-reply>
';

        $request->setBody($xml);
        $response = $this->request($request);
        $this->assertEquals(200, $response->status, $response->body);
    }

    public function testInviteBadXML()
    {
        $request = HTTP\Sapi::createFromServerArray([
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI' => '/calendars/user1',
            'CONTENT_TYPE' => 'text/xml',
        ]);

        $xml = '<?xml version="1.0"?>
<cs:invite-reply xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
</cs:invite-reply>
';
        $request->setBody($xml);
        $response = $this->request($request);
        $this->assertEquals(400, $response->status, $response->body);
    }

    public function testInviteWrongUrl()
    {
        $request = HTTP\Sapi::createFromServerArray([
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI' => '/calendars/user1/cal1',
            'CONTENT_TYPE' => 'text/xml',
        ]);

        $xml = '<?xml version="1.0"?>
<cs:invite-reply xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
    <cs:hosturl><d:href>/principals/owner</d:href></cs:hosturl>
</cs:invite-reply>
';
        $request->setBody($xml);
        $response = $this->request($request);
        $this->assertEquals(501, $response->status, $response->body);

        // If the plugin did not handle this request, it must ensure that the
        // body is still accessible by other plugins.
        $this->assertEquals($xml, $request->getBody(true));
    }

    public function testPostWithoutContentType()
    {
        $request = new HTTP\Request('POST', '/');
        $response = new HTTP\ResponseMock();

        $this->caldavSharingPlugin->httpPost($request, $response);
        $this->assertTrue(true);
    }

    public function testPublish()
    {
        $request = new HTTP\Request('POST', '/calendars/user1/cal1', ['Content-Type' => 'text/xml']);

        $xml = '<?xml version="1.0"?>
<cs:publish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
';

        $request->setBody($xml);

        $response = $this->request($request);
        $this->assertEquals(202, $response->status, $response->body);
    }

    public function testUnpublish()
    {
        $request = new HTTP\Request(
            'POST',
            '/calendars/user1/cal1',
            ['Content-Type' => 'text/xml']
        );

        $xml = '<?xml version="1.0"?>
<cs:unpublish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
';

        $request->setBody($xml);

        $response = $this->request($request);
        $this->assertEquals(200, $response->status, $response->body);
    }

    public function testPublishWrongUrl()
    {
        $request = new HTTP\Request(
            'POST',
            '/calendars/user1',
            ['Content-Type' => 'text/xml']
        );

        $xml = '<?xml version="1.0"?>
<cs:publish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
';

        $request->setBody($xml);
        $this->request($request, 501);
    }

    public function testUnpublishWrongUrl()
    {
        $request = new HTTP\Request(
            'POST',
            '/calendars/user1',
            ['Content-Type' => 'text/xml']
        );
        $xml = '<?xml version="1.0"?>
<cs:unpublish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
';

        $request->setBody($xml);

        $this->request($request, 501);
    }

    public function testUnknownXmlDoc()
    {
        $request = new HTTP\Request(
            'POST',
            '/calendars/user1/cal2',
            ['Content-Type' => 'text/xml']
        );

        $xml = '<?xml version="1.0"?>
<cs:foo-bar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />';

        $request->setBody($xml);

        $response = $this->request($request);
        $this->assertEquals(501, $response->status, $response->body);
    }
}