add some unit tests
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Родитель
3321e0dc84
Коммит
f7fb7700a3
|
@ -42,6 +42,13 @@ class U2FManager {
|
|||
/** @var IManager */
|
||||
private $activityManager;
|
||||
|
||||
/**
|
||||
* @param RegistrationMapper $mapper
|
||||
* @param ISession $session
|
||||
* @param ILogger $logger
|
||||
* @param IRequest $request
|
||||
* @param IManager $activityManager
|
||||
*/
|
||||
public function __construct(RegistrationMapper $mapper, ISession $session, ILogger $logger, IRequest $request, IManager $activityManager) {
|
||||
$this->mapper = $mapper;
|
||||
$this->session = $session;
|
||||
|
@ -127,8 +134,8 @@ class U2FManager {
|
|||
$activity->setApp('twofactor_u2f')
|
||||
->setType('twofactor')
|
||||
->setAuthor($user->getUID())
|
||||
->setAffectedUser($user->getUID());
|
||||
$activity->setSubject($event);
|
||||
->setAffectedUser($user->getUID())
|
||||
->setSubject($event);
|
||||
$this->activityManager->publish($activity);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,4 +13,4 @@ require_once __DIR__.'/../../../lib/base.php';
|
|||
require_once __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests');
|
||||
OC_App::loadApp('twofactor_totp');
|
||||
OC_App::loadApp('twofactor_u2f');
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Nextcloud - U2F 2FA
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later. See the COPYING file.
|
||||
*
|
||||
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
* @copyright Christoph Wurst 2016
|
||||
*/
|
||||
|
||||
namespace OCA\TwoFactorU2F\Test\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 Test\TestCase;
|
||||
|
||||
class SettingsControllerTest extends TestCase {
|
||||
|
||||
/** @var IRequest|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $request;
|
||||
|
||||
/** @var U2FManager|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $u2fManager;
|
||||
|
||||
/** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $userSession;
|
||||
|
||||
/** @var SettingsController */
|
||||
private $controller;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->request = $this->createMock(IRequest::class);
|
||||
$this->u2fManager = $this->getMockBuilder(U2FManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$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);
|
||||
$this->userSession->expects($this->once())
|
||||
->method('getUser')
|
||||
->will($this->returnValue($user));
|
||||
$this->u2fManager->expects($this->once())
|
||||
->method('isEnabled')
|
||||
->with($this->equalTo($user))
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$expected = [
|
||||
'enabled' => true,
|
||||
];
|
||||
$this->assertSame($expected, $this->controller->state());
|
||||
}
|
||||
|
||||
public function testDisable() {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$this->userSession->expects($this->once())
|
||||
->method('getUser')
|
||||
->will($this->returnValue($user));
|
||||
$this->u2fManager->expects($this->once())
|
||||
->method('disableU2F')
|
||||
->with($this->equalTo($user));
|
||||
|
||||
$this->controller->disable();
|
||||
}
|
||||
|
||||
public function testStartRegister() {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$this->userSession->expects($this->once())
|
||||
->method('getUser')
|
||||
->will($this->returnValue($user));
|
||||
|
||||
$this->u2fManager->expects($this->once())
|
||||
->method('startRegistration')
|
||||
->with($this->equalTo($user))
|
||||
->will($this->returnValue([]));
|
||||
|
||||
$this->assertEquals([], $this->controller->startRegister());
|
||||
}
|
||||
|
||||
public function testFinishRegister() {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$this->userSession->expects($this->once())
|
||||
->method('getUser')
|
||||
->will($this->returnValue($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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Nextcloud - U2F 2FA
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later. See the COPYING file.
|
||||
*
|
||||
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
* @copyright Christoph Wurst 2016
|
||||
*/
|
||||
|
||||
namespace OCA\TwoFactorU2F\Test\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 Test\TestCase;
|
||||
|
||||
class U2FProviderTest extends TestCase {
|
||||
|
||||
/** @var IL10N|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $l10n;
|
||||
|
||||
/** @var U2FManager|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $manager;
|
||||
|
||||
/** @var U2FProvider */
|
||||
private $provider;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
$this->manager = $this->getMockBuilder(U2FManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$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')
|
||||
->will($this->returnValue('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')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->assertFalse($this->provider->verifyChallenge($user, $val));
|
||||
}
|
||||
|
||||
public function testIsTwoFactorAuthEnabledForUser() {
|
||||
$user = $this->createMock(IUser::class);
|
||||
|
||||
$this->manager->expects($this->once())
|
||||
->method('isEnabled')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->assertFalse($this->provider->isTwoFactorAuthEnabledForUser($user));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,170 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Nextcloud - U2F 2FA
|
||||
*
|
||||
* This file is licensed under the Affero General Public License version 3 or
|
||||
* later. See the COPYING file.
|
||||
*
|
||||
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
||||
* @copyright Christoph Wurst 2016
|
||||
*/
|
||||
|
||||
namespace OCA\TwoFactorU2F\Test\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 Test\TestCase;
|
||||
use u2flib_server\U2F;
|
||||
|
||||
class U2FManagerTest extends TestCase {
|
||||
|
||||
/** @var RegistrationMapper|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $mapper;
|
||||
|
||||
/** @var ISession|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $session;
|
||||
|
||||
/** @var ILogger|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $logger;
|
||||
|
||||
/** @var IRequest|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $request;
|
||||
|
||||
/** @var IManager|PHPUnit_Framework_MockObject_MockObject */
|
||||
private $activityManager;
|
||||
|
||||
/** @var U2FManager */
|
||||
private $manager;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->mapper = $this->getMockBuilder(RegistrationMapper::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return U2F
|
||||
*/
|
||||
private function getU2f() {
|
||||
$this->request->expects($this->once())
|
||||
->method('getServerProtocol')
|
||||
->willReturn('https');
|
||||
$this->request->expects($this->once())
|
||||
->method('getServerHost')
|
||||
->willReturn('cloud.example.com');
|
||||
return new U2F('https://cloud.example.com');
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 testIsEnabled() {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$this->mockRegistrations($user, 2);
|
||||
|
||||
$this->assertTrue($this->manager->isEnabled($user));
|
||||
}
|
||||
|
||||
public function testIsEnabledDisabled() {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$this->mockRegistrations($user, 0);
|
||||
|
||||
$this->assertFalse($this->manager->isEnabled($user));
|
||||
}
|
||||
|
||||
public function testDisableU2F() {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$this->mockRegistrations($user, 1);
|
||||
$event = $this->createMock(IEvent::class);
|
||||
|
||||
$this->mapper->expects($this->once())
|
||||
->method('delete');
|
||||
$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('twofactor'))
|
||||
->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->disableU2F($user);
|
||||
}
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
}
|
Загрузка…
Ссылка в новой задаче