Skip to content

Commit

Permalink
Merge pull request #36052 from nextcloud/share-mount-check-cache-key-…
Browse files Browse the repository at this point in the history
…test

add test for shared mount conflict resolution
  • Loading branch information
PVince81 authored Jan 10, 2023
2 parents 0c37d05 + 6fcc1f5 commit 10cca65
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
114 changes: 114 additions & 0 deletions apps/files_sharing/tests/SharedMountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -325,6 +333,112 @@ public function testPermissionUpgradeOnUserDeletedGroupShare() {
$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 {
Expand Down
5 changes: 5 additions & 0 deletions apps/files_sharing/tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -124,6 +128,7 @@ protected function setUp(): void {

$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();
Expand Down

0 comments on commit 10cca65

Please sign in to comment.