Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable30] fix(ProvisioningAPI): set typed config values by via API #47393

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion apps/provisioning_api/lib/Controller/AppConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,8 +127,23 @@ public function setValue(string $app, string $key, string $value): DataResponse
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
}

$type = null;
try {
$configDetails = $this->appConfig->getDetails($app, $key);
$type = $configDetails['type'];
} catch (AppConfigUnknownKeyException) {
}

/** @psalm-suppress InternalMethod */
$this->appConfig->setValueMixed($app, $key, $value);
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),
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();
}

Expand Down
47 changes: 43 additions & 4 deletions apps/provisioning_api/tests/Controller/AppConfigControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,8 @@
use OCP\IUserSession;
use OCP\Settings\IManager;
use Test\TestCase;
use function json_decode;
use function json_encode;

/**
* Class AppConfigControllerTest
Expand Down Expand Up @@ -184,6 +187,13 @@ 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],
['app2', 'key', json_encode([4, 2]), null, null, Http::STATUS_OK, new AppConfigUnknownKeyException()],
];
}

Expand All @@ -194,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) {
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')
Expand Down Expand Up @@ -239,9 +249,38 @@ public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status
->method('verifyConfigKey')
->with($app, $key);

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('setValueMixed')
->with($app, $key, $value);
->method($configValueSetter)
->with($app, $key, $configValueSetter === 'setValueArray' ? json_decode($value, true) : $value);
}

$result = $api->setValue($app, $key, $value);
Expand Down
Loading