diff options
Diffstat (limited to 'apps/provisioning_api/tests/Controller/AppConfigControllerTest.php')
-rw-r--r-- | apps/provisioning_api/tests/Controller/AppConfigControllerTest.php | 312 |
1 files changed, 162 insertions, 150 deletions
diff --git a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php index 7f24ef5bcb3..1b09838cbc3 100644 --- a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php +++ b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php @@ -1,36 +1,30 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> - * - * @author Joas Schilling <coding@schilljs.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\Provisioning_API\Tests\Controller; - +use OC\AppConfig; use OCA\Provisioning_API\Controller\AppConfigController; +use OCP\App\IAppManager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; -use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IL10N; use OCP\IRequest; +use OCP\IUser; +use OCP\IUserSession; +use OCP\Server; +use OCP\Settings\IManager; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; +use function json_decode; +use function json_encode; /** * Class AppConfigControllerTest @@ -38,23 +32,27 @@ use Test\TestCase; * @package OCA\Provisioning_API\Tests */ class AppConfigControllerTest extends TestCase { - - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ - private $config; - /** @var IAppConfig|\PHPUnit_Framework_MockObject_MockObject */ - private $appConfig; - - protected function setUp() { + private IAppConfig&MockObject $appConfig; + private IUserSession&MockObject $userSession; + private IL10N&MockObject $l10n; + private IManager&MockObject $settingManager; + private IGroupManager&MockObject $groupManager; + private IAppManager $appManager; + + protected function setUp(): void { parent::setUp(); - $this->config = $this->createMock(IConfig::class); - $this->appConfig = $this->createMock(IAppConfig::class); - + $this->appConfig = $this->createMock(AppConfig::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->l10n = $this->createMock(IL10N::class); + $this->settingManager = $this->createMock(IManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->appManager = Server::get(IAppManager::class); } /** * @param string[] $methods - * @return AppConfigController|\PHPUnit_Framework_MockObject_MockObject + * @return AppConfigController|MockObject */ protected function getInstance(array $methods = []) { $request = $this->createMock(IRequest::class); @@ -63,23 +61,31 @@ class AppConfigControllerTest extends TestCase { return new AppConfigController( 'provisioning_api', $request, - $this->config, - $this->appConfig + $this->appConfig, + $this->userSession, + $this->l10n, + $this->groupManager, + $this->settingManager, + $this->appManager, ); } else { return $this->getMockBuilder(AppConfigController::class) ->setConstructorArgs([ 'provisioning_api', $request, - $this->config, $this->appConfig, + $this->userSession, + $this->l10n, + $this->groupManager, + $this->settingManager, + $this->appManager, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } } - public function testGetApps() { + public function testGetApps(): void { $this->appConfig->expects($this->once()) ->method('getApps') ->willReturn(['apps']); @@ -90,22 +96,15 @@ class AppConfigControllerTest extends TestCase { $this->assertEquals(['data' => ['apps']], $result->getData()); } - public function dataGetKeys() { + public static function dataGetKeys(): array { return [ ['app1 ', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN], ['app2', ['keys'], null, Http::STATUS_OK], ]; } - /** - * @dataProvider dataGetKeys - * @param string $app - * @param array|null $keys - * @param \Exception|null $throws - * @param int $status - */ - public function testGetKeys($app, $keys, $throws, $status) { - + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetKeys')] + public function testGetKeys(string $app, ?array $keys, ?\Throwable $throws, int $status): void { $api = $this->getInstance(['verifyAppId']); if ($throws instanceof \Exception) { $api->expects($this->once()) @@ -113,15 +112,15 @@ class AppConfigControllerTest extends TestCase { ->with($app) ->willThrowException($throws); - $this->config->expects($this->never()) - ->method('getAppKeys'); + $this->appConfig->expects($this->never()) + ->method('getKeys'); } else { $api->expects($this->once()) ->method('verifyAppId') ->with($app); - $this->config->expects($this->once()) - ->method('getAppKeys') + $this->appConfig->expects($this->once()) + ->method('getKeys') ->with($app) ->willReturn($keys); } @@ -136,40 +135,28 @@ class AppConfigControllerTest extends TestCase { } } - public function dataGetValue() { + public static function dataGetValue(): array { return [ - ['app1 ', null, null, null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN], + ['app1', 'key', 'default', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN], ['app2', 'key', 'default', 'return', null, Http::STATUS_OK], ]; } - /** - * @dataProvider dataGetValue - * @param string $app - * @param string|null $key - * @param string|null $default - * @param string|null $return - * @param \Exception|null $throws - * @param int $status - */ - public function testGetValue($app, $key, $default, $return, $throws, $status) { - + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetValue')] + public function testGetValue(string $app, string $key, string $default, ?string $return, ?\Throwable $throws, int $status): void { $api = $this->getInstance(['verifyAppId']); if ($throws instanceof \Exception) { $api->expects($this->once()) ->method('verifyAppId') ->with($app) ->willThrowException($throws); - - $this->config->expects($this->never()) - ->method('getAppValue'); } else { $api->expects($this->once()) ->method('verifyAppId') ->with($app); - $this->config->expects($this->once()) - ->method('getAppValue') + $this->appConfig->expects($this->once()) + ->method('getValueMixed') ->with($app, $key, $default) ->willReturn($return); } @@ -184,25 +171,35 @@ class AppConfigControllerTest extends TestCase { } } - public function dataSetValue() { + public static function dataSetValue(): array { return [ - ['app1 ', null, null, new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], - ['app2', 'key', null, null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN], + ['app1', 'key', 'default', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], + ['app2', 'key', 'default', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN], ['app2', 'key', 'default', null, null, Http::STATUS_OK], + ['app2', 'key', '1', null, null, Http::STATUS_OK, IAppConfig::VALUE_BOOL], + ['app2', 'key', '42', null, null, Http::STATUS_OK, IAppConfig::VALUE_INT], + ['app2', 'key', '4.2', null, null, Http::STATUS_OK, IAppConfig::VALUE_FLOAT], + ['app2', 'key', '42', null, null, Http::STATUS_OK, IAppConfig::VALUE_STRING], + ['app2', 'key', 'secret', null, null, Http::STATUS_OK, IAppConfig::VALUE_STRING | IAppConfig::VALUE_SENSITIVE], + ['app2', 'key', json_encode([4, 2]), null, null, Http::STATUS_OK, IAppConfig::VALUE_ARRAY], + ['app2', 'key', json_encode([4, 2]), null, null, Http::STATUS_OK, new AppConfigUnknownKeyException()], ]; } - /** - * @dataProvider dataSetValue - * @param string $app - * @param string|null $key - * @param string|null $value - * @param \Exception|null $appThrows - * @param \Exception|null $keyThrows - * @param int $status - */ - public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status) { - + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetValue')] + public function testSetValue(string $app, string $key, string $value, ?\Throwable $appThrows, ?\Throwable $keyThrows, int $status, int|\Throwable $type = IAppConfig::VALUE_MIXED): void { + $adminUser = $this->createMock(IUser::class); + $adminUser->expects($this->once()) + ->method('getUid') + ->willReturn('admin'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($adminUser); + $this->groupManager->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->willReturn(true); $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']); if ($appThrows instanceof \Exception) { $api->expects($this->once()) @@ -212,9 +209,9 @@ class AppConfigControllerTest extends TestCase { $api->expects($this->never()) ->method('verifyConfigKey'); - $this->config->expects($this->never()) - ->method('setAppValue'); - } else if ($keyThrows instanceof \Exception) { + $this->appConfig->expects($this->never()) + ->method('setValueMixed'); + } elseif ($keyThrows instanceof \Exception) { $api->expects($this->once()) ->method('verifyAppId') ->with($app); @@ -223,8 +220,8 @@ class AppConfigControllerTest extends TestCase { ->with($app, $key) ->willThrowException($keyThrows); - $this->config->expects($this->never()) - ->method('setAppValue'); + $this->appConfig->expects($this->never()) + ->method('setValueMixed'); } else { $api->expects($this->once()) ->method('verifyAppId') @@ -233,9 +230,38 @@ class AppConfigControllerTest extends TestCase { ->method('verifyConfigKey') ->with($app, $key); - $this->config->expects($this->once()) - ->method('setAppValue') - ->with($app, $key, $value); + if ($type instanceof \Throwable) { + $this->appConfig->expects($this->once()) + ->method('getDetails') + ->with($app, $key) + ->willThrowException($type); + } else { + $this->appConfig->expects($this->once()) + ->method('getDetails') + ->with($app, $key) + ->willReturn([ + 'app' => $app, + 'key' => $key, + 'value' => '', // 🤷 + 'type' => $type, + 'lazy' => false, + 'typeString' => (string)$type, // this is not accurate, but acceptable + 'sensitive' => ($type & IAppConfig::VALUE_SENSITIVE) !== 0, + ]); + } + + $configValueSetter = match ($type) { + IAppConfig::VALUE_BOOL => 'setValueBool', + IAppConfig::VALUE_FLOAT => 'setValueFloat', + IAppConfig::VALUE_INT => 'setValueInt', + IAppConfig::VALUE_STRING => 'setValueString', + IAppConfig::VALUE_ARRAY => 'setValueArray', + default => 'setValueMixed', + }; + + $this->appConfig->expects($this->once()) + ->method($configValueSetter) + ->with($app, $key, $configValueSetter === 'setValueArray' ? json_decode($value, true) : $value); } $result = $api->setValue($app, $key, $value); @@ -243,31 +269,23 @@ class AppConfigControllerTest extends TestCase { $this->assertSame($status, $result->getStatus()); if ($appThrows instanceof \Exception) { $this->assertEquals(['data' => ['message' => $appThrows->getMessage()]], $result->getData()); - } else if ($keyThrows instanceof \Exception) { + } elseif ($keyThrows instanceof \Exception) { $this->assertEquals(['data' => ['message' => $keyThrows->getMessage()]], $result->getData()); } else { $this->assertEquals([], $result->getData()); } } - public function dataDeleteValue() { + public static function dataDeleteValue(): array { return [ - ['app1 ', null, new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], + ['app1', 'key', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], ['app2', 'key', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN], ['app2', 'key', null, null, Http::STATUS_OK], ]; } - /** - * @dataProvider dataDeleteValue - * @param string $app - * @param string|null $key - * @param \Exception|null $appThrows - * @param \Exception|null $keyThrows - * @param int $status - */ - public function testDeleteValue($app, $key, $appThrows, $keyThrows, $status) { - + #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteValue')] + public function testDeleteValue(string $app, string $key, ?\Throwable $appThrows, ?\Throwable $keyThrows, int $status): void { $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']); if ($appThrows instanceof \Exception) { $api->expects($this->once()) @@ -277,9 +295,9 @@ class AppConfigControllerTest extends TestCase { $api->expects($this->never()) ->method('verifyConfigKey'); - $this->config->expects($this->never()) - ->method('deleteAppValue'); - } else if ($keyThrows instanceof \Exception) { + $this->appConfig->expects($this->never()) + ->method('deleteKey'); + } elseif ($keyThrows instanceof \Exception) { $api->expects($this->once()) ->method('verifyAppId') ->with($app); @@ -288,8 +306,8 @@ class AppConfigControllerTest extends TestCase { ->with($app, $key) ->willThrowException($keyThrows); - $this->config->expects($this->never()) - ->method('deleteAppValue'); + $this->appConfig->expects($this->never()) + ->method('deleteKey'); } else { $api->expects($this->once()) ->method('verifyAppId') @@ -298,8 +316,8 @@ class AppConfigControllerTest extends TestCase { ->method('verifyConfigKey') ->with($app, $key); - $this->config->expects($this->once()) - ->method('deleteAppValue') + $this->appConfig->expects($this->once()) + ->method('deleteKey') ->with($app, $key); } @@ -308,20 +326,20 @@ class AppConfigControllerTest extends TestCase { $this->assertSame($status, $result->getStatus()); if ($appThrows instanceof \Exception) { $this->assertEquals(['data' => ['message' => $appThrows->getMessage()]], $result->getData()); - } else if ($keyThrows instanceof \Exception) { + } elseif ($keyThrows instanceof \Exception) { $this->assertEquals(['data' => ['message' => $keyThrows->getMessage()]], $result->getData()); } else { $this->assertEquals([], $result->getData()); } } - public function testVerifyAppId() { + public function testVerifyAppId(): void { $api = $this->getInstance(); $this->invokePrivate($api, 'verifyAppId', ['activity']); - $this->assertTrue(true); + $this->addToAssertionCount(1); } - public function dataVerifyAppIdThrows() { + public static function dataVerifyAppIdThrows(): array { return [ ['activity..'], ['activity/'], @@ -330,55 +348,49 @@ class AppConfigControllerTest extends TestCase { ]; } - /** - * @dataProvider dataVerifyAppIdThrows - * @expectedException \InvalidArgumentException - * @param string $app - */ - public function testVerifyAppIdThrows($app) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataVerifyAppIdThrows')] + public function testVerifyAppIdThrows(string $app): void { + $this->expectException(\InvalidArgumentException::class); + $api = $this->getInstance(); $this->invokePrivate($api, 'verifyAppId', [$app]); } - public function dataVerifyConfigKey() { + public static function dataVerifyConfigKey(): array { return [ - ['activity', 'abc'], - ['dav', 'public_route'], - ['files', 'remote_route'], + ['activity', 'abc', ''], + ['dav', 'public_route', ''], + ['files', 'remote_route', ''], + ['core', 'encryption_enabled', 'yes'], ]; } - /** - * @dataProvider dataVerifyConfigKey - * @param string $app - * @param string $key - */ - public function testVerifyConfigKey($app, $key) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataVerifyConfigKey')] + public function testVerifyConfigKey(string $app, string $key, string $value): void { $api = $this->getInstance(); - $this->invokePrivate($api, 'verifyConfigKey', [$app, $key]); - $this->assertTrue(true); + $this->invokePrivate($api, 'verifyConfigKey', [$app, $key, $value]); + $this->addToAssertionCount(1); } - public function dataVerifyConfigKeyThrows() { + public static function dataVerifyConfigKeyThrows(): array { return [ - ['activity', 'installed_version'], - ['calendar', 'enabled'], - ['contacts', 'types'], - ['core', 'public_files'], - ['core', 'public_dav'], - ['core', 'remote_files'], - ['core', 'remote_dav'], + ['activity', 'installed_version', ''], + ['calendar', 'enabled', ''], + ['contacts', 'types', ''], + ['core', 'encryption_enabled', 'no'], + ['core', 'encryption_enabled', ''], + ['core', 'public_files', ''], + ['core', 'public_dav', ''], + ['core', 'remote_files', ''], + ['core', 'remote_dav', ''], ]; } - /** - * @dataProvider dataVerifyConfigKeyThrows - * @expectedException \InvalidArgumentException - * @param string $app - * @param string $key - */ - public function testVerifyConfigKeyThrows($app, $key) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataVerifyConfigKeyThrows')] + public function testVerifyConfigKeyThrows(string $app, string $key, string $value): void { + $this->expectException(\InvalidArgumentException::class); + $api = $this->getInstance(); - $this->invokePrivate($api, 'verifyConfigKey', [$app, $key]); + $this->invokePrivate($api, 'verifyConfigKey', [$app, $key, $value]); } } |