fix tests +del import
parent
ba7ea75fd5
commit
3a40420453
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue