diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index e2f194db..3b7ec4c4 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -303,6 +303,11 @@ class GuiBaseTest(object): # We should have timed out now self.assertEqual(mode.server_status.status, 0) + # Hack to close an Alert dialog that would otherwise block tests + def accept_dialog(self): + window = self.gui.qtapp.activeWindow() + if window: + window.close() # 'Grouped' tests follow from here diff --git a/tests/GuiReceiveTest.py b/tests/GuiReceiveTest.py index 84d6a55a..0c0cb770 100644 --- a/tests/GuiReceiveTest.py +++ b/tests/GuiReceiveTest.py @@ -1,6 +1,6 @@ import os import requests -from PyQt5 import QtTest +from PyQt5 import QtCore, QtTest from .GuiBaseTest import GuiBaseTest class GuiReceiveTest(GuiBaseTest): @@ -24,8 +24,7 @@ class GuiReceiveTest(GuiBaseTest): path = 'http://127.0.0.1:{}/upload'.format(self.gui.app.port) response = requests.post(path, files=files) - # A nasty hack to avoid the Alert dialog that blocks the rest of the test - self.gui.qtapp.exit() + QtCore.QTimer.singleShot(1000, self.accept_dialog) self.assertTrue('Error uploading, please inform the OnionShare user' in response.text) def upload_dir_permissions(self, mode=0o755): diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index 4f2f58e7..716bab73 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -12,10 +12,10 @@ class GuiShareTest(GuiBaseTest): self.assertEqual(self.gui.share_mode.server_status.web.slug, slug) # Share-specific tests - - def file_selection_widget_has_files(self): - '''Test that the number of items in the list is 2''' - self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 2) + + def file_selection_widget_has_files(self, num=2): + '''Test that the number of items in the list is as expected''' + self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), num) def deleting_all_files_hides_delete_button(self): @@ -40,14 +40,14 @@ class GuiShareTest(GuiBaseTest): '''Test that we can also delete a file by clicking on its [X] widget''' self.gui.share_mode.server_status.file_selection.file_list.add_file('/etc/hosts') QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.item(0).item_button, QtCore.Qt.LeftButton) - self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 0) + self.file_selection_widget_has_files(0) def file_selection_widget_readd_files(self): '''Re-add some files to the list so we can share''' self.gui.share_mode.server_status.file_selection.file_list.add_file('/etc/hosts') self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/test.txt') - self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 2) + self.file_selection_widget_has_files(2) def add_large_file(self): @@ -102,7 +102,7 @@ class GuiShareTest(GuiBaseTest): # A nasty hack to avoid the Alert dialog that blocks the rest of the test if not public_mode: - self.gui.qtapp.exit() + QtCore.QTimer.singleShot(1000, self.accept_dialog) # In public mode, we should still be running (no rate-limiting) if public_mode: @@ -195,3 +195,10 @@ class GuiShareTest(GuiBaseTest): self.server_timed_out(self.gui.share_mode, 10000) self.web_server_is_stopped() + + def run_all_share_mode_unreadable_file_tests(self): + '''Attempt to share an unreadable file''' + self.run_all_share_mode_setup_tests() + QtCore.QTimer.singleShot(1000, self.accept_dialog) + self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/nonexistent.txt') + self.file_selection_widget_has_files(2) diff --git a/tests/TorGuiBaseTest.py b/tests/TorGuiBaseTest.py index 611cb202..2c88bb94 100644 --- a/tests/TorGuiBaseTest.py +++ b/tests/TorGuiBaseTest.py @@ -162,3 +162,12 @@ class TorGuiBaseTest(GuiBaseTest): def hidserv_auth_string(self): '''Test the validity of the HidservAuth string''' self.assertRegex(self.gui.app.auth_string, r'HidServAuth {} [a-zA-Z1-9]'.format(self.gui.app.onion_host)) + + + + # Miscellaneous tests + def tor_killed_statusbar_message_shown(self, mode): + '''Test that the status bar message shows Tor was disconnected''' + self.gui.app.onion.c = None + QtTest.QTest.qWait(1000) + self.assertTrue(mode.status_bar.currentMessage(), strings._('gui_tor_connection_lost')) diff --git a/tests/local_onionshare_share_mode_timer_too_short_test.py b/tests/local_onionshare_share_mode_timer_too_short_test.py new file mode 100644 index 00000000..16153c3e --- /dev/null +++ b/tests/local_onionshare_share_mode_timer_too_short_test.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +import unittest +from PyQt5 import QtCore, QtTest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeTimerTooShortTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": False, + "shutdown_timeout": True, + } + cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModeTimerTooShortTest') + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + def test_gui(self): + self.run_all_common_setup_tests() + self.run_all_share_mode_setup_tests() + # Set a low timeout + self.set_timeout(self.gui.share_mode, 2) + QtTest.QTest.qWait(3000) + QtCore.QTimer.singleShot(4000, self.accept_dialog) + QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) + self.assertEqual(self.gui.share_mode.server_status.status, 0) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_unreadable_file_test.py b/tests/local_onionshare_share_mode_unreadable_file_test.py new file mode 100644 index 00000000..0e0970ea --- /dev/null +++ b/tests/local_onionshare_share_mode_unreadable_file_test.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeUnReadableFileTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModeUnReadableFileTest') + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + def test_gui(self): + self.run_all_common_setup_tests() + self.run_all_share_mode_unreadable_file_tests() + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_tor_connection_killed_test.py b/tests/onionshare_share_mode_tor_connection_killed_test.py new file mode 100644 index 00000000..382ed547 --- /dev/null +++ b/tests/onionshare_share_mode_tor_connection_killed_test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class ShareModeTorConnectionKilledTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModeTorConnectionKilledTest') + + @pytest.mark.tor + def test_gui(self): + self.run_all_common_setup_tests() + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(False) + self.tor_killed_statusbar_message_shown(self.gui.share_mode) + self.server_is_stopped(self.gui.share_mode, False) + self.web_server_is_stopped() + + +if __name__ == "__main__": + unittest.main()