diff options
Diffstat (limited to 'tests/lib/Traits')
-rw-r--r-- | tests/lib/Traits/ClientServiceTrait.php | 106 | ||||
-rw-r--r-- | tests/lib/Traits/EncryptionTrait.php | 125 | ||||
-rw-r--r-- | tests/lib/Traits/MountProviderTrait.php | 67 | ||||
-rw-r--r-- | tests/lib/Traits/UserTrait.php | 52 |
4 files changed, 350 insertions, 0 deletions
diff --git a/tests/lib/Traits/ClientServiceTrait.php b/tests/lib/Traits/ClientServiceTrait.php new file mode 100644 index 00000000000..5a091544889 --- /dev/null +++ b/tests/lib/Traits/ClientServiceTrait.php @@ -0,0 +1,106 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Traits; + +use OCP\Http\Client\IClient; +use OCP\Http\Client\IClientService; +use OCP\Http\Client\IResponse; + +trait ClientServiceTrait { + /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */ + private $clientService; + /** @var IClient|\PHPUnit\Framework\MockObject\MockObject */ + private $client; + private $expectedGetRequests = []; + private $expectedPostRequests = []; + + /** + * Wrapper to be forward compatible to phpunit 5.4+ + * + * @param string $originalClassName + * @return \PHPUnit\Framework\MockObject\MockObject + */ + abstract protected function createMock(string $originalClassName); + + /** + * Returns a matcher that matches when the method is executed + * zero or more times. + * + * @return \PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount + * + * @since Method available since Release 3.0.0 + */ + abstract public static function any(); + + protected function setUpClientServiceTrait() { + $this->clientService = $this->createMock(IClientService::class); + $this->client = $this->createMock(IClient::class); + $this->clientService->expects($this->any()) + ->method('newClient') + ->willReturn($this->client); + $this->client->expects($this->any()) + ->method('get') + ->willReturnCallback(function ($url) { + if (!isset($this->expectedGetRequests[$url])) { + throw new \Exception('unexpected request: ' . $url); + } + $result = $this->expectedGetRequests[$url]; + + if ($result instanceof \Exception) { + throw $result; + } else { + $response = $this->createMock(IResponse::class); + $response->expects($this->any()) + ->method('getBody') + ->willReturn($result); + return $response; + } + }); + $this->client->expects($this->any()) + ->method('post') + ->willReturnCallback(function ($url) { + if (!isset($this->expectedPostRequests[$url])) { + throw new \Exception('unexpected request: ' . $url); + } + $result = $this->expectedPostRequests[$url]; + + if ($result instanceof \Exception) { + throw $result; + } else { + $response = $this->createMock(IResponse::class); + $response->expects($this->any()) + ->method('getBody') + ->willReturn($result); + return $response; + } + }); + } + + /** + * @param string $url + * @param string|\Exception $result + */ + protected function expectGetRequest($url, $result) { + $this->expectedGetRequests[$url] = $result; + } + + /** + * @param string $url + * @param string|\Exception $result + */ + protected function expectPostRequest($url, $result) { + $this->expectedPostRequests[$url] = $result; + } + + /** + * @return IClientService|\PHPUnit\Framework\MockObject\MockObject + */ + protected function getClientService() { + return $this->clientService; + } +} diff --git a/tests/lib/Traits/EncryptionTrait.php b/tests/lib/Traits/EncryptionTrait.php new file mode 100644 index 00000000000..44d5dcab8cb --- /dev/null +++ b/tests/lib/Traits/EncryptionTrait.php @@ -0,0 +1,125 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Traits; + +use OC\Encryption\EncryptionWrapper; +use OC\Files\SetupManager; +use OC\Memcache\ArrayCache; +use OCA\Encryption\AppInfo\Application; +use OCA\Encryption\Crypto\Encryption; +use OCA\Encryption\KeyManager; +use OCA\Encryption\Users\Setup; +use OCP\App\IAppManager; +use OCP\Encryption\IManager; +use OCP\IConfig; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Server; +use Psr\Log\LoggerInterface; + +/** + * Enables encryption + */ +trait EncryptionTrait { + // from MountProviderTrait + abstract protected function registerStorageWrapper($name, $wrapper); + + // from phpunit + abstract protected static function markTestSkipped(string $message = ''): void; + abstract protected static function assertTrue($condition, string $message = ''): void; + + private $encryptionWasEnabled; + + private $originalEncryptionModule; + + /** @var IUserManager */ + private $userManager; + /** @var SetupManager */ + private $setupManager; + + /** + * @var IConfig + */ + private $config; + + /** + * @var Application + */ + private $encryptionApp; + + protected function loginWithEncryption($user = '') { + \OC_Util::tearDownFS(); + \OC_User::setUserId(''); + // needed for fully logout + Server::get(IUserSession::class)->setUser(null); + + $this->setupManager->tearDown(); + + \OC_User::setUserId($user); + $this->postLogin(); + \OC_Util::setupFS($user); + if ($this->userManager->userExists($user)) { + \OC::$server->getUserFolder($user); + } + } + + protected function setupForUser($name, $password) { + $this->setupManager->tearDown(); + $this->setupManager->setupForUser($this->userManager->get($name)); + + $container = $this->encryptionApp->getContainer(); + /** @var KeyManager $keyManager */ + $keyManager = $container->query(KeyManager::class); + /** @var Setup $userSetup */ + $userSetup = $container->query(Setup::class); + $userSetup->setupUser($name, $password); + $encryptionManager = $container->query(IManager::class); + $this->encryptionApp->setUp($encryptionManager); + $keyManager->init($name, $password); + } + + protected function postLogin() { + $encryptionWrapper = new EncryptionWrapper( + new ArrayCache(), + Server::get(\OCP\Encryption\IManager::class), + Server::get(LoggerInterface::class) + ); + + $this->registerStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage']); + } + + protected function setUpEncryptionTrait() { + $isReady = Server::get(\OCP\Encryption\IManager::class)->isReady(); + if (!$isReady) { + $this->markTestSkipped('Encryption not ready'); + } + + $this->userManager = Server::get(IUserManager::class); + $this->setupManager = Server::get(SetupManager::class); + + Server::get(IAppManager::class)->loadApp('encryption'); + + $this->encryptionApp = new Application([], $isReady); + + $this->config = Server::get(IConfig::class); + $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); + $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module'); + $this->config->setAppValue('core', 'default_encryption_module', Encryption::ID); + $this->config->setAppValue('core', 'encryption_enabled', 'yes'); + $this->assertTrue(Server::get(\OCP\Encryption\IManager::class)->isEnabled()); + } + + protected function tearDownEncryptionTrait() { + if ($this->config) { + $this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled); + $this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule); + $this->config->deleteAppValue('encryption', 'useMasterKey'); + } + } +} diff --git a/tests/lib/Traits/MountProviderTrait.php b/tests/lib/Traits/MountProviderTrait.php new file mode 100644 index 00000000000..b680c71b2d6 --- /dev/null +++ b/tests/lib/Traits/MountProviderTrait.php @@ -0,0 +1,67 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Traits; + +use OC\Files\Mount\MountPoint; +use OC\Files\Storage\StorageFactory; +use OCP\Files\Config\IMountProvider; +use OCP\Files\Config\IMountProviderCollection; +use OCP\IUser; +use OCP\Server; + +/** + * Allow setting mounts for users + */ +trait MountProviderTrait { + /** + * @var IMountProvider + */ + protected $mountProvider; + + /** + * @var \OC\Files\Storage\StorageFactory + */ + protected $storageFactory; + + protected $mounts = []; + + protected function registerMount($userId, $storage, $mountPoint, $arguments = null) { + if (!isset($this->mounts[$userId])) { + $this->mounts[$userId] = []; + } + $this->mounts[$userId][] = ['storage' => $storage, 'mountPoint' => $mountPoint, 'arguments' => $arguments]; + + if ($this->IsDatabaseAccessAllowed()) { + $mount = new MountPoint($storage, $mountPoint, $arguments, $this->storageFactory); + $storage = $mount->getStorage(); + $storage->getScanner()->scan(''); + } + } + + protected function registerStorageWrapper($name, $wrapper) { + $this->storageFactory->addStorageWrapper($name, $wrapper); + } + + protected function setUpMountProviderTrait() { + $this->storageFactory = new StorageFactory(); + $this->mountProvider = $this->getMockBuilder('\OCP\Files\Config\IMountProvider')->getMock(); + $this->mountProvider->expects($this->any()) + ->method('getMountsForUser') + ->willReturnCallback(function (IUser $user) { + if (isset($this->mounts[$user->getUID()])) { + return array_map(function ($config) { + return new MountPoint($config['storage'], $config['mountPoint'], $config['arguments'], $this->storageFactory); + }, $this->mounts[$user->getUID()]); + } else { + return []; + } + }); + Server::get(IMountProviderCollection::class)->registerProvider($this->mountProvider); + } +} diff --git a/tests/lib/Traits/UserTrait.php b/tests/lib/Traits/UserTrait.php new file mode 100644 index 00000000000..f80adb76be8 --- /dev/null +++ b/tests/lib/Traits/UserTrait.php @@ -0,0 +1,52 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Traits; + +use OC\User\User; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IUser; +use OCP\IUserManager; +use OCP\Server; +use OCP\UserInterface; + +class DummyUser extends User { + public function __construct( + private string $uid, + ) { + parent::__construct($this->uid, null, Server::get(IEventDispatcher::class)); + } + + public function getUID(): string { + return $this->uid; + } +} + +/** + * Allow creating users in a temporary backend + */ +trait UserTrait { + /** + * @var \Test\Util\User\Dummy|UserInterface + */ + protected $userBackend; + + protected function createUser($name, $password): IUser { + $this->userBackend->createUser($name, $password); + return new DummyUser($name); + } + + protected function setUpUserTrait() { + $this->userBackend = new \Test\Util\User\Dummy(); + Server::get(IUserManager::class)->registerBackend($this->userBackend); + } + + protected function tearDownUserTrait() { + Server::get(IUserManager::class)->removeBackend($this->userBackend); + } +} |