From 3a1143cf5698bad7a95e797ebcf4e8b7b11256f6 Mon Sep 17 00:00:00 2001 From: def Date: Sat, 30 Jul 2022 23:43:40 +0200 Subject: [PATCH] fix missing --- selfprivacy_api/graphql/mutations/users_utils.py | 3 +++ selfprivacy_api/graphql/queries/users.py | 4 +++- selfprivacy_api/utils/__init__.py | 1 + tests/test_graphql/test_users.py | 11 +++++++++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/selfprivacy_api/graphql/mutations/users_utils.py b/selfprivacy_api/graphql/mutations/users_utils.py index 4709990..1b4a07b 100644 --- a/selfprivacy_api/graphql/mutations/users_utils.py +++ b/selfprivacy_api/graphql/mutations/users_utils.py @@ -55,6 +55,7 @@ def create_user_util(username, password): def delete_user_util(username): with WriteUserData() as data: + ensure_ssh_and_users_fields_exist(data) if username == data["username"] or username == "root": return False, "Cannot delete main or root user", 400 @@ -78,6 +79,8 @@ def update_user_util(username, password): hashed_password = hash_password(password) with WriteUserData() as data: + ensure_ssh_and_users_fields_exist(data) + if username == data["username"]: data["hashedMasterPassword"] = hashed_password diff --git a/selfprivacy_api/graphql/queries/users.py b/selfprivacy_api/graphql/queries/users.py index ecfa5ac..adeebaf 100644 --- a/selfprivacy_api/graphql/queries/users.py +++ b/selfprivacy_api/graphql/queries/users.py @@ -4,7 +4,7 @@ import typing import strawberry from selfprivacy_api.graphql.common_types.user import User, get_user_by_username -from selfprivacy_api.utils import ReadUserData +from selfprivacy_api.utils import ReadUserData, ensure_ssh_and_users_fields_exist from selfprivacy_api.graphql import IsAuthenticated @@ -12,6 +12,8 @@ def get_users() -> typing.List[User]: """Get users""" user_list = [] with ReadUserData() as data: + ensure_ssh_and_users_fields_exist(data) + for user in data["users"]: user_list.append(get_user_by_username(user["username"])) diff --git a/selfprivacy_api/utils/__init__.py b/selfprivacy_api/utils/__init__.py index 91eaf57..d4f82a4 100644 --- a/selfprivacy_api/utils/__init__.py +++ b/selfprivacy_api/utils/__init__.py @@ -178,6 +178,7 @@ def hash_password(password): def ensure_ssh_and_users_fields_exist(data): if "ssh" not in data: data["ssh"] = [] + data["ssh"]["rootKeys"] = [] elif data["ssh"].get("rootKeys") is None: data["ssh"]["rootKeys"] = [] diff --git a/tests/test_graphql/test_users.py b/tests/test_graphql/test_users.py index 18998db..32052c2 100644 --- a/tests/test_graphql/test_users.py +++ b/tests/test_graphql/test_users.py @@ -468,7 +468,10 @@ def test_graphql_add_existing_user(authorized_client, one_user, mock_subprocess_ assert response.json["data"]["createUser"]["success"] is False assert response.json["data"]["createUser"]["user"]["username"] == "user1" - assert response.json["data"]["createUser"]["user"]["sshKeys"][0] == "ssh-rsa KEY user1@pc" + assert ( + response.json["data"]["createUser"]["user"]["sshKeys"][0] + == "ssh-rsa KEY user1@pc" + ) def test_graphql_add_main_user(authorized_client, one_user, mock_subprocess_popen): @@ -492,7 +495,11 @@ def test_graphql_add_main_user(authorized_client, one_user, mock_subprocess_pope assert response.json["data"]["createUser"]["success"] is False assert response.json["data"]["createUser"]["user"]["username"] == "tester" - assert response.json["data"]["createUser"]["user"]["sshKeys"][0] == "ssh-rsa KEY test@pc" + assert ( + response.json["data"]["createUser"]["user"]["sshKeys"][0] + == "ssh-rsa KEY test@pc" + ) + def test_graphql_add_long_username(authorized_client, one_user, mock_subprocess_popen): response = authorized_client.post(