ApiTest.php 55.5 KB
Newer Older
1
2
<?php
/**
Lukas Reschke's avatar
Lukas Reschke committed
3
 * @author Björn Schießle <bjoern@schiessle.org>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
4
5
6
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Robin Appelman <icewind@owncloud.com>
Thomas Müller's avatar
Thomas Müller committed
7
 * @author Robin McCorkell <robin@mccorkell.me.uk>
Lukas Reschke's avatar
Lukas Reschke committed
8
 * @author Roeland Jago Douma <rullzer@owncloud.com>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
9
10
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 * @author Vincent Petry <pvince81@owncloud.com>
11
 *
Thomas Müller's avatar
Thomas Müller committed
12
 * @copyright Copyright (c) 2016, ownCloud, Inc.
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
13
 * @license AGPL-3.0
14
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
15
16
17
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
18
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
21
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
23
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
24
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
27
 *
 */
28

Joas Schilling's avatar
Joas Schilling committed
29
namespace OCA\Files_Sharing\Tests;
30
31

/**
Joas Schilling's avatar
Joas Schilling committed
32
 * Class ApiTest
Thomas Müller's avatar
Thomas Müller committed
33
34
 *
 * @group DB
35
 */
Joas Schilling's avatar
Joas Schilling committed
36
class ApiTest extends TestCase {
37

38
39
40
41
	const TEST_FOLDER_NAME = '/folder_share_api_test';

	private static $tempStorage;

Roeland Jago Douma's avatar
Roeland Jago Douma committed
42
43
44
	/** @var \OCP\Files\Folder */
	private $userFolder;

45
	protected function setUp() {
46
		parent::setUp();
47

Robin Appelman's avatar
Robin Appelman committed
48
49
50
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_expire_after_n_days', '7');

51
		$this->folder = self::TEST_FOLDER_NAME;
52
		$this->subfolder  = '/subfolder_share_api_test';
53
		$this->subsubfolder = '/subsubfolder_share_api_test';
54

55
		$this->filename = '/share-api-test.txt';
56
57
58
59

		// save file with content
		$this->view->file_put_contents($this->filename, $this->data);
		$this->view->mkdir($this->folder);
60
61
62
63
		$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);
Roeland Jago Douma's avatar
Roeland Jago Douma committed
64

Roeland Jago Douma's avatar
Roeland Jago Douma committed
65
		$this->userFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
66
67
	}

68
	protected function tearDown() {
Morris Jobke's avatar
Morris Jobke committed
69
70
71
72
		if($this->view instanceof \OC\Files\View) {
			$this->view->unlink($this->filename);
			$this->view->deleteAll($this->folder);
		}
73

74
75
		self::$tempStorage = null;

76
		parent::tearDown();
77
78
	}

Roeland Jago Douma's avatar
Roeland Jago Douma committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	/**
	 * @param array $data
	 * @return \OCP\IRequest
	 */
	private function createRequest(array $data) {
		$request = $this->getMock('\OCP\IRequest');
		$request->method('getParam')
			->will($this->returnCallback(function($param, $default = null) use ($data) {
				if (isset($data[$param])) {
					return $data[$param];
				}
				return $default;
			}));
		return $request;
	}

	/**
	 * @param \OCP\IRequest $request
	 * @param string $userId The userId of the caller
	 * @return \OCA\Files_Sharing\API\Share20OCS
	 */
	private function createOCS($request, $userId) {
		$currentUser = \OC::$server->getUserManager()->get($userId);
Roeland Jago Douma's avatar
Roeland Jago Douma committed
102

103
104
105
106
107
108
		$l = $this->getMock('\OCP\IL10N');
		$l->method('t')
			->will($this->returnCallback(function($text, $parameters = []) {
				return vsprintf($text, $parameters);
			}));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
109
110
111
112
113
114
115
		return new \OCA\Files_Sharing\API\Share20OCS(
			$this->shareManager,
			\OC::$server->getGroupManager(),
			\OC::$server->getUserManager(),
			$request,
			\OC::$server->getRootFolder(),
			\OC::$server->getURLGenerator(),
116
117
			$currentUser,
			$l
Roeland Jago Douma's avatar
Roeland Jago Douma committed
118
119
120
		);
	}

121
122
123
	/**
	 * @medium
	 */
124
125
	function testCreateShareUserFile() {
		// simulate a post request
Roeland Jago Douma's avatar
Roeland Jago Douma committed
126
		$data['path'] = $this->filename;
Joas Schilling's avatar
Joas Schilling committed
127
		$data['shareWith'] = self::TEST_FILES_SHARING_API_USER2;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
128
		$data['shareType'] = \OCP\Share::SHARE_TYPE_USER;
129

Roeland Jago Douma's avatar
Roeland Jago Douma committed
130
131
132
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
133
134
135

		$this->assertTrue($result->succeeded());
		$data = $result->getData();
Roeland Jago Douma's avatar
Roeland Jago Douma committed
136
		$this->assertEquals(19, $data['permissions']);
137
138
		$this->assertEmpty($data['expiration']);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
139
		$this->shareManager->getShareById('ocinternal:'.$data['id']);
140

Roeland Jago Douma's avatar
Roeland Jago Douma committed
141
142
143
144
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
145
	}
146

147
	function testCreateShareUserFolder() {
148
		// simulate a post request
Roeland Jago Douma's avatar
Roeland Jago Douma committed
149
		$data['path'] = $this->folder;
Joas Schilling's avatar
Joas Schilling committed
150
		$data['shareWith'] = self::TEST_FILES_SHARING_API_USER2;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
151
		$data['shareType'] = \OCP\Share::SHARE_TYPE_USER;
152

Roeland Jago Douma's avatar
Roeland Jago Douma committed
153
154
155
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
156
157
158

		$this->assertTrue($result->succeeded());
		$data = $result->getData();
159
160
		$this->assertEquals(31, $data['permissions']);
		$this->assertEmpty($data['expiration']);
161

Roeland Jago Douma's avatar
Roeland Jago Douma committed
162
		$this->shareManager->getShareById('ocinternal:'.$data['id']);
163

Roeland Jago Douma's avatar
Roeland Jago Douma committed
164
165
166
167
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
168
169
170
171
172
	}


	function testCreateShareGroupFile() {
		// simulate a post request
Roeland Jago Douma's avatar
Roeland Jago Douma committed
173
		$data['path'] = $this->filename;
Joas Schilling's avatar
Joas Schilling committed
174
		$data['shareWith'] = self::TEST_FILES_SHARING_API_GROUP1;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
175
		$data['shareType'] = \OCP\Share::SHARE_TYPE_GROUP;
176

Roeland Jago Douma's avatar
Roeland Jago Douma committed
177
178
179
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
180

181
182
		$this->assertTrue($result->succeeded());
		$data = $result->getData();
Roeland Jago Douma's avatar
Roeland Jago Douma committed
183
		$this->assertEquals(19, $data['permissions']);
184
185
		$this->assertEmpty($data['expiration']);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
186
		$this->shareManager->getShareById('ocinternal:'.$data['id']);
187

Roeland Jago Douma's avatar
Roeland Jago Douma committed
188
189
190
191
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
192
193
194
195
	}

	function testCreateShareGroupFolder() {
		// simulate a post request
Roeland Jago Douma's avatar
Roeland Jago Douma committed
196
		$data['path'] = $this->folder;
Joas Schilling's avatar
Joas Schilling committed
197
		$data['shareWith'] = self::TEST_FILES_SHARING_API_GROUP1;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
198
		$data['shareType'] = \OCP\Share::SHARE_TYPE_GROUP;
199

Roeland Jago Douma's avatar
Roeland Jago Douma committed
200
201
202
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
203
204
205
206
207

		$this->assertTrue($result->succeeded());
		$data = $result->getData();
		$this->assertEquals(31, $data['permissions']);
		$this->assertEmpty($data['expiration']);
208

Roeland Jago Douma's avatar
Roeland Jago Douma committed
209
		$this->shareManager->getShareById('ocinternal:'.$data['id']);
210

Roeland Jago Douma's avatar
Roeland Jago Douma committed
211
212
213
214
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
215
	}
216

217
	public function testCreateShareLink() {
218
		// simulate a post request
Roeland Jago Douma's avatar
Roeland Jago Douma committed
219
220
		$data['path'] = $this->folder;
		$data['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
221

Roeland Jago Douma's avatar
Roeland Jago Douma committed
222
223
224
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
225
226
227
228
229

		// check if API call was successful
		$this->assertTrue($result->succeeded());

		$data = $result->getData();
230
231
		$this->assertEquals(1, $data['permissions']);
		$this->assertEmpty($data['expiration']);
232
233
		$this->assertTrue(is_string($data['token']));

Roeland Jago Douma's avatar
Roeland Jago Douma committed
234
235
236
237
		// check for correct link
		$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
		$this->assertEquals($url, $data['url']);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
238
		$this->shareManager->getShareById('ocinternal:'.$data['id']);
Roeland Jago Douma's avatar
Roeland Jago Douma committed
239

Roeland Jago Douma's avatar
Roeland Jago Douma committed
240
241
242
243
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
244
	}
245

246
247
	public function testCreateShareLinkPublicUpload() {
		// simulate a post request
Roeland Jago Douma's avatar
Roeland Jago Douma committed
248
249
250
		$data['path'] = $this->folder;
		$data['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
		$data['publicUpload'] = 'true';
251

Roeland Jago Douma's avatar
Roeland Jago Douma committed
252
253
254
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
255
256
257
258
259

		// check if API call was successful
		$this->assertTrue($result->succeeded());

		$data = $result->getData();
260
261
262
263
264
265
266
		$this->assertEquals(
			\OCP\Constants::PERMISSION_READ |
			\OCP\Constants::PERMISSION_CREATE |
			\OCP\Constants::PERMISSION_UPDATE |
			\OCP\Constants::PERMISSION_DELETE,
			$data['permissions']
		);
267
268
		$this->assertEmpty($data['expiration']);
		$this->assertTrue(is_string($data['token']));
269

270
271
272
273
		// check for correct link
		$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
		$this->assertEquals($url, $data['url']);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
274
		$this->shareManager->getShareById('ocinternal:'.$data['id']);
275

Roeland Jago Douma's avatar
Roeland Jago Douma committed
276
277
278
279
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
280
281
	}

282
283
284
285
286
287
	function testEnfoceLinkPassword() {

		$appConfig = \OC::$server->getAppConfig();
		$appConfig->setValue('core', 'shareapi_enforce_links_password', 'yes');

		// don't allow to share link without a password
Roeland Jago Douma's avatar
Roeland Jago Douma committed
288
289
		$data['path'] = $this->folder;
		$data['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
290

Roeland Jago Douma's avatar
Roeland Jago Douma committed
291
292
293
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
294
295
296
		$this->assertFalse($result->succeeded());

		// don't allow to share link without a empty password
Roeland Jago Douma's avatar
Roeland Jago Douma committed
297
298
299
300
		$data = [];
		$data['path'] = $this->folder;
		$data['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
		$data['password'] = '';
301

Roeland Jago Douma's avatar
Roeland Jago Douma committed
302
303
304
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
305
306
307
		$this->assertFalse($result->succeeded());

		// share with password should succeed
Roeland Jago Douma's avatar
Roeland Jago Douma committed
308
309
310
311
		$data = [];
		$data['path'] = $this->folder;
		$data['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
		$data['password'] = 'foo';
312

Roeland Jago Douma's avatar
Roeland Jago Douma committed
313
314
315
		$request = $this->createRequest($data);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
316
317
318
319
320
		$this->assertTrue($result->succeeded());

		$data = $result->getData();

		// setting new password should succeed
Roeland Jago Douma's avatar
Roeland Jago Douma committed
321
322
323
324
325
326
		$data2 = [
			'password' => 'bar',
		];
		$request = $this->createRequest($data2);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->updateShare($data['id']);
327
328
329
		$this->assertTrue($result->succeeded());

		// removing password should fail
Roeland Jago Douma's avatar
Roeland Jago Douma committed
330
331
332
333
334
335
		$data2 = [
			'password' => '',
		];
		$request = $this->createRequest($data2);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->updateShare($data['id']);
336
337
338
		$this->assertFalse($result->succeeded());

		// cleanup
Roeland Jago Douma's avatar
Roeland Jago Douma committed
339
340
341
342
343
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());

344
		$appConfig->setValue('core', 'shareapi_enforce_links_password', 'no');
345
346
	}

347
348
349
350
351
352
	/**
	 * @medium
	*/
	function testSharePermissions() {
		// sharing file to a user should work if shareapi_exclude_groups is set
		// to no
353
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
Roeland Jago Douma's avatar
Roeland Jago Douma committed
354
		$post['path'] = $this->filename;
Joas Schilling's avatar
Joas Schilling committed
355
		$post['shareWith'] = self::TEST_FILES_SHARING_API_USER2;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
356
		$post['shareType'] = \OCP\Share::SHARE_TYPE_USER;
357

Roeland Jago Douma's avatar
Roeland Jago Douma committed
358
359
360
		$request = $this->createRequest($post);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
361
362
363
364

		$this->assertTrue($result->succeeded());
		$data = $result->getData();

Roeland Jago Douma's avatar
Roeland Jago Douma committed
365
		$this->shareManager->getShareById('ocinternal:'.$data['id']);
366

Roeland Jago Douma's avatar
Roeland Jago Douma committed
367
368
369
370
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
371
372

		// exclude groups, but not the group the user belongs to. Sharing should still work
373
374
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'yes');
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
375

Roeland Jago Douma's avatar
Roeland Jago Douma committed
376
377
		$post = [];
		$post['path'] = $this->filename;
Joas Schilling's avatar
Joas Schilling committed
378
		$post['shareWith'] = self::TEST_FILES_SHARING_API_USER2;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
379
		$post['shareType'] = \OCP\Share::SHARE_TYPE_USER;
380

Roeland Jago Douma's avatar
Roeland Jago Douma committed
381
382
383
		$request = $this->createRequest($post);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
384
385
386
387

		$this->assertTrue($result->succeeded());
		$data = $result->getData();

Roeland Jago Douma's avatar
Roeland Jago Douma committed
388
		$this->shareManager->getShareById('ocinternal:' . $data['id']);
389

Roeland Jago Douma's avatar
Roeland Jago Douma committed
390
391
392
393
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($data['id']);
		$this->assertTrue($result->succeeded());
394
395

		// now we exclude the group the user belongs to ('group'), sharing should fail now
396
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group');
397

Roeland Jago Douma's avatar
Roeland Jago Douma committed
398
399
		$post = [];
		$post['path'] = $this->filename;
Joas Schilling's avatar
Joas Schilling committed
400
		$post['shareWith'] = self::TEST_FILES_SHARING_API_USER2;
Roeland Jago Douma's avatar
Roeland Jago Douma committed
401
		$post['shareType'] = \OCP\Share::SHARE_TYPE_USER;
402

Roeland Jago Douma's avatar
Roeland Jago Douma committed
403
404
405
		$request = $this->createRequest($post);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
406
407

		// cleanup
408
409
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
		\OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', '');
410
411
	}

412

413
414
415
416
	/**
	 * @medium
	 */
	function testGetAllShares() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
417
		$node = $this->userFolder->get($this->filename);
418

Roeland Jago Douma's avatar
Roeland Jago Douma committed
419
420
421
422
423
424
		$share = $this->shareManager->newShare();
		$share->setNode($node)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
425

Roeland Jago Douma's avatar
Roeland Jago Douma committed
426
		$share = $this->shareManager->createShare($share);
427

Roeland Jago Douma's avatar
Roeland Jago Douma committed
428
429
430
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
431
432

		$this->assertTrue($result->succeeded());
433
434
		$this->assertTrue(count($result->getData()) === 1);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
435
		$this->shareManager->deleteShare($share);
436
437
	}

438
	function testGetAllSharesWithMe() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
		$node1 = $this->userFolder->get($this->filename);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share1);

		$node2 = $this->userFolder->get($this->folder);
		$share2 = $this->shareManager->newShare();
		$share2->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share2 = $this->shareManager->createShare($share2);

		$request = $this->createRequest(['shared_with_me' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
		$result = $ocs->getShares();
460
461
462
463

		$this->assertTrue($result->succeeded());
		$this->assertTrue(count($result->getData()) === 2);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
464
465
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
466
467
	}

468
469
470
471
472
	/**
	 * @medium
	 */
	function testPublicLinkUrl() {
		// simulate a post request
Roeland Jago Douma's avatar
Roeland Jago Douma committed
473
474
		$post['path'] = $this->folder;
		$post['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
475

Roeland Jago Douma's avatar
Roeland Jago Douma committed
476
477
478
		$request = $this->createRequest($post);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->createShare();
479
		$this->assertTrue($result->succeeded());
Roeland Jago Douma's avatar
Roeland Jago Douma committed
480

481
482
483
484
485
486
487
488
489
490
491
		$data = $result->getData();

		// check if we have a token
		$this->assertTrue(is_string($data['token']));
		$id = $data['id'];

		// check for correct link
		$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
		$this->assertEquals($url, $data['url']);

		// check for link in getall shares
Roeland Jago Douma's avatar
Roeland Jago Douma committed
492
493
494
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
495
		$this->assertTrue($result->succeeded());
Roeland Jago Douma's avatar
Roeland Jago Douma committed
496

497
498
499
500
		$data = $result->getData();
		$this->assertEquals($url, current($data)['url']);

		// check for path
Roeland Jago Douma's avatar
Roeland Jago Douma committed
501
502
503
		$request = $this->createRequest(['path' => $this->folder]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
504
		$this->assertTrue($result->succeeded());
Roeland Jago Douma's avatar
Roeland Jago Douma committed
505

506
507
508
509
		$data = $result->getData();
		$this->assertEquals($url, current($data)['url']);

		// check in share id
Roeland Jago Douma's avatar
Roeland Jago Douma committed
510
511
512
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShare($id);
513
		$this->assertTrue($result->succeeded());
Roeland Jago Douma's avatar
Roeland Jago Douma committed
514

515
516
517
		$data = $result->getData();
		$this->assertEquals($url, current($data)['url']);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
518
519
520
521
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->deleteShare($id);
		$this->assertTrue($result->succeeded());
522
523
	}

524
525
	/**
	 * @medium
526
527
	 * @depends testCreateShareUserFile
	 * @depends testCreateShareLink
528
	 */
Bjoern Schiessle's avatar
Bjoern Schiessle committed
529
	function testGetShareFromSource() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
		$node = $this->userFolder->get($this->filename);
		$share = $this->shareManager->newShare();
		$share->setNode($node)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share);

		$share = $this->shareManager->newShare();
		$share->setNode($node)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share2 = $this->shareManager->createShare($share);

		$request = $this->createRequest(['path' => $this->filename]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
549
550
		$this->assertTrue($result->succeeded());

Robin McCorkell's avatar
Robin McCorkell committed
551
		// test should return one share created from testCreateShare()
552
553
		$this->assertTrue(count($result->getData()) === 2);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
554
555
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
556
557
	}

558
559
	/**
	 * @medium
560
561
	 * @depends testCreateShareUserFile
	 * @depends testCreateShareLink
562
563
	 */
	function testGetShareFromSourceWithReshares() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
		$node = $this->userFolder->get($this->filename);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share1);

		$share2 = $this->shareManager->newShare();
		$share2->setNode($node)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share2 = $this->shareManager->createShare($share2);

		$request = $this->createRequest(['path' => $this->filename]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
584
585
586
587
588
589
		$this->assertTrue($result->succeeded());

		// test should return one share
		$this->assertTrue(count($result->getData()) === 1);

		// now also ask for the reshares
Roeland Jago Douma's avatar
Roeland Jago Douma committed
590
591
592
593
		$request = $this->createRequest(['path' => $this->filename, 'reshares' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
		$this->assertTrue($result->succeeded());
594
595
596
597

		$this->assertTrue($result->succeeded());

		// now we should get two shares, the initial share and the reshare
Roeland Jago Douma's avatar
Roeland Jago Douma committed
598
		$this->assertCount(2, $result->getData());
599

Roeland Jago Douma's avatar
Roeland Jago Douma committed
600
601
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
602
603
	}

604
605
	/**
	 * @medium
606
	 * @depends testCreateShareUserFile
Bjoern Schiessle's avatar
Bjoern Schiessle committed
607
608
	 */
	function testGetShareFromId() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
609
610
611
612
613
614
615
616
		$node = $this->userFolder->get($this->filename);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share1);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
617

Bjoern Schiessle's avatar
Bjoern Schiessle committed
618
		// call getShare() with share ID
Roeland Jago Douma's avatar
Roeland Jago Douma committed
619
620
621
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShare($share1->getId());
Bjoern Schiessle's avatar
Bjoern Schiessle committed
622
623
		$this->assertTrue($result->succeeded());

624
		// test should return one share created from testCreateShare()
625
		$this->assertEquals(1, count($result->getData()));
Bjoern Schiessle's avatar
Bjoern Schiessle committed
626

Roeland Jago Douma's avatar
Roeland Jago Douma committed
627
		$this->shareManager->deleteShare($share1);
628
629
	}

630
631
632
633
	/**
	 * @medium
	 */
	function testGetShareFromFolder() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
		$node1 = $this->userFolder->get($this->filename);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share1);

		$node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
		$share2 = $this->shareManager->newShare();
		$share2->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share2 = $this->shareManager->createShare($share2);


		$request = $this->createRequest(['path' => $this->folder, 'subfiles' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
655
656
		$this->assertTrue($result->succeeded());

Robin McCorkell's avatar
Robin McCorkell committed
657
		// test should return one share within $this->folder
658
659
		$this->assertTrue(count($result->getData()) === 1);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
660
661
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
662
663
664
	}

	function testGetShareFromFolderWithFile() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
665
666
667
668
669
670
671
672
673
674
675
676
		$node1 = $this->userFolder->get($this->filename);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share1);

		$request = $this->createRequest(['path' => $this->filename, 'subfiles' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
677
678
679

		$this->assertFalse($result->succeeded());
		$this->assertEquals(400, $result->getStatusCode());
680
		$this->assertEquals('Not a directory', $result->getMeta()['message']);
681

Roeland Jago Douma's avatar
Roeland Jago Douma committed
682
		$this->shareManager->deleteShare($share1);
683
684
	}

685
	/**
686
	 * share a folder, than reshare a file within the shared folder and check if we construct the correct path
687
688
689
	 * @medium
	 */
	function testGetShareFromFolderReshares() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
		$node1 = $this->userFolder->get($this->folder);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share1 = $this->shareManager->createShare($share1);

		$node2 = $this->userFolder->get($this->folder.'/'.$this->filename);
		$share2 = $this->shareManager->newShare();
		$share2->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share2 = $this->shareManager->createShare($share2);

		$node3 = $this->userFolder->get($this->folder.'/'.$this->subfolder.'/'.$this->filename);
		$share3 = $this->shareManager->newShare();
		$share3->setNode($node3)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share3 = $this->shareManager->createShare($share3);
714
715

		$testValues=array(
Bjoern Schiessle's avatar
Bjoern Schiessle committed
716
717
718
719
			array('query' => $this->folder,
				'expectedResult' => $this->folder . $this->filename),
			array('query' => $this->folder . $this->subfolder,
				'expectedResult' => $this->folder . $this->subfolder . $this->filename),
720
721
722
		);
		foreach ($testValues as $value) {

Roeland Jago Douma's avatar
Roeland Jago Douma committed
723
724
725
			$request = $this->createRequest(['path' => $value['query'], 'subfiles' => 'true']);
			$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
			$result = $ocs->getShares();
726
727
728
729
730
731
732
733
734
			$this->assertTrue($result->succeeded());

			// test should return one share within $this->folder
			$data = $result->getData();

			$this->assertEquals($value['expectedResult'], $data[0]['path']);
		}

		// cleanup
Roeland Jago Douma's avatar
Roeland Jago Douma committed
735
736
737
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
		$this->shareManager->deleteShare($share3);
738
739
	}

740
	/**
741
	 * reshare a sub folder and check if we get the correct path
742
743
744
	 * @medium
	 */
	function testGetShareFromSubFolderReShares() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
		$node1 = $this->userFolder->get($this->folder . $this->subfolder);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share1 = $this->shareManager->createShare($share1);

		$node2 = \OC::$server->getRootFolder()->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get($this->subfolder);
		$share2 = $this->shareManager->newShare();
		$share2->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share2 = $this->shareManager->createShare($share2);

		$request = $this->createRequest(['path' => '/', 'subfiles' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
		$result = $ocs->getShares();
765
766
767
768
769
770
		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
Roeland Jago Douma's avatar
Roeland Jago Douma committed
771
		$this->assertCount(1, $data);
772

Roeland Jago Douma's avatar
Roeland Jago Douma committed
773
		$this->assertEquals($this->subfolder, $data[0]['path']);
774

Roeland Jago Douma's avatar
Roeland Jago Douma committed
775
776
		$this->shareManager->deleteShare($share2);
		$this->shareManager->deleteShare($share1);
777
778
779
	}

	/**
780
	 * test re-re-share of folder if the path gets constructed correctly
781
782
783
	 * @medium
	 */
	function testGetShareFromFolderReReShares() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
		$node1 = $this->userFolder->get($this->folder . $this->subfolder);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share1 = $this->shareManager->createShare($share1);

		$node2 = $this->userFolder->get($this->folder . $this->subfolder . $this->subsubfolder);
		$share2 = $this->shareManager->newShare();
		$share2->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share2 = $this->shareManager->createShare($share2);

		$share3 = $this->shareManager->newShare();
		$share3->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share3 = $this->shareManager->createShare($share3);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
809
810
811
		/*
		 * Test as recipient
		 */
Roeland Jago Douma's avatar
Roeland Jago Douma committed
812
813
814
		$request = $this->createRequest(['path' => '/', 'subfiles' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER3);
		$result = $ocs->getShares();
815
816
817
818
819
820
		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
Roeland Jago Douma's avatar
Roeland Jago Douma committed
821
		$this->assertCount(1, $data);
Roeland Jago Douma's avatar
Roeland Jago Douma committed
822
		$this->assertEquals($this->subsubfolder, $data[0]['path']);
823

Roeland Jago Douma's avatar
Roeland Jago Douma committed
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
		/*
		 * Test for first owner/initiator
		 */
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->getShares();
		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
		$this->assertCount(1, $data);
		$this->assertEquals($this->folder . $this->subfolder, $data[0]['path']);

		/*
		 * Test for second initiator
		 */
		$request = $this->createRequest([]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
		$result = $ocs->getShares();
		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
		$this->assertCount(1, $data);
		$this->assertEquals($this->subfolder . $this->subsubfolder, $data[0]['path']);

Roeland Jago Douma's avatar
Roeland Jago Douma committed
854
855
856
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
		$this->shareManager->deleteShare($share3);
857
858
	}

859
	/**
860
	 * test multiple shared folder if the path gets constructed correctly
861
862
863
	 * @medium
	 */
	function testGetShareMultipleSharedFolder() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
		$node1 = $this->userFolder->get($this->folder . $this->subfolder);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share1 = $this->shareManager->createShare($share1);

		$node2 = $this->userFolder->get($this->folder);
		$share2 = $this->shareManager->newShare();
		$share2->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share2 = $this->shareManager->createShare($share2);

		$share3 = $this->shareManager->newShare();
		$share3->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share3 = $this->shareManager->createShare($share3);

		$request = $this->createRequest(['path' => $this->subfolder]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
		$result1 = $ocs->getShares();
892
893
894
895
		$this->assertTrue($result1->succeeded());

		// test should return one share within $this->folder
		$data1 = $result1->getData();
Roeland Jago Douma's avatar
Roeland Jago Douma committed
896
897
		$this->assertCount(1, $data1);
		$s1 = reset($data1);
898

Roeland Jago Douma's avatar
Roeland Jago Douma committed
899
900
901
		$request = $this->createRequest(['path' => $this->folder.$this->subfolder]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
		$result2 = $ocs->getShares();
902
903
904
905
		$this->assertTrue($result2->succeeded());

		// test should return one share within $this->folder
		$data2 = $result2->getData();
Roeland Jago Douma's avatar
Roeland Jago Douma committed
906
907
		$this->assertCount(1, $data2);
		$s2 = reset($data2);
908

Roeland Jago Douma's avatar
Roeland Jago Douma committed
909
910
		$this->assertEquals($this->folder.$this->subfolder, $s1['path']);
		$this->assertEquals($this->folder.$this->subfolder, $s2['path']);
911

Roeland Jago Douma's avatar
Roeland Jago Douma committed
912
913
914
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
		$this->shareManager->deleteShare($share3);
915
916
	}

917
	/**
918
	 * test re-re-share of folder if the path gets constructed correctly
919
920
921
	 * @medium
	 */
	function testGetShareFromFileReReShares() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
922
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
		$node1 = $this->userFolder->get($this->folder . $this->subfolder);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(31);
		$share1 = $this->shareManager->createShare($share1);

		$user2Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2);
		$node2 = $user2Folder->get($this->subfolder . $this->filename);
		$share2 = $this->shareManager->newShare();
		$share2->setNode($node2)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER2)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share2 = $this->shareManager->createShare($share2);

		$user3Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3);
		$node3 = $user3Folder->get($this->filename);
		$share3 = $this->shareManager->newShare();
		$share3->setNode($node3)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER3)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share3 = $this->shareManager->createShare($share3);

		$request = $this->createRequest(['path' => '/', 'subfiles' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER3);
		$result = $ocs->getShares();
953
954
955
956
957
958
		$this->assertTrue($result->succeeded());

		// test should return one share within $this->folder
		$data = $result->getData();

		// we should get exactly one result
Roeland Jago Douma's avatar
Roeland Jago Douma committed
959
		$this->assertCount(1, $data);
960

Roeland Jago Douma's avatar
Roeland Jago Douma committed
961
		$this->assertEquals($this->filename, $data[0]['path']);
962

Roeland Jago Douma's avatar
Roeland Jago Douma committed
963
964
965
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
		$this->shareManager->deleteShare($share3);
966
967
	}

968
969
970
971
	/**
	 * @medium
	 */
	function testGetShareFromUnknownId() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
972
973
974
975
		$request = $this->createRequest(['path' => '/', 'subfiles' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER3);
		$result = $ocs->getShare(0);
		$this->assertFalse($result->succeeded());
976
977

		$this->assertEquals(404, $result->getStatusCode());
Robin McCorkell's avatar
Robin McCorkell committed
978
		$meta = $result->getMeta();
979
		$this->assertEquals('Wrong share ID, share doesn\'t exist', $meta['message']);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
980
981
982
983
	}

	/**
	 * @medium
984
985
	 * @depends testCreateShareUserFile
	 * @depends testCreateShareLink
986
987
	 */
	function testUpdateShare() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
		$node1 = $this->userFolder->get($this->filename);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share1);

		$share2 = $this->shareManager->newShare();
		$share2->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share2 = $this->shareManager->createShare($share2);
1003

1004
		// update permissions
1005
		$params = array();
Roeland Jago Douma's avatar
Roeland Jago Douma committed
1006
		$params['permissions'] = 1;
1007

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1008
1009
1010
1011
		$request = $this->createRequest(['permissions' => 1]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->updateShare($share1->getId());
		$this->assertTrue($result->succeeded());
1012

Robin McCorkell's avatar
Robin McCorkell committed
1013
		$meta = $result->getMeta();
1014
		$this->assertTrue($result->succeeded(), $meta['message']);
1015

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1016
1017
		$share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId());
		$this->assertEquals(1, $share1->getPermissions());
1018
1019

		// update password for link share
Roeland Jago Douma's avatar
Roeland Jago Douma committed
1020
		$this->assertNull($share2->getPassword());
1021

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1022
1023
1024
		$request = $this->createRequest(['password' => 'foo']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->updateShare($share2->getId());
1025
1026
		$this->assertTrue($result->succeeded());

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1027
1028
		$share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
		$this->assertNotNull($share2->getPassword());
1029

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1030
1031
1032
		$request = $this->createRequest(['password' => '']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->updateShare($share2->getId());
1033
1034
		$this->assertTrue($result->succeeded());

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1035
1036
		$share2 = $this->shareManager->getShareById('ocinternal:' . $share2->getId());
		$this->assertNull($share2->getPassword());
1037

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1038
1039
		$this->shareManager->deleteShare($share1);
		$this->shareManager->deleteShare($share2);
1040
1041
	}

1042
1043
	/**
	 * @medium
1044
	 * @depends testCreateShareUserFile
1045
1046
	 */
	public function testUpdateShareInvalidPermissions() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
		$node1 = $this->userFolder->get($this->filename);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
			->setShareType(\OCP\Share::SHARE_TYPE_USER)
			->setPermissions(19);
		$share1 = $this->shareManager->createShare($share1);

		$request = $this->createRequest(['permissions' => \OCP\Constants::PERMISSION_ALL]);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->updateShare($share1->getId());
1059
1060
1061
1062
1063
1064

		//Updating should fail with 400
		$this->assertFalse($result->succeeded());
		$this->assertEquals(400, $result->getStatusCode());

		//Permissions should not have changed!
Roeland Jago Douma's avatar
Roeland Jago Douma committed
1065
1066
		$share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId());
		$this->assertEquals(19, $share1->getPermissions());
1067

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1068
		$this->shareManager->deleteShare($share1);
1069
1070
	}

Bjoern Schiessle's avatar
Bjoern Schiessle committed
1071
1072
1073
1074
	/**
	 * @medium
	 */
	function testUpdateShareUpload() {
Roeland Jago Douma's avatar
Roeland Jago Douma committed
1075
1076
1077
1078
1079
1080
1081
		$node1 = $this->userFolder->get($this->folder);
		$share1 = $this->shareManager->newShare();
		$share1->setNode($node1)
			->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
			->setShareType(\OCP\Share::SHARE_TYPE_LINK)
			->setPermissions(1);
		$share1 = $this->shareManager->createShare($share1);
Bjoern Schiessle's avatar
Bjoern Schiessle committed
1082
1083

		// update public upload
Roeland Jago Douma's avatar
Roeland Jago Douma committed
1084
1085
1086
		$request = $this->createRequest(['publicUpload' => 'true']);
		$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
		$result = $ocs->updateShare($share1->getId());
Bjoern Schiessle's avatar
Bjoern Schiessle committed
1087
1088
		$this->assertTrue($result->succeeded());

Roeland Jago Douma's avatar
Roeland Jago Douma committed
1089
		$share1 = $this->shareManager->getShareById($share1->getFullId());
1090
1091
1092
1093
1094
1095
1096
		$this->assertEquals(
			\OCP\Constants::PERMISSION_READ |
			\OCP\Constants::PERMISSION_CREATE |
			\OCP\Constants::PERMISSION_UPDATE |
			\OCP\Constants::PERMISSION_DELETE,
			$share1->getPermissions()
		);
Bjoern Schiessle's avatar