From a9d8ab0a322085c12dda6811ce053091348dc76c Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Sat, 5 Dec 2020 18:10:17 +0100
Subject: [PATCH] [fix] add missing network/peers bma request as network.peers
 (BREAK BC!)

peering/peers was wrapped under "network.peers" to simplify peering/peers requests.
But now, with this new request, peering/peers becomes "network.peering_peers"
---
 duniterpy/api/bma/network.py  | 54 ++++++++++++++++++++++++++++++++---
 tests/api/bma/test_network.py | 38 ++++++++++++++++++++----
 2 files changed, 82 insertions(+), 10 deletions(-)

diff --git a/duniterpy/api/bma/network.py b/duniterpy/api/bma/network.py
index 1db715f7..a23e7fa7 100644
--- a/duniterpy/api/bma/network.py
+++ b/duniterpy/api/bma/network.py
@@ -25,6 +25,41 @@ logger = logging.getLogger("duniter/network")
 
 MODULE = "network"
 
+PEERS_SCHEMA = {
+    "type": "object",
+    "properties": {
+        "peers": {
+            "type": "array",
+            "items": {
+                "type": "object",
+                "properties": {
+                    "version": {"type": ["number", "string"]},
+                    "currency": {"type": "string"},
+                    "status": {"type": "string"},
+                    "first_down": {"type": ["null", "integer"]},
+                    "last_try": {"type": ["null", "integer"]},
+                    "pubkey": {"type": "string"},
+                    "block": {"type": "string"},
+                    "signature": {"type": "string"},
+                    "endpoints": {"type": "array", "items": {"type": "string"}},
+                },
+                "required": [
+                    "version",
+                    "currency",
+                    "status",
+                    "first_down",
+                    "last_try",
+                    "pubkey",
+                    "block",
+                    "signature",
+                    "endpoints",
+                ],
+            },
+        }
+    },
+    "required": ["peers"],
+}
+
 PEERING_SCHEMA = {
     "type": "object",
     "properties": {
@@ -37,7 +72,7 @@ PEERING_SCHEMA = {
     "required": ["version", "currency", "pubkey", "endpoints", "signature"],
 }
 
-PEERS_SCHEMA = schema = {
+PEERING_PEERS_SCHEMA = {
     "type": ["object"],
     "properties": {
         "depth": {"type": "number"},
@@ -85,6 +120,17 @@ WS2P_HEADS_SCHEMA = {
 }
 
 
+async def peers(client: Client) -> dict:
+    """
+    GET the exhaustive list of peers known by the node
+
+    :param client: Client to connect to the api
+    :return:
+    """
+
+    return await client.get(MODULE + "/peers", schema=PEERS_SCHEMA)
+
+
 async def peering(client: Client) -> dict:
     """
     GET peering information about a peer
@@ -95,7 +141,7 @@ async def peering(client: Client) -> dict:
     return await client.get(MODULE + "/peering", schema=PEERING_SCHEMA)
 
 
-async def peers(client: Client, leaves: bool = False, leaf: str = "") -> dict:
+async def peering_peers(client: Client, leaves: bool = False, leaf: str = "") -> dict:
     """
     GET peering entries of every node inside the currency network
 
@@ -106,11 +152,11 @@ async def peers(client: Client, leaves: bool = False, leaf: str = "") -> dict:
     """
     if leaves is True:
         response = await client.get(
-            MODULE + "/peering/peers", {"leaves": "true"}, schema=PEERS_SCHEMA
+            MODULE + "/peering/peers", {"leaves": "true"}, schema=PEERING_PEERS_SCHEMA
         )
     else:
         response = await client.get(
-            MODULE + "/peering/peers", {"leaf": leaf}, schema=PEERS_SCHEMA
+            MODULE + "/peering/peers", {"leaf": leaf}, schema=PEERING_PEERS_SCHEMA
         )
     return response
 
diff --git a/tests/api/bma/test_network.py b/tests/api/bma/test_network.py
index f0247c17..0e2c5d87 100644
--- a/tests/api/bma/test_network.py
+++ b/tests/api/bma/test_network.py
@@ -27,6 +27,32 @@ from tests.api.webserver import WebFunctionalSetupMixin, web
 
 
 class TestBmaNetwork(WebFunctionalSetupMixin, unittest.TestCase):
+    def test_peers(self):
+        json_sample = {
+            "peers": [
+                {
+                    "version": "1",
+                    "currency": "beta_brouzouf",
+                    "status": "UP",
+                    "first_down": None,
+                    "last_try": 1607180847,
+                    "pubkey": "HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY",
+                    "block": "378529-00028D6F71E384565A1A106C1247E5F4B0392645A84EDB121173AC930540D552",
+                    "signature": "42yQm4hGTJYWkPg39hQAUgP6S6EQ4vTfXdJuxKEHL1ih6YHiDL2hcwrFgBHjXLRgxRhj2VNVqqc6b4JayKqTE14r",
+                    "endpoints": [
+                        "BASIC_MERKLED_API some.dns.name 88.77.66.55 2001:0db8:0000:85a3:0000:0000:ac1f 9001",
+                        "BASIC_MERKLED_API some.dns.name 88.77.66.55 2001:0db8:0000:85a3:0000:0000:ac1f 9002",
+                        "OTHER_PROTOCOL 88.77.66.55 9001",
+                    ],
+                }
+            ]
+        }
+
+        try:
+            jsonschema.validate(json_sample, network.PEERS_SCHEMA)
+        except (SchemaError, ValidationError) as e:
+            raise self.failureException from e
+
     def test_peering(self):
         json_sample = {
             "version": "1",
@@ -58,7 +84,7 @@ class TestBmaNetwork(WebFunctionalSetupMixin, unittest.TestCase):
 
         self.loop.run_until_complete(go())
 
-    def test_peers_root(self):
+    def test_peering_peers_root(self):
         json_sample = {
             "depth": 3,
             "nodesCount": 6,
@@ -66,11 +92,11 @@ class TestBmaNetwork(WebFunctionalSetupMixin, unittest.TestCase):
             "root": "114B6E61CB5BB93D862CA3C1DFA8B99E313E66E9",
         }
         try:
-            jsonschema.validate(json_sample, network.PEERS_SCHEMA)
+            jsonschema.validate(json_sample, network.PEERING_PEERS_SCHEMA)
         except (SchemaError, ValidationError) as e:
             raise self.failureException from e
 
-    def test_peers_leaf(self):
+    def test_peering_peers_leaf(self):
         json_sample = {
             "hash": "2E69197FAB029D8669EF85E82457A1587CA0ED9C",
             "value": {
@@ -86,11 +112,11 @@ class TestBmaNetwork(WebFunctionalSetupMixin, unittest.TestCase):
             },
         }
         try:
-            jsonschema.validate(json_sample, network.PEERS_SCHEMA)
+            jsonschema.validate(json_sample, network.PEERING_PEERS_SCHEMA)
         except (SchemaError, ValidationError) as e:
             raise self.failureException from e
 
-    def test_peers_bad(self):
+    def test_peering_peers_bad(self):
         async def handler(request):
             await request.read()
             return web.Response(body=b"{}", content_type="application/json")
@@ -101,7 +127,7 @@ class TestBmaNetwork(WebFunctionalSetupMixin, unittest.TestCase):
             )
             with self.assertRaises(jsonschema.exceptions.ValidationError):
                 client = Client(BMAEndpoint("127.0.0.1", "", "", port))
-                await client(network.peers)
+                await client(network.peering_peers)
             await client.close()
 
         self.loop.run_until_complete(go())
-- 
GitLab