From 54c6f1be80be99985e7b4cc1f0ad484b541e15e8 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 2 Aug 2024 18:05:37 +0200 Subject: [PATCH 1/2] fix(ProvisioningAPI): set typed config values by via API Signed-off-by: Arthur Schiwon --- .../lib/Controller/AppConfigController.php | 11 +++++- .../Controller/AppConfigControllerTest.php | 36 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/apps/provisioning_api/lib/Controller/AppConfigController.php b/apps/provisioning_api/lib/Controller/AppConfigController.php index 65b301245b396..2ea4602883c12 100644 --- a/apps/provisioning_api/lib/Controller/AppConfigController.php +++ b/apps/provisioning_api/lib/Controller/AppConfigController.php @@ -126,8 +126,17 @@ public function setValue(string $app, string $key, string $value): DataResponse return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN); } + $configDetails = $this->appConfig->getDetails($app, $key); /** @psalm-suppress InternalMethod */ - $this->appConfig->setValueMixed($app, $key, $value); + match ($configDetails['type']) { + IAppConfig::VALUE_BOOL => $this->appConfig->setValueBool($app, $key, (bool)$value), + IAppConfig::VALUE_FLOAT => $this->appConfig->setValueFloat($app, $key, (float)$value), + IAppConfig::VALUE_INT => $this->appConfig->setValueInt($app, $key, (int)$value), + IAppConfig::VALUE_STRING => $this->appConfig->setValueString($app, $key, $value), + IAppConfig::VALUE_ARRAY => $this->appConfig->setValueArray($app, $key, \json_decode($value, true)), + default => $this->appConfig->setValueMixed($app, $key, $value), + }; + return new DataResponse(); } diff --git a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php index f932aceaf8e72..1a8a95e60d8c6 100644 --- a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php +++ b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php @@ -17,6 +17,8 @@ use OCP\IUserSession; use OCP\Settings\IManager; use Test\TestCase; +use function json_decode; +use function json_encode; /** * Class AppConfigControllerTest @@ -184,6 +186,12 @@ public function dataSetValue() { ['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], ]; } @@ -196,7 +204,7 @@ public function dataSetValue() { * @param \Exception|null $keyThrows * @param int $status */ - public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status) { + public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status, int $type = IAppConfig::VALUE_MIXED) { $adminUser = $this->createMock(IUser::class); $adminUser->expects($this->once()) ->method('getUid') @@ -240,8 +248,30 @@ public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status ->with($app, $key); $this->appConfig->expects($this->once()) - ->method('setValueMixed') - ->with($app, $key, $value); + ->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); From aeac7217958b9caf6fdb32e1c9186609f1b99b10 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 22 Aug 2024 08:02:15 +0200 Subject: [PATCH 2/2] fix(provisioning): Support setting new app configs as well Signed-off-by: Joas Schilling --- .../lib/Controller/AppConfigController.php | 11 +++++- .../Controller/AppConfigControllerTest.php | 37 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/apps/provisioning_api/lib/Controller/AppConfigController.php b/apps/provisioning_api/lib/Controller/AppConfigController.php index 2ea4602883c12..2ff82213997b6 100644 --- a/apps/provisioning_api/lib/Controller/AppConfigController.php +++ b/apps/provisioning_api/lib/Controller/AppConfigController.php @@ -15,6 +15,7 @@ use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; +use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; use OCP\IGroupManager; use OCP\IL10N; @@ -126,9 +127,15 @@ public function setValue(string $app, string $key, string $value): DataResponse return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN); } - $configDetails = $this->appConfig->getDetails($app, $key); + $type = null; + try { + $configDetails = $this->appConfig->getDetails($app, $key); + $type = $configDetails['type']; + } catch (AppConfigUnknownKeyException) { + } + /** @psalm-suppress InternalMethod */ - match ($configDetails['type']) { + match ($type) { IAppConfig::VALUE_BOOL => $this->appConfig->setValueBool($app, $key, (bool)$value), IAppConfig::VALUE_FLOAT => $this->appConfig->setValueFloat($app, $key, (float)$value), IAppConfig::VALUE_INT => $this->appConfig->setValueInt($app, $key, (int)$value), diff --git a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php index 1a8a95e60d8c6..0bdee39cebc00 100644 --- a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php +++ b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php @@ -9,6 +9,7 @@ use OCA\Provisioning_API\Controller\AppConfigController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; use OCP\IGroupManager; use OCP\IL10N; @@ -192,6 +193,7 @@ public function dataSetValue() { ['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()], ]; } @@ -202,9 +204,9 @@ public function dataSetValue() { * @param string|null $value * @param \Exception|null $appThrows * @param \Exception|null $keyThrows - * @param int $status + * @param int|\Throwable $status */ - public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status, int $type = IAppConfig::VALUE_MIXED) { + public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status, int|\Throwable $type = IAppConfig::VALUE_MIXED) { $adminUser = $this->createMock(IUser::class); $adminUser->expects($this->once()) ->method('getUid') @@ -247,18 +249,25 @@ public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status ->method('verifyConfigKey') ->with($app, $key); - $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, - ]); + 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',