diff --git a/silkaj/idty_tools.py b/silkaj/idty_tools.py index e9a6575bac6ab6f737c7993a90e82801e68b0000..453285fbc0a04c3d791165c42e329a17c831a19c 100644 --- a/silkaj/idty_tools.py +++ b/silkaj/idty_tools.py @@ -16,12 +16,12 @@ import shutil import sys import urllib +from typing import Union import click import pendulum from duniterpy.api import bma -from duniterpy.documents.block_id import BlockID -from duniterpy.documents.identity import Identity +from duniterpy.documents import BlockID, Identity, Revocation from texttable import Texttable from silkaj import wot_tools as wt @@ -50,12 +50,14 @@ def display_identity(idty: Identity): return table -def check_many_identities(idty: Identity, doc_type: str = "Identity"): +def check_many_identities(document: Union[Identity, Revocation]): """ Checks if many identities match the one looked after. Returns True if the same identity is found, False if not. """ + doc_type = document.__class__.__name__ error_no_identical_id = f"{doc_type} document does not match any valid identity." + idty = document if doc_type == "Identity" else document.identity try: results_pubkey = wt.wot_lookup(idty.pubkey) diff --git a/silkaj/revocation.py b/silkaj/revocation.py index 39451f7f7e58d80869d5fb08462d2b7449d785a1..9db6aa267db40ec385cc42630adc8b4f587638ff 100644 --- a/silkaj/revocation.py +++ b/silkaj/revocation.py @@ -229,13 +229,12 @@ def verify_document(doc: str): rev_doc = Revocation.from_signed_raw(original_doc) except (MalformedDocumentError, IndexError): sys.exit(error_invalid_doc) - idty = rev_doc.identity - verif_key = VerifyingKey(idty.pubkey) + verif_key = VerifyingKey(rev_doc.pubkey) if not verif_key.check_signature(rev_doc.raw(), rev_doc.signature): sys.exit(error_invalid_sign) - many_idtys = idty_tools.check_many_identities(idty, "Revocation") + many_idtys = idty_tools.check_many_identities(rev_doc) if many_idtys: return rev_doc sys.exit(FAILURE_EXIT_STATUS) diff --git a/tests/test_idty_tools.py b/tests/test_idty_tools.py index 5a8591dac9538b2a20c515c81285c979944f446d..2817e145d3fd66ffff78daac40bbe449af34f990 100644 --- a/tests/test_idty_tools.py +++ b/tests/test_idty_tools.py @@ -280,12 +280,11 @@ def test_display_identity(idty, monkeypatch, capsys): @pytest.mark.parametrize( - "idty, doc_type, lookup_pk, lookup_uid, expected, expect_bool", + "idty, lookup_pk, lookup_uid, expected, expect_bool", [ # normal case : 1 same lookup on pubkey and uid ( idty1, - "Revocation", lookup_one, lookup_one, None, @@ -294,7 +293,6 @@ def test_display_identity(idty, monkeypatch, capsys): # 2 lookups on pubkey, one on uid, match similar. ( idty1, - "Revocation", lookup_one, lookup_two, [ @@ -307,11 +305,10 @@ def test_display_identity(idty, monkeypatch, capsys): # 2 lookups on pubkey, one on uid, no match similar. ( idty2, - "Revocation", lookup_one, lookup_two, [ - "Revocation document does not match any valid identity.", + "Identity document does not match any valid identity.", "Claude", "Claudia", ], @@ -320,7 +317,6 @@ def test_display_identity(idty, monkeypatch, capsys): # 1 lookup on pubkey, 2 on uid, match. ( idty1, - "Revocation", lookup_one, lookup_three, [ @@ -334,11 +330,10 @@ def test_display_identity(idty, monkeypatch, capsys): # 1 lookup on pubkey, 2 on uid, no match. ( idty2, - "Revocation", lookup_one, lookup_three, [ - "Revocation document does not match any valid identity.", + "Identity document does not match any valid identity.", "Similar identities:", "Claude", "XXXX", @@ -348,18 +343,17 @@ def test_display_identity(idty, monkeypatch, capsys): # no lookup found. ( idty1, - "Revocation", None, None, [ - f"Revocation document does not match any valid identity.\nuid: {idty1.uid}\npubkey: {display_pubkey_and_checksum(idty1.pubkey)}" + f"Identity document does not match any valid identity.\nuid: {idty1.uid}\npubkey: {display_pubkey_and_checksum(idty1.pubkey)}" ], False, ), ], ) def test_check_many_identities( - idty, doc_type, lookup_pk, lookup_uid, expected, expect_bool, monkeypatch, capsys + idty, lookup_pk, lookup_uid, expected, expect_bool, monkeypatch, capsys ): # Patch BMA lookup so it returns different lookups if we request a pk or a uid @@ -385,15 +379,15 @@ def test_check_many_identities( # identity does not exist if expected == [ - f"Revocation document does not match any valid identity.\nuid: {idty.uid}\npubkey: {display_pubkey_and_checksum(idty.pubkey)}" + f"Identity document does not match any valid identity.\nuid: {idty.uid}\npubkey: {display_pubkey_and_checksum(idty.pubkey)}" ]: with pytest.raises(SystemExit) as pytest_exit: - result = idty_tools.check_many_identities(idty, doc_type) + result = idty_tools.check_many_identities(idty) assert pytest_exit.type == SystemExit assert expected[0] == str(pytest_exit.value.code) # test cases with an identity else: - result = idty_tools.check_many_identities(idty, doc_type) + result = idty_tools.check_many_identities(idty) assert result == expect_bool display_result = capsys.readouterr().out if expected != None: diff --git a/tests/test_revocation.py b/tests/test_revocation.py index edabb87ae9aaa467c6347189a0b8b770cb884c52..c608858053da223a9ebf5fa709db1396d4c9d698 100644 --- a/tests/test_revocation.py +++ b/tests/test_revocation.py @@ -110,7 +110,7 @@ def patched_auth_method_Claude(): return patched_auth_method("a") -def patch_check_many_identities(idty, doc_type): +def patch_check_many_identities(idty): return True