From 019527fc728b02ccbc3c52da53576aa4a7982fd6 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 22 Jan 2018 10:36:28 +0100 Subject: Update to phpunit 6 Signed-off-by: Christoph Wurst --- tests/Unit/Activity/ProviderTest.php | 112 ++++++++++++++++ tests/Unit/Activity/SettingTest.php | 58 +++++++++ tests/Unit/Controller/SettingsControllerTest.php | 102 +++++++++++++++ tests/Unit/Provider/U2FProviderTest.php | 109 ++++++++++++++++ tests/Unit/Service/U2FManagerTest.php | 159 +++++++++++++++++++++++ 5 files changed, 540 insertions(+) create mode 100644 tests/Unit/Activity/ProviderTest.php create mode 100644 tests/Unit/Activity/SettingTest.php create mode 100644 tests/Unit/Controller/SettingsControllerTest.php create mode 100644 tests/Unit/Provider/U2FProviderTest.php create mode 100644 tests/Unit/Service/U2FManagerTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/Activity/ProviderTest.php b/tests/Unit/Activity/ProviderTest.php new file mode 100644 index 0000000..0813259 --- /dev/null +++ b/tests/Unit/Activity/ProviderTest.php @@ -0,0 +1,112 @@ + + * @copyright Copyright (c) 2016 Christoph Wurst + * + * Two-factor U2F + * + * 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, + * along with this program. If not, see + * + */ + +namespace OCA\TwoFactorU2F\Tests\Unit\Activity; + +use InvalidArgumentException; +use OCA\TwoFactorU2F\Activity\Provider; +use OCP\Activity\IEvent; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; +use PHPUnit\Framework\TestCase; + +class ProviderTest extends TestCase { + + private $l10n; + private $urlGenerator; + private $logger; + + /** @var Provider */ + private $provider; + + protected function setUp() { + parent::setUp(); + + $this->l10n = $this->createMock(IFactory::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->logger = $this->createMock(ILogger::class); + + $this->provider = new Provider($this->l10n, $this->urlGenerator, $this->logger); + } + + public function testParseUnrelated() { + $lang = 'ru'; + $event = $this->createMock(IEvent::class); + $event->expects($this->once()) + ->method('getApp') + ->willReturn('comments'); + $this->expectException(InvalidArgumentException::class); + + $this->provider->parse($lang, $event); + } + + public function subjectData() { + return [ + ['u2f_device_added'], + ['u2f_device_removed'], + [null], + ]; + } + + /** + * @dataProvider subjectData + */ + public function testParse($subject) { + $lang = 'ru'; + $event = $this->createMock(IEvent::class); + $l = $this->createMock(IL10N::class); + + $event->expects($this->once()) + ->method('getApp') + ->willReturn('twofactor_u2f'); + $this->l10n->expects($this->once()) + ->method('get') + ->with('twofactor_u2f', $lang) + ->willReturn($l); + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('core', 'actions/password.svg') + ->willReturn('path/to/image'); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with('path/to/image') + ->willReturn('absolute/path/to/image'); + $event->expects($this->once()) + ->method('setIcon') + ->with('absolute/path/to/image'); + $event->expects($this->once()) + ->method('getSubject') + ->willReturn($subject); + if (is_null($subject)) { + $event->expects($this->never()) + ->method('setSubject'); + } else { + $event->expects($this->once()) + ->method('setSubject'); + } + + $this->provider->parse($lang, $event); + } + +} diff --git a/tests/Unit/Activity/SettingTest.php b/tests/Unit/Activity/SettingTest.php new file mode 100644 index 0000000..7179557 --- /dev/null +++ b/tests/Unit/Activity/SettingTest.php @@ -0,0 +1,58 @@ + + * @copyright Copyright (c) 2016 Christoph Wurst + * + * Two-factor U2F + * + * 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, + * along with this program. If not, see + * + */ + +namespace OCA\TwoFactorU2F\Tests\Unit\Activity; + +use OCA\TwoFactorU2F\Activity\Setting; +use OCP\IL10N; +use PHPUnit\Framework\TestCase; + +class SettingTest extends TestCase { + + private $l10n; + + /** @var Setting */ + private $setting; + + protected function setUp() { + parent::setUp(); + + $this->l10n = $this->createMock(IL10N::class); + + $this->setting = new Setting($this->l10n); + } + + public function testAll() { + $this->assertEquals(false, $this->setting->canChangeMail()); + $this->assertEquals(false, $this->setting->canChangeStream()); + $this->assertEquals('twofactor_u2f', $this->setting->getIdentifier()); + $this->l10n->expects($this->once()) + ->method('t') + ->with('U2F device') + ->willReturn('U2F Gerät'); + $this->assertEquals('U2F Gerät', $this->setting->getName()); + $this->assertEquals(30, $this->setting->getPriority()); + $this->assertEquals(true, $this->setting->isDefaultEnabledMail()); + $this->assertEquals(true, $this->setting->isDefaultEnabledStream()); + } + +} diff --git a/tests/Unit/Controller/SettingsControllerTest.php b/tests/Unit/Controller/SettingsControllerTest.php new file mode 100644 index 0000000..0735bf2 --- /dev/null +++ b/tests/Unit/Controller/SettingsControllerTest.php @@ -0,0 +1,102 @@ + + * @copyright Christoph Wurst 2016 + */ + +namespace OCA\TwoFactorU2F\Tests\Unit\Controller; + +use OCA\TwoFactorU2F\Controller\SettingsController; +use OCA\TwoFactorU2F\Service\U2FManager; +use OCP\IRequest; +use OCP\IUser; +use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +class SettingsControllerTest extends TestCase { + + /** @var IRequest|MockObject */ + private $request; + + /** @var U2FManager|MockObject */ + private $u2fManager; + + /** @var IUserSession|MockObject */ + private $userSession; + + /** @var SettingsController */ + private $controller; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->u2fManager = $this->createMock(U2FManager::class); + $this->userSession = $this->createMock(IUserSession::class); + + $this->controller = new SettingsController('twofactor_u2f', $this->request, $this->u2fManager, $this->userSession); + } + + public function testState() { + $user = $this->createMock(IUser::class); + $devices = [ + [ + 'id' => 1, + 'name' => null, + ], + [ + 'id' => 2, + 'name' => 'Yolokey', + ], + ]; + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $this->u2fManager->expects($this->once()) + ->method('getDevices') + ->with($this->equalTo($user)) + ->willReturn($devices); + + $expected = [ + 'devices' => $devices, + ]; + $this->assertSame($expected, $this->controller->state()); + } + + public function testStartRegister() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + + $this->u2fManager->expects($this->once()) + ->method('startRegistration') + ->with($this->equalTo($user)) + ->willReturn([]); + + $this->assertEquals([], $this->controller->startRegister()); + } + + public function testFinishRegister() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $registrationData = 'regData'; + $data = 'some data'; + + $this->u2fManager->expects($this->once()) + ->method('finishRegistration') + ->with($this->equalTo($user), $this->equalTo($registrationData), $this->equalTo($data)); + + $this->controller->finishRegister($registrationData, $data); + } + +} diff --git a/tests/Unit/Provider/U2FProviderTest.php b/tests/Unit/Provider/U2FProviderTest.php new file mode 100644 index 0000000..13b9480 --- /dev/null +++ b/tests/Unit/Provider/U2FProviderTest.php @@ -0,0 +1,109 @@ + + * @copyright Christoph Wurst 2016 + */ + +namespace OCA\TwoFactorU2F\Tests\Unit\Provider; + +use OCA\TwoFactorU2F\Provider\U2FProvider; +use OCA\TwoFactorU2F\Service\U2FManager; +use OCP\IL10N; +use OCP\IUser; +use OCP\Template; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +class U2FProviderTest extends TestCase { + + /** @var IL10N|MockObject */ + private $l10n; + + /** @var U2FManager|MockObject */ + private $manager; + + /** @var U2FProvider */ + private $provider; + + protected function setUp() { + parent::setUp(); + + $this->l10n = $this->createMock(IL10N::class); + $this->manager = $this->createMock(U2FManager::class); + + $this->provider = new U2FProvider($this->l10n, $this->manager); + } + + public function testGetId() { + $this->assertSame('u2f', $this->provider->getId()); + } + + public function testGetDisplayName() { + $this->assertSame('U2F device', $this->provider->getDisplayName()); + } + + public function testGetDescription() { + $this->l10n->expects($this->once()) + ->method('t') + ->with('Authenticate with an U2F device') + ->willReturn('translated'); + + $this->assertSame('translated', $this->provider->getDescription()); + } + + public function testGetTemplate() { + $user = $this->createMock(IUser::class); + $this->manager->expects($this->once()) + ->method('startAuthenticate') + ->willReturn([]); + + $tmpl = new Template('twofactor_u2f', 'challenge'); + $tmpl->assign('reqs', []); + + $actual = $this->provider->getTemplate($user); + $this->assertEquals($tmpl, $actual); + $actual->fetchPage(); + } + + public function testVerifyChallenge() { + $user = $this->createMock(IUser::class); + $val = '123'; + + $this->manager->expects($this->once()) + ->method('finishAuthenticate') + ->willReturn(false); + + $this->assertFalse($this->provider->verifyChallenge($user, $val)); + } + + public function testIsTwoFactorAuthEnabledForUser() { + $user = $this->createMock(IUser::class); + $devices = [ + 'dev1', + ]; + + $this->manager->expects($this->once()) + ->method('getDevices') + ->willReturn($devices); + + $this->assertTrue($this->provider->isTwoFactorAuthEnabledForUser($user)); + } + + public function testIsTwoFactorAuthDisabledForUser() { + $user = $this->createMock(IUser::class); + $devices = []; + + $this->manager->expects($this->once()) + ->method('getDevices') + ->willReturn($devices); + + $this->assertFalse($this->provider->isTwoFactorAuthEnabledForUser($user)); + } + +} diff --git a/tests/Unit/Service/U2FManagerTest.php b/tests/Unit/Service/U2FManagerTest.php new file mode 100644 index 0000000..fdf65a2 --- /dev/null +++ b/tests/Unit/Service/U2FManagerTest.php @@ -0,0 +1,159 @@ + + * @copyright Christoph Wurst 2016 + */ + +namespace OCA\TwoFactorU2F\Tests\Unit\Service; + +use OCA\TwoFactorU2F\Db\Registration; +use OCA\TwoFactorU2F\Db\RegistrationMapper; +use OCA\TwoFactorU2F\Service\U2FManager; +use OCP\Activity\IEvent; +use OCP\Activity\IManager; +use OCP\ILogger; +use OCP\IRequest; +use OCP\ISession; +use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +class U2FManagerTest extends TestCase { + + /** @var RegistrationMapper|MockObject */ + private $mapper; + + /** @var ISession|MockObject */ + private $session; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var IRequest|MockObject */ + private $request; + + /** @var IManager|MockObject */ + private $activityManager; + + /** @var U2FManager */ + private $manager; + + protected function setUp() { + parent::setUp(); + + $this->mapper = $this->createMock(RegistrationMapper::class); + $this->session = $this->createMock(ISession::class); + $this->logger = $this->createMock(ILogger::class); + $this->request = $this->createMock(IRequest::class); + $this->activityManager = $this->createMock(IManager::class); + + $this->manager = new U2FManager($this->mapper, $this->session, $this->logger, $this->request, $this->activityManager); + } + + /** + * @param IUser $user + * @param int $nr + */ + private function mockRegistrations(IUser $user, $nr) { + $regs = []; + for ($i = 0; $i < $nr; $i++) { + $reg = new Registration(); + array_push($regs, $reg); + } + $this->mapper->expects($this->once()) + ->method('findRegistrations') + ->with($this->equalTo($user)) + ->willReturn($regs); + } + + public function testGetDevices() { + $user = $this->createMock(IUser::class); + $this->mockRegistrations($user, 2); + + $this->assertCount(2, $this->manager->getDevices($user)); + } + + public function testGetNoDevices() { + $user = $this->createMock(IUser::class); + $this->mockRegistrations($user, 0); + + $this->assertEmpty($this->manager->getDevices($user)); + } + + public function testDisableU2F() { + $user = $this->createMock(IUser::class); + $event = $this->createMock(IEvent::class); + $reg = $this->createMock(Registration::class); + + $this->mapper->expects($this->once()) + ->method('findRegistration') + ->with($user, 13) + ->willReturn($reg); + $this->mapper->expects($this->once()) + ->method('delete') + ->with($reg); + $this->activityManager->expects($this->once()) + ->method('generateEvent') + ->willReturn($event); + $event->expects($this->once()) + ->method('setApp') + ->with($this->equalTo('twofactor_u2f')) + ->willReturnSelf(); + $event->expects($this->once()) + ->method('setType') + ->with($this->equalTo('security')) + ->willReturnSelf(); + $user->expects($this->any()) + ->method('getUID') + ->willReturn('ursula'); + $event->expects($this->once()) + ->method('setAuthor') + ->with($this->equalTo('ursula')) + ->willReturnSelf(); + $event->expects($this->once()) + ->method('setAffectedUser') + ->with($this->equalTo('ursula')) + ->willReturnSelf(); + $event->expects($this->once()) + ->method('setSubject') + ->with($this->equalTo('u2f_device_removed')) + ->willReturnSelf(); + $this->activityManager->expects($this->once()) + ->method('publish') + ->with($this->equalTo($event)); + + $this->manager->removeDevice($user, 13); + } + + public function testStartRegistrationFirstDevice() { + $user = $this->createMock(IUser::class); + $this->mockRegistrations($user, 0); + + $this->session->expects($this->once()) + ->method('set'); + + $this->manager->startRegistration($user); + } + + public function testFinishRegistration() { + // TODO: get a grasp of how the u2f lib works and feed it with + // realistic data or mock it. + } + + public function testStartAuthenticate() { + // TODO: get a grasp of how the u2f lib works and feed it with + // realistic data or mock it. + } + + public function testFinishAuthenticate() { + // TODO: get a grasp of how the u2f lib works and feed it with + // realistic data or mock it. + } + +} -- cgit v1.2.3