From 26f9393d953aac3ae564519af71841e3cd9c8cfa Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 12 Jul 2022 16:24:29 +0300 Subject: [PATCH] Implement change system settings Co-authored-by: Detlaff --- .../graphql/mutations/system_mutations.py | 86 +++++++++++++++++++ selfprivacy_api/graphql/schema.py | 3 +- tests/test_graphql/test_system.py | 4 +- 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 selfprivacy_api/graphql/mutations/system_mutations.py diff --git a/selfprivacy_api/graphql/mutations/system_mutations.py b/selfprivacy_api/graphql/mutations/system_mutations.py new file mode 100644 index 0000000..d080af3 --- /dev/null +++ b/selfprivacy_api/graphql/mutations/system_mutations.py @@ -0,0 +1,86 @@ +"""System management mutations""" +# pylint: disable=too-few-public-methods +import typing +import pytz +import strawberry +from selfprivacy_api.graphql import IsAuthenticated +from selfprivacy_api.graphql.mutations.mutation_interface import ( + MutationReturnInterface, +) +from selfprivacy_api.utils import ReadUserData, WriteUserData + + +@strawberry.type +class TimezoneMutationReturn(MutationReturnInterface): + """Return type of the timezone mutation, contains timezone""" + + timezone: typing.Optional[str] + + +@strawberry.type +class AutoUpgradeSettingsMutationReturn(MutationReturnInterface): + """Return type autoUpgrade Settings""" + + enableAutoUpgrade: bool + allowReboot: bool + + +@strawberry.input +class AutoUpgradeSettingsInput: + """Input type for auto upgrade settings""" + + enableAutoUpgrade: typing.Optional[bool] = None + allowReboot: typing.Optional[bool] = None + + +@strawberry.type +class SystemMutations: + """Mutations related to system settings""" + + @strawberry.mutation(permission_classes=[IsAuthenticated]) + def change_timezone(self, timezone: str) -> TimezoneMutationReturn: + """Change the timezone of the server. Timezone is a tzdatabase name.""" + if timezone not in pytz.all_timezones: + return TimezoneMutationReturn( + success=False, + message="Invalid timezone", + code=400, + timezone=None, + ) + with WriteUserData() as data: + data["timezone"] = timezone + return TimezoneMutationReturn( + success=True, + message="Timezone changed", + code=200, + timezone=timezone, + ) + + @strawberry.mutation(permission_classes=[IsAuthenticated]) + def change_auto_upgrade_settings( + self, settings: AutoUpgradeSettingsInput + ) -> AutoUpgradeSettingsMutationReturn: + """Change auto upgrade settings of the server.""" + with WriteUserData() as data: + if "autoUpgrade" not in data: + data["autoUpgrade"] = {} + if "enable" not in data["autoUpgrade"]: + data["autoUpgrade"]["enable"] = True + if "allowReboot" not in data["autoUpgrade"]: + data["autoUpgrade"]["allowReboot"] = False + + if settings.enableAutoUpgrade is not None: + data["autoUpgrade"]["enable"] = settings.enableAutoUpgrade + if settings.allowReboot is not None: + data["autoUpgrade"]["allowReboot"] = settings.allowReboot + + auto_upgrade = data["autoUpgrade"]["enable"] + allow_reboot = data["autoUpgrade"]["allowReboot"] + + return AutoUpgradeSettingsMutationReturn( + success=True, + message="Auto-upgrade settings changed", + code=200, + enableAutoUpgrade=auto_upgrade, + allowReboot=allow_reboot, + ) diff --git a/selfprivacy_api/graphql/schema.py b/selfprivacy_api/graphql/schema.py index 5aba9b3..69735a2 100644 --- a/selfprivacy_api/graphql/schema.py +++ b/selfprivacy_api/graphql/schema.py @@ -4,6 +4,7 @@ import typing import strawberry from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql.mutations.api_mutations import ApiMutations +from selfprivacy_api.graphql.mutations.system_mutations import SystemMutations from selfprivacy_api.graphql.queries.api_queries import Api from selfprivacy_api.graphql.queries.system import System @@ -25,7 +26,7 @@ class Query: @strawberry.type -class Mutation(ApiMutations): +class Mutation(ApiMutations, SystemMutations): """Root schema for mutations""" pass diff --git a/tests/test_graphql/test_system.py b/tests/test_graphql/test_system.py index fe76095..d5cf6e6 100644 --- a/tests/test_graphql/test_system.py +++ b/tests/test_graphql/test_system.py @@ -200,7 +200,7 @@ def test_graphql_get_system_version(authorized_client, mock_subprocess_check_out assert response.status_code == 200 assert response.json.get("data") is not None - assert response.json["data"]["sytem"]["info"]["systemVersion"] == "Testing Linux" + assert response.json["data"]["system"]["info"]["systemVersion"] == "Testing Linux" assert mock_subprocess_check_output.call_count == 1 assert mock_subprocess_check_output.call_args[0][0] == ["uname", "-a"] @@ -775,7 +775,7 @@ def test_graphql_change_auto_upgrade_with_empty_input(authorized_client, turned_ }, ) assert response.status_code == 200 - assert response.json.get("data") is None + assert response.json.get("data") is not None assert response.json["data"]["changeAutoUpgradeSettings"]["success"] is True assert response.json["data"]["changeAutoUpgradeSettings"]["message"] is not None assert response.json["data"]["changeAutoUpgradeSettings"]["code"] == 200