diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-01-23 13:46:55 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-02-05 23:37:35 +0300 |
commit | 4045525705aec23cbfad978e58bdf34a9ca8d3fa (patch) | |
tree | 3be3a87e31e8e41b5c75f799cc051cf75dbdc22f /tests/Unit | |
parent | 7ea75bf02a1364289585d989ccba34ca25936c74 (diff) |
add acceptance tests and run them on the sauce labs cloud
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/Activity/ProviderTest.php | 112 | ||||
-rw-r--r-- | tests/Unit/Activity/SettingTest.php | 58 | ||||
-rw-r--r-- | tests/Unit/Controller/SettingsControllerTest.php | 115 |
3 files changed, 285 insertions, 0 deletions
diff --git a/tests/Unit/Activity/ProviderTest.php b/tests/Unit/Activity/ProviderTest.php new file mode 100644 index 0000000..bf13dc8 --- /dev/null +++ b/tests/Unit/Activity/ProviderTest.php @@ -0,0 +1,112 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Two-factor TOTP + * + * 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 <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\TwoFactorTOTP\Test\Unit\Activity; + +use InvalidArgumentException; +use OCA\TwoFactorTOTP\Activity\Provider; +use OCP\Activity\IEvent; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; +use Test\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') + ->will($this->returnValue('comments')); + $this->setExpectedException(InvalidArgumentException::class); + + $this->provider->parse($lang, $event); + } + + public function subjectData() { + return [ + ['totp_enabled_subject'], + ['totp_disabled_subject'], + [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') + ->will($this->returnValue('twofactor_totp')); + $this->l10n->expects($this->once()) + ->method('get') + ->with('twofactor_totp', $lang) + ->will($this->returnValue($l)); + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('core', 'actions/password.svg') + ->will($this->returnValue('path/to/image')); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with('path/to/image') + ->will($this->returnValue('absolute/path/to/image')); + $event->expects($this->once()) + ->method('setIcon') + ->with('absolute/path/to/image'); + $event->expects($this->once()) + ->method('getSubject') + ->will($this->returnValue($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..6fbaba4 --- /dev/null +++ b/tests/Unit/Activity/SettingTest.php @@ -0,0 +1,58 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Two-factor TOTP + * + * 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 <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\TwoFactorTOTP\Test\Unit\Activity; + +use OCA\TwoFactorTOTP\Activity\Setting; +use OCP\IL10N; +use Test\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_totp', $this->setting->getIdentifier()); + $this->l10n->expects($this->once()) + ->method('t') + ->with('TOTP (Authenticator app)') + ->will($this->returnValue('TOTP (Google Authentifizierer)')); + $this->assertEquals('TOTP (Google Authentifizierer)', $this->setting->getName()); + $this->assertEquals(10, $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..f011c5d --- /dev/null +++ b/tests/Unit/Controller/SettingsControllerTest.php @@ -0,0 +1,115 @@ +<?php + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Two-factor TOTP + * + * 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 <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\TwoFactorTOTP\Unit\Controller; + +use Endroid\QrCode\QrCode; +use OCA\TwoFactorTOTP\Controller\SettingsController; +use OCA\TwoFactorTOTP\Service\Totp; +use OCP\Defaults; +use OCP\IRequest; +use OCP\IUser; +use OCP\IUserSession; +use Test\TestCase; + +class SettingsControllerTest extends TestCase { + + private $request; + private $userSession; + private $totp; + private $defaults; + + /** @var SettingsController */ + private $controller; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->totp = $this->createMock(Totp::class); + $this->defaults = new Defaults(); + + $this->controller = new SettingsController('twofactor_totp', $this->request, $this->userSession, $this->totp, $this->defaults); + } + + public function testNothing() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->totp->expects($this->once()) + ->method('hasSecret') + ->with($user) + ->will($this->returnValue(true)); + + $expected = [ + 'enabled' => true, + ]; + + $this->assertEquals($expected, $this->controller->state()); + } + + public function testEnable() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->exactly(2)) + ->method('getUser') + ->will($this->returnValue($user)); + $user->expects($this->once()) + ->method('getCloudId') + ->will($this->returnValue('user@instance.com')); + $this->totp->expects($this->once()) + ->method('createSecret') + ->with($user) + ->will($this->returnValue('newsecret')); + + $qrCode = new QrCode(); + $issuer = rawurlencode($this->defaults->getName()); + $qr = $qrCode->setText("otpauth://totp/$issuer%3Auser%40instance.com?secret=newsecret&issuer=$issuer") + ->setSize(150) + ->getDataUri(); + + $expected = [ + 'enabled' => true, + 'secret' => 'newsecret', + 'qr' => $qr, + ]; + + $this->assertEquals($expected, $this->controller->enable(true)); + } + + public function testEnableDisable() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->totp->expects($this->once()) + ->method('deleteSecret'); + + $expected = [ + 'enabled' => false, + ]; + + $this->assertEquals($expected, $this->controller->enable(false)); + } + +} |