mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-03-18 12:34:49 +00:00
fix(users): handle no admin name defined when adding a user
This commit is contained in:
parent
c470ec45e8
commit
2e775dad90
3 changed files with 26 additions and 1 deletions
|
@ -107,6 +107,12 @@ class PasswordIsEmpty(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidConfiguration(Exception):
|
||||||
|
"""The userdata is broken"""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def create_user(username: str, password: str):
|
def create_user(username: str, password: str):
|
||||||
if password == "":
|
if password == "":
|
||||||
raise PasswordIsEmpty("Password is empty")
|
raise PasswordIsEmpty("Password is empty")
|
||||||
|
@ -124,6 +130,10 @@ def create_user(username: str, password: str):
|
||||||
|
|
||||||
with ReadUserData() as user_data:
|
with ReadUserData() as user_data:
|
||||||
ensure_ssh_and_users_fields_exist(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"]:
|
if username == user_data["username"]:
|
||||||
raise UserAlreadyExists("User already exists")
|
raise UserAlreadyExists("User already exists")
|
||||||
if username in [user["username"] for user in user_data["users"]]:
|
if username in [user["username"] for user in user_data["users"]]:
|
||||||
|
|
|
@ -69,6 +69,12 @@ class UsersMutations:
|
||||||
message=str(e),
|
message=str(e),
|
||||||
code=400,
|
code=400,
|
||||||
)
|
)
|
||||||
|
except users_actions.InvalidConfiguration as e:
|
||||||
|
return UserMutationReturn(
|
||||||
|
success=False,
|
||||||
|
message=str(e),
|
||||||
|
code=400,
|
||||||
|
)
|
||||||
except users_actions.UserAlreadyExists as e:
|
except users_actions.UserAlreadyExists as e:
|
||||||
return UserMutationReturn(
|
return UserMutationReturn(
|
||||||
success=False,
|
success=False,
|
||||||
|
|
|
@ -467,7 +467,7 @@ def test_graphql_add_existing_user(authorized_client, one_user):
|
||||||
assert output["user"]["sshKeys"][0] == "ssh-rsa KEY user1@pc"
|
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")
|
output = api_add_user(authorized_client, "tester", password="12345678")
|
||||||
|
|
||||||
assert_errorcode(output, code=409)
|
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"
|
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):
|
def test_graphql_add_long_username(authorized_client, one_user, mock_subprocess_popen):
|
||||||
output = api_add_user(authorized_client, "a" * 32, password="12345678")
|
output = api_add_user(authorized_client, "a" * 32, password="12345678")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue