test(ssh): parametrized testing of ssh key addition, more

This commit is contained in:
Houkime 2023-12-22 07:37:04 +00:00
parent 7c4c5929df
commit 65c2023366
2 changed files with 20 additions and 20 deletions

View file

@ -4,14 +4,14 @@ from tests.conftest import TOKENS_FILE_CONTENTS, DEVICE_WE_AUTH_TESTS_WITH
ORIGINAL_DEVICES = TOKENS_FILE_CONTENTS["tokens"] ORIGINAL_DEVICES = TOKENS_FILE_CONTENTS["tokens"]
def assert_ok(output: dict) -> None: def assert_ok(output: dict, code=200) -> None:
if output["success"] is False: if output["success"] is False:
# convenience for debugging, this should display error # convenience for debugging, this should display error
# if message is empty, consider adding helpful messages # if message is empty, consider adding helpful messages
raise ValueError(output["code"], output["message"]) raise ValueError(output["code"], output["message"])
assert output["success"] is True assert output["success"] is True
assert output["message"] is not None assert output["message"] is not None
assert output["code"] == 200 assert output["code"] == code
def assert_errorcode(output: dict, code) -> None: def assert_errorcode(output: dict, code) -> None:

View file

@ -413,9 +413,7 @@ def test_graphql_add_ssh_key_when_none(authorized_client, no_keys, user):
output = api_add_ssh_key(authorized_client, user, key1) output = api_add_ssh_key(authorized_client, user, key1)
assert output["code"] == 201 assert_ok(output, code=201)
assert output["message"] is not None
assert output["success"] is True
assert output["user"]["username"] == user assert output["user"]["username"] == user
assert output["user"]["sshKeys"] == [key1] assert output["user"]["sshKeys"] == [key1]
@ -426,24 +424,26 @@ def test_graphql_add_ssh_key_when_none(authorized_client, no_keys, user):
assert api_get_user_keys(authorized_client, user) == [key1] assert api_get_user_keys(authorized_client, user) == [key1]
def test_graphql_add_root_ssh_key_one_more(authorized_client, no_rootkeys): @pytest.mark.parametrize("user", key_users)
output = api_add_ssh_key(authorized_client, "root", "ssh-rsa KEY test_key@pc") def test_graphql_add_ssh_key_one_more(authorized_client, no_keys, user):
assert output["user"]["sshKeys"] == ["ssh-rsa KEY test_key@pc"] keys = [
output = api_add_ssh_key(authorized_client, "root", "ssh-rsa KEY2 test_key@pc")
assert output["code"] == 201
assert output["message"] is not None
assert output["success"] is True
assert output["user"]["username"] == "root"
expected_keys = [
"ssh-rsa KEY test_key@pc", "ssh-rsa KEY test_key@pc",
"ssh-rsa KEY2 test_key@pc", "ssh-rsa KEY2 test_key@pc",
] ]
output = api_add_ssh_key(authorized_client, user, keys[0])
assert output["user"]["sshKeys"] == [keys[0]]
assert output["user"]["sshKeys"] == expected_keys output = api_add_ssh_key(authorized_client, user, keys[1])
assert api_rootkeys(authorized_client) == expected_keys
assert_ok(output, code=201)
assert output["user"]["username"] == user
assert output["user"]["sshKeys"] == keys
if user == "root":
assert api_rootkeys(authorized_client) == keys
else:
assert api_get_user_keys(authorized_client, user) == keys
def test_graphql_add_root_ssh_key_same(authorized_client, no_rootkeys): def test_graphql_add_root_ssh_key_same(authorized_client, no_rootkeys):
@ -585,7 +585,7 @@ def test_graphql_remove_root_ssh_key(authorized_client, some_users):
assert response.json()["data"]["users"]["removeSshKey"]["user"]["sshKeys"] == [] assert response.json()["data"]["users"]["removeSshKey"]["user"]["sshKeys"] == []
def test_graphql_remove_main_ssh_key( def test_graphql_remove_admin_ssh_key(
authorized_client, some_users, mock_subprocess_popen authorized_client, some_users, mock_subprocess_popen
): ):
response = authorized_client.post( response = authorized_client.post(