|
|
|
@ -1,9 +1,13 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
|
/** |
|
|
|
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors |
|
|
|
* SPDX-FileCopyrightText: 2014 ownCloud, Inc. |
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
|
|
*/ |
|
|
|
|
|
|
|
namespace OCA\Settings\Tests\Middleware; |
|
|
|
|
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException; |
|
|
|
@ -11,7 +15,11 @@ use OC\AppFramework\Utility\ControllerMethodReflector; |
|
|
|
use OCA\Settings\Middleware\SubadminMiddleware; |
|
|
|
use OCP\AppFramework\Controller; |
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
|
|
|
use OCP\Group\ISubAdmin; |
|
|
|
use OCP\IL10N; |
|
|
|
use OCP\IUser; |
|
|
|
use OCP\IUserSession; |
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
|
|
|
|
|
|
/** |
|
|
|
* Verifies whether an user has at least subadmin rights. |
|
|
|
@ -20,31 +28,40 @@ use OCP\IL10N; |
|
|
|
* @package Tests\Settings\Middleware |
|
|
|
*/ |
|
|
|
class SubadminMiddlewareTest extends \Test\TestCase { |
|
|
|
/** @var SubadminMiddleware */ |
|
|
|
private $subadminMiddlewareAsSubAdmin; |
|
|
|
/** @var SubadminMiddleware */ |
|
|
|
private $subadminMiddleware; |
|
|
|
/** @var ControllerMethodReflector */ |
|
|
|
private $reflector; |
|
|
|
/** @var Controller */ |
|
|
|
private $controller; |
|
|
|
/** @var IL10N */ |
|
|
|
private $l10n; |
|
|
|
private SubadminMiddleware $subadminMiddleware; |
|
|
|
|
|
|
|
private IUserSession&MockObject $userSession; |
|
|
|
private ISubAdmin&MockObject $subAdminManager; |
|
|
|
private ControllerMethodReflector&MockObject $reflector; |
|
|
|
private Controller&MockObject $controller; |
|
|
|
private IL10N&MockObject $l10n; |
|
|
|
|
|
|
|
protected function setUp(): void { |
|
|
|
parent::setUp(); |
|
|
|
$this->reflector = $this->getMockBuilder(ControllerMethodReflector::class) |
|
|
|
->disableOriginalConstructor()->getMock(); |
|
|
|
$this->userSession = $this->createMock(IUserSession::class); |
|
|
|
$this->subAdminManager = $this->createMock(ISubAdmin::class); |
|
|
|
$this->l10n = $this->createMock(IL10N::class); |
|
|
|
|
|
|
|
$this->subadminMiddleware = new SubadminMiddleware( |
|
|
|
$this->reflector, |
|
|
|
$this->userSession, |
|
|
|
$this->subAdminManager, |
|
|
|
$this->l10n, |
|
|
|
); |
|
|
|
|
|
|
|
$this->controller = $this->getMockBuilder(Controller::class) |
|
|
|
->disableOriginalConstructor()->getMock(); |
|
|
|
$this->l10n = $this->createMock(IL10N::class); |
|
|
|
|
|
|
|
$this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true, $this->l10n); |
|
|
|
$this->subadminMiddleware = new SubadminMiddleware($this->reflector, false, $this->l10n); |
|
|
|
$this->userSession |
|
|
|
->expects(self::any()) |
|
|
|
->method('getUser') |
|
|
|
->willReturn($this->createMock(IUser::class)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function testBeforeControllerAsUserWithExemption(): void { |
|
|
|
public function testBeforeControllerAsUserWithoutAnnotation(): void { |
|
|
|
$this->expectException(NotAdminException::class); |
|
|
|
|
|
|
|
$this->reflector |
|
|
|
@ -54,20 +71,31 @@ class SubadminMiddlewareTest extends \Test\TestCase { |
|
|
|
['NoSubAdminRequired'], |
|
|
|
['AuthorizedAdminSetting'], |
|
|
|
)->willReturn(false); |
|
|
|
|
|
|
|
$this->subAdminManager |
|
|
|
->expects(self::once()) |
|
|
|
->method('isSubAdmin') |
|
|
|
->willReturn(false); |
|
|
|
|
|
|
|
$this->subadminMiddleware->beforeController($this->controller, 'foo'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function testBeforeControllerAsUserWithoutExemption(): void { |
|
|
|
public function testBeforeControllerWithAnnotation(): void { |
|
|
|
$this->reflector |
|
|
|
->expects($this->once()) |
|
|
|
->method('hasAnnotation') |
|
|
|
->with('NoSubAdminRequired') |
|
|
|
->willReturn(true); |
|
|
|
|
|
|
|
$this->subAdminManager |
|
|
|
->expects(self::never()) |
|
|
|
->method('isSubAdmin'); |
|
|
|
|
|
|
|
$this->subadminMiddleware->beforeController($this->controller, 'foo'); |
|
|
|
} |
|
|
|
|
|
|
|
public function testBeforeControllerAsSubAdminWithoutExemption(): void { |
|
|
|
public function testBeforeControllerAsSubAdminWithoutAnnotation(): void { |
|
|
|
$this->reflector |
|
|
|
->expects($this->exactly(2)) |
|
|
|
->method('hasAnnotation') |
|
|
|
@ -75,16 +103,13 @@ class SubadminMiddlewareTest extends \Test\TestCase { |
|
|
|
['NoSubAdminRequired'], |
|
|
|
['AuthorizedAdminSetting'], |
|
|
|
)->willReturn(false); |
|
|
|
$this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo'); |
|
|
|
} |
|
|
|
|
|
|
|
public function testBeforeControllerAsSubAdminWithExemption(): void { |
|
|
|
$this->reflector |
|
|
|
->expects($this->once()) |
|
|
|
->method('hasAnnotation') |
|
|
|
->with('NoSubAdminRequired') |
|
|
|
$this->subAdminManager |
|
|
|
->expects(self::once()) |
|
|
|
->method('isSubAdmin') |
|
|
|
->willReturn(true); |
|
|
|
$this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo'); |
|
|
|
|
|
|
|
$this->subadminMiddleware->beforeController($this->controller, 'foo'); |
|
|
|
} |
|
|
|
|
|
|
|
public function testAfterNotAdminException(): void { |
|
|
|
|