diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2018-08-06 16:09:21 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2018-08-06 16:59:29 +0300 |
commit | 5a90c2a51adce307a8451f7957df8a27aa581dba (patch) | |
tree | 033f252750948afc38891c2cfcd5a992b7229bd7 /tests | |
parent | ebf8405be7c118f5d91ac3dea82ed2d0b9043abf (diff) |
Refactor class structure to resolve cyclic dependencies
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Unit/Provider/StateTest.php | 53 | ||||
-rw-r--r-- | tests/Unit/Service/SetupServiceTest.php | 113 | ||||
-rw-r--r-- | tests/Unit/Service/StateStorageTest.php | 168 |
3 files changed, 293 insertions, 41 deletions
diff --git a/tests/Unit/Provider/StateTest.php b/tests/Unit/Provider/StateTest.php new file mode 100644 index 0000000..9a49ecb --- /dev/null +++ b/tests/Unit/Provider/StateTest.php @@ -0,0 +1,53 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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/>. + * + */ + +namespace OCA\TwoFactorGateway\Tests\Unit\Provider; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\TwoFactorGateway\Provider\SmsProvider; +use OCA\TwoFactorGateway\Provider\State; +use OCP\IUser; + +class StateTest extends TestCase { + + public function testVerify() { + $user = $this->createMock(IUser::class); + $original = State::verifying( + $user, + 'signal', + '0123456789', + '123456' + ); + $expected = new State( + $user, + SmsProvider::STATE_ENABLED, + 'signal', + '0123456789', + '123456' + ); + + $actual = $original->verify(); + + $this->assertEquals($expected, $actual); + } + +}
\ No newline at end of file diff --git a/tests/Unit/Service/SetupServiceTest.php b/tests/Unit/Service/SetupServiceTest.php index 5710615..fa634cb 100644 --- a/tests/Unit/Service/SetupServiceTest.php +++ b/tests/Unit/Service/SetupServiceTest.php @@ -23,24 +23,24 @@ namespace OCA\TwoFactorGateway\Tests\Unit\Service; use ChristophWurst\Nextcloud\Testing\TestCase; -use OC\Accounts\AccountManager; use OCA\TwoFactorGateway\Exception\IdentifierMissingException; use OCA\TwoFactorGateway\Exception\VerificationException; use OCA\TwoFactorGateway\Exception\VerificationTransmissionException; +use OCA\TwoFactorGateway\Provider\State; use OCA\TwoFactorGateway\Service\IGateway; use OCA\TwoFactorGateway\Service\SetupService; -use OCP\IConfig; +use OCA\TwoFactorGateway\Service\StateStorage; use OCP\IUser; use OCP\Security\ISecureRandom; use PHPUnit_Framework_MockObject_MockObject; class SetupServiceTest extends TestCase { - /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */ - private $config; + /** @var StateStorage|PHPUnit_Framework_MockObject_MockObject */ + private $stateStorage; /** @var IGateway|PHPUnit_Framework_MockObject_MockObject */ - private $smsService; + private $gateway; /** @var ISecureRandom|PHPUnit_Framework_MockObject_MockObject */ private $random; @@ -51,17 +51,42 @@ class SetupServiceTest extends TestCase { protected function setUp() { parent::setUp(); - $this->config = $this->createMock(IConfig::class); - $this->smsService = $this->createMock(IGateway::class); + $this->stateStorage = $this->createMock(StateStorage::class); + $this->gateway = $this->createMock(IGateway::class); $this->random = $this->createMock(ISecureRandom::class); - $this->setupService = new SetupService($this->config, $this->smsService, $this->random); + $this->setupService = new SetupService($this->stateStorage, $this->gateway, $this->random); + } + + public function testGetIdentifierNoneFound() { + /** @var IUser $user */ + $user = $this->createMock(IUser::class); + $state = State::disabled($user); + $this->stateStorage->expects($this->once()) + ->method('get') + ->willReturn($state); + $this->setExpectedException(IdentifierMissingException::class); + + $this->setupService->getChallengePhoneNumber($user); + } + + public function testGetIdentifier() { + /** @var IUser $user */ + $user = $this->createMock(IUser::class); + $state = State::verifying($user, 'websms', '01234', '1234'); + $this->stateStorage->expects($this->once()) + ->method('get') + ->willReturn($state); + + $identifier = $this->setupService->getChallengePhoneNumber($user); + + $this->assertSame('01234', $identifier); } public function testStartSetupTransmissionError() { - $identifier = "1234"; + $identifier = '1234'; $user = $this->createMock(IUser::class); - $this->smsService->expects($this->once()) + $this->gateway->expects($this->once()) ->method('send') ->willThrowException(new VerificationTransmissionException()); $this->expectException(VerificationTransmissionException::class); @@ -70,34 +95,34 @@ class SetupServiceTest extends TestCase { } public function testStartSetup() { - $identifier = "0123456789"; + $identifier = '0123456789'; + $gatewayName = 'websms'; + $this->gateway->method('getShortName')->willReturn($gatewayName); $user = $this->createMock(IUser::class); - $this->smsService->expects($this->once()) + $this->gateway->expects($this->once()) ->method('send'); $this->random->expects($this->once()) ->method('generate') ->willReturn('963852'); - $user->method('getUID')->willReturn('user123'); - $this->config->expects($this->at(0)) - ->method('setUserValue') - ->with('user123', 'twofactor_gateway', 'identifier', '0123456789'); - $this->config->expects($this->at(1)) - ->method('setUserValue') - ->with('user123', 'twofactor_gateway', 'verification_code', '963852'); - $this->config->expects($this->at(2)) - ->method('setUserValue') - ->with('user123', 'twofactor_gateway', 'verified', 'false'); + $state = State::verifying($user, $gatewayName, $identifier, '963852'); + $this->stateStorage->expects($this->once()) + ->method('persist') + ->with($this->equalTo($state)) + ->willReturnArgument(0); - $this->setupService->startSetup($user, $identifier); + $actualState = $this->setupService->startSetup($user, $identifier); + + $this->assertEquals($state, $actualState); } public function testFinishSetupNoVerificationNumberSet() { $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('user123'); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('user123', 'twofactor_gateway', 'verification_code', null) - ->willReturn(null); + $state = State::disabled($user); + $this->stateStorage->expects($this->once()) + ->method('get') + ->willReturn($state); + $this->stateStorage->expects($this->never()) + ->method('persist'); $this->expectException(\Exception::class); $this->setupService->finishSetup($user, '123456'); @@ -106,10 +131,12 @@ class SetupServiceTest extends TestCase { public function testFinishSetupWithWrongVerificationNumber() { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('user123'); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('user123', 'twofactor_gateway', 'verification_code', null) - ->willReturn('111111'); + $state = State::verifying($user, 'websms', '0123456789', '654321'); + $this->stateStorage->expects($this->once()) + ->method('get') + ->willReturn($state); + $this->stateStorage->expects($this->never()) + ->method('persist'); $this->expectException(VerificationException::class); $this->setupService->finishSetup($user, '123456'); @@ -118,15 +145,19 @@ class SetupServiceTest extends TestCase { public function testFinishSetup() { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('user123'); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('user123', 'twofactor_gateway', 'verification_code', null) - ->willReturn('123456'); - $this->config->expects($this->once()) - ->method('setUserValue') - ->with('user123', 'twofactor_gateway', 'verified', 'true'); - - $this->setupService->finishSetup($user, '123456'); + $state = State::verifying($user, 'websms', '0123456789', '123456'); + $this->stateStorage->expects($this->once()) + ->method('get') + ->willReturn($state); + $verfied = $state->verify(); + $this->stateStorage->expects($this->once()) + ->method('persist') + ->with($this->equalTo($verfied)) + ->willReturnArgument(0); + + $actualState = $this->setupService->finishSetup($user, '123456'); + + $this->assertEquals($verfied, $actualState); } } diff --git a/tests/Unit/Service/StateStorageTest.php b/tests/Unit/Service/StateStorageTest.php new file mode 100644 index 0000000..0416efe --- /dev/null +++ b/tests/Unit/Service/StateStorageTest.php @@ -0,0 +1,168 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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/>. + * + */ + +namespace OCA\TwoFactorGateway\Tests\Unit\Provider; + +use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\TwoFactorGateway\Provider\SmsProvider; +use OCA\TwoFactorGateway\Provider\State; +use OCA\TwoFactorGateway\Service\IGateway; +use OCA\TwoFactorGateway\Service\StateStorage; +use OCP\IConfig; +use OCP\IUser; +use PHPUnit_Framework_MockObject_MockObject; + +class StateStorageTest extends TestCase { + + /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */ + private $config; + + /** @var IGateway|PHPUnit_Framework_MockObject_MockObject */ + private $gateway; + + /** @var StateStorage */ + private $storage; + + protected function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->gateway = $this->createMock(IGateway::class); + + $this->storage = new StateStorage($this->config, $this->gateway); + } + + public function testGetNoDataYet() { + $uid = 'user123'; + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($uid); + $this->config->method('getUserValue') + ->willReturnMap([ + [$uid, 'twofactor_gateway', 'verified', 'false', 'false'], + ]); + + $state = $this->storage->get($user); + + $this->assertSame(SmsProvider::STATE_DISABLED, $state->getState()); + } + + public function testGetVerifyingState() { + $uid = 'user123'; + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($uid); + $this->config->method('getUserValue') + ->willReturnMap([ + [$uid, 'twofactor_gateway', 'verified', 'false', 'false'], + [$uid, 'twofactor_gateway', 'identifier', null, '0123456789'], + [$uid, 'twofactor_gateway', 'verification_code', null, '123456'], + ]); + + $state = $this->storage->get($user); + + $this->assertSame(SmsProvider::STATE_VERIFYING, $state->getState()); + $this->assertSame('0123456789', $state->getIdentifier()); + $this->assertSame('123456', $state->getVerificationCode()); + } + + public function testGetEnabledState() { + $uid = 'user123'; + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($uid); + $this->config->method('getUserValue') + ->willReturnMap([ + [$uid, 'twofactor_gateway', 'verified', 'false', 'true'], + [$uid, 'twofactor_gateway', 'identifier', null, '0123456789'], + ]); + + $state = $this->storage->get($user); + + $this->assertSame(SmsProvider::STATE_ENABLED, $state->getState()); + $this->assertSame('0123456789', $state->getIdentifier()); + } + + public function testDisabledState() { + $uid = 'user123'; + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($uid); + $this->config->method('getUserValue') + ->willReturnMap([ + [$uid, 'twofactor_gateway', 'verified', 'false', 'false'], + [$uid, 'twofactor_gateway', 'identifier', null, '0123456789'], + ]); + + $state = $this->storage->get($user); + + $this->assertSame(SmsProvider::STATE_DISABLED, $state->getState()); + $this->assertSame('0123456789', $state->getIdentifier()); + } + + public function testPersistVerifyingState() { + $user = $this->createMock(IUser::class); + $uid = 'user321'; + $user->method('getUID')->willReturn($uid); + $state = State::verifying( + $user, + 'telegram', + '0123456789', + '1234' + ); + $this->config + ->expects($this->exactly(3)) + ->method('setUserValue') + ->withConsecutive( + [$uid, 'twofactor_gateway', 'identifier', '0123456789'], + [$uid, 'twofactor_gateway', 'verification_code', '1234'], + [$uid, 'twofactor_gateway', 'verified', 'false'] + ); + + $persisted = $this->storage->persist($state); + + $this->assertSame($persisted, $state); + } + + public function testVerifiedState() { + $user = $this->createMock(IUser::class); + $uid = 'user321'; + $user->method('getUID')->willReturn($uid); + $state = new State( + $user, + SmsProvider::STATE_ENABLED, + 'telegram', + '0123456789', + '1234' + ); + $this->config + ->expects($this->exactly(3)) + ->method('setUserValue') + ->withConsecutive( + [$uid, 'twofactor_gateway', 'identifier', '0123456789'], + [$uid, 'twofactor_gateway', 'verification_code', '1234'], + [$uid, 'twofactor_gateway', 'verified', 'true'] + ); + + $persisted = $this->storage->persist($state); + + $this->assertSame($persisted, $state); + } + + +}
\ No newline at end of file |