diff options
Diffstat (limited to 'tests/Core/Middleware/TwoFactorMiddlewareTest.php')
-rw-r--r-- | tests/Core/Middleware/TwoFactorMiddlewareTest.php | 117 |
1 files changed, 40 insertions, 77 deletions
diff --git a/tests/Core/Middleware/TwoFactorMiddlewareTest.php b/tests/Core/Middleware/TwoFactorMiddlewareTest.php index 5ef2b75fde6..10afdd7c5e1 100644 --- a/tests/Core/Middleware/TwoFactorMiddlewareTest.php +++ b/tests/Core/Middleware/TwoFactorMiddlewareTest.php @@ -1,23 +1,9 @@ <?php /** - * @author Christoph Wurst <christoph@owncloud.com> - * - * @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 <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Core\Middleware; @@ -31,21 +17,21 @@ use OC\Core\Controller\TwoFactorChallengeController; use OC\Core\Middleware\TwoFactorMiddleware; use OC\User\Session; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Utility\IControllerMethodReflector; use OCP\Authentication\TwoFactorAuth\ALoginSetupController; use OCP\Authentication\TwoFactorAuth\IProvider; use OCP\IConfig; use OCP\IRequest; +use OCP\IRequestId; use OCP\ISession; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; -use OCP\Security\ISecureRandom; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class TwoFactorMiddlewareTest extends TestCase { - /** @var Manager|MockObject */ private $twoFactorManager; @@ -88,7 +74,7 @@ class TwoFactorMiddlewareTest extends TestCase { 'REQUEST_URI' => 'test/url' ] ], - $this->createMock(ISecureRandom::class), + $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); @@ -96,11 +82,7 @@ class TwoFactorMiddlewareTest extends TestCase { $this->controller = $this->createMock(Controller::class); } - public function testBeforeControllerNotLoggedIn() { - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->willReturn(false); + public function testBeforeControllerNotLoggedIn(): void { $this->userSession->expects($this->once()) ->method('isLoggedIn') ->willReturn(false); @@ -111,24 +93,9 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($this->controller, 'index'); } - public function testBeforeControllerPublicPage() { - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->willReturn(true); - $this->userSession->expects($this->never()) - ->method('isLoggedIn'); - - $this->middleware->beforeController($this->controller, 'create'); - } - - public function testBeforeSetupController() { + public function testBeforeSetupController(): void { $user = $this->createMock(IUser::class); $controller = $this->createMock(ALoginSetupController::class); - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->willReturn(false); $this->userSession->expects($this->any()) ->method('getUser') ->willReturn($user); @@ -141,13 +108,9 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($controller, 'create'); } - public function testBeforeControllerNoTwoFactorCheckNeeded() { + public function testBeforeControllerNoTwoFactorCheckNeeded(): void { $user = $this->createMock(IUser::class); - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->willReturn(false); $this->userSession->expects($this->once()) ->method('isLoggedIn') ->willReturn(true); @@ -162,16 +125,12 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($this->controller, 'index'); } - - public function testBeforeControllerTwoFactorAuthRequired() { - $this->expectException(\OC\Authentication\Exceptions\TwoFactorAuthRequiredException::class); + + public function testBeforeControllerTwoFactorAuthRequired(): void { + $this->expectException(TwoFactorAuthRequiredException::class); $user = $this->createMock(IUser::class); - $this->reflector->expects($this->once()) - ->method('hasAnnotation') - ->with('PublicPage') - ->willReturn(false); $this->userSession->expects($this->once()) ->method('isLoggedIn') ->willReturn(true); @@ -190,9 +149,9 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($this->controller, 'index'); } - - public function testBeforeControllerUserAlreadyLoggedIn() { - $this->expectException(\OC\Authentication\Exceptions\UserAlreadyLoggedInException::class); + + public function testBeforeControllerUserAlreadyLoggedIn(): void { + $this->expectException(UserAlreadyLoggedInException::class); $user = $this->createMock(IUser::class); @@ -214,37 +173,37 @@ class TwoFactorMiddlewareTest extends TestCase { ->with($user) ->willReturn(false); - $twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController') + $twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class) ->disableOriginalConstructor() ->getMock(); $this->middleware->beforeController($twoFactorChallengeController, 'index'); } - public function testAfterExceptionTwoFactorAuthRequired() { - $ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException(); + public function testAfterExceptionTwoFactorAuthRequired(): void { + $ex = new TwoFactorAuthRequiredException(); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('core.TwoFactorChallenge.selectChallenge') ->willReturn('test/url'); - $expected = new \OCP\AppFramework\Http\RedirectResponse('test/url'); + $expected = new RedirectResponse('test/url'); $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex)); } - public function testAfterException() { - $ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException(); + public function testAfterException(): void { + $ex = new UserAlreadyLoggedInException(); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('files.view.index') ->willReturn('redirect/url'); - $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); + $expected = new RedirectResponse('redirect/url'); $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex)); } - public function testRequires2FASetupDoneAnnotated() { + public function testRequires2FASetupDoneAnnotated(): void { $user = $this->createMock(IUser::class); $this->reflector @@ -275,23 +234,27 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($twoFactorChallengeController, 'index'); } - public function dataRequires2FASetupDone() { - $provider = $this->createMock(IProvider::class); - $provider->method('getId') - ->willReturn('2FAftw'); - + public static function dataRequires2FASetupDone(): array { return [ - [[], false, false], - [[], true, true], - [[$provider], false, true], - [[$provider], true, true], + [false, false, false], + [false, true, true], + [true, false, true], + [true, true, true], ]; } - /** - * @dataProvider dataRequires2FASetupDone - */ - public function testRequires2FASetupDone(array $providers, bool $missingProviders, bool $expectEception) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataRequires2FASetupDone')] + public function testRequires2FASetupDone(bool $hasProvider, bool $missingProviders, bool $expectEception): void { + if ($hasProvider) { + $provider = $this->createMock(IProvider::class); + $provider->method('getId') + ->willReturn('2FAftw'); + $providers = [$provider]; + } else { + $providers = []; + } + + $user = $this->createMock(IUser::class); $this->reflector |