tests: improving peer id test
@@ -3,6 +3,10 @@ from hathor.p2p.peer_id import PeerId, InvalidPeerIdException
from hathor.p2p.peer_storage import PeerStorage
+import tempfile
+import os
+import json
+
class PeerIdTest(unittest.TestCase):
def test_invalid_id(self):
@@ -56,7 +60,7 @@ class PeerIdTest(unittest.TestCase):
signature = signature[::-1]
self.assertFalse(p1.verify_signature(signature, data))
- def test_add_peer_id(self):
+ def test_merge_peer(self):
# Testing peer storage with merge of peers
peer_storage = PeerStorage()
@@ -72,6 +76,7 @@ class PeerIdTest(unittest.TestCase):
p3.private_key = ''
p3.id = p2.id
p3.entrypoints.append('2')
+ p2.public_key = ''
peer_storage.add_or_merge(p3)
self.assertEqual(len(peer_storage), 2)
@@ -82,9 +87,20 @@ class PeerIdTest(unittest.TestCase):
peer = peer_storage[p2.id]
self.assertEqual(peer.id, p2.id)
self.assertEqual(peer.private_key, p2.private_key)
- self.assertEqual(peer.public_key, p2.public_key)
self.assertEqual(peer.entrypoints, ['2', '1'])
+ def test_save_peer_file(self):
+ p = PeerId()
+ tmpdir = tempfile.mkdtemp()
+ path = os.path.join(tmpdir, 'peer.json')
+
+ p.save_to_file(path)
+
+ with open(path, 'r') as f:
+ peer_from_file = json.loads(f.read())
+
+ self.assertEqual(p.to_json(include_private_key=True), peer_from_file)
+
if __name__ == '__main__':
unittest.main()
