Skip to content

Commit

Permalink
test: add test for permissions of copied share
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and solracsf committed Dec 14, 2024
1 parent cd86944 commit e2510ed
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions apps/files_sharing/tests/SharedStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
*/
namespace OCA\Files_Sharing\Tests;

use OC\Files\Filesystem;
use OC\Files\Storage\Temporary;
use OC\Files\View;
use OCA\Files_Sharing\SharedStorage;
use OCA\Files_Trashbin\AppInfo\Application;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\Constants;
use OCP\Files\NotFoundException;
use OCP\Share\IShare;

Expand Down Expand Up @@ -68,7 +71,7 @@ protected function tearDown(): void {
}
}

\OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');

parent::tearDown();
}
Expand All @@ -86,7 +89,7 @@ public function testParentOfMountPointIsGone() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -106,7 +109,7 @@ public function testParentOfMountPointIsGone() {

// delete the local folder
/** @var \OC\Files\Storage\Storage $storage */
[$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder');
[$storage, $internalPath] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder');
$storage->rmdir($internalPath);

//enforce reload of the mount points
Expand All @@ -131,7 +134,7 @@ public function testRenamePartFile() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);


Expand Down Expand Up @@ -171,24 +174,24 @@ public function testFilesize() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

$share2 = $this->share(
IShare::TYPE_USER,
$this->filename,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE
);


self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

// compare file size between user1 and user2, should always be the same
$this->assertSame($folderSize, \OC\Files\Filesystem::filesize($this->folder));
$this->assertSame($file1Size, \OC\Files\Filesystem::filesize($this->folder . $this->filename));
$this->assertSame($file2Size, \OC\Files\Filesystem::filesize($this->filename));
$this->assertSame($folderSize, Filesystem::filesize($this->folder));
$this->assertSame($file1Size, Filesystem::filesize($this->folder . $this->filename));
$this->assertSame($file2Size, Filesystem::filesize($this->filename));

//cleanup
$this->shareManager->deleteShare($share1);
Expand All @@ -201,23 +204,23 @@ public function testGetPermissions() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ
Constants::PERMISSION_READ
);


self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

$this->assertTrue(\OC\Files\Filesystem::is_dir($this->folder));
$this->assertTrue(Filesystem::is_dir($this->folder));

// for the share root we expect:
// the read permissions (1)
// the delete permission (8), to enable unshare
$rootInfo = \OC\Files\Filesystem::getFileInfo($this->folder);
$rootInfo = Filesystem::getFileInfo($this->folder);
$this->assertSame(9, $rootInfo->getPermissions());

// for the file within the shared folder we expect:
// the read permissions (1)
$subfileInfo = \OC\Files\Filesystem::getFileInfo($this->folder . $this->filename);
$subfileInfo = Filesystem::getFileInfo($this->folder . $this->filename);
$this->assertSame(1, $subfileInfo->getPermissions());


Expand All @@ -233,7 +236,7 @@ public function testFopenWithReadOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ
Constants::PERMISSION_READ
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -266,7 +269,7 @@ public function testFopenWithCreateOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE
Constants::PERMISSION_READ | Constants::PERMISSION_CREATE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -312,7 +315,7 @@ public function testFopenWithUpdateOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE
Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -358,7 +361,7 @@ public function testFopenWithDeleteOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE
Constants::PERMISSION_READ | Constants::PERMISSION_DELETE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -392,22 +395,22 @@ public function testMountSharesOtherUser() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);
$share2 = $this->share(
IShare::TYPE_USER,
$this->filename,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER3,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
$this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder));

$mountConfigManager = \OC::$server->getMountProviderCollection();
$mounts = $mountConfigManager->getMountsForUser(\OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3));
array_walk($mounts, [\OC\Files\Filesystem::getMountManager(), 'addMount']);
array_walk($mounts, [Filesystem::getMountManager(), 'addMount']);

$this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER3 . '/files/' . $this->filename));

Expand All @@ -431,7 +434,7 @@ public function testCopyFromStorage() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -444,7 +447,7 @@ public function testCopyFromStorage() {
[$sharedStorage,] = $view->resolvePath($this->folder);
$this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));

$sourceStorage = new \OC\Files\Storage\Temporary([]);
$sourceStorage = new Temporary([]);
$sourceStorage->file_put_contents('foo.txt', 'asd');

$sharedStorage->copyFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
Expand All @@ -464,7 +467,7 @@ public function testMoveFromStorage() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -477,7 +480,7 @@ public function testMoveFromStorage() {
[$sharedStorage,] = $view->resolvePath($this->folder);
$this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));

$sourceStorage = new \OC\Files\Storage\Temporary([]);
$sourceStorage = new Temporary([]);
$sourceStorage->file_put_contents('foo.txt', 'asd');
$sourceStorage->getScanner()->scan('');

Expand Down Expand Up @@ -507,7 +510,7 @@ public function testNameConflict() {
'foo',
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_GROUP1,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);
$this->shareManager->acceptShare($share1, self::TEST_FILES_SHARING_API_USER2);

Expand All @@ -520,7 +523,7 @@ public function testNameConflict() {
'foo',
self::TEST_FILES_SHARING_API_USER3,
self::TEST_FILES_SHARING_API_GROUP1,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);
$this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);

Expand Down Expand Up @@ -549,7 +552,7 @@ public function testOwnerPermissions() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_DELETE
Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -561,7 +564,7 @@ public function testOwnerPermissions() {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

$this->assertTrue($this->view->file_exists($this->folder . '/newfile.txt'));
$this->assertEquals(\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
$this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE,
$this->view->getFileInfo($this->folder . '/newfile.txt')->getPermissions());

$this->view->unlink($this->folder);
Expand Down Expand Up @@ -601,4 +604,30 @@ public function testInitWithNotFoundSource() {
$this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
$this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
}

public function testCopyPermissions(): void {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

$share = $this->share(
IShare::TYPE_USER,
$this->filename,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE - Constants::PERMISSION_DELETE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
$view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
$this->assertTrue($view->file_exists($this->filename));

$this->assertTrue($view->copy($this->filename, '/target.txt'));

$this->assertTrue($view->file_exists('/target.txt'));

$info = $view->getFileInfo('/target.txt');
$this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $info->getPermissions());

$this->view->unlink($this->filename);
$this->shareManager->deleteShare($share);
}
}

0 comments on commit e2510ed

Please sign in to comment.