summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2019-05-22 10:48:51 +0200
committerChristoph Wurst <christoph@winzerhof-wurst.at>2019-05-23 20:31:40 +0200
commit22ae6828237a516b1cd36a3dad623b8046dfd76a (patch)
treec00361d0747e295f98e1e089114b8bf48bcc0e57 /tests
parent09974ae92d6f3bc20143dab43baef9fc75139585 (diff)
downloadnextcloud-server-22ae6828237a516b1cd36a3dad623b8046dfd76a.tar.gz
nextcloud-server-22ae6828237a516b1cd36a3dad623b8046dfd76a.zip
Make it possible to show admin settings for sub admins
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r--tests/Settings/Controller/AdminSettingsControllerTest.php49
-rw-r--r--tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php63
-rw-r--r--tests/lib/Settings/ManagerTest.php32
3 files changed, 120 insertions, 24 deletions
diff --git a/tests/Settings/Controller/AdminSettingsControllerTest.php b/tests/Settings/Controller/AdminSettingsControllerTest.php
index 6c2b44f37e0..c86615ed590 100644
--- a/tests/Settings/Controller/AdminSettingsControllerTest.php
+++ b/tests/Settings/Controller/AdminSettingsControllerTest.php
@@ -25,9 +25,14 @@ namespace Tests\Settings\Controller;
use OC\Settings\Personal\ServerDevNotice;
use OC\Settings\Controller\AdminSettingsController;
use OCP\AppFramework\Http\TemplateResponse;
+use OCP\Group\ISubAdmin;
+use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IRequest;
+use OCP\IUser;
+use OCP\IUserSession;
use OCP\Settings\IManager;
+use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
/**
@@ -38,29 +43,42 @@ use Test\TestCase;
* @package Tests\Settings\Controller
*/
class AdminSettingsControllerTest extends TestCase {
+
/** @var AdminSettingsController */
private $adminSettingsController;
- /** @var IRequest */
+ /** @var IRequest|MockObject */
private $request;
- /** @var INavigationManager */
+ /** @var INavigationManager|MockObject */
private $navigationManager;
- /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
+ /** @var IManager|MockObject */
private $settingsManager;
+ /** @var IUserSession|MockObject */
+ private $userSession;
+ /** @var IGroupManager|MockObject */
+ private $groupManager;
+ /** @var ISubAdmin|MockObject */
+ private $subAdmin;
/** @var string */
private $adminUid = 'lololo';
public function setUp() {
parent::setUp();
- $this->request = $this->getMockBuilder(IRequest::class)->getMock();
- $this->navigationManager = $this->getMockBuilder(INavigationManager::class)->getMock();
- $this->settingsManager = $this->getMockBuilder(IManager::class)->getMock();
+ $this->request = $this->createMock(IRequest::class);
+ $this->navigationManager = $this->createMock(INavigationManager::class);
+ $this->settingsManager = $this->createMock(IManager::class);
+ $this->userSession = $this->createMock(IUserSession::class);
+ $this->groupManager = $this->createMock(IGroupManager::class);
+ $this->subAdmin = $this->createMock(ISubAdmin::class);
$this->adminSettingsController = new AdminSettingsController(
'settings',
$this->request,
$this->navigationManager,
- $this->settingsManager
+ $this->settingsManager,
+ $this->userSession,
+ $this->groupManager,
+ $this->subAdmin
);
$user = \OC::$server->getUserManager()->createUser($this->adminUid, 'olo');
@@ -75,6 +93,19 @@ class AdminSettingsControllerTest extends TestCase {
}
public function testIndex() {
+ $user = $this->createMock(IUser::class);
+ $this->userSession
+ ->method('getUser')
+ ->willReturn($user);
+ $user->method('getUID')->willReturn('user123');
+ $this->groupManager
+ ->method('isAdmin')
+ ->with('user123')
+ ->willReturn(true);
+ $this->subAdmin
+ ->method('isSubAdmin')
+ ->with($user)
+ ->willReturn(false);
$this->settingsManager
->expects($this->once())
->method('getAdminSections')
@@ -89,7 +120,9 @@ class AdminSettingsControllerTest extends TestCase {
->with('test')
->willReturn([5 => new ServerDevNotice()]);
+ $idx = $this->adminSettingsController->index('test');
+
$expected = new TemplateResponse('settings', 'settings/frame', ['forms' => ['personal' => [], 'admin' => []], 'content' => '']);
- $this->assertEquals($expected, $this->adminSettingsController->index('test'));
+ $this->assertEquals($expected, $idx);
}
}
diff --git a/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php
index 13c5379b142..ab243616be0 100644
--- a/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php
+++ b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php
@@ -96,12 +96,12 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$this->csrfTokenManager = $this->createMock(CsrfTokenManager::class);
$this->cspNonceManager = $this->createMock(ContentSecurityPolicyNonceManager::class);
$this->l10n = $this->createMock(IL10N::class);
- $this->middleware = $this->getMiddleware(true, true);
+ $this->middleware = $this->getMiddleware(true, true, false);
$this->secException = new SecurityException('hey', false);
$this->secAjaxException = new SecurityException('hey', true);
}
- private function getMiddleware(bool $isLoggedIn, bool $isAdminUser, bool $isAppEnabledForUser = true): SecurityMiddleware {
+ private function getMiddleware(bool $isLoggedIn, bool $isAdminUser, bool $isSubAdmin, bool $isAppEnabledForUser = true): SecurityMiddleware {
$this->appManager = $this->createMock(IAppManager::class);
$this->appManager->expects($this->any())
@@ -117,6 +117,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
'files',
$isLoggedIn,
$isAdminUser,
+ $isSubAdmin,
$this->contentSecurityPolicyManager,
$this->csrfTokenManager,
$this->cspNonceManager,
@@ -153,7 +154,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$isLoggedIn = true;
}
- $sec = $this->getMiddleware($isLoggedIn, $isAdminUser);
+ $sec = $this->getMiddleware($isLoggedIn, $isAdminUser, false);
try {
$this->reader->reflect(__CLASS__, $method);
@@ -216,11 +217,6 @@ class SecurityMiddlewareTest extends \Test\TestCase {
);
$this->ajaxExceptionStatus(
__FUNCTION__,
- 'isSubAdminUser',
- 0
- );
- $this->ajaxExceptionStatus(
- __FUNCTION__,
'passesCSRFCheck',
0
);
@@ -236,7 +232,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
->method('passesCSRFCheck')
->will($this->returnValue(false));
- $sec = $this->getMiddleware(false, false);
+ $sec = $this->getMiddleware(false, false, false);
$this->reader->reflect(__CLASS__, __FUNCTION__);
$sec->beforeController($this->controller, __FUNCTION__);
@@ -257,7 +253,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$isAdminUser = false;
}
- $sec = $this->getMiddleware($isLoggedIn, $isAdminUser);
+ $sec = $this->getMiddleware($isLoggedIn, $isAdminUser, false);
if($shouldFail) {
$this->expectException(SecurityException::class);
@@ -452,6 +448,41 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$this->securityCheck(__FUNCTION__, 'isAdminUser');
}
+ /**
+ * @NoCSRFRequired
+ * @SubAdminRequired
+ */
+ public function testIsNotSubAdminCheck(){
+ $this->reader->reflect(__CLASS__,__FUNCTION__);
+ $sec = $this->getMiddleware(true, false, false);
+
+ $this->expectException(SecurityException::class);
+ $sec->beforeController($this, __METHOD__);
+ }
+
+ /**
+ * @NoCSRFRequired
+ * @SubAdminRequired
+ */
+ public function testIsSubAdminCheck(){
+ $this->reader->reflect(__CLASS__,__FUNCTION__);
+ $sec = $this->getMiddleware(true, false, true);
+
+ $sec->beforeController($this, __METHOD__);
+ $this->addToAssertionCount(1);
+ }
+
+ /**
+ * @NoCSRFRequired
+ * @SubAdminRequired
+ */
+ public function testIsSubAdminAndAdminCheck(){
+ $this->reader->reflect(__CLASS__,__FUNCTION__);
+ $sec = $this->getMiddleware(true, true, true);
+
+ $sec->beforeController($this, __METHOD__);
+ $this->addToAssertionCount(1);
+ }
/**
* @NoCSRFRequired
@@ -479,7 +510,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
- $this->middleware = $this->getMiddleware(false, false);
+ $this->middleware = $this->getMiddleware(false, false, false);
$this->urlGenerator
->expects($this->once())
->method('linkToRoute')
@@ -514,7 +545,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$this->createMock(IConfig::class)
);
- $this->middleware = $this->getMiddleware(false, false);
+ $this->middleware = $this->getMiddleware(false, false, false);
$response = $this->middleware->afterException(
$this->controller,
'test',
@@ -559,7 +590,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
$this->createMock(ISecureRandom::class),
$this->createMock(IConfig::class)
);
- $this->middleware = $this->getMiddleware(false, false);
+ $this->middleware = $this->getMiddleware(false, false, false);
$this->logger
->expects($this->once())
->method('logException');
@@ -684,7 +715,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
* @NoCSRFRequired
*/
public function testRestrictedAppLoggedInPublicPage() {
- $middleware = $this->getMiddleware(true, false);
+ $middleware = $this->getMiddleware(true, false, false);
$this->reader->reflect(__CLASS__,__FUNCTION__);
$this->appManager->method('getAppPath')
@@ -705,7 +736,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
* @NoCSRFRequired
*/
public function testRestrictedAppNotLoggedInPublicPage() {
- $middleware = $this->getMiddleware(false, false);
+ $middleware = $this->getMiddleware(false, false, false);
$this->reader->reflect(__CLASS__,__FUNCTION__);
$this->appManager->method('getAppPath')
@@ -725,7 +756,7 @@ class SecurityMiddlewareTest extends \Test\TestCase {
* @NoCSRFRequired
*/
public function testRestrictedAppLoggedIn() {
- $middleware = $this->getMiddleware(true, false, false);
+ $middleware = $this->getMiddleware(true, false, false, false);
$this->reader->reflect(__CLASS__,__FUNCTION__);
$this->appManager->method('getAppPath')
diff --git a/tests/lib/Settings/ManagerTest.php b/tests/lib/Settings/ManagerTest.php
index 7372cae811b..4128e33aef1 100644
--- a/tests/lib/Settings/ManagerTest.php
+++ b/tests/lib/Settings/ManagerTest.php
@@ -23,6 +23,7 @@
namespace Tests\Settings;
+use function get_class;
use OC\Settings\Admin\Sharing;
use OC\Settings\Manager;
use OC\Settings\Mapper;
@@ -34,6 +35,8 @@ use OCP\ILogger;
use OCP\IServerContainer;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
+use OCP\Settings\ISettings;
+use OCP\Settings\ISubAdminSettings;
use Test\TestCase;
class ManagerTest extends TestCase {
@@ -207,6 +210,35 @@ class ManagerTest extends TestCase {
], $settings);
}
+ public function testGetAdminSettingsAsSubAdmin() {
+ $section = $this->createMock(Sharing::class);
+ $this->container->expects($this->once())
+ ->method('query')
+ ->with(Sharing::class)
+ ->willReturn($section);
+
+ $settings = $this->manager->getAdminSettings('sharing', true);
+
+ $this->assertEquals([], $settings);
+ }
+
+ public function testGetSubAdminSettingsAsSubAdmin() {
+ $section = $this->createMock(ISubAdminSettings::class);
+ $section->expects($this->once())
+ ->method('getPriority')
+ ->willReturn(13);
+ $this->container->expects($this->once())
+ ->method('query')
+ ->with(Sharing::class)
+ ->willReturn($section);
+
+ $settings = $this->manager->getAdminSettings('sharing', true);
+
+ $this->assertEquals([
+ 13 => [$section]
+ ], $settings);
+ }
+
public function testGetPersonalSettings() {
$section = $this->createMock(Security::class);
$section->expects($this->once())