aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/tests/Middleware/SubadminMiddlewareTest.php
blob: 2992810af6c19366c05e1796e5ca45cbf37e19a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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;
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.
 * To bypass use the `@NoSubAdminRequired` annotation
 *
 * @package Tests\Settings\Middleware
 */
class SubadminMiddlewareTest extends \Test\TestCase {
	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->userSession
			->expects(self::any())
			->method('getUser')
			->willReturn($this->createMock(IUser::class));
	}


	public function testBeforeControllerAsUserWithoutAnnotation(): void {
		$this->expectException(NotAdminException::class);

		$this->reflector
			->expects($this->exactly(2))
			->method('hasAnnotation')
			->withConsecutive(
				['NoSubAdminRequired'],
				['AuthorizedAdminSetting'],
			)->willReturn(false);

		$this->subAdminManager
			->expects(self::once())
			->method('isSubAdmin')
			->willReturn(false);

		$this->subadminMiddleware->beforeController($this->controller, 'foo');
	}


	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 testBeforeControllerAsSubAdminWithoutAnnotation(): void {
		$this->reflector
			->expects($this->exactly(2))
			->method('hasAnnotation')
			->withConsecutive(
				['NoSubAdminRequired'],
				['AuthorizedAdminSetting'],
			)->willReturn(false);

		$this->subAdminManager
			->expects(self::once())
			->method('isSubAdmin')
			->willReturn(true);

		$this->subadminMiddleware->beforeController($this->controller, 'foo');
	}

	public function testAfterNotAdminException(): void {
		$expectedResponse = new TemplateResponse('core', '403', [], 'guest');
		$expectedResponse->setStatus(403);
		$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException('')));
	}


	public function testAfterRegularException(): void {
		$this->expectException(\Exception::class);

		$expectedResponse = new TemplateResponse('core', '403', [], 'guest');
		$expectedResponse->setStatus(403);
		$this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception());
	}
}