From 20410ec790661cc2d3fb6e86a7cc9bf19faca5f3 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 21 Dec 2022 16:10:41 +0000 Subject: [PATCH] fix(tokens-repo): fix name pair validation being able to raise a notfound error --- .../repositories/tokens/abstract_tokens_repository.py | 7 +++++-- .../test_repository/test_tokens_repository.py | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/selfprivacy_api/repositories/tokens/abstract_tokens_repository.py b/selfprivacy_api/repositories/tokens/abstract_tokens_repository.py index 4f86e61..3b23a84 100644 --- a/selfprivacy_api/repositories/tokens/abstract_tokens_repository.py +++ b/selfprivacy_api/repositories/tokens/abstract_tokens_repository.py @@ -77,8 +77,11 @@ class AbstractTokensRepository(ABC): def is_token_name_pair_valid(self, token_name: str, token_string: str) -> bool: """Check if the token name and token are valid""" - token = self.get_token_by_name(token_name) - if token is None: + try: + token = self.get_token_by_name(token_name) + if token is None: + return False + except TokenNotFound: return False return token.token == token_string diff --git a/tests/test_graphql/test_repository/test_tokens_repository.py b/tests/test_graphql/test_repository/test_tokens_repository.py index 1a26247..d3650fe 100644 --- a/tests/test_graphql/test_repository/test_tokens_repository.py +++ b/tests/test_graphql/test_repository/test_tokens_repository.py @@ -207,6 +207,14 @@ def test_get_token_by_non_existent_name(some_tokens_repo): assert repo.get_token_by_name(token_name="badname") is None +def test_is_token_name_pair_valid(some_tokens_repo): + repo = some_tokens_repo + token = repo.get_tokens()[0] + assert repo.is_token_name_pair_valid(token.device_name, token.token) + assert not repo.is_token_name_pair_valid(token.device_name, "gibberish") + assert not repo.is_token_name_pair_valid("gibberish", token.token) + + def test_get_tokens(some_tokens_repo): repo = some_tokens_repo tokenstrings = []