fix(users): handle no admin name defined when adding a user

remove-rest
Houkime 2023-12-27 13:44:39 +00:00
parent c470ec45e8
commit 2e775dad90
3 changed files with 26 additions and 1 deletions

View File

@ -107,6 +107,12 @@ class PasswordIsEmpty(Exception):
pass
class InvalidConfiguration(Exception):
"""The userdata is broken"""
pass
def create_user(username: str, password: str):
if password == "":
raise PasswordIsEmpty("Password is empty")
@ -124,6 +130,10 @@ def create_user(username: str, password: str):
with ReadUserData() as user_data:
ensure_ssh_and_users_fields_exist(user_data)
if "username" not in user_data.keys():
raise InvalidConfiguration(
"Broken config: Admin name is not defined. Consider recovery or add it manually"
)
if username == user_data["username"]:
raise UserAlreadyExists("User already exists")
if username in [user["username"] for user in user_data["users"]]:

View File

@ -69,6 +69,12 @@ class UsersMutations:
message=str(e),
code=400,
)
except users_actions.InvalidConfiguration as e:
return UserMutationReturn(
success=False,
message=str(e),
code=400,
)
except users_actions.UserAlreadyExists as e:
return UserMutationReturn(
success=False,

View File

@ -467,7 +467,7 @@ def test_graphql_add_existing_user(authorized_client, one_user):
assert output["user"]["sshKeys"][0] == "ssh-rsa KEY user1@pc"
def test_graphql_add_main_user(authorized_client, one_user, mock_subprocess_popen):
def test_graphql_add_main_user(authorized_client, one_user):
output = api_add_user(authorized_client, "tester", password="12345678")
assert_errorcode(output, code=409)
@ -475,6 +475,15 @@ def test_graphql_add_main_user(authorized_client, one_user, mock_subprocess_pope
assert output["user"]["sshKeys"][0] == "ssh-rsa KEY test@pc"
def test_graphql_add_user_when_no_admin_defined(
authorized_client, no_users_no_admin_nobody
):
output = api_add_user(authorized_client, "tester", password="12345678")
assert_errorcode(output, code=400)
assert output["user"] is None
def test_graphql_add_long_username(authorized_client, one_user, mock_subprocess_popen):
output = api_add_user(authorized_client, "a" * 32, password="12345678")