2022-08-01 10:40:40 +00:00
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
import pytest
|
|
|
|
|
2023-12-15 09:46:59 +00:00
|
|
|
from selfprivacy_api.graphql.mutations.system_mutations import SystemMutations
|
2023-12-15 10:09:35 +00:00
|
|
|
from selfprivacy_api.graphql.queries.system import System
|
|
|
|
|
|
|
|
from tests.common import read_json, generate_system_query
|
2023-12-15 10:43:07 +00:00
|
|
|
from tests.test_graphql.common import assert_empty, assert_ok, get_data
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProcessMock:
|
|
|
|
"""Mock subprocess.Popen"""
|
|
|
|
|
|
|
|
def __init__(self, args, **kwargs):
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
def communicate(): # pylint: disable=no-method-argument
|
|
|
|
return (b"NEW_HASHED", None)
|
|
|
|
|
|
|
|
returncode = 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_subprocess_popen(mocker):
|
|
|
|
mock = mocker.patch("subprocess.Popen", autospec=True, return_value=ProcessMock)
|
|
|
|
return mock
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def some_users(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "some_users.json")
|
|
|
|
assert read_json(datadir / "some_users.json")["users"] == [
|
|
|
|
{
|
|
|
|
"username": "user1",
|
|
|
|
"hashedPassword": "HASHED_PASSWORD_1",
|
|
|
|
"sshKeys": ["ssh-rsa KEY user1@pc"],
|
|
|
|
},
|
|
|
|
{"username": "user2", "hashedPassword": "HASHED_PASSWORD_2", "sshKeys": []},
|
|
|
|
{"username": "user3", "hashedPassword": "HASHED_PASSWORD_3"},
|
|
|
|
]
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
|
|
|
# TESTS ########################################################
|
|
|
|
|
|
|
|
|
|
|
|
API_CREATE_SSH_KEY_MUTATION = """
|
|
|
|
mutation addSshKey($sshInput: SshMutationInput!) {
|
2023-06-21 03:46:56 +00:00
|
|
|
users {
|
|
|
|
addSshKey(sshInput: $sshInput) {
|
|
|
|
success
|
|
|
|
message
|
|
|
|
code
|
|
|
|
user {
|
|
|
|
username
|
|
|
|
sshKeys
|
|
|
|
}
|
2022-08-01 10:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2023-12-15 09:46:59 +00:00
|
|
|
API_SET_SSH_SETTINGS = """
|
2023-12-15 10:43:07 +00:00
|
|
|
mutation enableSsh($settings: SSHSettingsInput!) {
|
2023-12-15 09:46:59 +00:00
|
|
|
system {
|
2023-12-15 10:43:07 +00:00
|
|
|
changeSshSettings(settings: $settings) {
|
2023-12-15 09:46:59 +00:00
|
|
|
success
|
|
|
|
message
|
|
|
|
code
|
|
|
|
enable
|
2023-12-15 10:09:35 +00:00
|
|
|
passwordAuthentication
|
2023-12-15 09:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2023-12-15 10:09:35 +00:00
|
|
|
API_SSH_SETTINGS_QUERY = """
|
|
|
|
settings {
|
|
|
|
ssh {
|
|
|
|
enable
|
|
|
|
passwordAuthentication
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def api_ssh_settings(authorized_client):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={"query": generate_system_query([API_SSH_SETTINGS_QUERY])},
|
|
|
|
)
|
|
|
|
data = get_data(response)
|
|
|
|
result = data["system"]["settings"]["ssh"]
|
|
|
|
assert result is not None
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-12-15 10:43:07 +00:00
|
|
|
def api_set_ssh_settings(authorized_client, enable: bool, password_auth: bool):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_SET_SSH_SETTINGS,
|
|
|
|
"variables": {
|
|
|
|
"settings": {
|
|
|
|
"enable": enable,
|
|
|
|
"passwordAuthentication": password_auth,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
data = get_data(response)
|
|
|
|
result = data["system"]["changeSshSettings"]
|
|
|
|
assert result is not None
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_ssh_query(authorized_client, some_users):
|
2023-12-15 10:09:35 +00:00
|
|
|
settings = api_ssh_settings(authorized_client)
|
|
|
|
assert settings["enable"] is True
|
|
|
|
assert settings["passwordAuthentication"] is True
|
|
|
|
|
2023-12-15 09:46:59 +00:00
|
|
|
|
|
|
|
def test_graphql_change_ssh_settings_unauthorized(
|
|
|
|
client, some_users, mock_subprocess_popen
|
|
|
|
):
|
|
|
|
response = client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_SET_SSH_SETTINGS,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"enable": True,
|
|
|
|
"passwordAuthentication": True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert_empty(response)
|
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-12-15 10:43:07 +00:00
|
|
|
def test_graphql_disable_ssh(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
output = api_set_ssh_settings(authorized_client, enable=False, password_auth=False)
|
|
|
|
assert_ok(output)
|
|
|
|
assert output["enable"] == False
|
|
|
|
assert output["passwordAuthentication"] == False
|
|
|
|
|
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
def test_graphql_add_ssh_key_unauthorized(client, some_users, mock_subprocess_popen):
|
|
|
|
response = client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user1",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-01-06 09:54:07 +00:00
|
|
|
assert_empty(response)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_ssh_key(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user1",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["code"] == 201
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["user"]["username"] == "user1"
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["user"]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY user1@pc",
|
|
|
|
"ssh-rsa KEY test_key@pc",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_root_ssh_key(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "root",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["code"] == 201
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["user"]["username"] == "root"
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["user"]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-ed25519 KEY test@pc",
|
|
|
|
"ssh-rsa KEY test_key@pc",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_main_ssh_key(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "tester",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["code"] == 201
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["user"]["username"] == "tester"
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["user"]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY test@pc",
|
|
|
|
"ssh-rsa KEY test_key@pc",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_bad_ssh_key(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user1",
|
|
|
|
"sshKey": "trust me, this is the ssh key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["code"] == 400
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_ssh_key_nonexistent_user(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user666",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["code"] == 404
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["addSshKey"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
API_REMOVE_SSH_KEY_MUTATION = """
|
|
|
|
mutation removeSshKey($sshInput: SshMutationInput!) {
|
2023-06-21 03:46:56 +00:00
|
|
|
users {
|
|
|
|
removeSshKey(sshInput: $sshInput) {
|
|
|
|
success
|
|
|
|
message
|
|
|
|
code
|
|
|
|
user {
|
|
|
|
username
|
|
|
|
sshKeys
|
|
|
|
}
|
2022-08-01 10:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_remove_ssh_key_unauthorized(client, some_users, mock_subprocess_popen):
|
|
|
|
response = client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_REMOVE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user1",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-01-06 09:54:07 +00:00
|
|
|
assert_empty(response)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_remove_ssh_key(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_REMOVE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user1",
|
|
|
|
"sshKey": "ssh-rsa KEY user1@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["code"] == 200
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert (
|
|
|
|
response.json()["data"]["users"]["removeSshKey"]["user"]["username"] == "user1"
|
|
|
|
)
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["user"]["sshKeys"] == []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_remove_root_ssh_key(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_REMOVE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "root",
|
|
|
|
"sshKey": "ssh-ed25519 KEY test@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["code"] == 200
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert (
|
|
|
|
response.json()["data"]["users"]["removeSshKey"]["user"]["username"] == "root"
|
|
|
|
)
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["user"]["sshKeys"] == []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_remove_main_ssh_key(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_REMOVE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "tester",
|
|
|
|
"sshKey": "ssh-rsa KEY test@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["code"] == 200
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert (
|
|
|
|
response.json()["data"]["users"]["removeSshKey"]["user"]["username"] == "tester"
|
|
|
|
)
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["user"]["sshKeys"] == []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_remove_nonexistent_ssh_key(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_REMOVE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user1",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["code"] == 404
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_remove_ssh_key_nonexistent_user(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_REMOVE_SSH_KEY_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"sshInput": {
|
|
|
|
"username": "user666",
|
|
|
|
"sshKey": "ssh-rsa KEY test_key@pc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["code"] == 404
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["removeSshKey"]["success"] is False
|