Skip to content

Commit

Permalink
Fix encryption util tests for files_external
Browse files Browse the repository at this point in the history
Don't rely on files_external being enabled and mock all the classes from
it.

Signed-off-by: Vincent Petry <vincent@nextcloud.com>
  • Loading branch information
PVince81 committed Jun 8, 2022
1 parent 32cab81 commit de93d17
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/private/Encryption/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCP\Encryption\IEncryptionModule;
use OCP\IConfig;
use OCP\IUser;
use OCP\App\IAppManager;

class Util {
public const HEADER_START = 'HBEGIN';
Expand Down Expand Up @@ -299,7 +300,8 @@ public function getUserWithAccessToMountPoint($users, $groups) {
* @return boolean
*/
public function isSystemWideMountPoint($path, $uid) {
if (\OCP\App::isEnabled("files_external")) {
$appManager = \OC::$server->get(IAppManager::class);
if ($appManager->isEnabledForUser('files_external', null)) {
/** @var GlobalStoragesService $storageService */
$storageService = \OC::$server->get(GlobalStoragesService::class);
$storages = $storageService->getAllStorages();
Expand Down
29 changes: 24 additions & 5 deletions tests/lib/Encryption/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use OC\Encryption\Util;
use OC\Files\View;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\App\IAppManager;
use OCP\Encryption\IEncryptionModule;
use OCP\IConfig;
use Test\TestCase;
Expand Down Expand Up @@ -207,6 +206,15 @@ public function dataTestIsSystemWideMountPoint() {
* @dataProvider dataTestIsSystemWideMountPoint
*/
public function testIsSystemWideMountPoint($expectedResult, $expectationText, $applicableUsers, $applicableGroups, $mountPointName = '/mp') {
$appManager = $this->createMock(IAppManager::class);
$appManager
->expects($this->once())
->method('isEnabledForUser')
->with('files_external')
->willReturn(true);

$this->overwriteService(IAppManager::class, $appManager);

$this->groupManager->method('isInGroup')
->will($this->returnValueMap([
['user1', 'group1', true], // user is only in group1
Expand All @@ -215,17 +223,28 @@ public function testIsSystemWideMountPoint($expectedResult, $expectationText, $a

$storages = [];

$storageConfig = $this->createMock(StorageConfig::class);
// StorageConfig
$storageConfig = $this->getMockBuilder('OCA\\Files_External\\Lib\\StorageConfig')
->setMethods([
'getMountPoint',
'getApplicableUsers',
'getApplicableGroups',
])
->getMock();
$storageConfig->method('getMountPoint')->willReturn($mountPointName);
$storageConfig->method('getApplicableUsers')->willReturn($applicableUsers);
$storageConfig->method('getApplicableGroups')->willReturn($applicableGroups);
$storages[] = $storageConfig;

$storagesServiceMock = $this->createMock(GlobalStoragesService::class);
$storagesServiceMock = $this->getMockBuilder('OCA\\Files_External\\Service\\GlobalStoragesService')
->setMethods([
'getAllStorages',
])
->getMock();
$storagesServiceMock->expects($this->atLeastOnce())->method('getAllStorages')
->willReturn($storages);

$this->overwriteService(GlobalStoragesService::class, $storagesServiceMock);
$this->overwriteService('OCA\\Files_External\\Service\\GlobalStoragesService', $storagesServiceMock);

$this->assertEquals($expectedResult, $this->util->isSystemWideMountPoint('/files/mp', 'user1'), 'Test case: ' . $expectationText);
}
Expand Down

0 comments on commit de93d17

Please sign in to comment.