Skip to content

Commit

Permalink
added sensitive flag for AppCfg (#60)
Browse files Browse the repository at this point in the history
Relates to nextcloud/app_api/issues/21

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 authored Aug 2, 2023
1 parent ff10d39 commit e501238
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
44 changes: 30 additions & 14 deletions nc_py_api/appconfig_preferences_ex.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
"""Nextcloud API for working with apps V2's storage w/wo user context(table oc_appconfig_ex/oc_preferences_ex)."""
from typing import Optional, TypedDict, Union
from dataclasses import dataclass
from typing import Optional, Union

from ._session import NcSessionBasic
from .constants import APP_V2_BASIC_URL
from .exceptions import NextcloudExceptionNotFound
from .misc import require_capabilities


class AppCfgPrefRecord(TypedDict):
configkey: str
configvalue: str
@dataclass
class CfgRecord:
key: str
value: str

def __init__(self, raw_data: dict):
self.key = raw_data["configkey"]
self.value = raw_data["configvalue"]


class BasicAppCfgPref:
Expand All @@ -24,24 +30,18 @@ def get_value(self, key: str, default=None) -> Optional[str]:
require_capabilities("app_ecosystem_v2", self._session.capabilities)
r = self.get_values([key])
if r:
return r[0]["configvalue"]
return r[0].value
return default

def get_values(self, keys: list[str]) -> list[AppCfgPrefRecord]:
def get_values(self, keys: list[str]) -> list[CfgRecord]:
if not keys:
return []
if not all(keys):
raise ValueError("`key` parameter can not be empty")
require_capabilities("app_ecosystem_v2", self._session.capabilities)
data = {"configKeys": keys}
return self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}/get-values", json=data)

def set_value(self, key: str, value: str) -> None:
if not key:
raise ValueError("`key` parameter can not be empty")
require_capabilities("app_ecosystem_v2", self._session.capabilities)
params = {"configKey": key, "configValue": value}
self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json=params)
results = self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}/get-values", json=data)
return [CfgRecord(i) for i in results]

def delete(self, keys: Union[str, list[str]], not_fail=True) -> None:
if isinstance(keys, str):
Expand All @@ -61,6 +61,22 @@ def delete(self, keys: Union[str, list[str]], not_fail=True) -> None:
class PreferencesExAPI(BasicAppCfgPref):
url_suffix = "ex-app/preference"

def set_value(self, key: str, value: str) -> None:
if not key:
raise ValueError("`key` parameter can not be empty")
require_capabilities("app_ecosystem_v2", self._session.capabilities)
params = {"configKey": key, "configValue": value}
self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json=params)


class AppConfigExAPI(BasicAppCfgPref):
url_suffix = "ex-app/config"

def set_value(self, key: str, value: str, sensitive: bool = False) -> None:
if not key:
raise ValueError("`key` parameter can not be empty")
require_capabilities("app_ecosystem_v2", self._session.capabilities)
params: dict = {"configKey": key, "configValue": value}
if sensitive:
params["sensitive"] = True
self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json=params)
30 changes: 25 additions & 5 deletions tests/appconfig_preferences_ex_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from gfixture import NC_APP

from nc_py_api import NextcloudExceptionNotFound
from nc_py_api import NextcloudExceptionNotFound, constants

if NC_APP is None or "app_ecosystem_v2" not in NC_APP.capabilities:
pytest.skip("app_ecosystem_v2 is not installed.", allow_module_level=True)
Expand Down Expand Up @@ -108,7 +108,27 @@ def test_cfg_ex_get_typing(class_to_test):
class_to_test.set_value("test key2", "321")
r = class_to_test.get_values(["test key", "test key2"])
assert isinstance(r, list)
assert r[0]["configkey"] == "test key"
assert r[1]["configkey"] == "test key2"
assert r[0]["configvalue"] == "123"
assert r[1]["configvalue"] == "321"
assert r[0].key == "test key"
assert r[1].key == "test key2"
assert r[0].value == "123"
assert r[1].value == "321"


def test_appcfg_sensitive():
appcfg = NC_APP.appconfig_ex_api
appcfg.delete("test_key")
appcfg.set_value("test_key", "123", sensitive=True)
assert appcfg.get_value("test_key") == "123"
assert appcfg.get_values(["test_key"])[0].value == "123"
appcfg.delete("test_key")
# next code tests `sensitive` value from the `AppEcosystem`
params = {"configKey": "test_key", "configValue": "123", "sensitive": True}
result = NC_APP._session.ocs(method="POST", path=f"{constants.APP_V2_BASIC_URL}/{appcfg.url_suffix}", json=params)
assert result["configkey"] == "test_key"
assert result["configvalue"] == "123"
assert bool(result["sensitive"]) is True
params["sensitive"] = False
result = NC_APP._session.ocs(method="POST", path=f"{constants.APP_V2_BASIC_URL}/{appcfg.url_suffix}", json=params)
assert result["configkey"] == "test_key"
assert result["configvalue"] == "123"
assert bool(result["sensitive"]) is False

0 comments on commit e501238

Please sign in to comment.