From a97705ef256f9c25a11e7e571664dbc9b8def7cf Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 19 Dec 2022 17:37:44 +0000 Subject: [PATCH] fix(tokens-repository): fix getting and setting recovery token expiration date --- .../tokens/json_tokens_repository.py | 7 +++++-- .../test_repository/test_tokens_repository.py | 21 ++++++++++++++++++- tests/test_models.py | 10 +++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 tests/test_models.py diff --git a/selfprivacy_api/repositories/tokens/json_tokens_repository.py b/selfprivacy_api/repositories/tokens/json_tokens_repository.py index c599e0f..e43a3eb 100644 --- a/selfprivacy_api/repositories/tokens/json_tokens_repository.py +++ b/selfprivacy_api/repositories/tokens/json_tokens_repository.py @@ -69,7 +69,7 @@ class JsonTokensRepository(AbstractTokensRepository): recovery_key = RecoveryKey( key=tokens_file["recovery_token"].get("token"), created_at=tokens_file["recovery_token"].get("date"), - expires_at=tokens_file["recovery_token"].get("expitation"), + expires_at=tokens_file["recovery_token"].get("expiration"), uses_left=tokens_file["recovery_token"].get("uses_left"), ) @@ -85,10 +85,13 @@ class JsonTokensRepository(AbstractTokensRepository): recovery_key = RecoveryKey.generate(expiration, uses_left) with WriteUserData(UserDataFiles.TOKENS) as tokens_file: + expiration = recovery_key.expires_at + if expiration is not None: + expiration = expiration.strftime(DATETIME_FORMAT) tokens_file["recovery_token"] = { "token": recovery_key.key, "date": recovery_key.created_at.strftime(DATETIME_FORMAT), - "expiration": recovery_key.expires_at, + "expiration": expiration, "uses_left": recovery_key.uses_left, } diff --git a/tests/test_graphql/test_repository/test_tokens_repository.py b/tests/test_graphql/test_repository/test_tokens_repository.py index 7a80b03..85cee00 100644 --- a/tests/test_graphql/test_repository/test_tokens_repository.py +++ b/tests/test_graphql/test_repository/test_tokens_repository.py @@ -2,7 +2,7 @@ # pylint: disable=unused-argument # pylint: disable=missing-function-docstring -from datetime import datetime +from datetime import datetime, timedelta from mnemonic import Mnemonic import pytest @@ -341,6 +341,25 @@ def test_use_mnemonic_not_valid_recovery_key( ) +def test_use_mnemonic_expired_recovery_key( + some_tokens_repo, +): + repo = some_tokens_repo + expiration = datetime.now() - timedelta(minutes=5) + assert repo.create_recovery_key(uses_left=2, expiration=expiration) is not None + recovery_key = repo.get_recovery_key() + assert recovery_key.expires_at == expiration + assert not repo.is_recovery_key_valid() + + with pytest.raises(RecoveryKeyNotFound): + token = repo.use_mnemonic_recovery_key( + mnemonic_phrase=Mnemonic(language="english").to_mnemonic( + bytes.fromhex(recovery_key.key) + ), + device_name="newdevice", + ) + + def test_use_mnemonic_not_mnemonic_recovery_key(some_tokens_repo): repo = some_tokens_repo assert repo.create_recovery_key(uses_left=1, expiration=None) is not None diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 0000000..078dfd5 --- /dev/null +++ b/tests/test_models.py @@ -0,0 +1,10 @@ +import pytest +from datetime import datetime, timedelta + +from selfprivacy_api.models.tokens.recovery_key import RecoveryKey + + +def test_recovery_key_expired(): + expiration = datetime.now() - timedelta(minutes=5) + key = RecoveryKey.generate(expiration=expiration, uses_left=2) + assert not key.is_valid()