From b39162f33c963bc2740b5f8dc47e1392a42b9189 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 10 May 2021 15:05:16 +1000 Subject: [PATCH] Add tests for Chat, and fix the server_status message bar when in Chat mode --- .../src/onionshare/resources/locale/en.json | 6 +- desktop/src/onionshare/tab/tab.py | 8 +- desktop/tests/gui_base_test.py | 13 ++++ desktop/tests/run.sh | 3 +- desktop/tests/test_gui_chat.py | 75 +++++++++++++++++++ desktop/tests/test_gui_tabs.py | 30 ++++++-- 6 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 desktop/tests/test_gui_chat.py diff --git a/desktop/src/onionshare/resources/locale/en.json b/desktop/src/onionshare/resources/locale/en.json index 8a69142c..a847264b 100644 --- a/desktop/src/onionshare/resources/locale/en.json +++ b/desktop/src/onionshare/resources/locale/en.json @@ -101,6 +101,10 @@ "gui_status_indicator_receive_working": "Starting…", "gui_status_indicator_receive_scheduled": "Scheduled…", "gui_status_indicator_receive_started": "Receiving", + "gui_status_indicator_chat_stopped": "Ready to chat", + "gui_status_indicator_chat_working": "Starting…", + "gui_status_indicator_chat_scheduled": "Scheduled…", + "gui_status_indicator_chat_started": "Chatting", "gui_file_info": "{} files, {}", "gui_file_info_single": "{} file, {}", "history_in_progress_tooltip": "{} in progress", @@ -198,4 +202,4 @@ "error_port_not_available": "OnionShare port not available", "history_receive_read_message_button": "Read Message", "error_tor_protocol_error": "There was an error with Tor: {}" -} \ No newline at end of file +} diff --git a/desktop/src/onionshare/tab/tab.py b/desktop/src/onionshare/tab/tab.py index d39cf826..09982de9 100644 --- a/desktop/src/onionshare/tab/tab.py +++ b/desktop/src/onionshare/tab/tab.py @@ -452,20 +452,20 @@ class Tab(QtWidgets.QWidget): # Chat mode if self.chat_mode.server_status.status == ServerStatus.STATUS_STOPPED: self.set_server_status_indicator_stopped( - strings._("gui_status_indicator_receive_stopped") + strings._("gui_status_indicator_chat_stopped") ) elif self.chat_mode.server_status.status == ServerStatus.STATUS_WORKING: if self.settings.get("general", "autostart_timer"): self.set_server_status_indicator_working( - strings._("gui_status_indicator_receive_scheduled") + strings._("gui_status_indicator_chat_scheduled") ) else: self.set_server_status_indicator_working( - strings._("gui_status_indicator_receive_working") + strings._("gui_status_indicator_chat_working") ) elif self.chat_mode.server_status.status == ServerStatus.STATUS_STARTED: self.set_server_status_indicator_started( - strings._("gui_status_indicator_receive_started") + strings._("gui_status_indicator_chat_started") ) def set_server_status_indicator_stopped(self, label_text): diff --git a/desktop/tests/gui_base_test.py b/desktop/tests/gui_base_test.py index c6a5da2f..3a38ff8e 100644 --- a/desktop/tests/gui_base_test.py +++ b/desktop/tests/gui_base_test.py @@ -14,6 +14,7 @@ from onionshare import Application, MainWindow, GuiCommon from onionshare.tab.mode.share_mode import ShareMode from onionshare.tab.mode.receive_mode import ReceiveMode from onionshare.tab.mode.website_mode import WebsiteMode +from onionshare.tab.mode.chat_mode import ChatMode from onionshare import strings @@ -133,6 +134,17 @@ class GuiBaseTest(unittest.TestCase): return tab + def new_chat_tab(self): + tab = self.gui.tabs.widget(0) + self.verify_new_tab(tab) + + # Chat + tab.chat_button.click() + self.assertFalse(tab.new_tab.isVisible()) + self.assertTrue(tab.chat_mode.isVisible()) + + return tab + def close_all_tabs(self): for _ in range(self.gui.tabs.count()): tab = self.gui.tabs.widget(0) @@ -361,6 +373,7 @@ class GuiBaseTest(unittest.TestCase): and not tab.settings.get("share", "autostop_sharing") ) or (type(tab.get_mode()) == WebsiteMode) + or (type(tab.get_mode()) == ChatMode) ): tab.get_mode().server_status.server_button.click() self.assertEqual(tab.get_mode().server_status.status, 0) diff --git a/desktop/tests/run.sh b/desktop/tests/run.sh index 833c1516..0f019b05 100755 --- a/desktop/tests/run.sh +++ b/desktop/tests/run.sh @@ -2,4 +2,5 @@ pytest -v tests/test_gui_tabs.py && \ pytest -v tests/test_gui_share.py && \ pytest -v tests/test_gui_receive.py && \ -pytest -v tests/test_gui_website.py +pytest -v tests/test_gui_website.py && \ +pytest -v tests/test_gui_chat.py diff --git a/desktop/tests/test_gui_chat.py b/desktop/tests/test_gui_chat.py new file mode 100644 index 00000000..7a19168b --- /dev/null +++ b/desktop/tests/test_gui_chat.py @@ -0,0 +1,75 @@ +import requests + +from PySide2 import QtTest + +from .gui_base_test import GuiBaseTest + + +class TestChat(GuiBaseTest): + # Shared test methods + + def view_chat(self, tab): + """Test that we can view the chat room""" + url = f"http://127.0.0.1:{tab.app.port}/" + if tab.settings.get("general", "public"): + r = requests.get(url) + else: + r = requests.get( + url, + auth=requests.auth.HTTPBasicAuth( + "onionshare", tab.get_mode().server_status.web.password + ), + ) + + QtTest.QTest.qWait(500, self.gui.qtapp) + self.assertTrue("Chat requires JavaScript" in r.text) + + cookies_dict = requests.utils.dict_from_cookiejar(r.cookies) + self.assertTrue("session" in cookies_dict.keys()) + + def change_username(self, tab): + """Test that we can change our username""" + url = f"http://127.0.0.1:{tab.app.port}/update-session-username" + data = {"username":"oniontest"} + if tab.settings.get("general", "public"): + r = requests.post(url, json=data) + else: + r = requests.post( + url, + json=data, + auth=requests.auth.HTTPBasicAuth( + "onionshare", tab.get_mode().server_status.web.password + ), + ) + + QtTest.QTest.qWait(500, self.gui.qtapp) + jsonResponse = r.json() + self.assertTrue(jsonResponse["success"]) + self.assertEqual(jsonResponse["username"], "oniontest") + + def run_all_chat_mode_tests(self, tab): + """Tests in chat mode after starting a chat""" + self.server_working_on_start_button_pressed(tab) + self.server_status_indicator_says_starting(tab) + self.server_is_started(tab, startup_time=500) + self.web_server_is_running(tab) + self.have_a_password(tab) + self.url_description_shown(tab) + self.have_copy_url_button(tab) + self.have_show_qr_code_button(tab) + self.server_status_indicator_says_started(tab) + self.view_chat(tab) + self.change_username(tab) + self.server_is_stopped(tab) + self.web_server_is_stopped(tab) + self.server_status_indicator_says_closed(tab) + + # Tests + + def test_chat(self): + """ + Test chat mode + """ + tab = self.new_chat_tab() + self.run_all_chat_mode_tests(tab) + self.close_all_tabs() diff --git a/desktop/tests/test_gui_tabs.py b/desktop/tests/test_gui_tabs.py index 84558420..09f38bb4 100644 --- a/desktop/tests/test_gui_tabs.py +++ b/desktop/tests/test_gui_tabs.py @@ -153,11 +153,21 @@ class TestTabs(GuiBaseTest): self.gui.status_bar.server_status_label.text(), "Ready to share" ) + # New tab, chat mode + self.gui.tabs.new_tab_button.click() + self.gui.tabs.widget(4).chat_button.click() + self.assertFalse(self.gui.tabs.widget(4).new_tab.isVisible()) + self.assertTrue(self.gui.tabs.widget(4).chat_mode.isVisible()) + self.assertEqual( + self.gui.status_bar.server_status_label.text(), "Ready to chat" + ) + # Close tabs self.gui.tabs.tabBar().tabButton(0, QtWidgets.QTabBar.RightSide).click() self.gui.tabs.tabBar().tabButton(0, QtWidgets.QTabBar.RightSide).click() self.gui.tabs.tabBar().tabButton(0, QtWidgets.QTabBar.RightSide).click() self.gui.tabs.tabBar().tabButton(0, QtWidgets.QTabBar.RightSide).click() + self.gui.tabs.tabBar().tabButton(0, QtWidgets.QTabBar.RightSide).click() def test_07_close_share_tab_while_server_started_should_warn(self): """Closing a share mode tab when the server is running should throw a warning""" @@ -165,7 +175,7 @@ class TestTabs(GuiBaseTest): self.close_tab_with_active_server(tab) def test_08_close_receive_tab_while_server_started_should_warn(self): - """Closing a recieve mode tab when the server is running should throw a warning""" + """Closing a receive mode tab when the server is running should throw a warning""" tab = self.new_receive_tab() self.close_tab_with_active_server(tab) @@ -174,22 +184,32 @@ class TestTabs(GuiBaseTest): tab = self.new_website_tab_with_files() self.close_tab_with_active_server(tab) - def test_10_close_persistent_share_tab_shows_warning(self): + def test_10_close_chat_tab_while_server_started_should_warn(self): + """Closing a chat mode tab when the server is running should throw a warning""" + tab = self.new_chat_tab() + self.close_tab_with_active_server(tab) + + def test_11_close_persistent_share_tab_shows_warning(self): """Closing a share mode tab that's persistent should show a warning""" tab = self.new_share_tab_with_files() self.close_persistent_tab(tab) - def test_11_close_persistent_receive_tab_shows_warning(self): + def test_12_close_persistent_receive_tab_shows_warning(self): """Closing a receive mode tab that's persistent should show a warning""" tab = self.new_receive_tab() self.close_persistent_tab(tab) - def test_12_close_persistent_website_tab_shows_warning(self): + def test_13_close_persistent_website_tab_shows_warning(self): """Closing a website mode tab that's persistent should show a warning""" tab = self.new_website_tab_with_files() self.close_persistent_tab(tab) - def test_13_quit_with_server_started_should_warn(self): + def test_14_close_persistent_chat_tab_shows_warning(self): + """Closing a chat mode tab that's persistent should show a warning""" + tab = self.new_chat_tab() + self.close_persistent_tab(tab) + + def test_15_quit_with_server_started_should_warn(self): """Quitting OnionShare with any active servers should show a warning""" tab = self.new_share_tab()