From 46d1856f71e678109442a2668790a621e6d3c6ca Mon Sep 17 00:00:00 2001 From: tecnovert Date: Fri, 23 Sep 2022 22:07:41 +0200 Subject: [PATCH] ui: Render 404 for unknown pages. Wait for wallet update to complete, to handle corner case where user opens wallet page before refreshing. Withdraw txn in selenium test. --- basicswap/basicswap.py | 17 +++++-- basicswap/http_server.py | 4 +- basicswap/templates/wallet.html | 5 +- basicswap/ui/page_wallet.py | 3 +- tests/basicswap/selenium/test_wallets.py | 63 +++++++++++++++++++++--- 5 files changed, 75 insertions(+), 17 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index da2b711..c3b6554 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -5398,7 +5398,7 @@ class BasicSwap(BaseApp): finally: self._updating_wallets_info[int(coin)] = False - def updateWalletsInfo(self, force_update=False, only_coin=None): + def updateWalletsInfo(self, force_update=False, only_coin=None, wait_for_complete=False): now = int(time.time()) if not force_update and now - self._last_updated_wallets_info < 30: return @@ -5407,11 +5407,18 @@ class BasicSwap(BaseApp): continue if c not in chainparams: continue - if self.coin_clients[c]['connection_type'] == 'rpc': + cc = self.coin_clients[c] + if cc['connection_type'] == 'rpc': + if not force_update and now - cc.get('last_updated_wallet_info', 0) < 30: + return + cc['last_updated_wallet_info'] = int(time.time()) self._updating_wallets_info[int(c)] = True - self.thread_pool.submit(self.updateWalletInfo, c) - if only_coin is None: - self._last_updated_wallets_info = int(time.time()) + handle = self.thread_pool.submit(self.updateWalletInfo, c) + if wait_for_complete: + try: + handle.result(timeout=10) + except Exception as e: + self.log.error(f'updateWalletInfo {e}') def getWalletsInfo(self, opts=None): rv = {} diff --git a/basicswap/http_server.py b/basicswap/http_server.py index de6445a..dad5527 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -146,7 +146,7 @@ class HttpHandler(BaseHTTPRequestHandler): template = env.get_template('info.html') return self.render_simple_template(template, { 'title_str': 'BasicSwap Info', - 'message_str': error_str, + 'message_str': info_str, }) def page_error(self, error_str): @@ -780,6 +780,8 @@ class HttpHandler(BaseHTTPRequestHandler): return page_automation_strategy_new(self, url_split, post_string) if page == 'shutdown': return self.page_shutdown(url_split, post_string) + if page != '': + return self.page_404(url_split) return self.page_index(url_split) except Exception as ex: if self.server.swap_client.debug is True: diff --git a/basicswap/templates/wallet.html b/basicswap/templates/wallet.html index 9b418a2..5612695 100644 --- a/basicswap/templates/wallet.html +++ b/basicswap/templates/wallet.html @@ -1,6 +1,5 @@ {% include 'header.html' %} -

refresh

back

@@ -10,7 +9,7 @@ {% endif %} {% for m in messages %} -

{{ m }}

+

{{ m }}

{% endfor %}
@@ -47,7 +46,7 @@ Main Address{{ w.main_address }} {{ w.deposit_address }} {% else %} -{{ w.deposit_address }} +{{ w.deposit_address }} {% endif %} Amount: Address: Subtract fee: {% if w.cid == '1' %} diff --git a/basicswap/ui/page_wallet.py b/basicswap/ui/page_wallet.py index 432d52d..7d7890c 100644 --- a/basicswap/ui/page_wallet.py +++ b/basicswap/ui/page_wallet.py @@ -239,7 +239,8 @@ def page_wallet(self, url_split, post_string): if swap_client.debug is True: swap_client.log.error(traceback.format_exc()) - swap_client.updateWalletsInfo() + swap_client.updateWalletsInfo(only_coin=coin_id, wait_for_complete=True) + wallets = swap_client.getCachedWalletsInfo({'coin_id': coin_id}) for k in wallets.keys(): w = wallets[k] diff --git a/tests/basicswap/selenium/test_wallets.py b/tests/basicswap/selenium/test_wallets.py index 9c59bcc..342d44b 100644 --- a/tests/basicswap/selenium/test_wallets.py +++ b/tests/basicswap/selenium/test_wallets.py @@ -7,7 +7,7 @@ """ cd /tmp -wget -4 https://chromedriver.storage.googleapis.com/96.0.4664.45/chromedriver_linux64.zip +wget -4 https://chromedriver.storage.googleapis.com/105.0.5195.52/chromedriver_linux64.zip 7z x chromedriver_linux64.zip sudo mv chromedriver /opt/chromedriver96 @@ -16,12 +16,12 @@ python tests/basicswap/extended/test_xmr_persistent.py python tests/basicswap/selenium/test_wallets.py -html = driver.page_source -print('html', html) - """ +import json import time + +from urllib.request import urlopen from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service @@ -29,20 +29,69 @@ from selenium.webdriver.chrome.service import Service def test_html(): base_url = 'http://localhost:12701' + node2_url = 'http://localhost:12702' driver = webdriver.Chrome(service=Service('/opt/chromedriver96')) - url = base_url + '/wallets' + + # Check json coins data + coins = json.loads(urlopen(base_url + '/json/coins').read()) + part_coin = [f for f in coins if f['ticker'] == 'PART'][0] + part_id = part_coin['id'] + assert (part_id == 1) + + # Check 404 pages + url = base_url + '/unknown' + driver.get(url) + p1 = driver.find_element(By.TAG_NAME, 'p') + assert ('404' in p1.text) + + url = base_url + '/static/nothing.png' + driver.get(url) + p1 = driver.find_element(By.TAG_NAME, 'p') + assert ('404' in p1.text) + + url = base_url + '/wallet' + driver.get(url) + h2 = driver.find_element(By.TAG_NAME, 'h2') + assert ('Error' in h2.text) + p1 = driver.find_element(By.TAG_NAME, 'p') + assert ('Wallet not specified' in p1.text) + + url = base_url + '/wallet/NOCOIN' driver.get(url) + h2 = driver.find_element(By.TAG_NAME, 'h2') + assert ('Error' in h2.text) + p1 = driver.find_element(By.TAG_NAME, 'p') + assert ('Unknown coin' in p1.text) + driver.get(base_url + '/wallets') time.sleep(1) driver.refresh() - - driver.find_element(By.ID, "refresh").click() + driver.find_element(By.ID, 'refresh').click() time.sleep(1) driver.refresh() + print('Finding deposit address of node 2') + driver.get(node2_url + '/wallet/PART') + e = driver.find_element(By.ID, 'deposit_address') + node2_deposit_address = e.text + + print('Withdrawing from node 1') + driver.get(base_url + '/wallet/PART') + driver.find_element(By.NAME, f'to_{part_id}').send_keys(node2_deposit_address) + driver.find_element(By.NAME, f'amt_{part_id}').send_keys('10') + driver.find_element(By.NAME, f'withdraw_{part_id}').click() + driver.switch_to.alert.accept() + time.sleep(1) + elements = driver.find_elements(By.CLASS_NAME, "infomsg") + assert (len(elements) == 1) + e = elements[0] + assert ('Withdrew 10 rtPART (plain to plain) to address' in e.text) + driver.close() + print('Done.') + if __name__ == '__main__': test_html()