From e2510ed9776921ba65504c120a0ca8a58ad22344 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 17 Oct 2024 18:29:52 +0200 Subject: [PATCH] test: add test for permissions of copied share Signed-off-by: Robin Appelman --- .../files_sharing/tests/SharedStorageTest.php | 85 +++++++++++++------ 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/apps/files_sharing/tests/SharedStorageTest.php b/apps/files_sharing/tests/SharedStorageTest.php index 14fdba0ade3cf..3e8b02c623531 100644 --- a/apps/files_sharing/tests/SharedStorageTest.php +++ b/apps/files_sharing/tests/SharedStorageTest.php @@ -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; @@ -68,7 +71,7 @@ protected function tearDown(): void { } } - \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); + Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); parent::tearDown(); } @@ -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); @@ -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 @@ -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 ); @@ -171,7 +174,7 @@ 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( @@ -179,16 +182,16 @@ public function testFilesize() { $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); @@ -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()); @@ -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); @@ -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); @@ -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); @@ -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); @@ -392,14 +395,14 @@ 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); @@ -407,7 +410,7 @@ public function testMountSharesOtherUser() { $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)); @@ -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); @@ -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'); @@ -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); @@ -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(''); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); + } }