From 3a404204537558c6f420d53555290d7fc1a55bd7 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 25 Jul 2022 19:30:54 +0200 Subject: [PATCH] fix tests +del import --- .../graphql/mutations/users_mutations.py | 11 +-- selfprivacy_api/graphql/schema.py | 2 +- tests/test_graphql/test_api.py | 1 - tests/test_graphql/test_api_recovery.py | 3 - tests/test_graphql/test_system.py | 8 +- tests/test_graphql/test_users.py | 74 +++++++++++-------- 6 files changed, 52 insertions(+), 47 deletions(-) diff --git a/selfprivacy_api/graphql/mutations/users_mutations.py b/selfprivacy_api/graphql/mutations/users_mutations.py index d145cda..06622e0 100644 --- a/selfprivacy_api/graphql/mutations/users_mutations.py +++ b/selfprivacy_api/graphql/mutations/users_mutations.py @@ -2,12 +2,9 @@ """Users management module""" # pylint: disable=too-few-public-methods import re -import subprocess -import typing import strawberry from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql.common_types.user import ( - User, UserMutationReturn, get_user_by_username, ) @@ -23,7 +20,7 @@ from selfprivacy_api.utils import hash_password @strawberry.input -class UserMutationsInput: +class UserMutationInput: """Input type for user mutation""" username: str @@ -35,7 +32,7 @@ class UserMutations: """Mutations change user settings""" @strawberry.mutation(permission_classes=[IsAuthenticated]) - def create_user(self, user: UserMutationsInput) -> UserMutationReturn: + def create_user(self, user: UserMutationInput) -> UserMutationReturn: """Create a new user""" hashed_password = hash_password(user.password) @@ -133,12 +130,11 @@ class UserMutations: ) @strawberry.mutation(permission_classes=[IsAuthenticated]) - def update_user(self, user: UserMutationsInput) -> UserMutationReturn: + def update_user(self, user: UserMutationInput) -> UserMutationReturn: """Update user mutation""" hashed_password = hash_password(user.password) with WriteUserData() as data: - ssh_keys = [] if user.username == data["username"]: data["hashedMasterPassword"] = hashed_password @@ -147,7 +143,6 @@ class UserMutations: for data_user in data["users"]: if data_user["username"] == user.username: data_user["hashedPassword"] = hashed_password - ssh_keys = data_user["sshKeys"] break else: return UserMutationReturn( diff --git a/selfprivacy_api/graphql/schema.py b/selfprivacy_api/graphql/schema.py index c0f2858..3a623f5 100644 --- a/selfprivacy_api/graphql/schema.py +++ b/selfprivacy_api/graphql/schema.py @@ -27,7 +27,7 @@ class Query: """API access status""" return Api() - @strawberry.field + @strawberry.field(permission_classes=[IsAuthenticated]) def users(self) -> Users: """Users queries""" return Users() diff --git a/tests/test_graphql/test_api.py b/tests/test_graphql/test_api.py index 031e052..6343d8f 100644 --- a/tests/test_graphql/test_api.py +++ b/tests/test_graphql/test_api.py @@ -1,7 +1,6 @@ # pylint: disable=redefined-outer-name # pylint: disable=unused-argument # pylint: disable=missing-function-docstring -import pytest from tests.common import generate_api_query from tests.test_graphql.test_api_devices import API_DEVICES_QUERY diff --git a/tests/test_graphql/test_api_recovery.py b/tests/test_graphql/test_api_recovery.py index 2d1e16a..be0fdff 100644 --- a/tests/test_graphql/test_api_recovery.py +++ b/tests/test_graphql/test_api_recovery.py @@ -1,9 +1,6 @@ # pylint: disable=redefined-outer-name # pylint: disable=unused-argument # pylint: disable=missing-function-docstring -import json -from time import strftime -import pytest import datetime from tests.common import generate_api_query, mnemonic_to_hex, read_json, write_json diff --git a/tests/test_graphql/test_system.py b/tests/test_graphql/test_system.py index d5cf6e6..bba6918 100644 --- a/tests/test_graphql/test_system.py +++ b/tests/test_graphql/test_system.py @@ -1,12 +1,10 @@ # pylint: disable=redefined-outer-name # pylint: disable=unused-argument # pylint: disable=missing-function-docstring -import json import os import pytest -import datetime -from tests.common import generate_system_query, read_json, write_json +from tests.common import generate_system_query, read_json @pytest.fixture @@ -56,7 +54,7 @@ class ProcessMock: self.args = args self.kwargs = kwargs - def communicate(): + def communicate(): # pylint: disable=no-method-argument return (b"", None) returncode = 0 @@ -65,7 +63,7 @@ class ProcessMock: class BrokenServiceMock(ProcessMock): """Mock subprocess.Popen for broken service""" - def communicate(): + def communicate(): # pylint: disable=no-method-argument return (b"Testing error", None) returncode = 3 diff --git a/tests/test_graphql/test_users.py b/tests/test_graphql/test_users.py index 4490f2b..5b19890 100644 --- a/tests/test_graphql/test_users.py +++ b/tests/test_graphql/test_users.py @@ -1,13 +1,10 @@ # pylint: disable=redefined-outer-name # pylint: disable=unused-argument -import json import pytest from tests.common import ( - generate_system_query, generate_users_query, read_json, - write_json, ) invalid_usernames = [ @@ -99,7 +96,7 @@ class ProcessMock: self.args = args self.kwargs = kwargs - def communicate(): + def communicate(): # pylint: disable=no-method-argument return (b"NEW_HASHED", None) returncode = 0 @@ -175,19 +172,22 @@ def test_graphql_get_no_users(authorized_client, no_users, mock_subprocess_popen API_GET_USERS = """ -getUser($username: String) { - getUser(username: $username) { - user +query TestUsers($username: String!) { + users { + getUser(username: $username) { + sshKeys + username + } } } """ def test_graphql_get_one_user_unauthorized(client, one_user, mock_subprocess_popen): - response = client.post( + response = client.get( "/graphql", json={ - "query": generate_users_query([API_GET_USERS]), + "query": API_GET_USERS, "variables": { "username": "user1", }, @@ -198,10 +198,11 @@ def test_graphql_get_one_user_unauthorized(client, one_user, mock_subprocess_pop def test_graphql_get_one_user(authorized_client, one_user, mock_subprocess_popen): + response = authorized_client.get( "/graphql", json={ - "query": generate_users_query([API_GET_USERS]), + "query": API_GET_USERS, "variables": { "username": "user1", }, @@ -210,16 +211,18 @@ def test_graphql_get_one_user(authorized_client, one_user, mock_subprocess_popen assert response.status_code == 200 assert response.json.get("data") is not None - # assert len(response.json["data"]["users"]["getUser"]) == 1 - # assert response.json["data"]["users"]["getUser"][0]["username"] == "user1" - # assert response.json["data"]["users"]["getUser"][0]["sshKeys"] == ["ssh-rsa KEY user1@pc"] + assert len(response.json["data"]["users"]["getUser"]) == 2 + assert response.json["data"]["users"]["getUser"]["username"] == "user1" + assert response.json["data"]["users"]["getUser"]["sshKeys"] == [ + "ssh-rsa KEY user1@pc" + ] -def test_graphql_get_some_user(authorized_client, some_user, mock_subprocess_popen): +def test_graphql_get_some_user(authorized_client, some_users, mock_subprocess_popen): response = authorized_client.get( "/graphql", json={ - "query": generate_users_query([API_GET_USERS]), + "query": API_GET_USERS, "variables": { "username": "user2", }, @@ -228,16 +231,16 @@ def test_graphql_get_some_user(authorized_client, some_user, mock_subprocess_pop assert response.status_code == 200 assert response.json.get("data") is not None - # assert len(response.json["data"]["users"]["getUser"]) == 1 - # assert response.json["data"]["users"]["getUser"][0]["username"] == "user1" - # assert response.json["data"]["users"]["getUser"][0]["sshKeys"] == ["ssh-rsa KEY user1@pc"] + assert len(response.json["data"]["users"]["getUser"]) == 2 + assert response.json["data"]["users"]["getUser"]["username"] == "user2" + assert response.json["data"]["users"]["getUser"]["sshKeys"] == [] - -def test_graphql_get_root_user(authorized_client, one_user, mock_subprocess_popen): +# fail +def test_graphql_get_root_user(authorized_client, some_users, mock_subprocess_popen): response = authorized_client.get( "/graphql", json={ - "query": generate_users_query([API_GET_USERS]), + "query": API_GET_USERS, "variables": { "username": "root", }, @@ -246,24 +249,33 @@ def test_graphql_get_root_user(authorized_client, one_user, mock_subprocess_pope assert response.status_code == 200 assert response.json.get("data") is not None + assert len(response.json["data"]["users"]["getUser"]) == 2 + assert response.json["data"]["users"]["getUser"]["username"] == "root" + assert response.json["data"]["users"]["getUser"]["sshKeys"] == [] + + def test_graphql_get_main_user(authorized_client, one_user, mock_subprocess_popen): response = authorized_client.get( "/graphql", json={ - "query": generate_users_query([API_GET_USERS]), + "query": API_GET_USERS, "variables": { - "username": "tester", # ssh-rsa KEY test@pc + "username": "tester", }, }, ) assert response.status_code == 200 assert response.json.get("data") is not None + assert len(response.json["data"]["users"]["getUser"]) == 2 + assert response.json["data"]["users"]["getUser"]["username"] == "tester" + assert response.json["data"]["users"]["getUser"]["sshKeys"] == ["ssh-rsa KEY test@pc"] + API_CHANGE_USERS_MUTATION = """ -mutation createUser($username: String, $password: String) { - createUser(username: $username, password: $password) { +mutation createUser($user: UserMutationInput!) { + createUser(user: $user) { success message code @@ -279,8 +291,10 @@ def test_graphql_add_user_unauthorize(client, one_user, mock_subprocess_popen): json={ "query": API_CHANGE_USERS_MUTATION, "variables": { - "username": "user1", - "password": "12345678", + "user": { + "username": "user1", + "password": "12345678", + }, }, }, ) @@ -294,8 +308,10 @@ def test_graphql_add_usere(authorized_client, one_user, mock_subprocess_popen): json={ "query": API_CHANGE_USERS_MUTATION, "variables": { - "username": "user1", - "password": "12345678", + "user": { + "username": "user1", + "password": "12345678", + }, }, }, )