Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/twofactor_gateway.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2018-04-11 17:28:30 +0300
committerChristoph Wurst <christoph@winzerhof-wurst.at>2018-04-11 17:28:30 +0300
commit5c461269f99359f24c4d31c817486af33dda3466 (patch)
treeb43da01646f44cffa5337aec175d14e8d8661e39 /tests
parent7b8c60dc19259eb7396579c89e25d0b6df808876 (diff)
Add setup class to verify the phone number
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Service/SetupServiceTest.php170
-rw-r--r--tests/bootstrap.php7
2 files changed, 176 insertions, 1 deletions
diff --git a/tests/Unit/Service/SetupServiceTest.php b/tests/Unit/Service/SetupServiceTest.php
new file mode 100644
index 0000000..eb59322
--- /dev/null
+++ b/tests/Unit/Service/SetupServiceTest.php
@@ -0,0 +1,170 @@
+<?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\TwoFactorSms\Tests\Unit\Service;
+
+use ChristophWurst\Nextcloud\Testing\TestCase;
+use OC\Accounts\AccountManager;
+use OCA\TwoFactorSms\Exception\PhoneNumberMissingException;
+use OCA\TwoFactorSms\Exception\VerificationException;
+use OCA\TwoFactorSms\Exception\VerificationTransmissionException;
+use OCA\TwoFactorSms\Service\ISmsService;
+use OCA\TwoFactorSms\Service\SetupService;
+use OCP\IConfig;
+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 AccountManager|PHPUnit_Framework_MockObject_MockObject */
+ private $accountManager;
+
+ /** @var ISmsService|PHPUnit_Framework_MockObject_MockObject */
+ private $smsService;
+
+ /** @var ISecureRandom|PHPUnit_Framework_MockObject_MockObject */
+ private $random;
+
+ /** @var SetupService */
+ private $setupService;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->config = $this->createMock(IConfig::class);
+ $this->accountManager = $this->createMock(AccountManager::class);
+ $this->smsService = $this->createMock(ISmsService::class);
+ $this->random = $this->createMock(ISecureRandom::class);
+
+ $this->setupService = new SetupService($this->config, $this->accountManager, $this->smsService, $this->random);
+ }
+
+ public function testStartSetupNoPhoneNumberSet() {
+ $user = $this->createMock(IUser::class);
+ $this->accountManager->expects($this->once())
+ ->method('getUser')
+ ->with($user)
+ ->willReturn([]);
+ $this->expectException(PhoneNumberMissingException::class);
+
+ $this->setupService->startSetup($user);
+ }
+
+ public function testStartSetupEmptyPhoneNumberSet() {
+ $user = $this->createMock(IUser::class);
+ $this->accountManager->expects($this->once())
+ ->method('getUser')
+ ->with($user)
+ ->willReturn([
+ AccountManager::PROPERTY_PHONE => '',
+ ]);
+ $this->expectException(PhoneNumberMissingException::class);
+
+ $this->setupService->startSetup($user);
+ }
+
+ public function testStartSetupTransmissionError() {
+ $user = $this->createMock(IUser::class);
+ $this->accountManager->expects($this->once())
+ ->method('getUser')
+ ->with($user)
+ ->willReturn([
+ AccountManager::PROPERTY_PHONE => '0123456789',
+ ]);
+ $this->smsService->expects($this->once())
+ ->method('send')
+ ->willThrowException(new VerificationTransmissionException());
+ $this->expectException(VerificationTransmissionException::class);
+
+ $this->setupService->startSetup($user);
+ }
+
+ public function testStartSetup() {
+ $user = $this->createMock(IUser::class);
+ $this->accountManager->expects($this->once())
+ ->method('getUser')
+ ->with($user)
+ ->willReturn([
+ AccountManager::PROPERTY_PHONE => '0123456789',
+ ]);
+ $this->smsService->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_sms', 'phone', '0123456789');
+ $this->config->expects($this->at(1))
+ ->method('setUserValue')
+ ->with('user123', 'twofactor_sms', 'verification_code', '963852');
+ $this->config->expects($this->at(2))
+ ->method('setUserValue')
+ ->with('user123', 'twofactor_sms', 'verified', false);
+
+ $this->setupService->startSetup($user);
+ }
+
+ public function testFinishSetupNoVerificationNumberSet() {
+ $user = $this->createMock(IUser::class);
+ $user->method('getUID')->willReturn('user123');
+ $this->config->expects($this->once())
+ ->method('getUserValue')
+ ->with('user123', 'twofactor_sms', 'verification_code', null)
+ ->willReturn(null);
+ $this->expectException(\Exception::class);
+
+ $this->setupService->finishSetup($user, '123456');
+ }
+
+ public function testFinishSetupWithWrongVerificationNumber() {
+ $user = $this->createMock(IUser::class);
+ $user->method('getUID')->willReturn('user123');
+ $this->config->expects($this->once())
+ ->method('getUserValue')
+ ->with('user123', 'twofactor_sms', 'verification_code', null)
+ ->willReturn('111111');
+ $this->expectException(VerificationException::class);
+
+ $this->setupService->finishSetup($user, '123456');
+ }
+
+ public function testFinishSetup() {
+ $user = $this->createMock(IUser::class);
+ $user->method('getUID')->willReturn('user123');
+ $this->config->expects($this->once())
+ ->method('getUserValue')
+ ->with('user123', 'twofactor_sms', 'verification_code', null)
+ ->willReturn('123456');
+ $this->config->expects($this->once())
+ ->method('setUserValue')
+ ->with('user123', 'twofactor_sms', 'verified', true);
+
+ $this->setupService->finishSetup($user, '123456');
+ }
+
+}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index bf70ebd..bbfc567 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -19,4 +19,9 @@
*
*/
-require_once __DIR__ . '/../../../tests/bootstrap.php';
+define('PHPUNIT_RUN', 1);
+
+require_once __DIR__.'/../../../lib/base.php';
+require_once __DIR__.'/../vendor/autoload.php';
+
+\OC_App::loadApp('twofactor_sms');