Blame view

sources/apps/files_sharing/tests/api.php 38.9 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  <?php
  /**
   * ownCloud
   *
   * @author Bjoern Schiessle
   * @copyright 2013 Bjoern Schiessle <schiessle@owncloud.com>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
31b7f2792   Kload   Upgrade to ownclo...
22
  require_once __DIR__ . '/base.php';
03e52840d   Kload   Init
23
24
25
26
27
28
  
  use OCA\Files\Share;
  
  /**
   * Class Test_Files_Sharing_Api
   */
31b7f2792   Kload   Upgrade to ownclo...
29
  class Test_Files_Sharing_Api extends Test_Files_Sharing_Base {
03e52840d   Kload   Init
30

6d9380f96   Cédric Dupont   Update sources OC...
31
32
33
  	const TEST_FOLDER_NAME = '/folder_share_api_test';
  
  	private static $tempStorage;
03e52840d   Kload   Init
34
  	function setUp() {
31b7f2792   Kload   Upgrade to ownclo...
35
  		parent::setUp();
03e52840d   Kload   Init
36

6d9380f96   Cédric Dupont   Update sources OC...
37
  		$this->folder = self::TEST_FOLDER_NAME;
a293d369c   Kload   Update sources to...
38
  		$this->subfolder  = '/subfolder_share_api_test';
837968727   Kload   [enh] Upgrade to ...
39
  		$this->subsubfolder = '/subsubfolder_share_api_test';
03e52840d   Kload   Init
40

837968727   Kload   [enh] Upgrade to ...
41
  		$this->filename = '/share-api-test.txt';
03e52840d   Kload   Init
42

03e52840d   Kload   Init
43
44
45
  		// save file with content
  		$this->view->file_put_contents($this->filename, $this->data);
  		$this->view->mkdir($this->folder);
837968727   Kload   [enh] Upgrade to ...
46
47
48
49
  		$this->view->mkdir($this->folder . $this->subfolder);
  		$this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  		$this->view->file_put_contents($this->folder.$this->filename, $this->data);
  		$this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
03e52840d   Kload   Init
50
51
52
53
54
  	}
  
  	function tearDown() {
  		$this->view->unlink($this->filename);
  		$this->view->deleteAll($this->folder);
03e52840d   Kload   Init
55

6d9380f96   Cédric Dupont   Update sources OC...
56
  		self::$tempStorage = null;
31b7f2792   Kload   Upgrade to ownclo...
57
  		parent::tearDown();
03e52840d   Kload   Init
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
  	}
  
  	/**
  	 * @medium
  	 */
  	function testCreateShare() {
  
  		// share to user
  
  		// simulate a post request
  		$_POST['path'] = $this->filename;
  		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  
  		$result = Share\Api::createShare(array());
  
  		$this->assertTrue($result->succeeded());
  		$data = $result->getData();
  
  		$share = $this->getShareFromId($data['id']);
  
  		$items = \OCP\Share::getItemShared('file', $share['item_source']);
  
  		$this->assertTrue(!empty($items));
  
  		// share link
  
  		// simulate a post request
  		$_POST['path'] = $this->folder;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  
  		$result = Share\Api::createShare(array());
  
  		// check if API call was successful
  		$this->assertTrue($result->succeeded());
  
  		$data = $result->getData();
  
  		// check if we have a token
  		$this->assertTrue(is_string($data['token']));
  
  		$share = $this->getShareFromId($data['id']);
  
  		$items = \OCP\Share::getItemShared('file', $share['item_source']);
  
  		$this->assertTrue(!empty($items));
  
  		$fileinfo = $this->view->getFileInfo($this->filename);
  
  		\OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		$fileinfo = $this->view->getFileInfo($this->folder);
  
  		\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
6d9380f96   Cédric Dupont   Update sources OC...
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
  	}
  
  	function testEnfoceLinkPassword() {
  
  		$appConfig = \OC::$server->getAppConfig();
  		$appConfig->setValue('core', 'shareapi_enforce_links_password', 'yes');
  
  		// don't allow to share link without a password
  		$_POST['path'] = $this->folder;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  
  
  		$result = Share\Api::createShare(array());
  		$this->assertFalse($result->succeeded());
  
  
  		// don't allow to share link without a empty password
  		$_POST['path'] = $this->folder;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  		$_POST['password'] = '';
  
  		$result = Share\Api::createShare(array());
  		$this->assertFalse($result->succeeded());
  
  		// share with password should succeed
  		$_POST['path'] = $this->folder;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  		$_POST['password'] = 'foo';
  
  		$result = Share\Api::createShare(array());
  		$this->assertTrue($result->succeeded());
  
  		$data = $result->getData();
  
  		// setting new password should succeed
  		$params = array();
  		$params['id'] = $data['id'];
  		$params['_put'] = array();
  		$params['_put']['password'] = 'bar';
  
  		$result = Share\Api::updateShare($params);
  		$this->assertTrue($result->succeeded());
  
  		// removing password should fail
  		$params = array();
  		$params['id'] = $data['id'];
  		$params['_put'] = array();
  		$params['_put']['password'] = '';
  
  		$result = Share\Api::updateShare($params);
  		$this->assertFalse($result->succeeded());
  
  		// cleanup
  		$fileinfo = $this->view->getFileInfo($this->folder);
  		\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  		$appConfig->setValue('core', 'shareapi_enforce_links_password', 'no');
  	}
  
  	/**
  	 * @medium
  	*/
  	function testSharePermissions() {
  
  		// sharing file to a user should work if shareapi_exclude_groups is set
  		// to no
  		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups', 'no');
  		$_POST['path'] = $this->filename;
  		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  
  		$result = Share\Api::createShare(array());
  
  		$this->assertTrue($result->succeeded());
  		$data = $result->getData();
  
  		$share = $this->getShareFromId($data['id']);
  
  		$items = \OCP\Share::getItemShared('file', $share['item_source']);
  
  		$this->assertTrue(!empty($items));
  
  		$fileinfo = $this->view->getFileInfo($this->filename);
  
  		$result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		$this->assertTrue($result);
  
  		// exclude groups, but not the group the user belongs to. Sharing should still work
  		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups', 'yes');
  		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
  
  		$_POST['path'] = $this->filename;
  		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  
  		$result = Share\Api::createShare(array());
03e52840d   Kload   Init
210

6d9380f96   Cédric Dupont   Update sources OC...
211
212
  		$this->assertTrue($result->succeeded());
  		$data = $result->getData();
03e52840d   Kload   Init
213

6d9380f96   Cédric Dupont   Update sources OC...
214
  		$share = $this->getShareFromId($data['id']);
03e52840d   Kload   Init
215

6d9380f96   Cédric Dupont   Update sources OC...
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
  		$items = \OCP\Share::getItemShared('file', $share['item_source']);
  
  		$this->assertTrue(!empty($items));
  
  		$fileinfo = $this->view->getFileInfo($this->filename);
  
  		$result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		$this->assertTrue($result);
  
  		// now we exclude the group the user belongs to ('group'), sharing should fail now
  		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups_list', 'admin,group');
  
  		$_POST['path'] = $this->filename;
  		$_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  
  		$result = Share\Api::createShare(array());
  
  		$this->assertFalse($result->succeeded());
  
  		// cleanup
  		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups', 'no');
  		\OC_Appconfig::setValue('core', 'shareapi_exclude_groups_list', '');
03e52840d   Kload   Init
241
  	}
6d9380f96   Cédric Dupont   Update sources OC...
242

03e52840d   Kload   Init
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  	/**
  	 * @medium
  	 * @depends testCreateShare
  	 */
  	function testGetAllShares() {
  
  		$fileinfo = $this->view->getFileInfo($this->filename);
  
  		\OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  		\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
6d9380f96   Cédric Dupont   Update sources OC...
257
  		// test should return two shares created from testCreateShare()
03e52840d   Kload   Init
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
  		$this->assertTrue(count($result->getData()) === 1);
  
  		\OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  	}
  
  	/**
  	 * @medium
  	 * @depends testCreateShare
  	 */
  	function testGetShareFromSource() {
  
  		$fileInfo = $this->view->getFileInfo($this->filename);
  
  		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  				null, 1);
  
  		$_GET['path'] = $this->filename;
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
6d9380f96   Cédric Dupont   Update sources OC...
283
  		// test should return one share created from testCreateShare()
03e52840d   Kload   Init
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
  		$this->assertTrue(count($result->getData()) === 2);
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  
  	}
  
  	/**
  	 * @medium
  	 * @depends testCreateShare
  	 */
  	function testGetShareFromSourceWithReshares() {
  
  		$fileInfo = $this->view->getFileInfo($this->filename);
  
  		// share the file as user1 to user2
  		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// login as user2 and reshare the file to user3
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  
  		// login as user1 again
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		$_GET['path'] = $this->filename;
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
  
  		// test should return one share
  		$this->assertTrue(count($result->getData()) === 1);
  
  		// now also ask for the reshares
  		$_GET['reshares'] = 'true';
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
  
  		// now we should get two shares, the initial share and the reshare
  		$this->assertTrue(count($result->getData()) === 2);
  
  		// unshare files again
  
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  	}
  
  	/**
  	 * @medium
  	 * @depends testCreateShare
  	 */
  	function testGetShareFromId() {
  
  		$fileInfo = $this->view->getFileInfo($this->filename);
  
  		$result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		// get item to determine share ID
  		$result = \OCP\Share::getItemShared('file', $fileInfo['fileid']);
  
  		$this->assertEquals(1, count($result));
  
  		// get first element
  		$share = reset($result);
  
  		// call getShare() with share ID
  		$params = array('id' => $share['id']);
  		$result = Share\Api::getShare($params);
  
  		$this->assertTrue($result->succeeded());
  
  		// test should return one share created from testCreateShare()
  		$this->assertEquals(1, count($result->getData()));
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  	}
  
  	/**
  	 * @medium
  	 */
  	function testGetShareFromFolder() {
  
  		$fileInfo1 = $this->view->getFileInfo($this->filename);
  		$fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  
  		$result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  				null, 1);
  
  		// share was successful?
  		$this->assertTrue(is_string($result));
  
  		$_GET['path'] = $this->folder;
  		$_GET['subfiles'] = 'true';
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
6d9380f96   Cédric Dupont   Update sources OC...
409
  		// test should return one share within $this->folder
03e52840d   Kload   Init
410
411
412
413
414
415
416
417
418
419
  		$this->assertTrue(count($result->getData()) === 1);
  
  		\OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		\OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
420
  	 * share a folder, than reshare a file within the shared folder and check if we construct the correct path
a293d369c   Kload   Update sources to...
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
  	 * @medium
  	 */
  	function testGetShareFromFolderReshares() {
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo1 = $this->view->getFileInfo($this->folder);
  		$fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  		$fileInfo3 = $this->view->getFileInfo($this->folder.'/' . $this->subfolder . '/' .$this->filename);
  
  		// share root folder to user2
  		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		// login as user2
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  
  		// share file in root folder
  		$result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  		// share was successful?
  		$this->assertTrue(is_string($result));
  
  		// share file in subfolder
  		$result = \OCP\Share::shareItem('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  		// share was successful?
  		$this->assertTrue(is_string($result));
  
  		$testValues=array(
6d9380f96   Cédric Dupont   Update sources OC...
452
453
454
455
  			array('query' => $this->folder,
  				'expectedResult' => $this->folder . $this->filename),
  			array('query' => $this->folder . $this->subfolder,
  				'expectedResult' => $this->folder . $this->subfolder . $this->filename),
a293d369c   Kload   Update sources to...
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
  		);
  		foreach ($testValues as $value) {
  
  			$_GET['path'] = $value['query'];
  			$_GET['subfiles'] = 'true';
  
  			$result = Share\Api::getAllShares(array());
  
  			$this->assertTrue($result->succeeded());
  
  			// test should return one share within $this->folder
  			$data = $result->getData();
  
  			$this->assertEquals($value['expectedResult'], $data[0]['path']);
  		}
  
  		// cleanup
  
  		\OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  		\OCP\Share::unshare('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		\OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
485
  	 * reshare a sub folder and check if we get the correct path
837968727   Kload   [enh] Upgrade to ...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  	 * @medium
  	 */
  	function testGetShareFromSubFolderReShares() {
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo = $this->view->getFileInfo($this->folder . $this->subfolder);
  
  		// share sub-folder to user2
  		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		// login as user2
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  
  		// reshare subfolder
  		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  
  		// share was successful?
  		$this->assertTrue(is_string($result));
6d9380f96   Cédric Dupont   Update sources OC...
509
  		$_GET['path'] = '/';
837968727   Kload   [enh] Upgrade to ...
510
511
512
513
514
515
516
517
518
519
520
  		$_GET['subfiles'] = 'true';
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
  
  		// test should return one share within $this->folder
  		$data = $result->getData();
  
  		// we should get exactly one result
  		$this->assertEquals(1, count($data));
6d9380f96   Cédric Dupont   Update sources OC...
521
  		$expectedPath = $this->subfolder;
837968727   Kload   [enh] Upgrade to ...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
  		$this->assertEquals($expectedPath, $data[0]['path']);
  
  		// cleanup
  		$result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  		$this->assertTrue($result);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  		$result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  		$this->assertTrue($result);
  
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
536
  	 * test re-re-share of folder if the path gets constructed correctly
837968727   Kload   [enh] Upgrade to ...
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
  	 * @medium
  	 */
  	function testGetShareFromFolderReReShares() {
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  		$fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder);
  
  		// share sub-folder to user2
  		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		// login as user2
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  
  		// reshare subsubfolder
  		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  		// share was successful?
  		$this->assertTrue($result);
  
  		// login as user3
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  
  		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  		// share was successful?
  		$this->assertTrue(is_string($result));
6d9380f96   Cédric Dupont   Update sources OC...
568
  		$_GET['path'] = '/';
837968727   Kload   [enh] Upgrade to ...
569
570
571
572
573
574
575
576
577
578
579
  		$_GET['subfiles'] = 'true';
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
  
  		// test should return one share within $this->folder
  		$data = $result->getData();
  
  		// we should get exactly one result
  		$this->assertEquals(1, count($data));
6d9380f96   Cédric Dupont   Update sources OC...
580
  		$expectedPath = $this->subsubfolder;
837968727   Kload   [enh] Upgrade to ...
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
  		$this->assertEquals($expectedPath, $data[0]['path']);
  
  
  		// cleanup
  		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  		$this->assertTrue($result);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  		$this->assertTrue($result);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  		$this->assertTrue($result);
  
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
601
  	 * test multiple shared folder if the path gets constructed correctly
837968727   Kload   [enh] Upgrade to ...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  	 * @medium
  	 */
  	function testGetShareMultipleSharedFolder() {
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo1 = $this->view->getFileInfo($this->folder);
  		$fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder);
  
  
  		// share sub-folder to user2
  		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		// share folder to user2
  		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  
  		// login as user2
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  
  		$result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  		// share was successful?
  		$this->assertTrue(is_string($result));
6d9380f96   Cédric Dupont   Update sources OC...
633
634
  		// ask for subfolder
  		$expectedPath1 = $this->subfolder;
837968727   Kload   [enh] Upgrade to ...
635
636
637
638
639
640
641
642
643
  		$_GET['path'] = $expectedPath1;
  
  		$result1 = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result1->succeeded());
  
  		// test should return one share within $this->folder
  		$data1 = $result1->getData();
  		$share1 = reset($data1);
6d9380f96   Cédric Dupont   Update sources OC...
644
645
  		// ask for folder/subfolder
  		$expectedPath2 = $this->folder . $this->subfolder;
837968727   Kload   [enh] Upgrade to ...
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
  		$_GET['path'] = $expectedPath2;
  
  		$result2 = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result2->succeeded());
  
  		// test should return one share within $this->folder
  		$data2 = $result2->getData();
  		$share2 = reset($data2);
  
  
  		// validate results
  		// we should get exactly one result each time
  		$this->assertEquals(1, count($data1));
  		$this->assertEquals(1, count($data2));
  
  		$this->assertEquals($expectedPath1, $share1['path']);
  		$this->assertEquals($expectedPath2, $share2['path']);
  
  
  		// cleanup
  		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  		$this->assertTrue($result);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  		$this->assertTrue($result);
  		$result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  		$this->assertTrue($result);
  
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
681
  	 * test re-re-share of folder if the path gets constructed correctly
837968727   Kload   [enh] Upgrade to ...
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
  	 * @medium
  	 */
  	function testGetShareFromFileReReShares() {
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  		$fileInfo2 = $this->view->getFileInfo($this->folder. $this->subfolder . $this->filename);
  
  		// share sub-folder to user2
  		$result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		// login as user2
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  
  		// reshare subsubfolder
  		$result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  		// share was successful?
  		$this->assertTrue($result);
  
  		// login as user3
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  
  		$result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  		// share was successful?
  		$this->assertTrue(is_string($result));
6d9380f96   Cédric Dupont   Update sources OC...
713
  		$_GET['path'] = '/';
837968727   Kload   [enh] Upgrade to ...
714
715
716
717
718
719
720
721
722
723
724
  		$_GET['subfiles'] = 'true';
  
  		$result = Share\Api::getAllShares(array());
  
  		$this->assertTrue($result->succeeded());
  
  		// test should return one share within $this->folder
  		$data = $result->getData();
  
  		// we should get exactly one result
  		$this->assertEquals(1, count($data));
6d9380f96   Cédric Dupont   Update sources OC...
725
  		$expectedPath = $this->filename;
837968727   Kload   [enh] Upgrade to ...
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
  		$this->assertEquals($expectedPath, $data[0]['path']);
  
  
  		// cleanup
  		$result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  		$this->assertTrue($result);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  		$result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  		$this->assertTrue($result);
  
  		self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  		$this->assertTrue($result);
  
  	}
  
  	/**
03e52840d   Kload   Init
746
747
748
749
750
751
752
753
754
  	 * @medium
  	 */
  	function testGetShareFromUnknownId() {
  
  		$params = array('id' => 0);
  
  		$result = Share\Api::getShare($params);
  
  		$this->assertEquals(404, $result->getStatusCode());
6d9380f96   Cédric Dupont   Update sources OC...
755
  		$meta = $result->getMeta();
03e52840d   Kload   Init
756
757
758
759
760
761
762
763
764
765
766
767
768
  		$this->assertEquals('share doesn\'t exist', $meta['message']);
  
  	}
  
  	/**
  	 * @medium
  	 * @depends testCreateShare
  	 */
  	function testUpdateShare() {
  
  		$fileInfo = $this->view->getFileInfo($this->filename);
  
  		$result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
6d9380f96   Cédric Dupont   Update sources OC...
769
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_ALL);
03e52840d   Kload   Init
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
  
  		// share was successful?
  		$this->assertTrue($result);
  
  		$result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  				null, 1);
  
  		// share was successful?
  		$this->assertTrue(is_string($result));
  
  		$items = \OCP\Share::getItemShared('file', null);
  
  		// make sure that we found a link share and a user share
  		$this->assertEquals(count($items), 2);
  
  		$linkShare = null;
  		$userShare = null;
  
  		foreach ($items as $item) {
  			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  				$linkShare = $item;
  			}
  			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  				$userShare = $item;
  			}
  		}
  
  		// make sure that we found a link share and a user share
  		$this->assertTrue(is_array($linkShare));
  		$this->assertTrue(is_array($userShare));
6d9380f96   Cédric Dupont   Update sources OC...
800
801
802
  		// check if share have expected permissions, single shared files never have
  		// delete permissions
  		$this->assertEquals(\OCP\PERMISSION_ALL & ~\OCP\PERMISSION_DELETE, $userShare['permissions']);
03e52840d   Kload   Init
803

6d9380f96   Cédric Dupont   Update sources OC...
804
  		// update permissions
03e52840d   Kload   Init
805
806
807
808
809
810
811
  
  		$params = array();
  		$params['id'] = $userShare['id'];
  		$params['_put'] = array();
  		$params['_put']['permissions'] = 1;
  
  		$result = Share\Api::updateShare($params);
6d9380f96   Cédric Dupont   Update sources OC...
812
  		$meta = $result->getMeta();
03e52840d   Kload   Init
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
  		$this->assertTrue($result->succeeded(), $meta['message']);
  
  		$items = \OCP\Share::getItemShared('file', $userShare['file_source']);
  
  		$newUserShare = null;
  		foreach ($items as $item) {
  			if ($item['share_with'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2) {
  				$newUserShare = $item;
  				break;
  			}
  		}
  
  		$this->assertTrue(is_array($newUserShare));
  
  		$this->assertEquals('1', $newUserShare['permissions']);
  
  		// update password for link share
  
  		$this->assertTrue(empty($linkShare['share_with']));
  
  		$params = array();
  		$params['id'] = $linkShare['id'];
  		$params['_put'] = array();
  		$params['_put']['password'] = 'foo';
  
  		$result = Share\Api::updateShare($params);
  
  		$this->assertTrue($result->succeeded());
  
  		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  
  		$newLinkShare = null;
  		foreach ($items as $item) {
  			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  				$newLinkShare = $item;
  				break;
  			}
  		}
  
  		$this->assertTrue(is_array($newLinkShare));
  		$this->assertTrue(!empty($newLinkShare['share_with']));
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  
  	}
  
  	/**
  	 * @medium
  	 */
  	function testUpdateShareUpload() {
  
  		$fileInfo = $this->view->getFileInfo($this->folder);
  
  		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  				null, 1);
  
  		// share was successful?
  		$this->assertTrue(is_string($result));
  
  		$items = \OCP\Share::getItemShared('file', null);
  
  		// make sure that we found a link share and a user share
6d9380f96   Cédric Dupont   Update sources OC...
878
  		$this->assertEquals(1, count($items));
03e52840d   Kload   Init
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
  
  		$linkShare = null;
  
  		foreach ($items as $item) {
  			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  				$linkShare = $item;
  			}
  		}
  
  		// make sure that we found a link share
  		$this->assertTrue(is_array($linkShare));
  
  		// update public upload
  
  		$params = array();
  		$params['id'] = $linkShare['id'];
  		$params['_put'] = array();
  		$params['_put']['publicUpload'] = 'true';
  
  		$result = Share\Api::updateShare($params);
  
  		$this->assertTrue($result->succeeded());
  
  		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  
  		$updatedLinkShare = null;
  		foreach ($items as $item) {
  			if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  				$updatedLinkShare = $item;
  				break;
  			}
  		}
  
  		$this->assertTrue(is_array($updatedLinkShare));
  		$this->assertEquals(7, $updatedLinkShare['permissions']);
  
  		// cleanup
  
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  
  	}
  
  	/**
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
  	 */
  	function testUpdateShareExpireDate() {
  
  		$fileInfo = $this->view->getFileInfo($this->folder);
  
  		// enforce expire date, by default 7 days after the file was shared
  		\OCP\Config::setAppValue('core', 'shareapi_default_expire_date', 'yes');
  		\OCP\Config::setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  
  		$dateWithinRange = new \DateTime();
  		$dateWithinRange->add(new \DateInterval('P5D'));
  		$dateOutOfRange = new \DateTime();
  		$dateOutOfRange->add(new \DateInterval('P8D'));
  
  		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  				null, 1);
  
  		// share was successful?
  		$this->assertTrue(is_string($result));
  
  		$items = \OCP\Share::getItemShared('file', null);
  
  		// make sure that we found a link share
  		$this->assertEquals(1, count($items));
  
  		$linkShare = reset($items);
  
  		// update expire date to a valid value
  		$params = array();
  		$params['id'] = $linkShare['id'];
  		$params['_put'] = array();
  		$params['_put']['expireDate'] = $dateWithinRange->format('Y-m-d');
  
  		$result = Share\Api::updateShare($params);
  
  		$this->assertTrue($result->succeeded());
  
  		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  
  		$updatedLinkShare = reset($items);
  
  		// date should be changed
  		$this->assertTrue(is_array($updatedLinkShare));
  		$this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  
  		// update expire date to a value out of range
  		$params = array();
  		$params['id'] = $linkShare['id'];
  		$params['_put'] = array();
  		$params['_put']['expireDate'] = $dateOutOfRange->format('Y-m-d');
  
  		$result = Share\Api::updateShare($params);
  
  		$this->assertFalse($result->succeeded());
  
  		$items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  
  		$updatedLinkShare = reset($items);
  
  		// date shouldn't be changed
  		$this->assertTrue(is_array($updatedLinkShare));
  		$this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  
  		// cleanup
  		\OCP\Config::setAppValue('core', 'shareapi_default_expire_date', 'no');
  		\OCP\Config::setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  		\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  
  	}
  
  	/**
  	 * @medium
03e52840d   Kload   Init
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  	 * @depends testCreateShare
  	 */
  	function testDeleteShare() {
  
  		$fileInfo = $this->view->getFileInfo($this->filename);
  
  		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  				null, 1);
  
  		$items = \OCP\Share::getItemShared('file', null);
  
  		$this->assertEquals(2, count($items));
  
  		foreach ($items as $item) {
  			$result = Share\Api::deleteShare(array('id' => $item['id']));
  
  			$this->assertTrue($result->succeeded());
  		}
  
  		$itemsAfterDelete = \OCP\Share::getItemShared('file', null);
  
  		$this->assertTrue(empty($itemsAfterDelete));
  
  	}
837968727   Kload   [enh] Upgrade to ...
1022
1023
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
1024
  	 * test unshare of a reshared file
837968727   Kload   [enh] Upgrade to ...
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  	 */
  	function testDeleteReshare() {
  
  		// user 1 shares a folder with user2
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo1 = $this->view->getFileInfo($this->folder);
  		$fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  
  		$result1 = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		$this->assertTrue($result1);
  
  		// user2 shares a file from the folder as link
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		$result2 = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  
  		$this->assertTrue(is_string($result2));
  
  		// test if we can unshare the link again
  		$items = \OCP\Share::getItemShared('file', null);
  		$this->assertEquals(1, count($items));
  
  		$item = reset($items);
  		$result3 = Share\Api::deleteShare(array('id' => $item['id']));
  
  		$this->assertTrue($result3->succeeded());
6d9380f96   Cédric Dupont   Update sources OC...
1054
1055
1056
1057
1058
1059
1060
  		// cleanup
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		$result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		$this->assertTrue($result);
837968727   Kload   [enh] Upgrade to ...
1061
  	}
6d9380f96   Cédric Dupont   Update sources OC...
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  	/**
  	 * share a folder which contains a share mount point, should be forbidden
  	 */
  	public function testShareFolderWithAMountPoint() {
  		// user 1 shares a folder with user2
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo = $this->view->getFileInfo($this->folder);
  
  		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		$this->assertTrue($result);
  
  		// user2 shares a file from the folder as link
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		$view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  		$view->mkdir("localDir");
  
  		// move mount point to the folder "localDir"
  		$result = $view->rename($this->folder, 'localDir/'.$this->folder);
  		$this->assertTrue($result !== false);
  
  		// try to share "localDir"
  		$fileInfo2 = $view->getFileInfo('localDir');
  
  		$this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);
  
  		try {
  			$result2 = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  					\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  		} catch (\Exception $e) {
  			$result2 = false;
  		}
  
  		$this->assertFalse($result2);
837968727   Kload   [enh] Upgrade to ...
1099

6d9380f96   Cédric Dupont   Update sources OC...
1100
  		//cleanup
837968727   Kload   [enh] Upgrade to ...
1101

6d9380f96   Cédric Dupont   Update sources OC...
1102
1103
1104
1105
1106
1107
1108
1109
  		$result = $view->rename('localDir/' . $this->folder, $this->folder);
  		$this->assertTrue($result !== false);
  		$view->unlink('localDir');
  
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		\OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
837968727   Kload   [enh] Upgrade to ...
1110
  	}
6d9380f96   Cédric Dupont   Update sources OC...
1111
1112
1113
1114
1115
1116
1117
1118
  	/**
  	 * Post init mount points hook for mounting simulated ext storage
  	 */
  	public static function initTestMountPointsHook($data) {
  		if ($data['user'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1) {
  			\OC\Files\Filesystem::mount(self::$tempStorage, array(), '/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  		}
  	}
837968727   Kload   [enh] Upgrade to ...
1119

6d9380f96   Cédric Dupont   Update sources OC...
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
  	/**
  	 * Tests mounting a folder that is an external storage mount point.
  	 */
  	public function testShareStorageMountPoint() {
  		self::$tempStorage = new \OC\Files\Storage\Temporary(array());
  		self::$tempStorage->file_put_contents('test.txt', 'abcdef');
  		self::$tempStorage->getScanner()->scan('');
  
  		// needed because the sharing code sometimes switches the user internally and mounts the user's
  		// storages. In our case the temp storage isn't mounted automatically, so doing it in the post hook
  		// (similar to how ext storage works)
  		OCP\Util::connectHook('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  
  		// logging in will auto-mount the temp storage for user1 as well
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		$fileInfo = $this->view->getFileInfo($this->folder);
  
  		// user 1 shares the mount point folder with user2
  		$result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  
  		$this->assertTrue($result);
  
  		// user2: check that mount point name appears correctly
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		$view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  
  		$this->assertTrue($view->file_exists($this->folder));
  		$this->assertTrue($view->file_exists($this->folder . '/test.txt'));
  
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		\OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  			\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  
  		\OC_Hook::clear('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  	}
  	/**
  	 * @expectedException \Exception
  	 */
  	public function testShareNonExisting() {
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		$id = PHP_INT_MAX - 1;
  		\OCP\Share::shareItem('file', $id, \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  	}
  
  	/**
  	 * @expectedException \Exception
  	 */
  	public function testShareNotOwner() {
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  		\OC\Files\Filesystem::file_put_contents('foo.txt', 'bar');
  		$info = \OC\Files\Filesystem::getFileInfo('foo.txt');
  
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  
  		\OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  	}
  
  	public function testDefaultExpireDate() {
  		\Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  		\OC_Appconfig::setValue('core', 'shareapi_default_expire_date', 'yes');
  		\OC_Appconfig::setValue('core', 'shareapi_enforce_expire_date', 'yes');
  		\OC_Appconfig::setValue('core', 'shareapi_expire_after_n_days', '2');
  
  		// default expire date is set to 2 days
  		// the time when the share was created is set to 3 days in the past
  		// user defined expire date is set to +2 days from now on
  		// -> link should be already expired by the default expire date but the user
  		//    share should still exists.
  		$now = time();
  		$dateFormat = 'Y-m-d H:i:s';
  		$shareCreated = $now - 3 * 24 * 60 * 60;
  		$expireDate = date($dateFormat, $now + 2 * 24 * 60 * 60);
  
  		$info = OC\Files\Filesystem::getFileInfo($this->filename);
  		$this->assertTrue($info instanceof \OC\Files\FileInfo);
  
  		$result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, null, \OCP\PERMISSION_READ);
  		$this->assertTrue(is_string($result));
  
  		$result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  		$this->assertTrue($result);
  
  		$result = \OCP\Share::setExpirationDate('file', $info->getId() , $expireDate, $now);
  		$this->assertTrue($result);
  
  		//manipulate stime so that both shares are older then the default expire date
  		$statement = "UPDATE `*PREFIX*share` SET `stime` = ? WHERE `share_type` = ?";
  		$query = \OCP\DB::prepare($statement);
  		$result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_LINK));
  		$this->assertSame(1, $result);
  		$query = \OCP\DB::prepare($statement);
  		$result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_USER));
  		$this->assertSame(1, $result);
  
  		// now the link share should expire because of enforced default expire date
  		// the user share should still exist
  		$result = \OCP\Share::getItemShared('file', $info->getId());
  		$this->assertTrue(is_array($result));
  		$this->assertSame(1, count($result));
  		$share = reset($result);
  		$this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
  
  		//cleanup
  		$result = \OCP\Share::unshare('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  		$this->assertTrue($result);
  		\OC_Appconfig::setValue('core', 'shareapi_default_expire_date', 'no');
  		\OC_Appconfig::setValue('core', 'shareapi_enforce_expire_date', 'no');
  
  	}
03e52840d   Kload   Init
1234
  }