From 8afbd803289a821a45bc63c2d2305d93dc757c48 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 25 May 2016 12:02:05 +0200 Subject: Move parallel merged auth tests to PSR-4 --- .../TwoFactorChallengeControllerTest.php | 220 +++++++++++++++++++++ tests/Core/Middleware/TwoFactorMiddlewareTest.php | 183 +++++++++++++++++ .../Controller/AuthSettingsControllerTest.php | 159 +++++++++++++++ .../TwoFactorChallengeControllerTest.php | 219 -------------------- tests/core/middleware/TwoFactorMiddlewareTest.php | 182 ----------------- .../controller/AuthSettingsControllerTest.php | 159 --------------- 6 files changed, 562 insertions(+), 560 deletions(-) create mode 100644 tests/Core/Controller/TwoFactorChallengeControllerTest.php create mode 100644 tests/Core/Middleware/TwoFactorMiddlewareTest.php create mode 100644 tests/Settings/Controller/AuthSettingsControllerTest.php delete mode 100644 tests/core/controller/TwoFactorChallengeControllerTest.php delete mode 100644 tests/core/middleware/TwoFactorMiddlewareTest.php delete mode 100644 tests/settings/controller/AuthSettingsControllerTest.php diff --git a/tests/Core/Controller/TwoFactorChallengeControllerTest.php b/tests/Core/Controller/TwoFactorChallengeControllerTest.php new file mode 100644 index 00000000000..aa1c7d39cfa --- /dev/null +++ b/tests/Core/Controller/TwoFactorChallengeControllerTest.php @@ -0,0 +1,220 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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 Test\Core\Controller; + +use OC\Core\Controller\TwoFactorChallengeController; +use Test\TestCase; + +class TwoFactorChallengeControllerTest extends TestCase { + + private $request; + private $twoFactorManager; + private $userSession; + private $session; + private $urlGenerator; + + /** TwoFactorChallengeController */ + private $controller; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->getMock('\OCP\IRequest'); + $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->session = $this->getMock('\OCP\ISession'); + $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); + + $this->controller = new TwoFactorChallengeController( + 'core', $this->request, $this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator + ); + } + + public function testSelectChallenge() { + $user = $this->getMock('\OCP\IUser'); + $providers = [ + 'prov1', + 'prov2', + ]; + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProviders') + ->with($user) + ->will($this->returnValue($providers)); + + $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [ + 'providers' => $providers, + ], 'guest'); + + $this->assertEquals($expected, $this->controller->selectChallenge()); + } + + public function testShowChallenge() { + $user = $this->getMock('\OCP\IUser'); + $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') + ->disableOriginalConstructor() + ->getMock(); + $tmpl = $this->getMockBuilder('\OCP\Template') + ->disableOriginalConstructor() + ->getMock(); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue($provider)); + + $this->session->expects($this->once()) + ->method('exists') + ->with('two_factor_auth_error') + ->will($this->returnValue(true)); + $this->session->expects($this->once()) + ->method('remove') + ->with('two_factor_auth_error'); + $provider->expects($this->once()) + ->method('getTemplate') + ->with($user) + ->will($this->returnValue($tmpl)); + $tmpl->expects($this->once()) + ->method('fetchPage') + ->will($this->returnValue('')); + + $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [ + 'error' => true, + 'provider' => $provider, + 'template' => '', + ], 'guest'); + + $this->assertEquals($expected, $this->controller->showChallenge('myprovider')); + } + + public function testShowInvalidChallenge() { + $user = $this->getMock('\OCP\IUser'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue(null)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.selectChallenge') + ->will($this->returnValue('select/challenge/url')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url'); + + $this->assertEquals($expected, $this->controller->showChallenge('myprovider')); + } + + public function testSolveChallenge() { + $user = $this->getMock('\OCP\IUser'); + $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') + ->disableOriginalConstructor() + ->getMock(); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue($provider)); + + $this->twoFactorManager->expects($this->once()) + ->method('verifyChallenge') + ->with('myprovider', $user, 'token') + ->will($this->returnValue(true)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue('files/index/url')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url'); + $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); + } + + public function testSolveChallengeInvalidProvider() { + $user = $this->getMock('\OCP\IUser'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue(null)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.selectChallenge') + ->will($this->returnValue('select/challenge/url')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url'); + + $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); + } + + public function testSolveInvalidChallenge() { + $user = $this->getMock('\OCP\IUser'); + $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') + ->disableOriginalConstructor() + ->getMock(); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue($provider)); + + $this->twoFactorManager->expects($this->once()) + ->method('verifyChallenge') + ->with('myprovider', $user, 'token') + ->will($this->returnValue(false)); + $this->session->expects($this->once()) + ->method('set') + ->with('two_factor_auth_error', true); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.showChallenge', [ + 'challengeProviderId' => 'myprovider', + ]) + ->will($this->returnValue('files/index/url')); + $provider->expects($this->once()) + ->method('getId') + ->will($this->returnValue('myprovider')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url'); + $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); + } + +} diff --git a/tests/Core/Middleware/TwoFactorMiddlewareTest.php b/tests/Core/Middleware/TwoFactorMiddlewareTest.php new file mode 100644 index 00000000000..248793bf987 --- /dev/null +++ b/tests/Core/Middleware/TwoFactorMiddlewareTest.php @@ -0,0 +1,183 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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 Test\Core\Middleware; + +use OC\Core\Middleware\TwoFactorMiddleware; +use Test\TestCase; + +class TwoFactorMiddlewareTest extends TestCase { + + private $twoFactorManager; + private $userSession; + private $session; + private $urlGenerator; + private $reflector; + + /** @var TwoFactorMiddleware */ + private $middleware; + + protected function setUp() { + parent::setUp(); + + $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession = $this->getMockBuilder('\OC\User\Session') + ->disableOriginalConstructor() + ->getMock(); + $this->session = $this->getMock('\OCP\ISession'); + $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); + $this->reflector = $this->getMock('\OCP\AppFramework\Utility\IControllerMethodReflector'); + + $this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector); + } + + public function testBeforeControllerNotLoggedIn() { + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(false)); + + $this->userSession->expects($this->never()) + ->method('getUser'); + + $this->middleware->beforeController(null, 'index'); + } + + public function testBeforeControllerPublicPage() { + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(true)); + $this->userSession->expects($this->never()) + ->method('isLoggedIn'); + + $this->middleware->beforeController(null, 'create'); + } + + public function testBeforeControllerNoTwoFactorCheckNeeded() { + $user = $this->getMock('\OCP\IUser'); + + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(true)); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(false)); + + $this->middleware->beforeController(null, 'index'); + } + + /** + * @expectedException \OC\Authentication\Exceptions\TwoFactorAuthRequiredException + */ + public function testBeforeControllerTwoFactorAuthRequired() { + $user = $this->getMock('\OCP\IUser'); + + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(true)); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(true)); + $this->twoFactorManager->expects($this->once()) + ->method('needsSecondFactor') + ->will($this->returnValue(true)); + + $this->middleware->beforeController(null, 'index'); + } + + /** + * @expectedException \OC\Authentication\Exceptions\UserAlreadyLoggedInException + */ + public function testBeforeControllerUserAlreadyLoggedIn() { + $user = $this->getMock('\OCP\IUser'); + + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(true)); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(true)); + $this->twoFactorManager->expects($this->once()) + ->method('needsSecondFactor') + ->will($this->returnValue(false)); + + $twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController') + ->disableOriginalConstructor() + ->getMock(); + $this->middleware->beforeController($twoFactorChallengeController, 'index'); + } + + public function testAfterExceptionTwoFactorAuthRequired() { + $ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException(); + + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.selectChallenge') + ->will($this->returnValue('redirect/url')); + $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); + + $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex)); + } + + public function testAfterException() { + $ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException(); + + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue('redirect/url')); + $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); + + $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex)); + } + +} diff --git a/tests/Settings/Controller/AuthSettingsControllerTest.php b/tests/Settings/Controller/AuthSettingsControllerTest.php new file mode 100644 index 00000000000..ee67b221022 --- /dev/null +++ b/tests/Settings/Controller/AuthSettingsControllerTest.php @@ -0,0 +1,159 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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 Test\Settings\Controller; + +use OC\AppFramework\Http; +use OC\Authentication\Exceptions\InvalidTokenException; +use OC\Authentication\Token\IToken; +use OC\Settings\Controller\AuthSettingsController; +use OCP\AppFramework\Http\JSONResponse; +use OCP\Session\Exceptions\SessionNotAvailableException; +use Test\TestCase; + +class AuthSettingsControllerTest extends TestCase { + + /** @var AuthSettingsController */ + private $controller; + private $request; + private $tokenProvider; + private $userManager; + private $session; + private $secureRandom; + private $uid; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->getMock('\OCP\IRequest'); + $this->tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $this->session = $this->getMock('\OCP\ISession'); + $this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom'); + $this->uid = 'jane'; + $this->user = $this->getMock('\OCP\IUser'); + + $this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->userManager, $this->session, $this->secureRandom, $this->uid); + } + + public function testIndex() { + $result = [ + 'token1', + 'token2', + ]; + $this->userManager->expects($this->once()) + ->method('get') + ->with($this->uid) + ->will($this->returnValue($this->user)); + $this->tokenProvider->expects($this->once()) + ->method('getTokenByUser') + ->with($this->user) + ->will($this->returnValue($result)); + + $this->assertEquals($result, $this->controller->index()); + } + + public function testCreate() { + $name = 'Nexus 4'; + $sessionToken = $this->getMock('\OC\Authentication\Token\IToken'); + $deviceToken = $this->getMock('\OC\Authentication\Token\IToken'); + $password = '123456'; + + $this->session->expects($this->once()) + ->method('getId') + ->will($this->returnValue('sessionid')); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sessionid') + ->will($this->returnValue($sessionToken)); + $this->tokenProvider->expects($this->once()) + ->method('getPassword') + ->with($sessionToken, 'sessionid') + ->will($this->returnValue($password)); + $sessionToken->expects($this->once()) + ->method('getLoginName') + ->will($this->returnValue('User13')); + + $this->secureRandom->expects($this->exactly(4)) + ->method('generate') + ->with(5, implode('', range('A', 'Z'))) + ->will($this->returnValue('XXXXX')); + $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX'; + + $this->tokenProvider->expects($this->once()) + ->method('generateToken') + ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN) + ->will($this->returnValue($deviceToken)); + + $expected = [ + 'token' => $newToken, + 'deviceToken' => $deviceToken, + ]; + $this->assertEquals($expected, $this->controller->create($name)); + } + + public function testCreateSessionNotAvailable() { + $name = 'personal phone'; + + $this->session->expects($this->once()) + ->method('getId') + ->will($this->throwException(new SessionNotAvailableException())); + + $expected = new JSONResponse(); + $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); + + $this->assertEquals($expected, $this->controller->create($name)); + } + + public function testCreateInvalidToken() { + $name = 'Company IPhone'; + + $this->session->expects($this->once()) + ->method('getId') + ->will($this->returnValue('sessionid')); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sessionid') + ->will($this->throwException(new InvalidTokenException())); + + $expected = new JSONResponse(); + $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); + + $this->assertEquals($expected, $this->controller->create($name)); + } + + public function testDestroy() { + $id = 123; + $user = $this->getMock('\OCP\IUser'); + + $this->userManager->expects($this->once()) + ->method('get') + ->with($this->uid) + ->will($this->returnValue($user)); + $this->tokenProvider->expects($this->once()) + ->method('invalidateTokenById') + ->with($user, $id); + + $this->assertEquals([], $this->controller->destroy($id)); + } + +} diff --git a/tests/core/controller/TwoFactorChallengeControllerTest.php b/tests/core/controller/TwoFactorChallengeControllerTest.php deleted file mode 100644 index c65625ec329..00000000000 --- a/tests/core/controller/TwoFactorChallengeControllerTest.php +++ /dev/null @@ -1,219 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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 OC\Core\Controller; - -use Test\TestCase; - -class TwoFactorChallengeControllerTest extends TestCase { - - private $request; - private $twoFactorManager; - private $userSession; - private $session; - private $urlGenerator; - - /** TwoFactorChallengeController */ - private $controller; - - protected function setUp() { - parent::setUp(); - - $this->request = $this->getMock('\OCP\IRequest'); - $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession = $this->getMock('\OCP\IUserSession'); - $this->session = $this->getMock('\OCP\ISession'); - $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); - - $this->controller = new TwoFactorChallengeController( - 'core', $this->request, $this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator - ); - } - - public function testSelectChallenge() { - $user = $this->getMock('\OCP\IUser'); - $providers = [ - 'prov1', - 'prov2', - ]; - - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('getProviders') - ->with($user) - ->will($this->returnValue($providers)); - - $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [ - 'providers' => $providers, - ], 'guest'); - - $this->assertEquals($expected, $this->controller->selectChallenge()); - } - - public function testShowChallenge() { - $user = $this->getMock('\OCP\IUser'); - $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') - ->disableOriginalConstructor() - ->getMock(); - $tmpl = $this->getMockBuilder('\OCP\Template') - ->disableOriginalConstructor() - ->getMock(); - - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('getProvider') - ->with($user, 'myprovider') - ->will($this->returnValue($provider)); - - $this->session->expects($this->once()) - ->method('exists') - ->with('two_factor_auth_error') - ->will($this->returnValue(true)); - $this->session->expects($this->once()) - ->method('remove') - ->with('two_factor_auth_error'); - $provider->expects($this->once()) - ->method('getTemplate') - ->with($user) - ->will($this->returnValue($tmpl)); - $tmpl->expects($this->once()) - ->method('fetchPage') - ->will($this->returnValue('')); - - $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [ - 'error' => true, - 'provider' => $provider, - 'template' => '', - ], 'guest'); - - $this->assertEquals($expected, $this->controller->showChallenge('myprovider')); - } - - public function testShowInvalidChallenge() { - $user = $this->getMock('\OCP\IUser'); - - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('getProvider') - ->with($user, 'myprovider') - ->will($this->returnValue(null)); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('core.TwoFactorChallenge.selectChallenge') - ->will($this->returnValue('select/challenge/url')); - - $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url'); - - $this->assertEquals($expected, $this->controller->showChallenge('myprovider')); - } - - public function testSolveChallenge() { - $user = $this->getMock('\OCP\IUser'); - $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') - ->disableOriginalConstructor() - ->getMock(); - - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('getProvider') - ->with($user, 'myprovider') - ->will($this->returnValue($provider)); - - $this->twoFactorManager->expects($this->once()) - ->method('verifyChallenge') - ->with('myprovider', $user, 'token') - ->will($this->returnValue(true)); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index') - ->will($this->returnValue('files/index/url')); - - $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url'); - $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); - } - - public function testSolveChallengeInvalidProvider() { - $user = $this->getMock('\OCP\IUser'); - - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('getProvider') - ->with($user, 'myprovider') - ->will($this->returnValue(null)); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('core.TwoFactorChallenge.selectChallenge') - ->will($this->returnValue('select/challenge/url')); - - $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url'); - - $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); - } - - public function testSolveInvalidChallenge() { - $user = $this->getMock('\OCP\IUser'); - $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') - ->disableOriginalConstructor() - ->getMock(); - - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('getProvider') - ->with($user, 'myprovider') - ->will($this->returnValue($provider)); - - $this->twoFactorManager->expects($this->once()) - ->method('verifyChallenge') - ->with('myprovider', $user, 'token') - ->will($this->returnValue(false)); - $this->session->expects($this->once()) - ->method('set') - ->with('two_factor_auth_error', true); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('core.TwoFactorChallenge.showChallenge', [ - 'challengeProviderId' => 'myprovider', - ]) - ->will($this->returnValue('files/index/url')); - $provider->expects($this->once()) - ->method('getId') - ->will($this->returnValue('myprovider')); - - $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url'); - $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); - } - -} diff --git a/tests/core/middleware/TwoFactorMiddlewareTest.php b/tests/core/middleware/TwoFactorMiddlewareTest.php deleted file mode 100644 index 12136db7fdf..00000000000 --- a/tests/core/middleware/TwoFactorMiddlewareTest.php +++ /dev/null @@ -1,182 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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 OC\Core\Middleware; - -use Test\TestCase; - -class TwoFactorMiddlewareTest extends TestCase { - - private $twoFactorManager; - private $userSession; - private $session; - private $urlGenerator; - private $reflector; - - /** @var TwoFactorMiddleware */ - private $middleware; - - protected function setUp() { - parent::setUp(); - - $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession = $this->getMockBuilder('\OC\User\Session') - ->disableOriginalConstructor() - ->getMock(); - $this->session = $this->getMock('\OCP\ISession'); - $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); - $this->reflector = $this->getMock('\OCP\AppFramework\Utility\IControllerMethodReflector'); - - $this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector); - } - - public function testBeforeControllerNotLoggedIn() { - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->will($this->returnValue(false)); - $this->userSession->expects($this->once()) - ->method('isLoggedIn') - ->will($this->returnValue(false)); - - $this->userSession->expects($this->never()) - ->method('getUser'); - - $this->middleware->beforeController(null, 'index'); - } - - public function testBeforeControllerPublicPage() { - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->will($this->returnValue(true)); - $this->userSession->expects($this->never()) - ->method('isLoggedIn'); - - $this->middleware->beforeController(null, 'create'); - } - - public function testBeforeControllerNoTwoFactorCheckNeeded() { - $user = $this->getMock('\OCP\IUser'); - - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->will($this->returnValue(false)); - $this->userSession->expects($this->once()) - ->method('isLoggedIn') - ->will($this->returnValue(true)); - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('isTwoFactorAuthenticated') - ->with($user) - ->will($this->returnValue(false)); - - $this->middleware->beforeController(null, 'index'); - } - - /** - * @expectedException \OC\Authentication\Exceptions\TwoFactorAuthRequiredException - */ - public function testBeforeControllerTwoFactorAuthRequired() { - $user = $this->getMock('\OCP\IUser'); - - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->will($this->returnValue(false)); - $this->userSession->expects($this->once()) - ->method('isLoggedIn') - ->will($this->returnValue(true)); - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('isTwoFactorAuthenticated') - ->with($user) - ->will($this->returnValue(true)); - $this->twoFactorManager->expects($this->once()) - ->method('needsSecondFactor') - ->will($this->returnValue(true)); - - $this->middleware->beforeController(null, 'index'); - } - - /** - * @expectedException \OC\Authentication\Exceptions\UserAlreadyLoggedInException - */ - public function testBeforeControllerUserAlreadyLoggedIn() { - $user = $this->getMock('\OCP\IUser'); - - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->will($this->returnValue(false)); - $this->userSession->expects($this->once()) - ->method('isLoggedIn') - ->will($this->returnValue(true)); - $this->userSession->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($user)); - $this->twoFactorManager->expects($this->once()) - ->method('isTwoFactorAuthenticated') - ->with($user) - ->will($this->returnValue(true)); - $this->twoFactorManager->expects($this->once()) - ->method('needsSecondFactor') - ->will($this->returnValue(false)); - - $twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController') - ->disableOriginalConstructor() - ->getMock(); - $this->middleware->beforeController($twoFactorChallengeController, 'index'); - } - - public function testAfterExceptionTwoFactorAuthRequired() { - $ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException(); - - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('core.TwoFactorChallenge.selectChallenge') - ->will($this->returnValue('redirect/url')); - $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); - - $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex)); - } - - public function testAfterException() { - $ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException(); - - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index') - ->will($this->returnValue('redirect/url')); - $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); - - $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex)); - } - -} diff --git a/tests/settings/controller/AuthSettingsControllerTest.php b/tests/settings/controller/AuthSettingsControllerTest.php deleted file mode 100644 index ee67b221022..00000000000 --- a/tests/settings/controller/AuthSettingsControllerTest.php +++ /dev/null @@ -1,159 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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 Test\Settings\Controller; - -use OC\AppFramework\Http; -use OC\Authentication\Exceptions\InvalidTokenException; -use OC\Authentication\Token\IToken; -use OC\Settings\Controller\AuthSettingsController; -use OCP\AppFramework\Http\JSONResponse; -use OCP\Session\Exceptions\SessionNotAvailableException; -use Test\TestCase; - -class AuthSettingsControllerTest extends TestCase { - - /** @var AuthSettingsController */ - private $controller; - private $request; - private $tokenProvider; - private $userManager; - private $session; - private $secureRandom; - private $uid; - - protected function setUp() { - parent::setUp(); - - $this->request = $this->getMock('\OCP\IRequest'); - $this->tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider'); - $this->userManager = $this->getMock('\OCP\IUserManager'); - $this->session = $this->getMock('\OCP\ISession'); - $this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom'); - $this->uid = 'jane'; - $this->user = $this->getMock('\OCP\IUser'); - - $this->controller = new AuthSettingsController('core', $this->request, $this->tokenProvider, $this->userManager, $this->session, $this->secureRandom, $this->uid); - } - - public function testIndex() { - $result = [ - 'token1', - 'token2', - ]; - $this->userManager->expects($this->once()) - ->method('get') - ->with($this->uid) - ->will($this->returnValue($this->user)); - $this->tokenProvider->expects($this->once()) - ->method('getTokenByUser') - ->with($this->user) - ->will($this->returnValue($result)); - - $this->assertEquals($result, $this->controller->index()); - } - - public function testCreate() { - $name = 'Nexus 4'; - $sessionToken = $this->getMock('\OC\Authentication\Token\IToken'); - $deviceToken = $this->getMock('\OC\Authentication\Token\IToken'); - $password = '123456'; - - $this->session->expects($this->once()) - ->method('getId') - ->will($this->returnValue('sessionid')); - $this->tokenProvider->expects($this->once()) - ->method('getToken') - ->with('sessionid') - ->will($this->returnValue($sessionToken)); - $this->tokenProvider->expects($this->once()) - ->method('getPassword') - ->with($sessionToken, 'sessionid') - ->will($this->returnValue($password)); - $sessionToken->expects($this->once()) - ->method('getLoginName') - ->will($this->returnValue('User13')); - - $this->secureRandom->expects($this->exactly(4)) - ->method('generate') - ->with(5, implode('', range('A', 'Z'))) - ->will($this->returnValue('XXXXX')); - $newToken = 'XXXXX-XXXXX-XXXXX-XXXXX'; - - $this->tokenProvider->expects($this->once()) - ->method('generateToken') - ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN) - ->will($this->returnValue($deviceToken)); - - $expected = [ - 'token' => $newToken, - 'deviceToken' => $deviceToken, - ]; - $this->assertEquals($expected, $this->controller->create($name)); - } - - public function testCreateSessionNotAvailable() { - $name = 'personal phone'; - - $this->session->expects($this->once()) - ->method('getId') - ->will($this->throwException(new SessionNotAvailableException())); - - $expected = new JSONResponse(); - $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); - - $this->assertEquals($expected, $this->controller->create($name)); - } - - public function testCreateInvalidToken() { - $name = 'Company IPhone'; - - $this->session->expects($this->once()) - ->method('getId') - ->will($this->returnValue('sessionid')); - $this->tokenProvider->expects($this->once()) - ->method('getToken') - ->with('sessionid') - ->will($this->throwException(new InvalidTokenException())); - - $expected = new JSONResponse(); - $expected->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); - - $this->assertEquals($expected, $this->controller->create($name)); - } - - public function testDestroy() { - $id = 123; - $user = $this->getMock('\OCP\IUser'); - - $this->userManager->expects($this->once()) - ->method('get') - ->with($this->uid) - ->will($this->returnValue($user)); - $this->tokenProvider->expects($this->once()) - ->method('invalidateTokenById') - ->with($user, $id); - - $this->assertEquals([], $this->controller->destroy($id)); - } - -} -- cgit v1.2.3