From 2b440904adc825239884ec2007e694d42114b96f Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sun, 6 Sep 2015 20:40:40 +0200
Subject: [PATCH] Fix research in identities tab

---
 src/cutecoin/gui/identities_tab.py            |   1 +
 .../tests/certification/test_certification.py |   2 +-
 src/cutecoin/tests/identities_tab/__init__.py |   0
 .../identities_tab/test_identities_table.py   | 101 +++++++++++++++
 .../tests/mocks/bma/nice_blockchain.py        | 119 ++++++++++++++++++
 .../test_add_community.py                     |   2 +-
 6 files changed, 223 insertions(+), 2 deletions(-)
 create mode 100644 src/cutecoin/tests/identities_tab/__init__.py
 create mode 100644 src/cutecoin/tests/identities_tab/test_identities_table.py
 create mode 100644 src/cutecoin/tests/mocks/bma/nice_blockchain.py

diff --git a/src/cutecoin/gui/identities_tab.py b/src/cutecoin/gui/identities_tab.py
index 646e59bb..6b91e26e 100644
--- a/src/cutecoin/gui/identities_tab.py
+++ b/src/cutecoin/gui/identities_tab.py
@@ -58,6 +58,7 @@ class IdentitiesTabWidget(QWidget, Ui_IdentitiesTab):
         direct_connections = QAction(self.tr("Direct connections"), self)
         direct_connections.triggered.connect(self.search_direct_connections)
         self.button_search.addAction(direct_connections)
+        self.button_search.clicked.connect(self.search_text)
 
     def change_account(self, account):
         self.account = account
diff --git a/src/cutecoin/tests/certification/test_certification.py b/src/cutecoin/tests/certification/test_certification.py
index a732c738..af9ef739 100644
--- a/src/cutecoin/tests/certification/test_certification.py
+++ b/src/cutecoin/tests/certification/test_certification.py
@@ -64,6 +64,7 @@ class TestCertificationDialog(unittest.TestCase):
     def test_certification_init_community(self):
         mock = init_new_community.get_mock()
         logging.debug(mock.pretend_url)
+        self.network_manager.set_mock_path(mock.pretend_url)
         certification_dialog = CertificationDialog(self.application,
                                                    self.account,
                                                    self.password_asker)
@@ -80,7 +81,6 @@ class TestCertificationDialog(unittest.TestCase):
         @asyncio.coroutine
         def exec_test():
             yield from asyncio.sleep(1)
-            self.network_manager.set_mock_path(mock.pretend_url)
             self.assertEqual(certification_dialog.button_box.button(QDialogButtonBox.Ok).text(), "&Ok")
             QTest.mouseClick(certification_dialog.radio_pubkey, Qt.LeftButton)
             QTest.keyClicks(certification_dialog.edit_pubkey, "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn")
diff --git a/src/cutecoin/tests/identities_tab/__init__.py b/src/cutecoin/tests/identities_tab/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/cutecoin/tests/identities_tab/test_identities_table.py b/src/cutecoin/tests/identities_tab/test_identities_table.py
new file mode 100644
index 00000000..526a7c94
--- /dev/null
+++ b/src/cutecoin/tests/identities_tab/test_identities_table.py
@@ -0,0 +1,101 @@
+import sys
+import unittest
+import asyncio
+import quamash
+import logging
+import time
+from ucoinpy.documents.peer import BMAEndpoint as PyBMAEndpoint
+from PyQt5.QtWidgets import QDialog
+from PyQt5.QtCore import QLocale, Qt
+from PyQt5.QtTest import QTest
+from cutecoin.core.net.api import bma as qtbma
+from cutecoin.tests.mocks.bma import nice_blockchain
+from cutecoin.tests.mocks.access_manager import MockNetworkAccessManager
+from cutecoin.core.registry.identities import IdentitiesRegistry
+from cutecoin.gui.identities_tab import IdentitiesTabWidget
+from cutecoin.gui.password_asker import PasswordAskerDialog
+from cutecoin.core.app import Application
+from cutecoin.core import Account, Community, Wallet
+from cutecoin.core.net import Network, Node
+from cutecoin.core.net.endpoint import BMAEndpoint
+from cutecoin.core.net.api.bma.access import BmaAccess
+from cutecoin.tests import get_application
+
+
+class TestIdentitiesTable(unittest.TestCase):
+    def setUp(self):
+        self.qapplication = get_application()
+        self.network_manager = MockNetworkAccessManager()
+        QLocale.setDefault(QLocale("en_GB"))
+        self.lp = quamash.QEventLoop(self.qapplication)
+        asyncio.set_event_loop(self.lp)
+        self.identities_registry = IdentitiesRegistry()
+
+        self.application = Application(self.qapplication, self.lp, self.network_manager, self.identities_registry)
+        self.application.preferences['notifications'] = False
+
+        self.endpoint = BMAEndpoint(PyBMAEndpoint("", "127.0.0.1", "", 50000))
+        self.node = Node(self.network_manager, "test_currency", [self.endpoint],
+                         "", "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk",
+                         qtbma.blockchain.Block.null_value, Node.ONLINE,
+                         time.time(), {}, "ucoin", "0.14.0", 0)
+        self.network = Network.create(self.network_manager, self.node)
+        self.bma_access = BmaAccess.create(self.network)
+        self.community = Community("test_currency", self.network, self.bma_access)
+
+        self.wallet = Wallet(0, "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
+                             "Wallet 1", self.identities_registry)
+
+        # Salt/password : "testcutecoin/testcutecoin"
+        # Pubkey : 7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ
+        self.account = Account("testcutecoin", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
+                               "john", [self.community], [self.wallet], [], self.identities_registry)
+
+        self.password_asker = PasswordAskerDialog(self.account)
+        self.password_asker.password = "testcutecoin"
+        self.password_asker.remember = True
+
+    def tearDown(self):
+        try:
+            self.lp.close()
+        finally:
+            asyncio.set_event_loop(None)
+
+    def test_search_identity_found(self):
+        mock = nice_blockchain.get_mock()
+        logging.debug(mock.pretend_url)
+        self.network_manager.set_mock_path(mock.pretend_url)
+        identities_tab = IdentitiesTabWidget(self.application)
+        identities_tab.change_account(self.account)
+        identities_tab.change_community(self.community)
+
+        @asyncio.coroutine
+        def exec_test():
+            yield from asyncio.sleep(1)
+            self.assertEqual(mock.get_request(0).method, 'GET')
+            self.assertEqual(mock.get_request(0).url,
+                             '/wot/certifiers-of/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')
+
+            # requests 1 to 3 are for getting certifiers-of and certified-by
+            # on john, + a lookup
+
+            QTest.keyClicks(identities_tab.edit_textsearch, "doe")
+            QTest.mouseClick(identities_tab.button_search, Qt.LeftButton)
+            yield from asyncio.sleep(1)
+            self.assertEqual(mock.get_request(4).method, 'GET')
+            self.assertEqual(mock.get_request(4).url,
+                             '/wot/lookup/doe')
+            self.assertEqual(mock.get_request(5).method, 'GET')
+            self.assertEqual(mock.get_request(5).url,
+                             '/wot/certifiers-of/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn')
+            self.assertEqual(mock.get_request(6).method, 'GET')
+            self.assertEqual(mock.get_request(6).url,
+                             '/wot/lookup/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn')
+            self.assertEqual(identities_tab.table_identities.model().rowCount(), 1)
+
+        self.lp.run_until_complete(exec_test())
+
+if __name__ == '__main__':
+    logging.basicConfig( stream=sys.stderr )
+    logging.getLogger().setLevel( logging.DEBUG )
+    unittest.main()
diff --git a/src/cutecoin/tests/mocks/bma/nice_blockchain.py b/src/cutecoin/tests/mocks/bma/nice_blockchain.py
new file mode 100644
index 00000000..04bca2ed
--- /dev/null
+++ b/src/cutecoin/tests/mocks/bma/nice_blockchain.py
@@ -0,0 +1,119 @@
+from pretenders.client.http import HTTPMock
+from pretenders.common.constants import FOREVER
+
+bma_peering = b"""{
+  "version": 1,
+  "currency": "test_currency",
+  "endpoints": [
+    "BASIC_MERKLED_API localhost 127.0.0.1 50000"
+  ],
+  "status": "UP",
+  "block": "30152-00003E7F9234E7542FCF669B69B0F84FF79CCCD3",
+  "signature": "cXuqZuDfyHvxYAEUkPH1TQ1M+8YNDpj8kiHGYi3LIaMqEdVqwVc4yQYGivjxFMYyngRfxXkyvqBKZA6rKOulCA==",
+  "raw": "Version: 1\\nType: Peer\\nCurrency: meta_brouzouf\\nPublicKey: HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk\\nBlock: 30152-00003E7F9234E7542FCF669B69B0F84FF79CCCD3\\nEndpoints:\\nBASIC_MERKLED_API localhost 127.0.0.1 50000\\n",
+  "pubkey": "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk"
+}"""
+
+bma_lookup_john = b"""{
+  "partial": false,
+  "results": [
+    {
+      "pubkey": "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
+      "uids": [
+        {
+          "uid": "john",
+          "meta": {
+            "timestamp": 1441130831
+          },
+          "self": "ZrHK0cCqrxWReROK0ciiSb45+dRphJa68qFaSjdve8bBdnGAu7+DIu0d+u/fXrNRXuObihOKMBIawaIVPNHqDw==",
+          "others": []
+        }
+      ],
+      "signed": []
+    }
+  ]
+}"""
+
+bma_lookup_doe = b"""{
+  "partial": false,
+  "results": [
+    {
+      "pubkey": "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn",
+      "uids": [
+        {
+          "uid": "doe",
+          "meta": {
+            "timestamp": 1441130831
+          },
+          "self": "cIkHPQQ5+xTb4cKWv85rcYcZT+E3GDtX8B2nCK9Vs12p2Yz4bVaZiMvBBwisAAy2WBOaqHS3ydpXGtADchOICw==",
+          "others": []
+        }
+      ],
+      "signed": []
+    }
+  ]
+}"""
+
+bma_certifiers_of_john = b"""{
+  "pubkey": "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
+  "uid": "john",
+  "isMember": true,
+  "certifications": [
+  ]
+}"""
+
+bma_certified_by_john = b"""{
+  "pubkey": "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
+  "uid": "john",
+  "isMember": true,
+  "certifications": [
+  ]
+}"""
+
+
+def get_mock():
+    mock = HTTPMock('127.0.0.1', 50000)
+
+    mock.when('GET /network/peering')\
+        .reply(body=bma_peering,
+                times=FOREVER,
+                headers={'Content-Type': 'application/json'})
+
+    mock.when('GET /wot/certifiers-of/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\
+            .reply(body=bma_certifiers_of_john,
+                status=200,
+                times=FOREVER,
+                headers={'Content-Type': 'application/json'})
+
+    mock.when('GET /wot/certified-by/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\
+            .reply(body=bma_certified_by_john,
+                status=200,
+                times=FOREVER,
+                headers={'Content-Type': 'application/json'})
+
+    mock.when('GET /wot/lookup/john')\
+            .reply(body=bma_lookup_john,
+                status=200,
+                times=FOREVER,
+                headers={'Content-Type': 'application/json'})
+
+    mock.when('GET /wot/lookup/7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ')\
+            .reply(body=bma_lookup_john,
+                status=200,
+                times=FOREVER,
+                headers={'Content-Type': 'application/json'})
+
+    mock.when('GET /wot/lookup/doe')\
+            .reply(body=bma_lookup_doe,
+                status=200,
+                times=1,
+                headers={'Content-Type': 'application/json'})
+
+    mock.when('GET /wot/lookup/FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn')\
+            .reply(body=bma_lookup_doe,
+                status=200,
+                times=FOREVER,
+                headers={'Content-Type': 'application/json'})
+
+
+    return mock
diff --git a/src/cutecoin/tests/process_cfg_community/test_add_community.py b/src/cutecoin/tests/process_cfg_community/test_add_community.py
index f4811752..73da23fc 100644
--- a/src/cutecoin/tests/process_cfg_community/test_add_community.py
+++ b/src/cutecoin/tests/process_cfg_community/test_add_community.py
@@ -44,6 +44,7 @@ class ProcessAddCommunity(unittest.TestCase):
     def test_add_community_empty_blockchain(self):
         mock = new_blockchain.get_mock()
         logging.debug(mock.pretend_url)
+        self.network_manager.set_mock_path(mock.pretend_url)
         process_community = ProcessConfigureCommunity(self.application,
                                                     self.account,
                                                     None, self.password_asker)
@@ -60,7 +61,6 @@ class ProcessAddCommunity(unittest.TestCase):
         @asyncio.coroutine
         def exec_test():
             yield from asyncio.sleep(1)
-            self.network_manager.set_mock_path(mock.pretend_url)
             QTest.mouseClick(process_community.lineedit_server, Qt.LeftButton)
             QTest.keyClicks(process_community.lineedit_server, "127.0.0.1")
             QTest.mouseDClick(process_community.spinbox_port, Qt.LeftButton)
-- 
GitLab