From 7bb01f9ddd8978712c92d0ad02ef9f72ce980bc4 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sat, 19 Mar 2022 14:14:32 +0100
Subject: [PATCH] [mod] #89: Determine document type in check_many_identities()

---
 silkaj/idty_tools.py     |  8 +++++---
 silkaj/revocation.py     |  5 ++---
 tests/test_idty_tools.py | 22 ++++++++--------------
 tests/test_revocation.py |  2 +-
 4 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/silkaj/idty_tools.py b/silkaj/idty_tools.py
index e9a6575b..453285fb 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 39451f7f..9db6aa26 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 5a8591da..2817e145 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 edabb87a..c6088580 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
 
 
-- 
GitLab