Skip to content

Commit

Permalink
Merge pull request #47409 from nextcloud/followup/46991/allow-setting…
Browse files Browse the repository at this point in the history
…-new-configs

fix(provisioning): Support setting new app configs as well
  • Loading branch information
nickvergessen authored Aug 22, 2024
2 parents b33260a + 9641433 commit 0df3a46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
11 changes: 9 additions & 2 deletions 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,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),
Expand Down
37 changes: 23 additions & 14 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 Down Expand Up @@ -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()],
];
}

Expand All @@ -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')
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 0df3a46

Please sign in to comment.