diff --git a/duniterpy/documents/ws2p/messages.py b/duniterpy/documents/ws2p/messages.py index 71e849cfbcd6bebce540437432fc3f5c0e277e13..9f45227e63c1c11770b89aa9fd73047351962f12 100644 --- a/duniterpy/documents/ws2p/messages.py +++ b/duniterpy/documents/ws2p/messages.py @@ -1,6 +1,6 @@ import json -from typing import Optional +from typing import Optional, Any from duniterpy.documents import Document from duniterpy.key import VerifyingKey, SigningKey @@ -46,7 +46,7 @@ class Connect(Document): def get_signed_json(self, signing_key: SigningKey) -> str: """ - Return the signed document in json format + Return the signed message in json format :param signing_key: Signing key instance @@ -100,7 +100,7 @@ class Ack(Document): def get_signed_json(self, signing_key: SigningKey) -> str: """ - Return the signed document in json format + Return the signed message in json format :param signing_key: Signing key instance @@ -153,7 +153,7 @@ class Ok(Document): def get_signed_json(self, signing_key: SigningKey) -> str: """ - Return the signed document in json format + Return the signed message in json format :param signing_key: Signing key instance @@ -168,3 +168,39 @@ class Ok(Document): def __str__(self) -> str: return self.raw() + + +# fixme: the document format to send is to be determine +# does raw or inline format works ? +class DocumentMessage: + PEER_TYPE_ID = 0 + TRANSACTION_TYPE_ID = 1 + MEMBERSHIP_TYPE_ID = 2 + CERTIFICATION_TYPE_ID = 3 + IDENTITY_TYPE_ID = 4 + BLOCK_TYPE_ID = 5 + + DOCUMENT_TYPE_NAMES = { + 0: "peer", + 1: "transaction", + 2: "membership", + 3: "certification", + 4: "identity", + 5: "block" + } + + def get_json(self, document_type_id: int, document: Any) -> str: + """ + Return the document message in json format + + :param document_type_id: Id of the document type, use class properties + :param document: Document object to send + :return: + """ + data = { + "body": { + "name": document_type_id, + self.DOCUMENT_TYPE_NAMES[document_type_id]: document + } + } + return json.dumps(data) diff --git a/tests/api/ws2p/test_ws2p.py b/tests/api/ws2p/test_ws2p.py index 6607bebe66302565b00949b685ed30469391abcd..169328f89f78bfbee800e6c31ccaf227a9017e23 100644 --- a/tests/api/ws2p/test_ws2p.py +++ b/tests/api/ws2p/test_ws2p.py @@ -1,3 +1,4 @@ +import json import unittest import jsonschema @@ -7,6 +8,8 @@ from duniterpy.api.endpoint import BMAEndpoint from duniterpy.api.ws2p.network import heads, WS2P_HEADS_SCHEMA from duniterpy.api.ws2p.requests import BLOCK_RESPONSE_SCHEMA, ERROR_RESPONSE_SCHEMA, BLOCKS_RESPONSE_SCHEMA, \ REQUIREMENTS_RESPONSE_SCHEMA +from duniterpy.documents import Identity, BlockUID +from duniterpy.documents.ws2p.messages import DocumentMessage from tests.api.webserver import WebFunctionalSetupMixin, web @@ -176,3 +179,26 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase): ]}} """ response = parse_text(response_string, REQUIREMENTS_RESPONSE_SCHEMA) self.assertIsInstance(response, dict) + + def test_document_message(self): + # prepare message + document_message = DocumentMessage() + # prepare document + identity_document = Identity( + 10, "beta_brousouf", "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd", "lolcat", + BlockUID(32, "DB30D958EE5CB75186972286ED3F4686B8A1C2CD"), + "J3G9oM5AKYZNLAB5Wx499w61NuUoS57JVccTShUbGpCMjCqj9yXXqNq7dyZpDWA6BxipsiaMZhujMeBfCznzyci") + # get json string message + json_document_message = document_message.get_json(DocumentMessage.IDENTITY_TYPE_ID, identity_document.inline()) + # convert to dict to verify + dict_document_message = json.loads(json_document_message) + + # verify + self.assertIn("body", dict_document_message) + self.assertIn("name", dict_document_message["body"]) + self.assertIn("identity", dict_document_message["body"]) + self.assertEqual(4, dict_document_message["body"]["name"]) + expected = """HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd\ +:J3G9oM5AKYZNLAB5Wx499w61NuUoS57JVccTShUbGpCMjCqj9yXXqNq7dyZpDWA6BxipsiaMZhujMeBfCznzyci:32\ +-DB30D958EE5CB75186972286ED3F4686B8A1C2CD:lolcat""" + self.assertEqual(expected, dict_document_message["body"]["identity"])