Implement change system settings

Co-authored-by: Detlaff <dettlaff@riseup.net>
This commit is contained in:
Inex Code 2022-07-12 16:24:29 +03:00
parent eb21b65bbc
commit 26f9393d95
3 changed files with 90 additions and 3 deletions

View file

@ -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,
)

View file

@ -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

View file

@ -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