diff --git a/tests/test_graphql/common.py b/tests/test_graphql/common.py new file mode 100644 index 0000000..f2cc54d --- /dev/null +++ b/tests/test_graphql/common.py @@ -0,0 +1,24 @@ +def assert_ok(response, request): + data = assert_data(response) + data[request]["success"] is True + data[request]["message"] is not None + data[request]["code"] == 200 + + +def assert_errorcode(response, request, code): + data = assert_data(response) + data[request]["success"] is False + data[request]["message"] is not None + data[request]["code"] == code + + +def assert_empty(response): + assert response.status_code == 200 + assert response.json().get("data") is None + + +def assert_data(response): + assert response.status_code == 200 + data = response.json().get("data") + assert data is not None + return data diff --git a/tests/test_graphql/test_api_devices.py b/tests/test_graphql/test_api_devices.py index fce99f7..3db8647 100644 --- a/tests/test_graphql/test_api_devices.py +++ b/tests/test_graphql/test_api_devices.py @@ -5,9 +5,15 @@ from tests.common import ( RECOVERY_KEY_VALIDATION_DATETIME, DEVICE_KEY_VALIDATION_DATETIME, NearFuture, - generate_api_query + generate_api_query, ) from tests.conftest import DEVICE_WE_AUTH_TESTS_WITH, TOKENS_FILE_CONTENTS +from tests.test_graphql.common import ( + assert_data, + assert_empty, + assert_ok, + assert_errorcode, +) ORIGINAL_DEVICES = TOKENS_FILE_CONTENTS["tokens"] @@ -59,32 +65,6 @@ def assert_original(client): assert device["isCaller"] is False -def assert_ok(response, request): - data = assert_data(response) - data[request]["success"] is True - data[request]["message"] is not None - data[request]["code"] == 200 - - -def assert_errorcode(response, request, code): - data = assert_data(response) - data[request]["success"] is False - data[request]["message"] is not None - data[request]["code"] == code - - -def assert_empty(response): - assert response.status_code == 200 - assert response.json().get("data") is None - - -def assert_data(response): - assert response.status_code == 200 - data = response.json().get("data") - assert data is not None - return data - - def set_client_token(client, token): client.headers.update({"Authorization": "Bearer " + token}) diff --git a/tests/test_graphql/test_api_recovery.py b/tests/test_graphql/test_api_recovery.py index 2cb824f..f34f12a 100644 --- a/tests/test_graphql/test_api_recovery.py +++ b/tests/test_graphql/test_api_recovery.py @@ -4,6 +4,7 @@ import datetime from tests.common import generate_api_query, mnemonic_to_hex, read_json, write_json +from tests.test_graphql.common import assert_empty API_RECOVERY_QUERY = """ recoveryKey { @@ -21,8 +22,7 @@ def test_graphql_recovery_key_status_unauthorized(client, tokens_file): "/graphql", json={"query": generate_api_query([API_RECOVERY_QUERY])}, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_recovery_key_status_when_none_exists(authorized_client, tokens_file): @@ -354,8 +354,7 @@ def test_graphql_generate_recovery_key_with_invalid_time_format( }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) assert "recovery_token" not in read_json(tokens_file) diff --git a/tests/test_graphql/test_ssh.py b/tests/test_graphql/test_ssh.py index 4831692..38c40f1 100644 --- a/tests/test_graphql/test_ssh.py +++ b/tests/test_graphql/test_ssh.py @@ -3,6 +3,7 @@ import pytest from tests.common import read_json +from tests.test_graphql.common import assert_empty class ProcessMock: @@ -70,8 +71,7 @@ def test_graphql_add_ssh_key_unauthorized(client, some_users, mock_subprocess_po }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_add_ssh_key(authorized_client, some_users, mock_subprocess_popen): @@ -227,8 +227,7 @@ def test_graphql_remove_ssh_key_unauthorized(client, some_users, mock_subprocess }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_remove_ssh_key(authorized_client, some_users, mock_subprocess_popen): diff --git a/tests/test_graphql/test_system.py b/tests/test_graphql/test_system.py index a021a16..5fdc06a 100644 --- a/tests/test_graphql/test_system.py +++ b/tests/test_graphql/test_system.py @@ -5,6 +5,7 @@ import os import pytest from tests.common import generate_system_query, read_json +from tests.test_graphql.common import assert_empty @pytest.fixture @@ -144,8 +145,7 @@ def test_graphql_get_python_version_wrong_auth( "query": generate_system_query([API_PYTHON_VERSION_INFO]), }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_get_python_version(authorized_client, mock_subprocess_check_output): @@ -181,8 +181,7 @@ def test_graphql_get_system_version_unauthorized( }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) assert mock_subprocess_check_output.call_count == 0 @@ -348,8 +347,7 @@ def test_graphql_get_timezone_unauthorized(client, turned_on): "query": generate_system_query([API_GET_TIMEZONE]), }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_get_timezone(authorized_client, turned_on): @@ -403,8 +401,7 @@ def test_graphql_change_timezone_unauthorized(client, turned_on): }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_change_timezone(authorized_client, turned_on): @@ -507,8 +504,7 @@ def test_graphql_get_auto_upgrade_unauthorized(client, turned_on): "query": generate_system_query([API_GET_AUTO_UPGRADE_SETTINGS_QUERY]), }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_get_auto_upgrade(authorized_client, turned_on): @@ -614,8 +610,7 @@ def test_graphql_change_auto_upgrade_unauthorized(client, turned_on): }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_change_auto_upgrade(authorized_client, turned_on): @@ -843,8 +838,7 @@ def test_graphql_pull_system_configuration_unauthorized(client, mock_subprocess_ }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) assert mock_subprocess_popen.call_count == 0 diff --git a/tests/test_graphql/test_users.py b/tests/test_graphql/test_users.py index 7a65736..503335d 100644 --- a/tests/test_graphql/test_users.py +++ b/tests/test_graphql/test_users.py @@ -6,6 +6,7 @@ from tests.common import ( generate_users_query, read_json, ) +from tests.test_graphql.common import assert_empty invalid_usernames = [ "messagebus", @@ -125,8 +126,7 @@ def test_graphql_get_users_unauthorized(client, some_users, mock_subprocess_pope "query": generate_users_query([API_USERS_INFO]), }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_get_some_users(authorized_client, some_users, mock_subprocess_popen): @@ -192,8 +192,7 @@ def test_graphql_get_one_user_unauthorized(client, one_user, mock_subprocess_pop }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_get_one_user(authorized_client, one_user, mock_subprocess_popen): @@ -321,8 +320,7 @@ def test_graphql_add_user_unauthorize(client, one_user, mock_subprocess_popen): }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_add_user(authorized_client, one_user, mock_subprocess_popen): @@ -570,8 +568,7 @@ def test_graphql_delete_user_unauthorized(client, some_users, mock_subprocess_po "variables": {"username": "user1"}, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_delete_user(authorized_client, some_users, mock_subprocess_popen): @@ -675,8 +672,7 @@ def test_graphql_update_user_unauthorized(client, some_users, mock_subprocess_po }, }, ) - assert response.status_code == 200 - assert response.json().get("data") is None + assert_empty(response) def test_graphql_update_user(authorized_client, some_users, mock_subprocess_popen):