mirror of
https://github.com/nextcloud/server.git
synced 2026-06-10 09:13:19 -04:00
test: more group/user test trait usage and setup/teardown improvements
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
6100590516
commit
7db238319f
7 changed files with 68 additions and 133 deletions
|
|
@ -34,8 +34,6 @@ class CacheTest extends TestCase {
|
|||
protected Cache $sharedCache;
|
||||
protected Storage $ownerStorage;
|
||||
protected Storage $sharedStorage;
|
||||
/** @var \OCP\Share\IManager $shareManager */
|
||||
protected $shareManager;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
|
|
|||
|
|
@ -50,12 +50,14 @@ use OCP\Share\IAttributes;
|
|||
use OCP\Share\IPublicShareTemplateFactory;
|
||||
use OCP\Share\IShare;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Test\Traits\UserTrait;
|
||||
|
||||
/**
|
||||
* @package OCA\Files_Sharing\Controllers
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
||||
class ShareControllerTest extends \Test\TestCase {
|
||||
use UserTrait;
|
||||
|
||||
private string $user;
|
||||
private string $oldUser;
|
||||
|
|
@ -150,7 +152,7 @@ class ShareControllerTest extends \Test\TestCase {
|
|||
// Create a dummy user
|
||||
$this->user = Server::get(ISecureRandom::class)->generate(12, ISecureRandom::CHAR_LOWER);
|
||||
|
||||
Server::get(IUserManager::class)->createUser($this->user, $this->user);
|
||||
$this->createUser($this->user, $this->user);
|
||||
\OC_Util::tearDownFS();
|
||||
$this->loginAsUser($this->user);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use OCP\IDBConnection;
|
|||
use OCP\IUserManager;
|
||||
use OCP\Server;
|
||||
use OCP\Share\IShare;
|
||||
use Test\Traits\UserTrait;
|
||||
|
||||
/**
|
||||
* Class DeleteOrphanedSharesJobTest
|
||||
|
|
@ -26,6 +27,8 @@ use OCP\Share\IShare;
|
|||
*/
|
||||
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
||||
class DeleteOrphanedSharesJobTest extends \Test\TestCase {
|
||||
use UserTrait;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
|
|
@ -76,9 +79,8 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase {
|
|||
$this->user1 = $this->getUniqueID('user1_');
|
||||
$this->user2 = $this->getUniqueID('user2_');
|
||||
|
||||
$userManager = Server::get(IUserManager::class);
|
||||
$userManager->createUser($this->user1, 'pass');
|
||||
$userManager->createUser($this->user2, 'pass');
|
||||
$this->createUser($this->user1, 'pass');
|
||||
$this->createUser($this->user2, 'pass');
|
||||
|
||||
\OC::registerShareHooks(Server::get(SystemConfig::class));
|
||||
|
||||
|
|
|
|||
|
|
@ -15,11 +15,11 @@ use OCP\Constants;
|
|||
use OCP\Files\IRootFolder;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
use OCP\Server;
|
||||
use OCP\Share\IManager;
|
||||
use OCP\Share\IShare;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Test\Traits\UserTrait;
|
||||
|
||||
/**
|
||||
* Class ExpireSharesJobTest
|
||||
|
|
@ -29,6 +29,7 @@ use PHPUnit\Framework\Attributes\DataProvider;
|
|||
*/
|
||||
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
||||
class ExpireSharesJobTest extends \Test\TestCase {
|
||||
use UserTrait;
|
||||
|
||||
private ExpireSharesJob $job;
|
||||
|
||||
|
|
@ -51,9 +52,8 @@ class ExpireSharesJobTest extends \Test\TestCase {
|
|||
$user1 = $this->getUniqueID('user1_');
|
||||
$user2 = $this->getUniqueID('user2_');
|
||||
|
||||
$userManager = Server::get(IUserManager::class);
|
||||
$this->user1 = $userManager->createUser($user1, 'longrandompassword');
|
||||
$this->user2 = $userManager->createUser($user2, 'longrandompassword');
|
||||
$this->user1 = $this->createUser($user1, 'longrandompassword');
|
||||
$this->user2 = $this->createUser($user2, 'longrandompassword');
|
||||
|
||||
\OC::registerShareHooks(Server::get(SystemConfig::class));
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ class ManagerTest extends TestCase {
|
|||
protected ICloudFederationFactory&MockObject $cloudFederationFactory;
|
||||
protected IGroupManager&MockObject $groupManager;
|
||||
protected IUserManager&MockObject $userManager;
|
||||
protected ISetupManager&MockObject $setupManager;
|
||||
protected ISetupManager&MockObject $setupManagerEncTrait;
|
||||
protected ICertificateManager&MockObject $certificateManager;
|
||||
private ExternalShareMapper $externalShareMapper;
|
||||
private IConfig $config;
|
||||
|
|
@ -88,7 +88,7 @@ class ManagerTest extends TestCase {
|
|||
$this->groupManager = $this->createMock(IGroupManager::class);
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
|
||||
$this->setupManager = $this->createMock(ISetupManager::class);
|
||||
$this->setupManagerEncTrait = $this->createMock(ISetupManager::class);
|
||||
$this->rootFolder = $this->createMock(IRootFolder::class);
|
||||
$this->rootFolder->method('getUserFolder')
|
||||
->willReturnCallback(function (string $userId): Folder {
|
||||
|
|
@ -170,7 +170,7 @@ class ManagerTest extends TestCase {
|
|||
$this->eventDispatcher,
|
||||
$this->logger,
|
||||
$this->rootFolder,
|
||||
$this->setupManager,
|
||||
$this->setupManagerEncTrait,
|
||||
$this->certificateManager,
|
||||
$this->externalShareMapper,
|
||||
$this->config,
|
||||
|
|
|
|||
|
|
@ -11,11 +11,10 @@ namespace OCA\Files_Sharing\Tests;
|
|||
use OC\Files\Filesystem;
|
||||
use OC\Files\View;
|
||||
use OCP\Constants;
|
||||
use OCP\IUserManager;
|
||||
use OCP\Lock\ILockingProvider;
|
||||
use OCP\Lock\LockedException;
|
||||
use OCP\Server;
|
||||
use OCP\Share\IShare;
|
||||
use Test\Traits\UserTrait;
|
||||
|
||||
/**
|
||||
* Class LockingTest
|
||||
|
|
@ -25,10 +24,7 @@ use OCP\Share\IShare;
|
|||
*/
|
||||
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
||||
class LockingTest extends TestCase {
|
||||
/**
|
||||
* @var \Test\Util\User\Dummy
|
||||
*/
|
||||
private $userBackend;
|
||||
use UserTrait;
|
||||
|
||||
private $ownerUid;
|
||||
private $recipientUid;
|
||||
|
|
@ -36,13 +32,10 @@ class LockingTest extends TestCase {
|
|||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->userBackend = new \Test\Util\User\Dummy();
|
||||
Server::get(IUserManager::class)->registerBackend($this->userBackend);
|
||||
|
||||
$this->ownerUid = $this->getUniqueID('owner_');
|
||||
$this->recipientUid = $this->getUniqueID('recipient_');
|
||||
$this->userBackend->createUser($this->ownerUid, '');
|
||||
$this->userBackend->createUser($this->recipientUid, '');
|
||||
$this->createUser($this->ownerUid, '');
|
||||
$this->createUser($this->recipientUid, '');
|
||||
|
||||
$this->loginAsUser($this->ownerUid);
|
||||
Filesystem::mkdir('/foo');
|
||||
|
|
@ -61,11 +54,6 @@ class LockingTest extends TestCase {
|
|||
$this->assertTrue(Filesystem::file_exists('bar.txt'));
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
Server::get(IUserManager::class)->removeBackend($this->userBackend);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testLockAsRecipient(): void {
|
||||
$this->expectException(LockedException::class);
|
||||
|
||||
|
|
|
|||
|
|
@ -8,25 +8,26 @@
|
|||
|
||||
namespace OCA\Files_Sharing\Tests;
|
||||
|
||||
use OC\Files\Cache\Storage;
|
||||
use OC\Files\Filesystem;
|
||||
use OC\Files\View;
|
||||
use OC\Group\Database;
|
||||
use OC\SystemConfig;
|
||||
use OC\User\DisplayNameCache;
|
||||
use OC\User\Session;
|
||||
use OCA\Files_Sharing\AppInfo\Application;
|
||||
use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
|
||||
use OCA\Files_Sharing\Listener\SharesUpdatedListener;
|
||||
use OCA\Files_Sharing\MountProvider;
|
||||
use OCP\Files\Config\IMountProviderCollection;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\ISetupManager;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IGroupManager;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
use OCP\Server;
|
||||
use OCP\Share\IManager;
|
||||
use OCP\Share\IShare;
|
||||
use Test\Traits\GroupTrait;
|
||||
use Test\Traits\MountProviderTrait;
|
||||
use Test\Traits\UserTrait;
|
||||
|
||||
/**
|
||||
* Base class for sharing tests.
|
||||
|
|
@ -34,6 +35,8 @@ use Test\Traits\MountProviderTrait;
|
|||
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
|
||||
abstract class TestCase extends \Test\TestCase {
|
||||
use MountProviderTrait;
|
||||
use UserTrait;
|
||||
use GroupTrait;
|
||||
|
||||
public const TEST_FILES_SHARING_API_USER1 = 'test-share-user1';
|
||||
public const TEST_FILES_SHARING_API_USER2 = 'test-share-user2';
|
||||
|
|
@ -55,13 +58,21 @@ abstract class TestCase extends \Test\TestCase {
|
|||
public $folder;
|
||||
public $subfolder;
|
||||
|
||||
/** @var \OCP\Share\IManager */
|
||||
/** @var IManager */
|
||||
protected $shareManager;
|
||||
/** @var IRootFolder */
|
||||
protected $rootFolder;
|
||||
protected Session $userSession;
|
||||
protected ISetupManager $setupManager;
|
||||
|
||||
public static function setUpBeforeClass(): void {
|
||||
parent::setUpBeforeClass();
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->shareManager = Server::get(IManager::class);
|
||||
$this->rootFolder = Server::get(IRootFolder::class);
|
||||
|
||||
$this->setupManager = Server::get(ISetupManager::class);
|
||||
$this->userSession = Server::get(IUserSession::class);
|
||||
|
||||
$app = new Application();
|
||||
$app->registerMountProviders(
|
||||
|
|
@ -70,54 +81,43 @@ abstract class TestCase extends \Test\TestCase {
|
|||
Server::get(ExternalMountProvider::class),
|
||||
);
|
||||
|
||||
// reset backend
|
||||
Server::get(IUserManager::class)->clearBackends();
|
||||
Server::get(IGroupManager::class)->clearBackends();
|
||||
|
||||
// clear share hooks
|
||||
\OC_Hook::clear('OCP\\Share');
|
||||
\OC::registerShareHooks(Server::get(SystemConfig::class));
|
||||
|
||||
// create users
|
||||
$backend = new \Test\Util\User\Dummy();
|
||||
Server::get(IUserManager::class)->registerBackend($backend);
|
||||
$backend->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1);
|
||||
$backend->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2);
|
||||
$backend->createUser(self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER3);
|
||||
$backend->createUser(self::TEST_FILES_SHARING_API_USER4, self::TEST_FILES_SHARING_API_USER4);
|
||||
|
||||
// create group
|
||||
$groupBackend = new \Test\Util\Group\Dummy();
|
||||
$groupBackend->createGroup(self::TEST_FILES_SHARING_API_GROUP1);
|
||||
$groupBackend->createGroup('group');
|
||||
$groupBackend->createGroup('group1');
|
||||
$groupBackend->createGroup('group2');
|
||||
$groupBackend->createGroup('group3');
|
||||
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER1, 'group');
|
||||
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, 'group');
|
||||
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group');
|
||||
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, 'group1');
|
||||
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group2');
|
||||
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER4, 'group3');
|
||||
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_GROUP1);
|
||||
Server::get(IGroupManager::class)->addBackend($groupBackend);
|
||||
|
||||
Server::get(SharesUpdatedListener::class)->setCutOffMarkTime(-1);
|
||||
}
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
Server::get(DisplayNameCache::class)->clear();
|
||||
|
||||
$this->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1);
|
||||
$this->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2);
|
||||
$this->createUser(self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER3);
|
||||
$this->createUser(self::TEST_FILES_SHARING_API_USER4, self::TEST_FILES_SHARING_API_USER4);
|
||||
|
||||
$this->createGroup(self::TEST_FILES_SHARING_API_GROUP1, [
|
||||
self::TEST_FILES_SHARING_API_USER2,
|
||||
]);
|
||||
$this->createGroup('group', [
|
||||
self::TEST_FILES_SHARING_API_USER1,
|
||||
self::TEST_FILES_SHARING_API_USER2,
|
||||
self::TEST_FILES_SHARING_API_USER3,
|
||||
]);
|
||||
$this->createGroup('group1', [
|
||||
self::TEST_FILES_SHARING_API_USER2,
|
||||
]);
|
||||
$this->createGroup('group2', [
|
||||
self::TEST_FILES_SHARING_API_USER3,
|
||||
]);
|
||||
$this->createGroup('group3', [
|
||||
self::TEST_FILES_SHARING_API_USER4,
|
||||
]);
|
||||
|
||||
//login as user1
|
||||
$this->loginHelper(self::TEST_FILES_SHARING_API_USER1);
|
||||
|
||||
$this->data = 'foobar';
|
||||
$this->view = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
|
||||
$this->view2 = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
|
||||
|
||||
$this->shareManager = Server::get(\OCP\Share\IManager::class);
|
||||
$this->rootFolder = Server::get(IRootFolder::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
|
|
@ -133,73 +133,18 @@ abstract class TestCase extends \Test\TestCase {
|
|||
$qb->delete('filecache')->runAcrossAllShards();
|
||||
$qb->executeStatement();
|
||||
|
||||
$this->userSession->setUser(null);
|
||||
$this->setupManager->tearDown();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass(): void {
|
||||
// cleanup users
|
||||
$user = Server::get(IUserManager::class)->get(self::TEST_FILES_SHARING_API_USER1);
|
||||
if ($user !== null) {
|
||||
$user->delete();
|
||||
}
|
||||
$user = Server::get(IUserManager::class)->get(self::TEST_FILES_SHARING_API_USER2);
|
||||
if ($user !== null) {
|
||||
$user->delete();
|
||||
}
|
||||
$user = Server::get(IUserManager::class)->get(self::TEST_FILES_SHARING_API_USER3);
|
||||
if ($user !== null) {
|
||||
$user->delete();
|
||||
}
|
||||
protected function loginHelper(string $uid) {
|
||||
$this->setupManager->tearDown();
|
||||
$user = Server::get(IUserManager::class)->get($uid);
|
||||
$this->userSession->completeLogin($user, ['loginName' => $uid, 'password' => $uid], false);
|
||||
|
||||
// delete group
|
||||
$group = Server::get(IGroupManager::class)->get(self::TEST_FILES_SHARING_API_GROUP1);
|
||||
if ($group) {
|
||||
$group->delete();
|
||||
}
|
||||
|
||||
\OC_Util::tearDownFS();
|
||||
\OC_User::setUserId('');
|
||||
Filesystem::tearDown();
|
||||
|
||||
// reset backend
|
||||
Server::get(IUserManager::class)->clearBackends();
|
||||
Server::get(IUserManager::class)->registerBackend(new \OC\User\Database());
|
||||
Server::get(IGroupManager::class)->clearBackends();
|
||||
Server::get(IGroupManager::class)->addBackend(new Database());
|
||||
|
||||
parent::tearDownAfterClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $user
|
||||
* @param bool $create
|
||||
* @param bool $password
|
||||
*/
|
||||
protected function loginHelper($user, $create = false, $password = false) {
|
||||
if ($password === false) {
|
||||
$password = $user;
|
||||
}
|
||||
|
||||
if ($create) {
|
||||
$userManager = Server::get(IUserManager::class);
|
||||
$groupManager = Server::get(IGroupManager::class);
|
||||
|
||||
$userObject = $userManager->createUser($user, $password);
|
||||
$group = $groupManager->createGroup('group');
|
||||
|
||||
if ($group && $userObject) {
|
||||
$group->addUser($userObject);
|
||||
}
|
||||
}
|
||||
|
||||
\OC_Util::tearDownFS();
|
||||
Storage::getGlobalCache()->clearCache();
|
||||
Server::get(IUserSession::class)->setUser(null);
|
||||
Filesystem::tearDown();
|
||||
Server::get(IUserSession::class)->login($user, $password);
|
||||
\OC::$server->getUserFolder($user);
|
||||
|
||||
\OC_Util::setupFS($user);
|
||||
$this->rootFolder->newFolder('/' . $uid . '/files');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue