fix: tests

This commit is contained in:
Inex Code 2024-07-30 16:39:05 +03:00
parent d97a3954f0
commit 158b4ca50d
3 changed files with 420 additions and 418 deletions

View file

@ -145,7 +145,7 @@ def check_if_subdomain_is_taken(subdomain: str) -> bool:
with ReadUserData() as data: with ReadUserData() as data:
for module in data["modules"]: for module in data["modules"]:
if ( if (
data["modules"][module].get("subdomain", DEFAULT_SUBDOMAINS[module]) data["modules"][module].get("subdomain", DEFAULT_SUBDOMAINS.get(module, ""))
== subdomain == subdomain
): ):
return True return True

View file

@ -66,7 +66,7 @@
"roundcube": { "roundcube": {
"enable": true "enable": true
}, },
"prometheus": { "monitoring": {
"enable": true, "enable": true,
"location": "sdb" "location": "sdb"
} }

View file

@ -2,468 +2,470 @@
# pylint: disable=unused-argument # pylint: disable=unused-argument
# pylint: disable=missing-function-docstring # pylint: disable=missing-function-docstring
from datetime import datetime # TODO(def): Finish this please.
import pytest
from selfprivacy_api.models.services import ServiceStatus # from datetime import datetime
# import pytest
from tests.test_graphql.common import ( # from selfprivacy_api.models.services import ServiceStatus
assert_empty,
get_data, # from tests.test_graphql.common import (
) # assert_empty,
# get_data,
# )
@pytest.fixture # @pytest.fixture
def mock_get_status_active(mocker): # def mock_get_status_active(mocker):
mock = mocker.patch( # mock = mocker.patch(
"selfprivacy_api.graphql.queries.monitoring.Prometheus.get_status", # "selfprivacy_api.graphql.queries.monitoring.Prometheus.get_status",
return_value=ServiceStatus.ACTIVE, # return_value=ServiceStatus.ACTIVE,
) # )
return mock # return mock
@pytest.fixture # @pytest.fixture
def mock_send_query(mocker): # def mock_send_query(mocker):
mock = mocker.patch( # mock = mocker.patch(
"selfprivacy_api.utils.monitoring.MonitoringQueries._send_range_query", # "selfprivacy_api.utils.monitoring.MonitoringQueries._send_range_query",
# "selfprivacy_api.graphql.queries.monitoring._send_query", # # "selfprivacy_api.graphql.queries.monitoring._send_query",
return_value=["test result"], # return_value=["test result"],
) # )
return mock # return mock
# .... # # ....
CPU_USAGE_QUERY = """ # CPU_USAGE_QUERY = """
query { # query {
monitoring { # monitoring {
cpuUsage { # cpuUsage {
start # start
end # end
step # step
overallUsage { # overallUsage {
... on MonitoringValues { # ... on MonitoringValues {
values { # values {
timestamp # timestamp
value # value
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
} # }
} # }
} # }
""" # """
CPU_USAGE_QUERY_WITH_OPTIONS = """ # CPU_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: String!, $start: String!, $step: String!) { # query Query($end: String!, $start: String!, $step: String!) {
monitoring { # monitoring {
cpuUsage(end: $end, start: $start, step: $step) { # cpuUsage(end: $end, start: $start, step: $step) {
end # end
overallUsage { # overallUsage {
... on MonitoringValues { # ... on MonitoringValues {
values { # values {
timestamp # timestamp
value # value
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
start # start
step # step
} # }
} # }
} # }
""" # """
MEMORY_USAGE_QUERY = """ # MEMORY_USAGE_QUERY = """
query Query { # query Query {
monitoring { # monitoring {
memoryUsage { # memoryUsage {
averageUsageByService { # averageUsageByService {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
end # end
maxUsageByService { # maxUsageByService {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
overallUsage { # overallUsage {
... on MonitoringValues { # ... on MonitoringValues {
values { # values {
timestamp # timestamp
value # value
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
start # start
step # step
} # }
} # }
} # }
""" # """
MEMORY_USAGE_QUERY_WITH_OPTIONS = """ # MEMORY_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: String!, $start: String!, $step: String!) { # query Query($end: String!, $start: String!, $step: String!) {
monitoring { # monitoring {
memoryUsage(end: $end, start: $start, step: $step) { # memoryUsage(end: $end, start: $start, step: $step) {
averageUsageByService { # averageUsageByService {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
end # end
maxUsageByService { # maxUsageByService {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
overallUsage { # overallUsage {
... on MonitoringValues { # ... on MonitoringValues {
values { # values {
timestamp # timestamp
value # value
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
start # start
step # step
} # }
} # }
} # }
""" # """
NETWORK_USAGE_QUERY = """ # NETWORK_USAGE_QUERY = """
query Query { # query Query {
monitoring { # monitoring {
networkUsage { # networkUsage {
end # end
start # start
step # step
overallUsage { # overallUsage {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
} # }
} # }
} # }
""" # """
NETWORK_USAGE_QUERY_WITH_OPTIONS = """ # NETWORK_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: String!, $start: String!, $step: String!) { # query Query($end: String!, $start: String!, $step: String!) {
monitoring { # monitoring {
networkUsage(end: $end, start: $start, step: $step) { # networkUsage(end: $end, start: $start, step: $step) {
end # end
overallUsage { # overallUsage {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
start # start
step # step
} # }
} # }
} # }
""" # """
DISK_USAGE_QUERY = """ # DISK_USAGE_QUERY = """
query Query { # query Query {
monitoring { # monitoring {
diskUsage { # diskUsage {
__typename # __typename
start # start
end # end
step # step
overallUsage { # overallUsage {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
} # }
} # }
} # }
""" # """
DISK_USAGE_QUERY_WITH_OPTIONS = """ # DISK_USAGE_QUERY_WITH_OPTIONS = """
query Query($end: String!, $start: String!, $step: String!) { # query Query($end: String!, $start: String!, $step: String!) {
monitoring { # monitoring {
diskUsage(end: $end, start: $start, step: $step) { # diskUsage(end: $end, start: $start, step: $step) {
end # end
overallUsage { # overallUsage {
... on MonitoringMetrics { # ... on MonitoringMetrics {
metrics { # metrics {
metricId # metricId
values { # values {
timestamp # timestamp
value # value
} # }
} # }
} # }
... on MonitoringQueryError { # ... on MonitoringQueryError {
error # error
} # }
} # }
start # start
step # step
} # }
} # }
} # }
""" # """
def test_graphql_get_disk_usage( # def test_graphql_get_disk_usage(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={"query": DISK_USAGE_QUERY}, # json={"query": DISK_USAGE_QUERY},
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_disk_usage_with_options( # def test_graphql_get_disk_usage_with_options(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={ # json={
"query": DISK_USAGE_QUERY, # "query": DISK_USAGE_QUERY,
"variables": { # "variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(), # "start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(), # "end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90, # "step": 90,
}, # },
}, # },
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_disk_usage_unauthorized(client): # def test_graphql_get_disk_usage_unauthorized(client):
response = client.post( # response = client.post(
"/graphql", # "/graphql",
json={"query": DISK_USAGE_QUERY}, # json={"query": DISK_USAGE_QUERY},
) # )
assert_empty(response) # assert_empty(response)
def test_graphql_get_memory_usage( # def test_graphql_get_memory_usage(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={"query": MEMORY_USAGE_QUERY}, # json={"query": MEMORY_USAGE_QUERY},
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_memory_usage_with_options( # def test_graphql_get_memory_usage_with_options(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={ # json={
"query": MEMORY_USAGE_QUERY_WITH_OPTIONS, # "query": MEMORY_USAGE_QUERY_WITH_OPTIONS,
"variables": { # "variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(), # "start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(), # "end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90, # "step": 90,
}, # },
}, # },
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_memory_usage_unauthorized(client): # def test_graphql_get_memory_usage_unauthorized(client):
response = client.post( # response = client.post(
"/graphql", # "/graphql",
json={"query": MEMORY_USAGE_QUERY}, # json={"query": MEMORY_USAGE_QUERY},
) # )
assert_empty(response) # assert_empty(response)
def test_graphql_get_cpu_usage( # def test_graphql_get_cpu_usage(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={"query": CPU_USAGE_QUERY}, # json={"query": CPU_USAGE_QUERY},
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_cpu_usage_with_options( # def test_graphql_get_cpu_usage_with_options(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={ # json={
"query": CPU_USAGE_QUERY_WITH_OPTIONS, # "query": CPU_USAGE_QUERY_WITH_OPTIONS,
"variables": { # "variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(), # "start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(), # "end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90, # "step": 90,
}, # },
}, # },
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_cpu_usage_unauthorized(client): # def test_graphql_get_cpu_usage_unauthorized(client):
response = client.post( # response = client.post(
"/graphql", # "/graphql",
json={"query": CPU_USAGE_QUERY}, # json={"query": CPU_USAGE_QUERY},
) # )
assert_empty(response) # assert_empty(response)
def test_graphql_get_network_usage( # def test_graphql_get_network_usage(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={"query": NETWORK_USAGE_QUERY}, # json={"query": NETWORK_USAGE_QUERY},
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_network_usage_with_options( # def test_graphql_get_network_usage_with_options(
client, # client,
authorized_client, # authorized_client,
mock_send_query, # mock_send_query,
mock_get_status_active, # mock_get_status_active,
): # ):
response = authorized_client.post( # response = authorized_client.post(
"/graphql", # "/graphql",
json={ # json={
"query": NETWORK_USAGE_QUERY_WITH_OPTIONS, # "query": NETWORK_USAGE_QUERY_WITH_OPTIONS,
"variables": { # "variables": {
"start": datetime.fromtimestamp(1720136108).isoformat(), # "start": datetime.fromtimestamp(1720136108).isoformat(),
"end": datetime.fromtimestamp(1720137319).isoformat(), # "end": datetime.fromtimestamp(1720137319).isoformat(),
"step": 90, # "step": 90,
}, # },
}, # },
) # )
data = get_data(response) # data = get_data(response)
assert data == ["test result"] # assert data == ["test result"]
def test_graphql_get_network_usage_unauthorized(client): # def test_graphql_get_network_usage_unauthorized(client):
response = client.post( # response = client.post(
"/graphql", # "/graphql",
json={"query": NETWORK_USAGE_QUERY}, # json={"query": NETWORK_USAGE_QUERY},
) # )
assert_empty(response) # assert_empty(response)