2022-01-10 20:35:00 +00:00
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
# pylint: disable=unused-argument
|
2021-11-29 19:16:08 +00:00
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def read_json(file_path):
|
2022-01-10 20:35:00 +00:00
|
|
|
with open(file_path, "r", encoding="utf-8") as file:
|
|
|
|
return json.load(file)
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
## FIXTURES ###################################################
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def ssh_off(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "turned_off.json")
|
2022-01-10 20:35:00 +00:00
|
|
|
assert not read_json(datadir / "turned_off.json")["ssh"]["enable"]
|
|
|
|
assert read_json(datadir / "turned_off.json")["ssh"]["passwordAuthentication"]
|
2021-11-29 19:16:08 +00:00
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def ssh_on(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "turned_on.json")
|
2022-01-10 20:35:00 +00:00
|
|
|
assert read_json(datadir / "turned_off.json")["ssh"]["passwordAuthentication"]
|
|
|
|
assert read_json(datadir / "turned_on.json")["ssh"]["enable"]
|
2021-11-29 19:16:08 +00:00
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def all_off(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "all_off.json")
|
2022-01-10 20:35:00 +00:00
|
|
|
assert not read_json(datadir / "all_off.json")["ssh"]["passwordAuthentication"]
|
|
|
|
assert not read_json(datadir / "all_off.json")["ssh"]["enable"]
|
2021-11-29 19:16:08 +00:00
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def undefined_settings(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "undefined.json")
|
|
|
|
assert "ssh" not in read_json(datadir / "undefined.json")
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def undefined_values(mocker, datadir):
|
|
|
|
mocker.patch(
|
|
|
|
"selfprivacy_api.utils.USERDATA_FILE", new=datadir / "undefined_values.json"
|
|
|
|
)
|
|
|
|
assert "ssh" in read_json(datadir / "undefined_values.json")
|
|
|
|
assert "enable" not in read_json(datadir / "undefined_values.json")["ssh"]
|
|
|
|
assert (
|
|
|
|
"passwordAuthentication"
|
|
|
|
not in read_json(datadir / "undefined_values.json")["ssh"]
|
|
|
|
)
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def root_and_admin_have_keys(mocker, datadir):
|
|
|
|
mocker.patch(
|
|
|
|
"selfprivacy_api.utils.USERDATA_FILE",
|
|
|
|
new=datadir / "root_and_admin_have_keys.json",
|
|
|
|
)
|
2022-01-10 20:35:00 +00:00
|
|
|
assert read_json(datadir / "root_and_admin_have_keys.json")["ssh"]["enable"]
|
|
|
|
assert read_json(datadir / "root_and_admin_have_keys.json")["ssh"][
|
|
|
|
"passwordAuthentication"
|
|
|
|
]
|
2021-11-29 19:16:08 +00:00
|
|
|
assert read_json(datadir / "root_and_admin_have_keys.json")["ssh"]["rootKeys"] == [
|
|
|
|
"ssh-ed25519 KEY test@pc"
|
|
|
|
]
|
|
|
|
assert read_json(datadir / "root_and_admin_have_keys.json")["sshKeys"] == [
|
|
|
|
"ssh-rsa KEY test@pc"
|
|
|
|
]
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def some_users(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "some_users.json")
|
|
|
|
assert "users" in read_json(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
|
|
|
|
|
|
|
|
|
|
|
|
## TEST 401 ######################################################
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@pytest.mark.parametrize("endpoint", ["ssh/enable", "ssh/keys/user"])
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_unauthorized(client, ssh_off, endpoint):
|
|
|
|
response = client.post(f"/services/{endpoint}")
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@pytest.mark.parametrize("endpoint", ["ssh", "ssh/key/send"])
|
|
|
|
def test_unauthorized_put(client, ssh_off, endpoint):
|
|
|
|
response = client.put(f"/services/{endpoint}")
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
## TEST ENABLE ######################################################
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_legacy_enable(authorized_client, ssh_off):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.post("/services/ssh/enable")
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert read_json(ssh_off / "turned_off.json") == read_json(
|
|
|
|
ssh_off / "turned_on.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
def test_legacy_on_undefined(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.post("/services/ssh/enable")
|
|
|
|
assert response.status_code == 200
|
|
|
|
data = read_json(undefined_settings / "undefined.json")
|
|
|
|
assert data["ssh"]["enable"] == True
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_legacy_enable_when_enabled(authorized_client, ssh_on):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.post("/services/ssh/enable")
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert read_json(ssh_on / "turned_on.json") == read_json(ssh_on / "turned_on.json")
|
|
|
|
|
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
## GET ON /ssh ######################################################
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_get_current_settings_ssh_off(authorized_client, ssh_off):
|
|
|
|
response = authorized_client.get("/services/ssh")
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == {"enable": False, "passwordAuthentication": True}
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_current_settings_ssh_on(authorized_client, ssh_on):
|
|
|
|
response = authorized_client.get("/services/ssh")
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == {"enable": True, "passwordAuthentication": True}
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_current_settings_all_off(authorized_client, all_off):
|
|
|
|
response = authorized_client.get("/services/ssh")
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == {"enable": False, "passwordAuthentication": False}
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_current_settings_undefined(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.get("/services/ssh")
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == {"enable": True, "passwordAuthentication": True}
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
def test_get_current_settings_mostly_undefined(authorized_client, undefined_values):
|
|
|
|
response = authorized_client.get("/services/ssh")
|
|
|
|
assert response.status_code == 200
|
2023-11-21 20:14:42 +00:00
|
|
|
assert response.json() == {"enable": True, "passwordAuthentication": False}
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
## PUT ON /ssh ######################################################
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
available_settings = [
|
|
|
|
{"enable": True, "passwordAuthentication": True},
|
|
|
|
{"enable": True, "passwordAuthentication": False},
|
|
|
|
{"enable": False, "passwordAuthentication": True},
|
|
|
|
{"enable": False, "passwordAuthentication": False},
|
|
|
|
{"enable": True},
|
|
|
|
{"enable": False},
|
|
|
|
{"passwordAuthentication": True},
|
|
|
|
{"passwordAuthentication": False},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("settings", available_settings)
|
|
|
|
def test_set_settings_ssh_off(authorized_client, ssh_off, settings):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.put("/services/ssh", json=settings)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
data = read_json(ssh_off / "turned_off.json")["ssh"]
|
|
|
|
if "enable" in settings:
|
|
|
|
assert data["enable"] == settings["enable"]
|
|
|
|
if "passwordAuthentication" in settings:
|
|
|
|
assert data["passwordAuthentication"] == settings["passwordAuthentication"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("settings", available_settings)
|
|
|
|
def test_set_settings_ssh_on(authorized_client, ssh_on, settings):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.put("/services/ssh", json=settings)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
data = read_json(ssh_on / "turned_on.json")["ssh"]
|
|
|
|
if "enable" in settings:
|
|
|
|
assert data["enable"] == settings["enable"]
|
|
|
|
if "passwordAuthentication" in settings:
|
|
|
|
assert data["passwordAuthentication"] == settings["passwordAuthentication"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("settings", available_settings)
|
|
|
|
def test_set_settings_all_off(authorized_client, all_off, settings):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.put("/services/ssh", json=settings)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
data = read_json(all_off / "all_off.json")["ssh"]
|
|
|
|
if "enable" in settings:
|
|
|
|
assert data["enable"] == settings["enable"]
|
|
|
|
if "passwordAuthentication" in settings:
|
|
|
|
assert data["passwordAuthentication"] == settings["passwordAuthentication"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("settings", available_settings)
|
|
|
|
def test_set_settings_undefined(authorized_client, undefined_settings, settings):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.put("/services/ssh", json=settings)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
data = read_json(undefined_settings / "undefined.json")["ssh"]
|
|
|
|
if "enable" in settings:
|
|
|
|
assert data["enable"] == settings["enable"]
|
|
|
|
if "passwordAuthentication" in settings:
|
|
|
|
assert data["passwordAuthentication"] == settings["passwordAuthentication"]
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
## PUT ON /ssh/key/send ######################################################
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_add_root_key(authorized_client, ssh_on):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.put(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/key/send", json={"public_key": "ssh-rsa KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 201
|
|
|
|
assert read_json(ssh_on / "turned_on.json")["ssh"]["rootKeys"] == [
|
|
|
|
"ssh-rsa KEY test@pc",
|
|
|
|
]
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
def test_add_root_key_on_undefined(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.put(
|
|
|
|
"/services/ssh/key/send", json={"public_key": "ssh-rsa KEY test@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
|
|
data = read_json(undefined_settings / "undefined.json")
|
|
|
|
assert data["ssh"]["rootKeys"] == ["ssh-rsa KEY test@pc"]
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_add_root_key_one_more(authorized_client, root_and_admin_have_keys):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.put(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/key/send", json={"public_key": "ssh-rsa KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 201
|
2021-11-30 21:53:39 +00:00
|
|
|
assert read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")["ssh"][
|
|
|
|
"rootKeys"
|
|
|
|
] == [
|
2021-11-29 19:16:08 +00:00
|
|
|
"ssh-ed25519 KEY test@pc",
|
|
|
|
"ssh-rsa KEY test@pc",
|
|
|
|
]
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_add_existing_root_key(authorized_client, root_and_admin_have_keys):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.put(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/key/send", json={"public_key": "ssh-ed25519 KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 409
|
2021-11-30 21:53:39 +00:00
|
|
|
assert read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")["ssh"][
|
|
|
|
"rootKeys"
|
|
|
|
] == [
|
2021-11-29 19:16:08 +00:00
|
|
|
"ssh-ed25519 KEY test@pc",
|
|
|
|
]
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_add_invalid_root_key(authorized_client, ssh_on):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.put(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/key/send", json={"public_key": "INVALID KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 400
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
## /ssh/keys/{user} ######################################################
|
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_get_root_key(authorized_client, root_and_admin_have_keys):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.get("/services/ssh/keys/root")
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == ["ssh-ed25519 KEY test@pc"]
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_get_root_key_when_none(authorized_client, ssh_on):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.get("/services/ssh/keys/root")
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == []
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_root_key_on_undefined(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.get("/services/ssh/keys/root")
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == []
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_delete_root_key(authorized_client, root_and_admin_have_keys):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.delete(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/keys/root", json={"public_key": "ssh-ed25519 KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
2022-03-20 16:48:00 +00:00
|
|
|
assert (
|
2022-03-20 16:32:31 +00:00
|
|
|
"rootKeys"
|
2022-03-20 16:48:00 +00:00
|
|
|
not in read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")[
|
|
|
|
"ssh"
|
|
|
|
]
|
2022-08-25 17:03:56 +00:00
|
|
|
or read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")["ssh"][
|
|
|
|
"rootKeys"
|
|
|
|
]
|
|
|
|
== []
|
2022-03-20 16:48:00 +00:00
|
|
|
)
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
def test_delete_root_nonexistent_key(authorized_client, root_and_admin_have_keys):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.delete(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/keys/root", json={"public_key": "ssh-rsa KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 404
|
2021-11-30 21:53:39 +00:00
|
|
|
assert read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")["ssh"][
|
|
|
|
"rootKeys"
|
|
|
|
] == [
|
2021-11-29 19:16:08 +00:00
|
|
|
"ssh-ed25519 KEY test@pc",
|
|
|
|
]
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
def test_delete_root_key_on_undefined(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.delete(
|
|
|
|
"/services/ssh/keys/root", json={"public_key": "ssh-ed25519 KEY test@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|
2022-08-25 17:03:56 +00:00
|
|
|
assert "ssh" not in read_json(undefined_settings / "undefined.json")
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_get_admin_key(authorized_client, root_and_admin_have_keys):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.get("/services/ssh/keys/tester")
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == ["ssh-rsa KEY test@pc"]
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_get_admin_key_when_none(authorized_client, ssh_on):
|
2022-01-10 20:35:00 +00:00
|
|
|
response = authorized_client.get("/services/ssh/keys/tester")
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == []
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_delete_admin_key(authorized_client, root_and_admin_have_keys):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.delete(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/keys/tester", json={"public_key": "ssh-rsa KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 200
|
2021-11-30 21:53:39 +00:00
|
|
|
assert (
|
|
|
|
read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")["sshKeys"]
|
|
|
|
== []
|
|
|
|
)
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
|
2022-01-10 20:35:00 +00:00
|
|
|
def test_delete_nonexistent_admin_key(authorized_client, root_and_admin_have_keys):
|
|
|
|
response = authorized_client.delete(
|
|
|
|
"/services/ssh/keys/tester", json={"public_key": "ssh-rsa NO KEY test@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
|
|
assert read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")[
|
|
|
|
"sshKeys"
|
|
|
|
] == ["ssh-rsa KEY test@pc"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_admin_key_on_undefined(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.delete(
|
|
|
|
"/services/ssh/keys/tester", json={"public_key": "ssh-rsa KEY test@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|
2022-08-25 17:03:56 +00:00
|
|
|
assert "sshKeys" not in read_json(undefined_settings / "undefined.json")
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_add_admin_key(authorized_client, ssh_on):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.post(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/keys/tester", json={"public_key": "ssh-rsa KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 201
|
|
|
|
assert read_json(ssh_on / "turned_on.json")["sshKeys"] == [
|
|
|
|
"ssh-rsa KEY test@pc",
|
|
|
|
]
|
|
|
|
|
2021-11-30 21:53:39 +00:00
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
def test_add_admin_key_one_more(authorized_client, root_and_admin_have_keys):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.post(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/keys/tester", json={"public_key": "ssh-rsa KEY_2 test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 201
|
2021-11-30 21:53:39 +00:00
|
|
|
assert read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")[
|
|
|
|
"sshKeys"
|
|
|
|
] == ["ssh-rsa KEY test@pc", "ssh-rsa KEY_2 test@pc"]
|
|
|
|
|
2021-11-29 19:16:08 +00:00
|
|
|
|
|
|
|
def test_add_existing_admin_key(authorized_client, root_and_admin_have_keys):
|
2021-11-30 21:53:39 +00:00
|
|
|
response = authorized_client.post(
|
2022-01-10 20:35:00 +00:00
|
|
|
"/services/ssh/keys/tester", json={"public_key": "ssh-rsa KEY test@pc"}
|
2021-11-30 21:53:39 +00:00
|
|
|
)
|
2021-11-29 19:16:08 +00:00
|
|
|
assert response.status_code == 409
|
2021-11-30 21:53:39 +00:00
|
|
|
assert read_json(root_and_admin_have_keys / "root_and_admin_have_keys.json")[
|
|
|
|
"sshKeys"
|
|
|
|
] == [
|
2021-11-29 19:16:08 +00:00
|
|
|
"ssh-rsa KEY test@pc",
|
2021-11-30 21:53:39 +00:00
|
|
|
]
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_invalid_admin_key(authorized_client, ssh_on):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/services/ssh/keys/tester", json={"public_key": "INVALID KEY test@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("user", [1, 2, 3])
|
|
|
|
def test_get_user_key(authorized_client, some_users, user):
|
|
|
|
response = authorized_client.get(f"/services/ssh/keys/user{user}")
|
|
|
|
assert response.status_code == 200
|
|
|
|
if user == 1:
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == ["ssh-rsa KEY user1@pc"]
|
2022-01-10 20:35:00 +00:00
|
|
|
else:
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json() == []
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_keys_of_nonexistent_user(authorized_client, some_users):
|
|
|
|
response = authorized_client.get("/services/ssh/keys/user4")
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_keys_of_undefined_users(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.get("/services/ssh/keys/user1")
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("user", [1, 2, 3])
|
|
|
|
def test_add_user_key(authorized_client, some_users, user):
|
|
|
|
response = authorized_client.post(
|
|
|
|
f"/services/ssh/keys/user{user}", json={"public_key": "ssh-ed25519 KEY test@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
|
|
if user == 1:
|
|
|
|
assert read_json(some_users / "some_users.json")["users"][user - 1][
|
|
|
|
"sshKeys"
|
|
|
|
] == [
|
|
|
|
"ssh-rsa KEY user1@pc",
|
|
|
|
"ssh-ed25519 KEY test@pc",
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
assert read_json(some_users / "some_users.json")["users"][user - 1][
|
|
|
|
"sshKeys"
|
|
|
|
] == ["ssh-ed25519 KEY test@pc"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_existing_user_key(authorized_client, some_users):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/services/ssh/keys/user1", json={"public_key": "ssh-rsa KEY user1@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 409
|
|
|
|
assert read_json(some_users / "some_users.json")["users"][0]["sshKeys"] == [
|
|
|
|
"ssh-rsa KEY user1@pc",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_invalid_user_key(authorized_client, some_users):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/services/ssh/keys/user1", json={"public_key": "INVALID KEY user1@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_user_key(authorized_client, some_users):
|
|
|
|
response = authorized_client.delete(
|
|
|
|
"/services/ssh/keys/user1", json={"public_key": "ssh-rsa KEY user1@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert read_json(some_users / "some_users.json")["users"][0]["sshKeys"] == []
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("user", [2, 3])
|
|
|
|
def test_delete_nonexistent_user_key(authorized_client, some_users, user):
|
|
|
|
response = authorized_client.delete(
|
|
|
|
f"/services/ssh/keys/user{user}", json={"public_key": "ssh-rsa KEY user1@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|
2022-08-25 17:03:56 +00:00
|
|
|
if user == 2:
|
|
|
|
assert (
|
|
|
|
read_json(some_users / "some_users.json")["users"][user - 1]["sshKeys"]
|
|
|
|
== []
|
|
|
|
)
|
|
|
|
if user == 3:
|
|
|
|
"sshKeys" not in read_json(some_users / "some_users.json")["users"][user - 1]
|
2022-01-10 20:35:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_keys_of_nonexistent_user(authorized_client, some_users):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/services/ssh/keys/user4", json={"public_key": "ssh-rsa KEY user4@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_key_on_undefined_users(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/services/ssh/keys/user1", json={"public_key": "ssh-rsa KEY user4@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_keys_of_nonexistent_user(authorized_client, some_users):
|
|
|
|
response = authorized_client.delete(
|
|
|
|
"/services/ssh/keys/user4", json={"public_key": "ssh-rsa KEY user4@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_key_when_undefined_users(authorized_client, undefined_settings):
|
|
|
|
response = authorized_client.delete(
|
|
|
|
"/services/ssh/keys/user1", json={"public_key": "ssh-rsa KEY user1@pc"}
|
|
|
|
)
|
|
|
|
assert response.status_code == 404
|