2022-08-01 10:40:40 +00:00
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from tests.common import (
|
|
|
|
generate_users_query,
|
|
|
|
read_json,
|
|
|
|
)
|
2023-12-27 11:58:07 +00:00
|
|
|
from selfprivacy_api.utils import WriteUserData
|
2023-12-27 12:43:58 +00:00
|
|
|
from tests.test_graphql.common import (
|
|
|
|
assert_empty,
|
|
|
|
assert_errorcode,
|
|
|
|
assert_ok,
|
|
|
|
get_data,
|
|
|
|
)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
invalid_usernames = [
|
|
|
|
"messagebus",
|
|
|
|
"postfix",
|
|
|
|
"polkituser",
|
|
|
|
"dovecot2",
|
|
|
|
"dovenull",
|
|
|
|
"nginx",
|
|
|
|
"postgres",
|
|
|
|
"systemd-journal-gateway",
|
|
|
|
"prosody",
|
|
|
|
"systemd-network",
|
|
|
|
"systemd-resolve",
|
|
|
|
"systemd-timesync",
|
|
|
|
"opendkim",
|
|
|
|
"rspamd",
|
|
|
|
"sshd",
|
|
|
|
"selfprivacy-api",
|
|
|
|
"restic",
|
|
|
|
"redis",
|
|
|
|
"pleroma",
|
|
|
|
"ocserv",
|
|
|
|
"nextcloud",
|
|
|
|
"memcached",
|
|
|
|
"knot-resolver",
|
|
|
|
"gitea",
|
|
|
|
"bitwarden_rs",
|
|
|
|
"vaultwarden",
|
|
|
|
"acme",
|
|
|
|
"virtualMail",
|
|
|
|
"nixbld1",
|
|
|
|
"nixbld2",
|
|
|
|
"nixbld29",
|
|
|
|
"nobody",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
## FIXTURES ###################################################
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def no_users(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "no_users.json")
|
|
|
|
assert read_json(datadir / "no_users.json")["users"] == []
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def one_user(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "one_user.json")
|
|
|
|
assert read_json(datadir / "one_user.json")["users"] == [
|
|
|
|
{
|
|
|
|
"username": "user1",
|
|
|
|
"hashedPassword": "HASHED_PASSWORD_1",
|
|
|
|
"sshKeys": ["ssh-rsa KEY user1@pc"],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def undefined_settings(mocker, datadir):
|
|
|
|
mocker.patch("selfprivacy_api.utils.USERDATA_FILE", new=datadir / "undefined.json")
|
|
|
|
assert "users" not in read_json(datadir / "undefined.json")
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
2023-12-27 11:58:07 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def no_users_no_admin_nobody(undefined_settings):
|
|
|
|
datadir = undefined_settings
|
|
|
|
with WriteUserData() as data:
|
|
|
|
del data["username"]
|
|
|
|
del data["sshKeys"]
|
|
|
|
return datadir
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
## TESTS ######################################################
|
|
|
|
|
|
|
|
API_USERS_INFO = """
|
|
|
|
allUsers {
|
|
|
|
username
|
|
|
|
sshKeys
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_users_unauthorized(client, some_users, mock_subprocess_popen):
|
|
|
|
"""Test wrong auth"""
|
2022-08-25 17:03:56 +00:00
|
|
|
response = client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": generate_users_query([API_USERS_INFO]),
|
|
|
|
},
|
|
|
|
)
|
2023-01-06 09:54:07 +00:00
|
|
|
assert_empty(response)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_some_users(authorized_client, some_users, mock_subprocess_popen):
|
2022-08-25 17:03:56 +00:00
|
|
|
response = authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": generate_users_query([API_USERS_INFO]),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json().get("data") is not None
|
|
|
|
assert len(response.json()["data"]["users"]["allUsers"]) == 4
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][0]["username"] == "user1"
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][0]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY user1@pc"
|
|
|
|
]
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json()["data"]["users"]["allUsers"][1]["username"] == "user2"
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][1]["sshKeys"] == []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json()["data"]["users"]["allUsers"][3]["username"] == "tester"
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][3]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY test@pc"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_no_users(authorized_client, no_users, mock_subprocess_popen):
|
2022-08-25 17:03:56 +00:00
|
|
|
response = authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": generate_users_query([API_USERS_INFO]),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert len(response.json()["data"]["users"]["allUsers"]) == 1
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][0]["username"] == "tester"
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][0]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY test@pc"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-12-27 11:58:07 +00:00
|
|
|
def test_graphql_get_users_undefined_but_admin(authorized_client, undefined_settings):
|
2023-12-22 08:34:45 +00:00
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": generate_users_query([API_USERS_INFO]),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json().get("data") is not None
|
|
|
|
|
|
|
|
assert len(response.json()["data"]["users"]["allUsers"]) == 1
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][0]["username"] == "tester"
|
|
|
|
assert response.json()["data"]["users"]["allUsers"][0]["sshKeys"] == [
|
|
|
|
"ssh-rsa KEY test@pc"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-12-27 11:58:07 +00:00
|
|
|
def test_graphql_get_users_undefined_no_admin(
|
|
|
|
authorized_client, no_users_no_admin_nobody
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": generate_users_query([API_USERS_INFO]),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json().get("data") is not None
|
|
|
|
|
|
|
|
assert len(response.json()["data"]["users"]["allUsers"]) == 0
|
|
|
|
|
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
API_GET_USERS = """
|
|
|
|
query TestUsers($username: String!) {
|
|
|
|
users {
|
|
|
|
getUser(username: $username) {
|
|
|
|
sshKeys
|
|
|
|
username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_one_user_unauthorized(client, one_user, mock_subprocess_popen):
|
2022-08-25 17:03:56 +00:00
|
|
|
response = client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_GET_USERS,
|
|
|
|
"variables": {
|
|
|
|
"username": "user1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-01-06 09:54:07 +00:00
|
|
|
assert_empty(response)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_one_user(authorized_client, one_user, mock_subprocess_popen):
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
response = authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_GET_USERS,
|
|
|
|
"variables": {
|
|
|
|
"username": "user1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert len(response.json()["data"]["users"]["getUser"]) == 2
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["username"] == "user1"
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY user1@pc"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-12-22 08:34:45 +00:00
|
|
|
def test_graphql_get_some_user_undefined(authorized_client, undefined_settings):
|
|
|
|
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_GET_USERS,
|
|
|
|
"variables": {
|
|
|
|
"username": "user1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json().get("data") is not None
|
|
|
|
|
|
|
|
assert response.json()["data"]["users"]["getUser"] is None
|
|
|
|
|
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
def test_graphql_get_some_user(authorized_client, some_users, mock_subprocess_popen):
|
2022-08-25 17:03:56 +00:00
|
|
|
response = authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_GET_USERS,
|
|
|
|
"variables": {
|
|
|
|
"username": "user2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert len(response.json()["data"]["users"]["getUser"]) == 2
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["username"] == "user2"
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["sshKeys"] == []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_root_user(authorized_client, some_users, mock_subprocess_popen):
|
2022-08-25 17:03:56 +00:00
|
|
|
response = authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_GET_USERS,
|
|
|
|
"variables": {
|
|
|
|
"username": "root",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert len(response.json()["data"]["users"]["getUser"]) == 2
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["username"] == "root"
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-ed25519 KEY test@pc"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_main_user(authorized_client, one_user, mock_subprocess_popen):
|
2022-08-25 17:03:56 +00:00
|
|
|
response = authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_GET_USERS,
|
|
|
|
"variables": {
|
|
|
|
"username": "tester",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert len(response.json()["data"]["users"]["getUser"]) == 2
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["username"] == "tester"
|
|
|
|
assert response.json()["data"]["users"]["getUser"]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY test@pc"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_get_nonexistent_user(
|
|
|
|
authorized_client, one_user, mock_subprocess_popen
|
|
|
|
):
|
2022-08-25 17:03:56 +00:00
|
|
|
response = authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_GET_USERS,
|
|
|
|
"variables": {
|
|
|
|
"username": "tyler_durden",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
assert response.json()["data"]["users"]["getUser"] is None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
API_CREATE_USERS_MUTATION = """
|
|
|
|
mutation createUser($user: UserMutationInput!) {
|
2023-06-21 03:46:56 +00:00
|
|
|
users {
|
|
|
|
createUser(user: $user) {
|
|
|
|
success
|
|
|
|
message
|
|
|
|
code
|
|
|
|
user {
|
|
|
|
username
|
|
|
|
sshKeys
|
|
|
|
}
|
2022-08-01 10:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
def api_add_user_json(authorized_client, user_json: dict):
|
|
|
|
# lowlevel for deeper testing of edgecases
|
|
|
|
return authorized_client.post(
|
2022-08-01 10:40:40 +00:00
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_USERS_MUTATION,
|
|
|
|
"variables": {
|
2023-12-27 12:43:58 +00:00
|
|
|
"user": user_json,
|
2022-08-01 10:40:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
def api_add_user(authorized_client, username, password):
|
|
|
|
response = api_add_user_json(
|
|
|
|
authorized_client, {"username": username, "password": password}
|
2022-08-01 10:40:40 +00:00
|
|
|
)
|
2023-12-27 12:43:58 +00:00
|
|
|
output = get_data(response)["users"]["createUser"]
|
|
|
|
return output
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
def test_graphql_add_user_unauthorized(client, one_user, mock_subprocess_popen):
|
|
|
|
response = api_add_user_json(client, {"username": "user2", "password": "12345678"})
|
|
|
|
assert_empty(response)
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_user(authorized_client, one_user, mock_subprocess_popen):
|
|
|
|
output = api_add_user(authorized_client, "user2", password="12345678")
|
|
|
|
assert_ok(output, code=201)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
assert output["user"]["username"] == "user2"
|
|
|
|
assert output["user"]["sshKeys"] == []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
|
|
|
|
def test_graphql_add_user_when_undefined_settings(
|
2022-08-01 10:40:40 +00:00
|
|
|
authorized_client, undefined_settings, mock_subprocess_popen
|
|
|
|
):
|
2023-12-27 12:43:58 +00:00
|
|
|
output = api_add_user(authorized_client, "user2", password="12345678")
|
|
|
|
assert_ok(output, code=201)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
assert output["user"]["username"] == "user2"
|
|
|
|
assert output["user"]["sshKeys"] == []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
users_witn_empty_fields = [
|
|
|
|
{"username": "user2", "password": ""},
|
|
|
|
{"username": "", "password": "12345678"},
|
|
|
|
{"username": "", "password": ""},
|
|
|
|
]
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
@pytest.mark.parametrize("user_json", users_witn_empty_fields)
|
|
|
|
def test_graphql_add_with_empty_fields(authorized_client, one_user, user_json):
|
|
|
|
response = api_add_user_json(authorized_client, user_json)
|
|
|
|
output = get_data(response)["users"]["createUser"]
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
assert_errorcode(output, 400)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
assert output["user"] is None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
users_witn_undefined_fields = [
|
|
|
|
{"username": "user2"},
|
|
|
|
{"password": "12345678"},
|
|
|
|
{},
|
|
|
|
]
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-12-27 12:43:58 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("user_json", users_witn_undefined_fields)
|
|
|
|
def test_graphql_add_with_undefined_fields(authorized_client, one_user, user_json):
|
|
|
|
# checking that all fields are mandatory
|
|
|
|
response = api_add_user_json(authorized_client, user_json)
|
|
|
|
assert response.json()["errors"] is not None
|
|
|
|
assert response.json()["errors"] != []
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("username", invalid_usernames)
|
|
|
|
def test_graphql_add_system_username(
|
|
|
|
authorized_client, one_user, mock_subprocess_popen, username
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_USERS_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": username,
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["createUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["code"] == 409
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["createUser"]["user"] is None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_existing_user(authorized_client, one_user, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_USERS_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": "user1",
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["createUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["code"] == 409
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["createUser"]["user"]["username"] == "user1"
|
2022-08-01 10:40:40 +00:00
|
|
|
assert (
|
2023-06-21 03:46:56 +00:00
|
|
|
response.json()["data"]["users"]["createUser"]["user"]["sshKeys"][0]
|
2022-08-01 10:40:40 +00:00
|
|
|
== "ssh-rsa KEY user1@pc"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_main_user(authorized_client, one_user, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_USERS_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": "tester",
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["createUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["code"] == 409
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
assert (
|
2023-06-21 03:46:56 +00:00
|
|
|
response.json()["data"]["users"]["createUser"]["user"]["username"] == "tester"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
response.json()["data"]["users"]["createUser"]["user"]["sshKeys"][0]
|
2022-08-01 10:40:40 +00:00
|
|
|
== "ssh-rsa KEY test@pc"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_add_long_username(authorized_client, one_user, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_USERS_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": "a" * 32,
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
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"]["createUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["code"] == 400
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["createUser"]["user"] is None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("username", ["", "1", "фыр", "user1@", "^-^"])
|
|
|
|
def test_graphql_add_invalid_username(
|
|
|
|
authorized_client, one_user, mock_subprocess_popen, username
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_CREATE_USERS_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": username,
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["createUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["code"] == 400
|
|
|
|
assert response.json()["data"]["users"]["createUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["createUser"]["user"] is None
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
API_DELETE_USER_MUTATION = """
|
|
|
|
mutation deleteUser($username: String!) {
|
2023-06-21 03:46:56 +00:00
|
|
|
users {
|
|
|
|
deleteUser(username: $username) {
|
|
|
|
success
|
|
|
|
message
|
|
|
|
code
|
|
|
|
}
|
2022-08-01 10:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_delete_user_unauthorized(client, some_users, mock_subprocess_popen):
|
|
|
|
response = client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_DELETE_USER_MUTATION,
|
|
|
|
"variables": {"username": "user1"},
|
|
|
|
},
|
|
|
|
)
|
2023-01-06 09:54:07 +00:00
|
|
|
assert_empty(response)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_delete_user(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_DELETE_USER_MUTATION,
|
|
|
|
"variables": {"username": "user1"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["deleteUser"]["code"] == 200
|
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("username", ["", "def"])
|
|
|
|
def test_graphql_delete_nonexistent_users(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen, username
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_DELETE_USER_MUTATION,
|
|
|
|
"variables": {"username": username},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["deleteUser"]["code"] == 404
|
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("username", invalid_usernames)
|
|
|
|
def test_graphql_delete_system_users(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen, username
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_DELETE_USER_MUTATION,
|
|
|
|
"variables": {"username": username},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
assert (
|
2023-06-21 03:46:56 +00:00
|
|
|
response.json()["data"]["users"]["deleteUser"]["code"] == 404
|
|
|
|
or response.json()["data"]["users"]["deleteUser"]["code"] == 400
|
2022-08-01 10:40:40 +00:00
|
|
|
)
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_delete_main_user(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_DELETE_USER_MUTATION,
|
|
|
|
"variables": {"username": "tester"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["deleteUser"]["code"] == 400
|
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["deleteUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
API_UPDATE_USER_MUTATION = """
|
|
|
|
mutation updateUser($user: UserMutationInput!) {
|
2023-06-21 03:46:56 +00:00
|
|
|
users {
|
|
|
|
updateUser(user: $user) {
|
|
|
|
success
|
|
|
|
message
|
|
|
|
code
|
|
|
|
user {
|
|
|
|
username
|
|
|
|
sshKeys
|
|
|
|
}
|
2022-08-01 10:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_update_user_unauthorized(client, some_users, mock_subprocess_popen):
|
|
|
|
response = client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_UPDATE_USER_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": "user1",
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-01-06 09:54:07 +00:00
|
|
|
assert_empty(response)
|
2022-08-01 10:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_update_user(authorized_client, some_users, mock_subprocess_popen):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_UPDATE_USER_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": "user1",
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["updateUser"]["code"] == 200
|
|
|
|
assert response.json()["data"]["users"]["updateUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["updateUser"]["success"] is True
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["updateUser"]["user"]["username"] == "user1"
|
|
|
|
assert response.json()["data"]["users"]["updateUser"]["user"]["sshKeys"] == [
|
2022-08-01 10:40:40 +00:00
|
|
|
"ssh-rsa KEY user1@pc"
|
|
|
|
]
|
|
|
|
assert mock_subprocess_popen.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_graphql_update_nonexistent_user(
|
|
|
|
authorized_client, some_users, mock_subprocess_popen
|
|
|
|
):
|
|
|
|
response = authorized_client.post(
|
|
|
|
"/graphql",
|
|
|
|
json={
|
|
|
|
"query": API_UPDATE_USER_MUTATION,
|
|
|
|
"variables": {
|
|
|
|
"user": {
|
|
|
|
"username": "user666",
|
|
|
|
"password": "12345678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
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"]["updateUser"]["code"] == 404
|
|
|
|
assert response.json()["data"]["users"]["updateUser"]["message"] is not None
|
|
|
|
assert response.json()["data"]["users"]["updateUser"]["success"] is False
|
2022-08-01 10:40:40 +00:00
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
assert response.json()["data"]["users"]["updateUser"]["user"] is None
|
2022-08-01 10:40:40 +00:00
|
|
|
assert mock_subprocess_popen.call_count == 1
|