From de2703219141b8d86bbde8b6423eb7ce35237644 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 6 Jan 2023 11:46:17 +0000 Subject: [PATCH] test(tokens-repo): move token utils to graphql common --- tests/test_graphql/common.py | 36 ++++++++++++++++++++++++ tests/test_graphql/test_api_devices.py | 38 ++++---------------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/tests/test_graphql/common.py b/tests/test_graphql/common.py index f2cc54d..7db5b35 100644 --- a/tests/test_graphql/common.py +++ b/tests/test_graphql/common.py @@ -1,3 +1,6 @@ +from tests.common import generate_api_query + + def assert_ok(response, request): data = assert_data(response) data[request]["success"] is True @@ -22,3 +25,36 @@ def assert_data(response): data = response.json().get("data") assert data is not None return data + + +API_DEVICES_QUERY = """ +devices { + creationDate + isCaller + name +} +""" + + +def request_devices(client): + return client.post( + "/graphql", + json={"query": generate_api_query([API_DEVICES_QUERY])}, + ) + + +def graphql_get_devices(client): + response = request_devices(client) + data = assert_data(response) + devices = data["api"]["devices"] + assert devices is not None + return devices + + +def set_client_token(client, token): + client.headers.update({"Authorization": "Bearer " + token}) + + +def assert_token_valid(client, token): + set_client_token(client, token) + assert graphql_get_devices(client) is not None diff --git a/tests/test_graphql/test_api_devices.py b/tests/test_graphql/test_api_devices.py index 3db8647..673ed53 100644 --- a/tests/test_graphql/test_api_devices.py +++ b/tests/test_graphql/test_api_devices.py @@ -13,29 +13,15 @@ from tests.test_graphql.common import ( assert_empty, assert_ok, assert_errorcode, + assert_token_valid, + graphql_get_devices, + request_devices, + set_client_token, + API_DEVICES_QUERY, ) ORIGINAL_DEVICES = TOKENS_FILE_CONTENTS["tokens"] -API_DEVICES_QUERY = """ -devices { - creationDate - isCaller - name -} -""" - - -def graphql_get_devices(client): - response = client.post( - "/graphql", - json={"query": generate_api_query([API_DEVICES_QUERY])}, - ) - data = assert_data(response) - devices = data["api"]["devices"] - assert devices is not None - return devices - def graphql_get_caller_token_info(client): devices = graphql_get_devices(client) @@ -65,15 +51,6 @@ def assert_original(client): assert device["isCaller"] is False -def set_client_token(client, token): - client.headers.update({"Authorization": "Bearer " + token}) - - -def assert_token_valid(client, token): - set_client_token(client, token) - assert graphql_get_devices(client) is not None - - def graphql_get_new_device_key(authorized_client) -> str: response = authorized_client.post( "/graphql", @@ -113,10 +90,7 @@ def test_graphql_tokens_info(authorized_client, tokens_file): def test_graphql_tokens_info_unauthorized(client, tokens_file): - response = client.post( - "/graphql", - json={"query": generate_api_query([API_DEVICES_QUERY])}, - ) + response = request_devices(client) assert_empty(response)