2016-06-21 07:55:07 -04:00
|
|
|
<?php
|
2025-05-17 05:46:26 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2016-06-21 07:55:07 -04:00
|
|
|
/**
|
2024-06-06 03:55:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-06-21 07:55:07 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files_External\Tests\Auth\Password;
|
|
|
|
|
|
|
|
|
|
use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
|
2025-05-17 05:46:26 -04:00
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2016-09-02 04:37:20 -04:00
|
|
|
use OCP\IL10N;
|
|
|
|
|
use OCP\Security\ICredentialsManager;
|
2025-05-17 05:46:26 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2016-06-21 07:55:07 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class GlobalAuthTest extends TestCase {
|
2025-05-17 05:46:26 -04:00
|
|
|
private IL10N&MockObject $l10n;
|
|
|
|
|
private ICredentialsManager&MockObject $credentialsManager;
|
|
|
|
|
private GlobalAuth $instance;
|
2016-06-21 07:55:07 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2016-06-21 07:55:07 -04:00
|
|
|
parent::setUp();
|
2016-09-02 04:37:20 -04:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
|
$this->credentialsManager = $this->createMock(ICredentialsManager::class);
|
2016-06-21 07:55:07 -04:00
|
|
|
$this->instance = new GlobalAuth($this->l10n, $this->credentialsManager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getStorageConfig($type, $config = []) {
|
2025-06-30 10:56:59 -04:00
|
|
|
/** @var StorageConfig&MockObject $storageConfig */
|
2016-09-02 04:37:20 -04:00
|
|
|
$storageConfig = $this->createMock(StorageConfig::class);
|
2016-06-21 07:55:07 -04:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
|
->method('getType')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($type);
|
2016-06-21 07:55:07 -04:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
|
->method('getBackendOptions')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnCallback(function () use (&$config) {
|
2016-06-21 07:55:07 -04:00
|
|
|
return $config;
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2016-06-21 07:55:07 -04:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
|
->method('getBackendOption')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnCallback(function ($key) use (&$config) {
|
2016-06-21 07:55:07 -04:00
|
|
|
return $config[$key];
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2016-06-21 07:55:07 -04:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
|
->method('setBackendOption')
|
2024-09-20 11:38:36 -04:00
|
|
|
->willReturnCallback(function ($key, $value) use (&$config): void {
|
2016-06-21 07:55:07 -04:00
|
|
|
$config[$key] = $value;
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2016-06-21 07:55:07 -04:00
|
|
|
|
|
|
|
|
return $storageConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNoCredentials(): void {
|
|
|
|
|
$this->credentialsManager->expects($this->once())
|
|
|
|
|
->method('retrieve')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(null);
|
2016-06-21 07:55:07 -04:00
|
|
|
|
|
|
|
|
$storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
|
|
|
|
|
|
|
|
|
|
$this->instance->manipulateStorageConfig($storage);
|
|
|
|
|
$this->assertEquals([], $storage->getBackendOptions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSavedCredentials(): void {
|
|
|
|
|
$this->credentialsManager->expects($this->once())
|
|
|
|
|
->method('retrieve')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn([
|
2016-06-21 07:55:07 -04:00
|
|
|
'user' => 'a',
|
|
|
|
|
'password' => 'b'
|
2020-03-25 17:21:27 -04:00
|
|
|
]);
|
2016-06-21 07:55:07 -04:00
|
|
|
|
|
|
|
|
$storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
|
|
|
|
|
|
|
|
|
|
$this->instance->manipulateStorageConfig($storage);
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
'user' => 'a',
|
|
|
|
|
'password' => 'b'
|
|
|
|
|
], $storage->getBackendOptions());
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
|
2016-06-21 07:55:07 -04:00
|
|
|
public function testNoCredentialsPersonal(): void {
|
2024-10-10 06:40:31 -04:00
|
|
|
$this->expectException(InsufficientDataForMeaningfulAnswerException::class);
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2016-06-21 07:55:07 -04:00
|
|
|
$this->credentialsManager->expects($this->never())
|
|
|
|
|
->method('retrieve');
|
|
|
|
|
|
2023-07-10 09:31:47 -04:00
|
|
|
$storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_PERSONAL);
|
2016-06-21 07:55:07 -04:00
|
|
|
|
|
|
|
|
$this->instance->manipulateStorageConfig($storage);
|
|
|
|
|
$this->assertEquals([], $storage->getBackendOptions());
|
|
|
|
|
}
|
|
|
|
|
}
|