diff options
author | Robin Appelman <robin@icewind.nl> | 2023-01-09 15:30:45 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2023-01-09 15:30:45 +0100 |
commit | 6fcc1f5684795b98144d08e199d17e29ab16c652 (patch) | |
tree | 31c19867ed3a088a0bcbfa8f6197e737b8ca9cab /apps/files_sharing/tests | |
parent | 927174e325b9a535e85ea195ca2d99e38790fd93 (diff) | |
download | nextcloud-server-6fcc1f5684795b98144d08e199d17e29ab16c652.tar.gz nextcloud-server-6fcc1f5684795b98144d08e199d17e29ab16c652.zip |
add test for shared mount conflict resolution
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'apps/files_sharing/tests')
-rw-r--r-- | apps/files_sharing/tests/SharedMountTest.php | 114 | ||||
-rw-r--r-- | apps/files_sharing/tests/TestCase.php | 5 |
2 files changed, 119 insertions, 0 deletions
diff --git a/apps/files_sharing/tests/SharedMountTest.php b/apps/files_sharing/tests/SharedMountTest.php index 8b19a856dda..2e169f878ce 100644 --- a/apps/files_sharing/tests/SharedMountTest.php +++ b/apps/files_sharing/tests/SharedMountTest.php @@ -29,6 +29,9 @@ */ namespace OCA\Files_Sharing\Tests; +use OC\Memcache\ArrayCache; +use OCA\Files_Sharing\MountProvider; +use OCP\ICacheFactory; use OCP\IGroupManager; use OCP\IUserManager; use OCP\Share\IShare; @@ -46,19 +49,24 @@ class SharedMountTest extends TestCase { /** @var IUserManager */ private $userManager; + private $folder2; + protected function setUp(): void { parent::setUp(); $this->folder = '/folder_share_storage_test'; + $this->folder2 = '/folder_share_storage_test2'; $this->filename = '/share-api-storage.txt'; $this->view->mkdir($this->folder); + $this->view->mkdir($this->folder2); // save file with content $this->view->file_put_contents($this->filename, 'root file'); $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder'); + $this->view->file_put_contents($this->folder2 . $this->filename, 'file in subfolder2'); $this->groupManager = \OC::$server->getGroupManager(); $this->userManager = \OC::$server->getUserManager(); @@ -325,6 +333,112 @@ class SharedMountTest extends TestCase { $testGroup->removeUser($user2); $testGroup->removeUser($user3); } + + /** + * test if the mount point gets renamed if a folder exists at the target + */ + public function testShareMountOverFolder() { + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + $this->view2->mkdir('bar'); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + + // share to user + $share = $this->share( + IShare::TYPE_USER, + $this->folder, + self::TEST_FILES_SHARING_API_USER1, + self::TEST_FILES_SHARING_API_USER2, + \OCP\Constants::PERMISSION_ALL); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); + + $share->setTarget('/bar'); + $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2); + + $share = $this->shareManager->getShareById($share->getFullId()); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + // share should have been moved + + $share = $this->shareManager->getShareById($share->getFullId()); + $this->assertSame('/bar (2)', $share->getTarget()); + + //cleanup + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + $this->shareManager->deleteShare($share); + $this->view->unlink($this->folder); + } + + /** + * test if the mount point gets renamed if another share exists at the target + */ + public function testShareMountOverShare() { + // create a shared cache + $caches = []; + $cacheFactory = $this->createMock(ICacheFactory::class); + $cacheFactory->method('createLocal') + ->willReturnCallback(function(string $prefix) use (&$caches) { + if (!isset($caches[$prefix])) { + $caches[$prefix] = new ArrayCache($prefix); + } + return $caches[$prefix]; + }); + $cacheFactory->method('createDistributed') + ->willReturnCallback(function(string $prefix) use (&$caches) { + if (!isset($caches[$prefix])) { + $caches[$prefix] = new ArrayCache($prefix); + } + return $caches[$prefix]; + }); + + // hack to overwrite the cache factory, we can't use the proper "overwriteService" since the mount provider is created before this test is called + $mountProvider = \OCP\Server::get(MountProvider::class); + $reflectionClass = new \ReflectionClass($mountProvider); + $reflectionCacheFactory = $reflectionClass->getProperty("cacheFactory"); + $reflectionCacheFactory->setAccessible(true); + $reflectionCacheFactory->setValue($mountProvider, $cacheFactory); + + // share to user + $share = $this->share( + IShare::TYPE_USER, + $this->folder, + self::TEST_FILES_SHARING_API_USER1, + self::TEST_FILES_SHARING_API_USER2, + \OCP\Constants::PERMISSION_ALL); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); + + $share->setTarget('/foobar'); + $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2); + + + // share to user + $share2 = $this->share( + IShare::TYPE_USER, + $this->folder2, + self::TEST_FILES_SHARING_API_USER1, + self::TEST_FILES_SHARING_API_USER2, + \OCP\Constants::PERMISSION_ALL); + $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2); + + $share2->setTarget('/foobar'); + $this->shareManager->moveShare($share2, self::TEST_FILES_SHARING_API_USER2); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + // one of the shares should have been moved + + $share = $this->shareManager->getShareById($share->getFullId()); + $share2 = $this->shareManager->getShareById($share2->getFullId()); + + // we don't know or care which share got the "(2)" just that one of them did + $this->assertNotEquals($share->getTarget(), $share2->getTarget()); + $this->assertSame('/foobar', min($share->getTarget(), $share2->getTarget())); + $this->assertSame('/foobar (2)', max($share->getTarget(), $share2->getTarget())); + + //cleanup + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + $this->shareManager->deleteShare($share); + $this->view->unlink($this->folder); + } } class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount { diff --git a/apps/files_sharing/tests/TestCase.php b/apps/files_sharing/tests/TestCase.php index d4077c84816..2bd83d6c9c2 100644 --- a/apps/files_sharing/tests/TestCase.php +++ b/apps/files_sharing/tests/TestCase.php @@ -64,6 +64,10 @@ abstract class TestCase extends \Test\TestCase { * @var \OC\Files\View */ public $view; + /** + * @var \OC\Files\View + */ + public $view2; public $folder; public $subfolder; @@ -124,6 +128,7 @@ abstract class TestCase extends \Test\TestCase { $this->data = 'foobar'; $this->view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); + $this->view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->shareManager = \OC::$server->getShareManager(); $this->rootFolder = \OC::$server->getRootFolder(); |