From e7c89e3e3fe6350a2d7497d1313eb748db75ed99 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 27 Dec 2023 14:31:50 +0000 Subject: [PATCH] test(users): delete a user and CHECK that it was deleted --- tests/test_graphql/test_users.py | 16 +++++++++++++++ tests/test_rest_endpoints/test_users.py | 10 ---------- tests/test_users.py | 26 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 tests/test_users.py diff --git a/tests/test_graphql/test_users.py b/tests/test_graphql/test_users.py index f3ba02d..99f5934 100644 --- a/tests/test_graphql/test_users.py +++ b/tests/test_graphql/test_users.py @@ -133,6 +133,17 @@ allUsers { """ +def api_all_users(authorized_client): + response = authorized_client.post( + "/graphql", + json={ + "query": generate_users_query([API_USERS_INFO]), + }, + ) + output = get_data(response)["users"]["allUsers"] + return output + + def test_graphql_get_users_unauthorized(client, some_users, mock_subprocess_popen): """Test wrong auth""" response = client.post( @@ -543,6 +554,11 @@ def test_graphql_delete_user(authorized_client, some_users, mock_subprocess_pope assert response.json()["data"]["users"]["deleteUser"]["message"] is not None assert response.json()["data"]["users"]["deleteUser"]["success"] is True + new_users = api_all_users(authorized_client) + assert len(new_users) == 3 + usernames = [user["username"] for user in new_users] + assert set(usernames) == set(["user2", "user3", "tester"]) + @pytest.mark.parametrize("username", ["", "def"]) def test_graphql_delete_nonexistent_users( diff --git a/tests/test_rest_endpoints/test_users.py b/tests/test_rest_endpoints/test_users.py index 89f4331..6123568 100644 --- a/tests/test_rest_endpoints/test_users.py +++ b/tests/test_rest_endpoints/test_users.py @@ -111,16 +111,6 @@ def mock_subprocess_popen(mocker): ## TESTS ###################################################### -# gql counterpart is too weak -def test_delete_user(authorized_client, some_users, mock_subprocess_popen): - response = authorized_client.delete("/users/user1") - assert response.status_code == 200 - assert read_json(some_users / "some_users.json")["users"] == [ - {"username": "user2", "hashedPassword": "HASHED_PASSWORD_2", "sshKeys": []}, - {"username": "user3", "hashedPassword": "HASHED_PASSWORD_3"}, - ] - - def test_delete_main_user(authorized_client, some_users, mock_subprocess_popen): response = authorized_client.delete("/users/tester") assert response.status_code == 400 diff --git a/tests/test_users.py b/tests/test_users.py new file mode 100644 index 0000000..2f613db --- /dev/null +++ b/tests/test_users.py @@ -0,0 +1,26 @@ +from selfprivacy_api.utils import ReadUserData, WriteUserData +from selfprivacy_api.actions.users import delete_user +""" + A place for user storage tests and other user tests that are not Graphql-specific. +""" + +# yes it is an incomplete suite. +# It was born in order to not lose things that REST API tests checked for +# In the future, user storage tests that are not dependent on actual API (graphql or otherwise) go here. + +def test_delete_user_writes_json(generic_userdata): + delete_user("user2") + with ReadUserData() as data: + assert data["users"] == [ + { + "username": "user1", + "hashedPassword": "HASHED_PASSWORD_1", + "sshKeys": ["ssh-rsa KEY user1@pc"] + }, + { + "username": "user3", + "hashedPassword": "HASHED_PASSWORD_3", + "sshKeys": ["ssh-rsa KEY user3@pc"] + } + ] +