From 39277419acbe61b70757cb7999148f0acb4f26f3 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 21 Dec 2022 13:09:51 +0000 Subject: [PATCH] refactor(tokens-repo): use tokens-repo to refresh tokens --- .../graphql/mutations/api_mutations.py | 24 ++++++++++--------- tests/test_graphql/test_api_devices.py | 24 ++++++++++++++----- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/selfprivacy_api/graphql/mutations/api_mutations.py b/selfprivacy_api/graphql/mutations/api_mutations.py index 1a67212..3626d83 100644 --- a/selfprivacy_api/graphql/mutations/api_mutations.py +++ b/selfprivacy_api/graphql/mutations/api_mutations.py @@ -21,7 +21,6 @@ from selfprivacy_api.graphql.mutations.mutation_interface import ( from selfprivacy_api.utils.auth import ( delete_new_device_auth_token, get_new_device_auth_token, - refresh_token, use_new_device_auth_token, ) @@ -126,32 +125,35 @@ class ApiMutations: @strawberry.mutation(permission_classes=[IsAuthenticated]) def refresh_device_api_token(self, info: Info) -> DeviceApiTokenMutationReturn: """Refresh device api token""" - token = ( + token_string = ( info.context["request"] .headers.get("Authorization", "") .replace("Bearer ", "") ) - if token is None: + if token_string is None: return DeviceApiTokenMutationReturn( success=False, message="Token not found", code=404, token=None, ) - new_token = refresh_token(token) - if new_token is None: + + try: + old_token = TOKEN_REPO.get_token_by_token_string(token_string) + new_token = TOKEN_REPO.refresh_token(old_token) + return DeviceApiTokenMutationReturn( + success=True, + message="Token refreshed", + code=200, + token=new_token.token, + ) + except: return DeviceApiTokenMutationReturn( success=False, message="Token not found", code=404, token=None, ) - return DeviceApiTokenMutationReturn( - success=True, - message="Token refreshed", - code=200, - token=new_token, - ) @strawberry.mutation(permission_classes=[IsAuthenticated]) def delete_device_api_token(self, device: str, info: Info) -> GenericMutationReturn: diff --git a/tests/test_graphql/test_api_devices.py b/tests/test_graphql/test_api_devices.py index d8dc974..07cf42a 100644 --- a/tests/test_graphql/test_api_devices.py +++ b/tests/test_graphql/test_api_devices.py @@ -2,8 +2,14 @@ # pylint: disable=unused-argument # pylint: disable=missing-function-docstring import datetime +import pytest from mnemonic import Mnemonic +from selfprivacy_api.repositories.tokens.json_tokens_repository import ( + JsonTokensRepository, +) +from selfprivacy_api.models.tokens.token import Token + from tests.common import generate_api_query, read_json, write_json TOKENS_FILE_CONTETS = { @@ -30,6 +36,11 @@ devices { """ +@pytest.fixture +def token_repo(): + return JsonTokensRepository() + + def test_graphql_tokens_info(authorized_client, tokens_file): response = authorized_client.post( "/graphql", @@ -170,7 +181,7 @@ def test_graphql_refresh_token_unauthorized(client, tokens_file): assert response.json()["data"] is None -def test_graphql_refresh_token(authorized_client, tokens_file): +def test_graphql_refresh_token(authorized_client, tokens_file, token_repo): response = authorized_client.post( "/graphql", json={"query": REFRESH_TOKEN_MUTATION}, @@ -180,11 +191,12 @@ def test_graphql_refresh_token(authorized_client, tokens_file): assert response.json()["data"]["refreshDeviceApiToken"]["success"] is True assert response.json()["data"]["refreshDeviceApiToken"]["message"] is not None assert response.json()["data"]["refreshDeviceApiToken"]["code"] == 200 - assert read_json(tokens_file)["tokens"][0] == { - "token": response.json()["data"]["refreshDeviceApiToken"]["token"], - "name": "test_token", - "date": "2022-01-14 08:31:10.789314", - } + token = token_repo.get_token_by_name("test_token") + assert token == Token( + token=response.json()["data"]["refreshDeviceApiToken"]["token"], + device_name="test_token", + created_at=datetime.datetime(2022, 1, 14, 8, 31, 10, 789314), + ) NEW_DEVICE_KEY_MUTATION = """