From 09caadac057094181d3ed8470bcd09b2dec18dbe Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 20 Dec 2016 16:35:42 +0100 Subject: Add tests for 2FA commands Signed-off-by: Roeland Jago Douma --- tests/Core/Command/TwoFactorAuth/DisableTest.php | 99 ++++++++++++++++++++++++ tests/Core/Command/TwoFactorAuth/EnableTest.php | 99 ++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 tests/Core/Command/TwoFactorAuth/DisableTest.php create mode 100644 tests/Core/Command/TwoFactorAuth/EnableTest.php (limited to 'tests/Core/Command') diff --git a/tests/Core/Command/TwoFactorAuth/DisableTest.php b/tests/Core/Command/TwoFactorAuth/DisableTest.php new file mode 100644 index 00000000000..1a0bbc6c3d3 --- /dev/null +++ b/tests/Core/Command/TwoFactorAuth/DisableTest.php @@ -0,0 +1,99 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace Test\Core\Command\TwoFactorAuth; + +use OC\Authentication\TwoFactorAuth\Manager; +use OC\Core\Command\TwoFactorAuth\Disable; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class DisableTest extends TestCase { + + /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */ + private $manager; + + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + + /** @var Disable */ + private $command; + + public function setUp() { + parent::setUp(); + + $this->manager = $this->createMock(Manager::class); + $this->userManager = $this->createMock(IUserManager::class); + + $this->command = new Disable($this->manager, $this->userManager); + } + + public function testDisableSuccess() { + $user = $this->createMock(IUser::class); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $input->method('getArgument') + ->with($this->equalTo('uid')) + ->willReturn('user'); + + $this->userManager->method('get') + ->with('user') + ->willReturn($user); + + $this->manager->expects($this->once()) + ->method('disableTwoFactorAuthentication') + ->with($this->equalTo($user)); + + $output->expects($this->once()) + ->method('writeln') + ->with('Two-factor authentication disabled for user user'); + + $this->invokePrivate($this->command, 'execute', [$input, $output]); + } + + public function testEnableFail() { + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $input->method('getArgument') + ->with($this->equalTo('uid')) + ->willReturn('user'); + + $this->userManager->method('get') + ->with('user') + ->willReturn(null); + + $this->manager->expects($this->never()) + ->method($this->anything()); + + $output->expects($this->once()) + ->method('writeln') + ->with('Invalid UID'); + + $this->invokePrivate($this->command, 'execute', [$input, $output]); + } +} diff --git a/tests/Core/Command/TwoFactorAuth/EnableTest.php b/tests/Core/Command/TwoFactorAuth/EnableTest.php new file mode 100644 index 00000000000..ebca40df9a5 --- /dev/null +++ b/tests/Core/Command/TwoFactorAuth/EnableTest.php @@ -0,0 +1,99 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace Test\Core\Command\TwoFactorAuth; + +use OC\Authentication\TwoFactorAuth\Manager; +use OC\Core\Command\TwoFactorAuth\Enable; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class EnableTest extends TestCase { + + /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */ + private $manager; + + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + + /** @var Enable */ + private $command; + + public function setUp() { + parent::setUp(); + + $this->manager = $this->createMock(Manager::class); + $this->userManager = $this->createMock(IUserManager::class); + + $this->command = new Enable($this->manager, $this->userManager); + } + + public function testEnableSuccess() { + $user = $this->createMock(IUser::class); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $input->method('getArgument') + ->with($this->equalTo('uid')) + ->willReturn('user'); + + $this->userManager->method('get') + ->with('user') + ->willReturn($user); + + $this->manager->expects($this->once()) + ->method('enableTwoFactorAuthentication') + ->with($this->equalTo($user)); + + $output->expects($this->once()) + ->method('writeln') + ->with('Two-factor authentication enabled for user user'); + + $this->invokePrivate($this->command, 'execute', [$input, $output]); + } + + public function testEnableFail() { + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $input->method('getArgument') + ->with($this->equalTo('uid')) + ->willReturn('user'); + + $this->userManager->method('get') + ->with('user') + ->willReturn(null); + + $this->manager->expects($this->never()) + ->method($this->anything()); + + $output->expects($this->once()) + ->method('writeln') + ->with('Invalid UID'); + + $this->invokePrivate($this->command, 'execute', [$input, $output]); + } +} -- cgit v1.2.3