From 9bf239c3a8cd494c717af9fe39a8af51b309b841 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 20 Sep 2023 14:30:18 +0000 Subject: [PATCH] test(services): disable usual services for testing --- tests/test_graphql/test_services.py | 106 ++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tests/test_graphql/test_services.py diff --git a/tests/test_graphql/test_services.py b/tests/test_graphql/test_services.py new file mode 100644 index 0000000..00b4633 --- /dev/null +++ b/tests/test_graphql/test_services.py @@ -0,0 +1,106 @@ +import pytest + +from selfprivacy_api.graphql.mutations.services_mutations import ServicesMutations +import selfprivacy_api.services as service_module +from selfprivacy_api.services.service import Service + +import tests.test_graphql.test_api_backup +from tests.test_common import raw_dummy_service, dummy_service +from tests.common import generate_service_query +from tests.test_graphql.common import get_data + + +@pytest.fixture() +def only_dummy_service(dummy_service): + # because queries to services that are not really there error out + back_copy = service_module.services.copy() + service_module.services.clear() + service_module.services.append(dummy_service) + yield dummy_service + service_module.services.clear() + service_module.services.extend(back_copy) + + + +API_START_MUTATION = """ +mutation TestStartService($service_id: String!) { + services { + startService(serviceId: $service_id) { + success + message + code + service { + id + status + } + } + } +} +""" + +API_STOP_MUTATION = """ +mutation TestStopService($service_id: String!) { + services { + stopService(serviceId: $service_id) { + success + message + code + service { + id + status + } + } + } +} + +""" +API_SERVICES_QUERY = """ +allServices { + id + status +} +""" + + +def api_start(client, service): + response = client.post( + "/graphql", + json={ + "query": API_START_MUTATION, + "variables": {"service_id": service.get_id()}, + }, + ) + return response + + +def api_stop(client, service): + response = client.post( + "/graphql", + json={ + "query": API_STOP_MUTATION, + "variables": {"service_id": service.get_id()}, + }, + ) + return response + + +def api_all_services(authorized_client): + response = authorized_client.post( + "/graphql", + json={"query": generate_service_query([API_SERVICES_QUERY])}, + ) + data = get_data(response) + result = data["services"]["allServices"] + assert result is not None + return result + + +def api_service(authorized_client, service: Service): + id = service.get_id() + for _service in api_all_services(authorized_client): + if _service["id"] == id: + return _service + + +def test_get_services(authorized_client, only_dummy_service): + assert len(api_all_services(authorized_client)) == 1