2014-12-04 14:15:55 +01:00
|
|
|
<?php
|
2025-02-13 12:48:12 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2014-12-04 14:15:55 +01:00
|
|
|
/**
|
2024-06-03 10:23:34 +02:00
|
|
|
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2014 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2014-12-04 14:15:55 +01:00
|
|
|
*/
|
2025-02-13 12:48:12 +01:00
|
|
|
|
2019-09-17 16:33:27 +02:00
|
|
|
namespace OCA\Settings\Tests\Middleware;
|
2014-12-04 14:15:55 +01:00
|
|
|
|
2016-04-22 15:28:48 +02:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
|
2014-12-04 14:15:55 +01:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2019-09-17 16:33:27 +02:00
|
|
|
use OCA\Settings\Middleware\SubadminMiddleware;
|
2014-12-04 14:15:55 +01:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2025-02-13 12:48:12 +01:00
|
|
|
use OCP\Group\ISubAdmin;
|
2018-02-26 15:32:17 +01:00
|
|
|
use OCP\IL10N;
|
2025-02-13 12:48:12 +01:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2014-12-04 14:15:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies whether an user has at least subadmin rights.
|
2020-06-23 20:18:38 +02:00
|
|
|
* To bypass use the `@NoSubAdminRequired` annotation
|
2014-12-04 14:15:55 +01:00
|
|
|
*
|
2016-05-19 11:17:01 +02:00
|
|
|
* @package Tests\Settings\Middleware
|
2014-12-04 14:15:55 +01:00
|
|
|
*/
|
|
|
|
class SubadminMiddlewareTest extends \Test\TestCase {
|
2025-02-13 12:48:12 +01:00
|
|
|
private SubadminMiddleware $subadminMiddleware;
|
|
|
|
|
|
|
|
private IUserSession&MockObject $userSession;
|
|
|
|
private ISubAdmin&MockObject $subAdminManager;
|
|
|
|
private ControllerMethodReflector&MockObject $reflector;
|
|
|
|
private Controller&MockObject $controller;
|
|
|
|
private IL10N&MockObject $l10n;
|
2014-12-04 14:15:55 +01:00
|
|
|
|
2019-11-21 16:40:38 +01:00
|
|
|
protected function setUp(): void {
|
2016-10-06 14:05:52 +02:00
|
|
|
parent::setUp();
|
2017-10-24 15:26:53 +02:00
|
|
|
$this->reflector = $this->getMockBuilder(ControllerMethodReflector::class)
|
2014-12-04 14:15:55 +01:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2025-02-13 12:48:12 +01:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$this->controller = $this->getMockBuilder(Controller::class)
|
2014-12-04 14:15:55 +01:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
2025-02-13 12:48:12 +01:00
|
|
|
$this->userSession
|
|
|
|
->expects(self::any())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($this->createMock(IUser::class));
|
2014-12-04 14:15:55 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 20:18:38 +02:00
|
|
|
|
2025-02-13 12:48:12 +01:00
|
|
|
public function testBeforeControllerAsUserWithoutAnnotation(): void {
|
2024-10-10 12:40:31 +02:00
|
|
|
$this->expectException(NotAdminException::class);
|
2019-11-27 15:27:18 +01:00
|
|
|
|
2014-12-04 14:15:55 +01:00
|
|
|
$this->reflector
|
2022-06-07 10:05:18 +02:00
|
|
|
->expects($this->exactly(2))
|
2014-12-04 14:15:55 +01:00
|
|
|
->method('hasAnnotation')
|
2022-06-07 10:05:18 +02:00
|
|
|
->withConsecutive(
|
|
|
|
['NoSubAdminRequired'],
|
|
|
|
['AuthorizedAdminSetting'],
|
|
|
|
)->willReturn(false);
|
2025-02-13 12:48:12 +01:00
|
|
|
|
|
|
|
$this->subAdminManager
|
|
|
|
->expects(self::once())
|
|
|
|
->method('isSubAdmin')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2014-12-04 14:15:55 +01:00
|
|
|
$this->subadminMiddleware->beforeController($this->controller, 'foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-13 12:48:12 +01:00
|
|
|
public function testBeforeControllerWithAnnotation(): void {
|
2014-12-04 14:15:55 +01:00
|
|
|
$this->reflector
|
|
|
|
->expects($this->once())
|
|
|
|
->method('hasAnnotation')
|
2020-06-23 20:18:38 +02:00
|
|
|
->with('NoSubAdminRequired')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn(true);
|
2025-02-13 12:48:12 +01:00
|
|
|
|
|
|
|
$this->subAdminManager
|
|
|
|
->expects(self::never())
|
|
|
|
->method('isSubAdmin');
|
|
|
|
|
2014-12-04 14:15:55 +01:00
|
|
|
$this->subadminMiddleware->beforeController($this->controller, 'foo');
|
|
|
|
}
|
|
|
|
|
2025-02-13 12:48:12 +01:00
|
|
|
public function testBeforeControllerAsSubAdminWithoutAnnotation(): void {
|
2014-12-04 14:15:55 +01:00
|
|
|
$this->reflector
|
2022-06-07 10:05:18 +02:00
|
|
|
->expects($this->exactly(2))
|
2021-07-22 11:41:29 +02:00
|
|
|
->method('hasAnnotation')
|
2022-06-07 10:05:18 +02:00
|
|
|
->withConsecutive(
|
|
|
|
['NoSubAdminRequired'],
|
|
|
|
['AuthorizedAdminSetting'],
|
|
|
|
)->willReturn(false);
|
2014-12-04 14:15:55 +01:00
|
|
|
|
2025-02-13 12:48:12 +01:00
|
|
|
$this->subAdminManager
|
|
|
|
->expects(self::once())
|
|
|
|
->method('isSubAdmin')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn(true);
|
2025-02-13 12:48:12 +01:00
|
|
|
|
|
|
|
$this->subadminMiddleware->beforeController($this->controller, 'foo');
|
2014-12-04 14:15:55 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 09:41:56 +01:00
|
|
|
public function testAfterNotAdminException(): void {
|
2020-03-26 09:30:18 +01:00
|
|
|
$expectedResponse = new TemplateResponse('core', '403', [], 'guest');
|
2016-02-22 09:41:56 +01:00
|
|
|
$expectedResponse->setStatus(403);
|
2018-02-26 15:32:17 +01:00
|
|
|
$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException('')));
|
2016-02-22 09:41:56 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 20:18:38 +02:00
|
|
|
|
2016-02-22 09:41:56 +01:00
|
|
|
public function testAfterRegularException(): void {
|
2019-11-27 15:27:18 +01:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
|
2020-03-26 09:30:18 +01:00
|
|
|
$expectedResponse = new TemplateResponse('core', '403', [], 'guest');
|
2015-01-29 12:04:54 +01:00
|
|
|
$expectedResponse->setStatus(403);
|
2016-02-22 09:41:56 +01:00
|
|
|
$this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
|
2014-12-04 14:15:55 +01:00
|
|
|
}
|
2015-01-29 12:04:54 +01:00
|
|
|
}
|