fix tests +del import
parent
ba7ea75fd5
commit
3a40420453
|
@ -2,12 +2,9 @@
|
||||||
"""Users management module"""
|
"""Users management module"""
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
import re
|
import re
|
||||||
import subprocess
|
|
||||||
import typing
|
|
||||||
import strawberry
|
import strawberry
|
||||||
from selfprivacy_api.graphql import IsAuthenticated
|
from selfprivacy_api.graphql import IsAuthenticated
|
||||||
from selfprivacy_api.graphql.common_types.user import (
|
from selfprivacy_api.graphql.common_types.user import (
|
||||||
User,
|
|
||||||
UserMutationReturn,
|
UserMutationReturn,
|
||||||
get_user_by_username,
|
get_user_by_username,
|
||||||
)
|
)
|
||||||
|
@ -23,7 +20,7 @@ from selfprivacy_api.utils import hash_password
|
||||||
|
|
||||||
|
|
||||||
@strawberry.input
|
@strawberry.input
|
||||||
class UserMutationsInput:
|
class UserMutationInput:
|
||||||
"""Input type for user mutation"""
|
"""Input type for user mutation"""
|
||||||
|
|
||||||
username: str
|
username: str
|
||||||
|
@ -35,7 +32,7 @@ class UserMutations:
|
||||||
"""Mutations change user settings"""
|
"""Mutations change user settings"""
|
||||||
|
|
||||||
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
||||||
def create_user(self, user: UserMutationsInput) -> UserMutationReturn:
|
def create_user(self, user: UserMutationInput) -> UserMutationReturn:
|
||||||
"""Create a new user"""
|
"""Create a new user"""
|
||||||
|
|
||||||
hashed_password = hash_password(user.password)
|
hashed_password = hash_password(user.password)
|
||||||
|
@ -133,12 +130,11 @@ class UserMutations:
|
||||||
)
|
)
|
||||||
|
|
||||||
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
||||||
def update_user(self, user: UserMutationsInput) -> UserMutationReturn:
|
def update_user(self, user: UserMutationInput) -> UserMutationReturn:
|
||||||
"""Update user mutation"""
|
"""Update user mutation"""
|
||||||
hashed_password = hash_password(user.password)
|
hashed_password = hash_password(user.password)
|
||||||
|
|
||||||
with WriteUserData() as data:
|
with WriteUserData() as data:
|
||||||
ssh_keys = []
|
|
||||||
if user.username == data["username"]:
|
if user.username == data["username"]:
|
||||||
data["hashedMasterPassword"] = hashed_password
|
data["hashedMasterPassword"] = hashed_password
|
||||||
|
|
||||||
|
@ -147,7 +143,6 @@ class UserMutations:
|
||||||
for data_user in data["users"]:
|
for data_user in data["users"]:
|
||||||
if data_user["username"] == user.username:
|
if data_user["username"] == user.username:
|
||||||
data_user["hashedPassword"] = hashed_password
|
data_user["hashedPassword"] = hashed_password
|
||||||
ssh_keys = data_user["sshKeys"]
|
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
return UserMutationReturn(
|
return UserMutationReturn(
|
||||||
|
|
|
@ -27,7 +27,7 @@ class Query:
|
||||||
"""API access status"""
|
"""API access status"""
|
||||||
return Api()
|
return Api()
|
||||||
|
|
||||||
@strawberry.field
|
@strawberry.field(permission_classes=[IsAuthenticated])
|
||||||
def users(self) -> Users:
|
def users(self) -> Users:
|
||||||
"""Users queries"""
|
"""Users queries"""
|
||||||
return Users()
|
return Users()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# pylint: disable=redefined-outer-name
|
# pylint: disable=redefined-outer-name
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
# pylint: disable=missing-function-docstring
|
# pylint: disable=missing-function-docstring
|
||||||
import pytest
|
|
||||||
|
|
||||||
from tests.common import generate_api_query
|
from tests.common import generate_api_query
|
||||||
from tests.test_graphql.test_api_devices import API_DEVICES_QUERY
|
from tests.test_graphql.test_api_devices import API_DEVICES_QUERY
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
# pylint: disable=redefined-outer-name
|
# pylint: disable=redefined-outer-name
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
# pylint: disable=missing-function-docstring
|
# pylint: disable=missing-function-docstring
|
||||||
import json
|
|
||||||
from time import strftime
|
|
||||||
import pytest
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from tests.common import generate_api_query, mnemonic_to_hex, read_json, write_json
|
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=redefined-outer-name
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
# pylint: disable=missing-function-docstring
|
# pylint: disable=missing-function-docstring
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import pytest
|
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
|
@pytest.fixture
|
||||||
|
@ -56,7 +54,7 @@ class ProcessMock:
|
||||||
self.args = args
|
self.args = args
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
|
|
||||||
def communicate():
|
def communicate(): # pylint: disable=no-method-argument
|
||||||
return (b"", None)
|
return (b"", None)
|
||||||
|
|
||||||
returncode = 0
|
returncode = 0
|
||||||
|
@ -65,7 +63,7 @@ class ProcessMock:
|
||||||
class BrokenServiceMock(ProcessMock):
|
class BrokenServiceMock(ProcessMock):
|
||||||
"""Mock subprocess.Popen for broken service"""
|
"""Mock subprocess.Popen for broken service"""
|
||||||
|
|
||||||
def communicate():
|
def communicate(): # pylint: disable=no-method-argument
|
||||||
return (b"Testing error", None)
|
return (b"Testing error", None)
|
||||||
|
|
||||||
returncode = 3
|
returncode = 3
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
# pylint: disable=redefined-outer-name
|
# pylint: disable=redefined-outer-name
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
import json
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
generate_system_query,
|
|
||||||
generate_users_query,
|
generate_users_query,
|
||||||
read_json,
|
read_json,
|
||||||
write_json,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
invalid_usernames = [
|
invalid_usernames = [
|
||||||
|
@ -99,7 +96,7 @@ class ProcessMock:
|
||||||
self.args = args
|
self.args = args
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
|
|
||||||
def communicate():
|
def communicate(): # pylint: disable=no-method-argument
|
||||||
return (b"NEW_HASHED", None)
|
return (b"NEW_HASHED", None)
|
||||||
|
|
||||||
returncode = 0
|
returncode = 0
|
||||||
|
@ -175,19 +172,22 @@ def test_graphql_get_no_users(authorized_client, no_users, mock_subprocess_popen
|
||||||
|
|
||||||
|
|
||||||
API_GET_USERS = """
|
API_GET_USERS = """
|
||||||
getUser($username: String) {
|
query TestUsers($username: String!) {
|
||||||
getUser(username: $username) {
|
users {
|
||||||
user
|
getUser(username: $username) {
|
||||||
|
sshKeys
|
||||||
|
username
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def test_graphql_get_one_user_unauthorized(client, one_user, mock_subprocess_popen):
|
def test_graphql_get_one_user_unauthorized(client, one_user, mock_subprocess_popen):
|
||||||
response = client.post(
|
response = client.get(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={
|
json={
|
||||||
"query": generate_users_query([API_GET_USERS]),
|
"query": API_GET_USERS,
|
||||||
"variables": {
|
"variables": {
|
||||||
"username": "user1",
|
"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):
|
def test_graphql_get_one_user(authorized_client, one_user, mock_subprocess_popen):
|
||||||
|
|
||||||
response = authorized_client.get(
|
response = authorized_client.get(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={
|
json={
|
||||||
"query": generate_users_query([API_GET_USERS]),
|
"query": API_GET_USERS,
|
||||||
"variables": {
|
"variables": {
|
||||||
"username": "user1",
|
"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.status_code == 200
|
||||||
assert response.json.get("data") is not None
|
assert response.json.get("data") is not None
|
||||||
|
|
||||||
# assert len(response.json["data"]["users"]["getUser"]) == 1
|
assert len(response.json["data"]["users"]["getUser"]) == 2
|
||||||
# assert response.json["data"]["users"]["getUser"][0]["username"] == "user1"
|
assert response.json["data"]["users"]["getUser"]["username"] == "user1"
|
||||||
# assert response.json["data"]["users"]["getUser"][0]["sshKeys"] == ["ssh-rsa KEY user1@pc"]
|
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(
|
response = authorized_client.get(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={
|
json={
|
||||||
"query": generate_users_query([API_GET_USERS]),
|
"query": API_GET_USERS,
|
||||||
"variables": {
|
"variables": {
|
||||||
"username": "user2",
|
"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.status_code == 200
|
||||||
assert response.json.get("data") is not None
|
assert response.json.get("data") is not None
|
||||||
|
|
||||||
# assert len(response.json["data"]["users"]["getUser"]) == 1
|
assert len(response.json["data"]["users"]["getUser"]) == 2
|
||||||
# assert response.json["data"]["users"]["getUser"][0]["username"] == "user1"
|
assert response.json["data"]["users"]["getUser"]["username"] == "user2"
|
||||||
# assert response.json["data"]["users"]["getUser"][0]["sshKeys"] == ["ssh-rsa KEY user1@pc"]
|
assert response.json["data"]["users"]["getUser"]["sshKeys"] == []
|
||||||
|
|
||||||
|
# fail
|
||||||
def test_graphql_get_root_user(authorized_client, one_user, mock_subprocess_popen):
|
def test_graphql_get_root_user(authorized_client, some_users, mock_subprocess_popen):
|
||||||
response = authorized_client.get(
|
response = authorized_client.get(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={
|
json={
|
||||||
"query": generate_users_query([API_GET_USERS]),
|
"query": API_GET_USERS,
|
||||||
"variables": {
|
"variables": {
|
||||||
"username": "root",
|
"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.status_code == 200
|
||||||
assert response.json.get("data") is not None
|
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):
|
def test_graphql_get_main_user(authorized_client, one_user, mock_subprocess_popen):
|
||||||
response = authorized_client.get(
|
response = authorized_client.get(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={
|
json={
|
||||||
"query": generate_users_query([API_GET_USERS]),
|
"query": API_GET_USERS,
|
||||||
"variables": {
|
"variables": {
|
||||||
"username": "tester", # ssh-rsa KEY test@pc
|
"username": "tester",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json.get("data") is not None
|
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 = """
|
API_CHANGE_USERS_MUTATION = """
|
||||||
mutation createUser($username: String, $password: String) {
|
mutation createUser($user: UserMutationInput!) {
|
||||||
createUser(username: $username, password: $password) {
|
createUser(user: $user) {
|
||||||
success
|
success
|
||||||
message
|
message
|
||||||
code
|
code
|
||||||
|
@ -279,8 +291,10 @@ def test_graphql_add_user_unauthorize(client, one_user, mock_subprocess_popen):
|
||||||
json={
|
json={
|
||||||
"query": API_CHANGE_USERS_MUTATION,
|
"query": API_CHANGE_USERS_MUTATION,
|
||||||
"variables": {
|
"variables": {
|
||||||
"username": "user1",
|
"user": {
|
||||||
"password": "12345678",
|
"username": "user1",
|
||||||
|
"password": "12345678",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -294,8 +308,10 @@ def test_graphql_add_usere(authorized_client, one_user, mock_subprocess_popen):
|
||||||
json={
|
json={
|
||||||
"query": API_CHANGE_USERS_MUTATION,
|
"query": API_CHANGE_USERS_MUTATION,
|
||||||
"variables": {
|
"variables": {
|
||||||
"username": "user1",
|
"user": {
|
||||||
"password": "12345678",
|
"username": "user1",
|
||||||
|
"password": "12345678",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue