2016-12-20 18:35:42 +03:00
|
|
|
<?php
|
2018-09-10 18:02:37 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-12-20 18:35:42 +03:00
|
|
|
/**
|
2018-09-10 18:02:37 +03:00
|
|
|
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-12-20 18:35:42 +03:00
|
|
|
*
|
2018-09-10 18:02:37 +03:00
|
|
|
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-12-20 18:35:42 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-09-10 18:02:37 +03:00
|
|
|
|
2016-12-20 18:35:42 +03:00
|
|
|
namespace Test\Core\Command\TwoFactorAuth;
|
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\ProviderManager;
|
2016-12-20 18:35:42 +03:00
|
|
|
use OC\Core\Command\TwoFactorAuth\Disable;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2018-09-10 18:02:37 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2016-12-20 18:35:42 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class DisableTest extends TestCase {
|
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
/** @var ProviderManager|MockObject */
|
|
|
|
private $providerManager;
|
2016-12-20 18:35:42 +03:00
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
/** @var IUserManager|MockObject */
|
2016-12-20 18:35:42 +03:00
|
|
|
private $userManager;
|
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
/** @var CommandTester */
|
2016-12-20 18:35:42 +03:00
|
|
|
private $command;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-12-20 18:35:42 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
$this->providerManager = $this->createMock(ProviderManager::class);
|
2016-12-20 18:35:42 +03:00
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
$cmd = new Disable($this->providerManager, $this->userManager);
|
|
|
|
$this->command = new CommandTester($cmd);
|
2016-12-20 18:35:42 +03:00
|
|
|
}
|
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
public function testInvalidUID() {
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('nope')
|
|
|
|
->willReturn(null);
|
2016-12-20 18:35:42 +03:00
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
$rc = $this->command->execute([
|
|
|
|
'uid' => 'nope',
|
|
|
|
'provider_id' => 'nope',
|
|
|
|
]);
|
2016-12-20 18:35:42 +03:00
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
$this->assertEquals(1, $rc);
|
|
|
|
$this->assertContains("Invalid UID", $this->command->getDisplay());
|
|
|
|
}
|
2016-12-20 18:35:42 +03:00
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
public function testEnableNotSupported() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('ricky')
|
2016-12-20 18:35:42 +03:00
|
|
|
->willReturn($user);
|
2018-09-10 18:02:37 +03:00
|
|
|
$this->providerManager->expects($this->once())
|
|
|
|
->method('tryDisableProviderFor')
|
|
|
|
->with('totp', $user)
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$rc = $this->command->execute([
|
|
|
|
'uid' => 'ricky',
|
|
|
|
'provider_id' => 'totp',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $rc);
|
|
|
|
$this->assertContains("The provider does not support this operation", $this->command->getDisplay());
|
2016-12-20 18:35:42 +03:00
|
|
|
}
|
|
|
|
|
2018-09-10 18:02:37 +03:00
|
|
|
public function testEnabled() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('ricky')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->providerManager->expects($this->once())
|
|
|
|
->method('tryDisableProviderFor')
|
|
|
|
->with('totp', $user)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$rc = $this->command->execute([
|
|
|
|
'uid' => 'ricky',
|
|
|
|
'provider_id' => 'totp',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $rc);
|
|
|
|
$this->assertContains("Two-factor provider totp disabled for user ricky", $this->command->getDisplay());
|
2016-12-20 18:35:42 +03:00
|
|
|
}
|
|
|
|
}
|