mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-28 19:56:39 +00:00
refactor(tokens-repo): use tokens-repo to refresh tokens
This commit is contained in:
parent
d3bf867bb5
commit
39277419ac
|
@ -21,7 +21,6 @@ from selfprivacy_api.graphql.mutations.mutation_interface import (
|
||||||
from selfprivacy_api.utils.auth import (
|
from selfprivacy_api.utils.auth import (
|
||||||
delete_new_device_auth_token,
|
delete_new_device_auth_token,
|
||||||
get_new_device_auth_token,
|
get_new_device_auth_token,
|
||||||
refresh_token,
|
|
||||||
use_new_device_auth_token,
|
use_new_device_auth_token,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -126,31 +125,34 @@ class ApiMutations:
|
||||||
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
||||||
def refresh_device_api_token(self, info: Info) -> DeviceApiTokenMutationReturn:
|
def refresh_device_api_token(self, info: Info) -> DeviceApiTokenMutationReturn:
|
||||||
"""Refresh device api token"""
|
"""Refresh device api token"""
|
||||||
token = (
|
token_string = (
|
||||||
info.context["request"]
|
info.context["request"]
|
||||||
.headers.get("Authorization", "")
|
.headers.get("Authorization", "")
|
||||||
.replace("Bearer ", "")
|
.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:
|
|
||||||
return DeviceApiTokenMutationReturn(
|
return DeviceApiTokenMutationReturn(
|
||||||
success=False,
|
success=False,
|
||||||
message="Token not found",
|
message="Token not found",
|
||||||
code=404,
|
code=404,
|
||||||
token=None,
|
token=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
old_token = TOKEN_REPO.get_token_by_token_string(token_string)
|
||||||
|
new_token = TOKEN_REPO.refresh_token(old_token)
|
||||||
return DeviceApiTokenMutationReturn(
|
return DeviceApiTokenMutationReturn(
|
||||||
success=True,
|
success=True,
|
||||||
message="Token refreshed",
|
message="Token refreshed",
|
||||||
code=200,
|
code=200,
|
||||||
token=new_token,
|
token=new_token.token,
|
||||||
|
)
|
||||||
|
except:
|
||||||
|
return DeviceApiTokenMutationReturn(
|
||||||
|
success=False,
|
||||||
|
message="Token not found",
|
||||||
|
code=404,
|
||||||
|
token=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
||||||
|
|
|
@ -2,8 +2,14 @@
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
# pylint: disable=missing-function-docstring
|
# pylint: disable=missing-function-docstring
|
||||||
import datetime
|
import datetime
|
||||||
|
import pytest
|
||||||
from mnemonic import Mnemonic
|
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
|
from tests.common import generate_api_query, read_json, write_json
|
||||||
|
|
||||||
TOKENS_FILE_CONTETS = {
|
TOKENS_FILE_CONTETS = {
|
||||||
|
@ -30,6 +36,11 @@ devices {
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def token_repo():
|
||||||
|
return JsonTokensRepository()
|
||||||
|
|
||||||
|
|
||||||
def test_graphql_tokens_info(authorized_client, tokens_file):
|
def test_graphql_tokens_info(authorized_client, tokens_file):
|
||||||
response = authorized_client.post(
|
response = authorized_client.post(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
|
@ -170,7 +181,7 @@ def test_graphql_refresh_token_unauthorized(client, tokens_file):
|
||||||
assert response.json()["data"] is None
|
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(
|
response = authorized_client.post(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={"query": REFRESH_TOKEN_MUTATION},
|
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"]["success"] is True
|
||||||
assert response.json()["data"]["refreshDeviceApiToken"]["message"] is not None
|
assert response.json()["data"]["refreshDeviceApiToken"]["message"] is not None
|
||||||
assert response.json()["data"]["refreshDeviceApiToken"]["code"] == 200
|
assert response.json()["data"]["refreshDeviceApiToken"]["code"] == 200
|
||||||
assert read_json(tokens_file)["tokens"][0] == {
|
token = token_repo.get_token_by_name("test_token")
|
||||||
"token": response.json()["data"]["refreshDeviceApiToken"]["token"],
|
assert token == Token(
|
||||||
"name": "test_token",
|
token=response.json()["data"]["refreshDeviceApiToken"]["token"],
|
||||||
"date": "2022-01-14 08:31:10.789314",
|
device_name="test_token",
|
||||||
}
|
created_at=datetime.datetime(2022, 1, 14, 8, 31, 10, 789314),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
NEW_DEVICE_KEY_MUTATION = """
|
NEW_DEVICE_KEY_MUTATION = """
|
||||||
|
|
Loading…
Reference in a new issue