2016-06-21 14:55:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-06-21 14:55:07 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-06-21 14:55:07 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_External\Tests\Auth\Password;
|
|
|
|
|
|
|
|
use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
|
|
|
|
use OCA\Files_external\Lib\StorageConfig;
|
2016-09-02 11:37:20 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\Security\ICredentialsManager;
|
2016-06-21 14:55:07 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class GlobalAuthTest extends TestCase {
|
|
|
|
/**
|
|
|
|
* @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $l10n;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Security\ICredentialsManager|\PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $credentialsManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GlobalAuth
|
|
|
|
*/
|
|
|
|
private $instance;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-06-21 14:55:07 +03:00
|
|
|
parent::setUp();
|
2016-09-02 11:37:20 +03:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
$this->credentialsManager = $this->createMock(ICredentialsManager::class);
|
2016-06-21 14:55:07 +03:00
|
|
|
$this->instance = new GlobalAuth($this->l10n, $this->credentialsManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getStorageConfig($type, $config = []) {
|
|
|
|
/** @var \OCA\Files_External\Lib\StorageConfig|\PHPUnit_Framework_MockObject_MockObject $storageConfig */
|
2016-09-02 11:37:20 +03:00
|
|
|
$storageConfig = $this->createMock(StorageConfig::class);
|
2016-06-21 14:55:07 +03:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
->method('getType')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($type);
|
2016-06-21 14:55:07 +03:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
->method('getBackendOptions')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function () use (&$config) {
|
2016-06-21 14:55:07 +03:00
|
|
|
return $config;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-06-21 14:55:07 +03:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
->method('getBackendOption')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($key) use (&$config) {
|
2016-06-21 14:55:07 +03:00
|
|
|
return $config[$key];
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-06-21 14:55:07 +03:00
|
|
|
$storageConfig->expects($this->any())
|
|
|
|
->method('setBackendOption')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($key, $value) use (&$config) {
|
2016-06-21 14:55:07 +03:00
|
|
|
$config[$key] = $value;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-06-21 14:55:07 +03:00
|
|
|
|
|
|
|
return $storageConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoCredentials() {
|
|
|
|
$this->credentialsManager->expects($this->once())
|
|
|
|
->method('retrieve')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(null);
|
2016-06-21 14:55:07 +03:00
|
|
|
|
|
|
|
$storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
|
|
|
|
|
|
|
|
$this->instance->manipulateStorageConfig($storage);
|
|
|
|
$this->assertEquals([], $storage->getBackendOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavedCredentials() {
|
|
|
|
$this->credentialsManager->expects($this->once())
|
|
|
|
->method('retrieve')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([
|
2016-06-21 14:55:07 +03:00
|
|
|
'user' => 'a',
|
|
|
|
'password' => 'b'
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2016-06-21 14:55:07 +03:00
|
|
|
|
|
|
|
$storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_ADMIN);
|
|
|
|
|
|
|
|
$this->instance->manipulateStorageConfig($storage);
|
|
|
|
$this->assertEquals([
|
|
|
|
'user' => 'a',
|
|
|
|
'password' => 'b'
|
|
|
|
], $storage->getBackendOptions());
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2016-06-21 14:55:07 +03:00
|
|
|
public function testNoCredentialsPersonal() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException::class);
|
|
|
|
|
2016-06-21 14:55:07 +03:00
|
|
|
$this->credentialsManager->expects($this->never())
|
|
|
|
->method('retrieve');
|
|
|
|
|
|
|
|
$storage = $this->getStorageConfig(StorageConfig::MOUNT_TYPE_PERSONAl);
|
|
|
|
|
|
|
|
$this->instance->manipulateStorageConfig($storage);
|
|
|
|
$this->assertEquals([], $storage->getBackendOptions());
|
|
|
|
}
|
|
|
|
}
|