From fe091db5960db2afa33533e54f2addb660045c3f Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Fri, 12 Oct 2018 11:28:47 +1100 Subject: [PATCH] Refactor tests to use proper inheritance of GuiReceiveTest/GuiShareTest (which inherit from GuiBaseTest). Prevent tests from auto-firing in these base objects. Clean up imported modules, rename files to end in _test.py --- tests_gui_local/GuiBaseTest.py | 260 ++++-------------- tests_gui_local/GuiReceiveTest.py | 45 +++ tests_gui_local/GuiShareTest.py | 154 +++++++++++ tests_gui_local/__init__.py | 1 - ...re_receive_mode_upload_public_mode_test.py | 29 ++ .../onionshare_receive_mode_upload_test.py | 26 +- ...re_receive_mode_upload_test_public_mode.py | 39 --- ...re_share_mode_download_public_mode_test.py | 28 ++ ...hare_share_mode_download_stay_open_test.py | 28 ++ .../onionshare_share_mode_download_test.py | 24 +- ...re_share_mode_download_test_public_mode.py | 38 --- ...hare_share_mode_download_test_stay_open.py | 38 --- ...nshare_share_mode_slug_persistent_test.py} | 24 +- .../onionshare_share_mode_timer_test.py | 29 ++ tests_gui_local/onionshare_timer_test.py | 39 --- 15 files changed, 389 insertions(+), 413 deletions(-) create mode 100644 tests_gui_local/GuiReceiveTest.py create mode 100644 tests_gui_local/GuiShareTest.py create mode 100644 tests_gui_local/onionshare_receive_mode_upload_public_mode_test.py delete mode 100644 tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py create mode 100644 tests_gui_local/onionshare_share_mode_download_public_mode_test.py create mode 100644 tests_gui_local/onionshare_share_mode_download_stay_open_test.py delete mode 100644 tests_gui_local/onionshare_share_mode_download_test_public_mode.py delete mode 100644 tests_gui_local/onionshare_share_mode_download_test_stay_open.py rename tests_gui_local/{onionshare_slug_persistent_test.py => onionshare_share_mode_slug_persistent_test.py} (61%) create mode 100644 tests_gui_local/onionshare_share_mode_timer_test.py delete mode 100644 tests_gui_local/onionshare_timer_test.py diff --git a/tests_gui_local/GuiBaseTest.py b/tests_gui_local/GuiBaseTest.py index e7d25031..8a8b127e 100644 --- a/tests_gui_local/GuiBaseTest.py +++ b/tests_gui_local/GuiBaseTest.py @@ -1,10 +1,9 @@ +import json import os import requests +import shutil import socket import socks -import zipfile -import json -import shutil from PyQt5 import QtCore, QtTest @@ -59,24 +58,28 @@ class GuiBaseTest(object): except: pass - - def test_gui_loaded(self): + + def gui_loaded(self): '''Test that the GUI actually is shown''' self.assertTrue(self.gui.show) - def test_windowTitle_seen(self): + + def windowTitle_seen(self): '''Test that the window title is OnionShare''' self.assertEqual(self.gui.windowTitle(), 'OnionShare') - def test_settings_button_is_visible(self): + + def settings_button_is_visible(self): '''Test that the settings button is visible''' self.assertTrue(self.gui.settings_button.isVisible()) - def test_server_status_bar_is_visible(self): + + def server_status_bar_is_visible(self): '''Test that the status bar is visible''' self.assertTrue(self.gui.status_bar.isVisible()) - def test_click_mode(self, mode): + + def click_mode(self, mode): '''Test that we can switch Mode by clicking the button''' if type(mode) == ReceiveMode: QtTest.QTest.mouseClick(self.gui.receive_mode_button, QtCore.Qt.LeftButton) @@ -85,13 +88,15 @@ class GuiBaseTest(object): QtTest.QTest.mouseClick(self.gui.share_mode_button, QtCore.Qt.LeftButton) self.assertTrue(self.gui.mode, self.gui.MODE_SHARE) - def test_click_toggle_history(self, mode): + + def click_toggle_history(self, mode): '''Test that we can toggle Download or Upload history by clicking the toggle button''' currently_visible = mode.history.isVisible() QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) self.assertEqual(mode.history.isVisible(), not currently_visible) - def test_history_indicator(self, mode, public_mode): + + def history_indicator(self, mode, public_mode): '''Test that we can make sure the history is toggled off, do an action, and the indiciator works''' # Make sure history is toggled off if mode.history.isVisible(): @@ -128,63 +133,75 @@ class GuiBaseTest(object): QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) self.assertFalse(mode.toggle_history.indicator_label.isVisible()) - def test_history_is_not_visible(self, mode): + + def history_is_not_visible(self, mode): '''Test that the History section is not visible''' self.assertFalse(mode.history.isVisible()) - def test_history_is_visible(self, mode): + + def history_is_visible(self, mode): '''Test that the History section is visible''' self.assertTrue(mode.history.isVisible()) - def test_server_working_on_start_button_pressed(self, mode): + + def server_working_on_start_button_pressed(self, mode): '''Test we can start the service''' # Should be in SERVER_WORKING state QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton) self.assertEqual(mode.server_status.status, 1) - def test_server_status_indicator_says_starting(self, mode): + + def server_status_indicator_says_starting(self, mode): '''Test that the Server Status indicator shows we are Starting''' self.assertEquals(mode.server_status_label.text(), strings._('gui_status_indicator_share_working')) - def test_settings_button_is_hidden(self): + + def settings_button_is_hidden(self): '''Test that the settings button is hidden when the server starts''' self.assertFalse(self.gui.settings_button.isVisible()) - def test_a_server_is_started(self, mode): + + def a_server_is_started(self, mode): '''Test that the server has started''' QtTest.QTest.qWait(2000) # Should now be in SERVER_STARTED state self.assertEqual(mode.server_status.status, 2) - def test_a_web_server_is_running(self): + + def a_web_server_is_running(self): '''Test that the web server has started''' sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.assertEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) - def test_have_a_slug(self, mode, public_mode): + + def have_a_slug(self, mode, public_mode): '''Test that we have a valid slug''' if not public_mode: self.assertRegex(mode.server_status.web.slug, r'(\w+)-(\w+)') else: self.assertIsNone(mode.server_status.web.slug, r'(\w+)-(\w+)') - def test_url_description_shown(self, mode): + + def url_description_shown(self, mode): '''Test that the URL label is showing''' self.assertTrue(mode.server_status.url_description.isVisible()) - def test_have_copy_url_button(self, mode): + + def have_copy_url_button(self, mode): '''Test that the Copy URL button is shown''' self.assertTrue(mode.server_status.copy_url_button.isVisible()) - def test_server_status_indicator_says_started(self, mode): + + def server_status_indicator_says_started(self, mode): '''Test that the Server Status indicator shows we are started''' if type(mode) == ReceiveMode: self.assertEquals(mode.server_status_label.text(), strings._('gui_status_indicator_receive_started')) if type(mode) == ShareMode: self.assertEquals(mode.server_status_label.text(), strings._('gui_status_indicator_share_started')) - def test_web_page(self, mode, string, public_mode): + + def web_page(self, mode, string, public_mode): '''Test that the web page contains a string''' s = socks.socksocket() s.settimeout(60) @@ -212,22 +229,26 @@ class GuiBaseTest(object): self.assertTrue(string in f.read()) f.close() - def test_history_widgets_present(self, mode): + + def history_widgets_present(self, mode): '''Test that the relevant widgets are present in the history view after activity has taken place''' self.assertFalse(mode.history.empty.isVisible()) self.assertTrue(mode.history.not_empty.isVisible()) - def test_counter_incremented(self, mode, count): + + def counter_incremented(self, mode, count): '''Test that the counter has incremented''' self.assertEquals(mode.history.completed_count, count) - def test_server_is_stopped(self, mode, stay_open): + + def server_is_stopped(self, mode, stay_open): '''Test that the server stops when we click Stop''' if type(mode) == ReceiveMode or (type(mode) == ShareMode and stay_open): QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton) self.assertEquals(mode.server_status.status, 0) - def test_web_service_is_stopped(self): + + def web_service_is_stopped(self): '''Test that the web server also stopped''' QtTest.QTest.qWait(2000) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -235,7 +256,8 @@ class GuiBaseTest(object): # We should be closed by now. Fail if not! self.assertNotEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) - def test_server_status_indicator_says_closed(self, mode, stay_open): + + def server_status_indicator_says_closed(self, mode, stay_open): '''Test that the Server Status indicator shows we closed''' if type(mode) == ReceiveMode: self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_stopped')) @@ -245,184 +267,10 @@ class GuiBaseTest(object): else: self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically')) - # Auto-stop timer tests - def test_set_timeout(self, mode, timeout): - '''Test that the timeout can be set''' - timer = QtCore.QDateTime.currentDateTime().addSecs(timeout) - mode.server_status.shutdown_timeout.setDateTime(timer) - self.assertTrue(mode.server_status.shutdown_timeout.dateTime(), timer) - - def test_timeout_widget_hidden(self, mode): - '''Test that the timeout widget is hidden when share has started''' - self.assertFalse(mode.server_status.shutdown_timeout_container.isVisible()) - - def test_server_timed_out(self, mode, wait): - '''Test that the server has timed out after the timer ran out''' - QtTest.QTest.qWait(wait) - # We should have timed out now - self.assertEqual(mode.server_status.status, 0) - - # Receive-specific tests - def test_upload_file(self, public_mode, expected_file): - '''Test that we can upload the file''' - files = {'file[]': open('/tmp/test.txt', 'rb')} - if not public_mode: - path = 'http://127.0.0.1:{}/{}/upload'.format(self.gui.app.port, self.gui.receive_mode.web.slug) - else: - path = 'http://127.0.0.1:{}/upload'.format(self.gui.app.port) - response = requests.post(path, files=files) - QtTest.QTest.qWait(2000) - self.assertTrue(os.path.isfile(expected_file)) - - # Share-specific tests - def test_file_selection_widget_has_a_file(self): - '''Test that the number of files in the list is 1''' - self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 1) - - def test_deleting_only_file_hides_delete_button(self): - '''Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button''' - rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(self.gui.share_mode.server_status.file_selection.file_list.item(0)) - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.viewport(), QtCore.Qt.LeftButton, pos=rect.center()) - # Delete button should be visible - self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) - # Click delete, and since there's no more files, the delete button should be hidden - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton) - self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) - - def test_add_a_file_and_delete_using_its_delete_widget(self): - '''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) - - def test_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) - - def test_add_delete_buttons_hidden(self): - '''Test that the add and delete buttons are hidden when the server starts''' - self.assertFalse(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) - self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) - - def test_download_share(self, public_mode): - '''Test that we can download the share''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - if public_mode: - path = '/download' - else: - path = '{}/download'.format(self.gui.share_mode.web.slug) - - http_request = 'GET {} HTTP/1.0\r\n'.format(path) - http_request += 'Host: 127.0.0.1\r\n' - http_request += '\r\n' - s.sendall(http_request.encode('utf-8')) - - with open('/tmp/download.zip', 'wb') as file_to_write: - while True: - data = s.recv(1024) - if not data: - break - file_to_write.write(data) - file_to_write.close() - - zip = zipfile.ZipFile('/tmp/download.zip') - QtTest.QTest.qWait(2000) - self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8')) - - def test_add_button_visible(self): - '''Test that the add button should be visible''' - self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) - - - # The following are 'groupings' of tests used by other objects that inherit GuiBaseTest - def run_all_common_setup_tests(self): - GuiBaseTest.test_gui_loaded(self) - GuiBaseTest.test_windowTitle_seen(self) - GuiBaseTest.test_settings_button_is_visible(self) - GuiBaseTest.test_server_status_bar_is_visible(self) + self.gui_loaded() + self.windowTitle_seen() + self.settings_button_is_visible() + self.server_status_bar_is_visible() - def run_all_share_mode_setup_tests(self): - """Tests in share mode prior to starting a share""" - GuiBaseTest.test_click_mode(self, self.gui.share_mode) - GuiBaseTest.test_file_selection_widget_has_a_file(self) - GuiBaseTest.test_history_is_not_visible(self, self.gui.share_mode) - GuiBaseTest.test_click_toggle_history(self, self.gui.share_mode) - GuiBaseTest.test_history_is_visible(self, self.gui.share_mode) - GuiBaseTest.test_deleting_only_file_hides_delete_button(self) - GuiBaseTest.test_add_a_file_and_delete_using_its_delete_widget(self) - GuiBaseTest.test_file_selection_widget_readd_files(self) - def run_all_share_mode_started_tests(self, public_mode): - """Tests in share mode after starting a share""" - GuiBaseTest.test_server_working_on_start_button_pressed(self, self.gui.share_mode) - GuiBaseTest.test_server_status_indicator_says_starting(self, self.gui.share_mode) - GuiBaseTest.test_add_delete_buttons_hidden(self) - GuiBaseTest.test_settings_button_is_hidden(self) - GuiBaseTest.test_a_server_is_started(self, self.gui.share_mode) - GuiBaseTest.test_a_web_server_is_running(self) - GuiBaseTest.test_have_a_slug(self, self.gui.share_mode, public_mode) - GuiBaseTest.test_url_description_shown(self, self.gui.share_mode) - GuiBaseTest.test_have_copy_url_button(self, self.gui.share_mode) - GuiBaseTest.test_server_status_indicator_says_started(self, self.gui.share_mode) - GuiBaseTest.test_web_page(self, self.gui.share_mode, 'Total size', public_mode) - - def run_all_share_mode_download_tests(self, public_mode, stay_open): - """Tests in share mode after downloading a share""" - GuiBaseTest.test_download_share(self, public_mode) - GuiBaseTest.test_history_widgets_present(self, self.gui.share_mode) - GuiBaseTest.test_server_is_stopped(self, self.gui.share_mode, stay_open) - GuiBaseTest.test_web_service_is_stopped(self) - GuiBaseTest.test_server_status_indicator_says_closed(self, self.gui.share_mode, stay_open) - GuiBaseTest.test_add_button_visible(self) - GuiBaseTest.test_server_working_on_start_button_pressed(self, self.gui.share_mode) - GuiBaseTest.test_a_server_is_started(self, self.gui.share_mode) - GuiBaseTest.test_history_indicator(self, self.gui.share_mode, public_mode) - - def run_all_share_mode_tests(self, public_mode, stay_open): - """End-to-end share tests""" - GuiBaseTest.run_all_share_mode_setup_tests(self) - GuiBaseTest.run_all_share_mode_started_tests(self, public_mode) - GuiBaseTest.run_all_share_mode_download_tests(self, public_mode, stay_open) - - def run_all_share_mode_timer_tests(self, public_mode): - """Auto-stop timer tests in share mode""" - GuiBaseTest.run_all_share_mode_setup_tests(self) - GuiBaseTest.test_set_timeout(self, self.gui.share_mode, 5) - GuiBaseTest.run_all_share_mode_started_tests(self, public_mode) - GuiBaseTest.test_timeout_widget_hidden(self, self.gui.share_mode) - GuiBaseTest.test_server_timed_out(self, self.gui.share_mode, 10000) - GuiBaseTest.test_web_service_is_stopped(self) - - def run_all_receive_mode_tests(self, public_mode, receive_allow_receiver_shutdown): - GuiBaseTest.test_click_mode(self, self.gui.receive_mode) - GuiBaseTest.test_history_is_not_visible(self, self.gui.receive_mode) - GuiBaseTest.test_click_toggle_history(self, self.gui.receive_mode) - GuiBaseTest.test_history_is_visible(self, self.gui.receive_mode) - GuiBaseTest.test_server_working_on_start_button_pressed(self, self.gui.receive_mode) - GuiBaseTest.test_server_status_indicator_says_starting(self, self.gui.receive_mode) - GuiBaseTest.test_settings_button_is_hidden(self) - GuiBaseTest.test_a_server_is_started(self, self.gui.receive_mode) - GuiBaseTest.test_a_web_server_is_running(self) - GuiBaseTest.test_have_a_slug(self, self.gui.receive_mode, public_mode) - GuiBaseTest.test_url_description_shown(self, self.gui.receive_mode) - GuiBaseTest.test_have_copy_url_button(self, self.gui.receive_mode) - GuiBaseTest.test_server_status_indicator_says_started(self, self.gui.receive_mode) - GuiBaseTest.test_web_page(self, self.gui.receive_mode, 'Select the files you want to send, then click', public_mode) - GuiBaseTest.test_upload_file(self, public_mode, '/tmp/OnionShare/test.txt') - GuiBaseTest.test_history_widgets_present(self, self.gui.receive_mode) - GuiBaseTest.test_counter_incremented(self, self.gui.receive_mode, 1) - GuiBaseTest.test_upload_file(self, public_mode, '/tmp/OnionShare/test-2.txt') - GuiBaseTest.test_counter_incremented(self, self.gui.receive_mode, 2) - GuiBaseTest.test_history_indicator(self, self.gui.receive_mode, public_mode) - GuiBaseTest.test_server_is_stopped(self, self.gui.receive_mode, False) - GuiBaseTest.test_web_service_is_stopped(self) - GuiBaseTest.test_server_status_indicator_says_closed(self, self.gui.receive_mode, False) - GuiBaseTest.test_server_working_on_start_button_pressed(self, self.gui.receive_mode) - GuiBaseTest.test_a_server_is_started(self, self.gui.receive_mode) - GuiBaseTest.test_history_indicator(self, self.gui.receive_mode, public_mode) diff --git a/tests_gui_local/GuiReceiveTest.py b/tests_gui_local/GuiReceiveTest.py new file mode 100644 index 00000000..1fa5c4dc --- /dev/null +++ b/tests_gui_local/GuiReceiveTest.py @@ -0,0 +1,45 @@ +import os +import requests +from PyQt5 import QtTest +from .GuiBaseTest import GuiBaseTest + +class GuiReceiveTest(GuiBaseTest): + def upload_file(self, public_mode, expected_file): + '''Test that we can upload the file''' + files = {'file[]': open('/tmp/test.txt', 'rb')} + if not public_mode: + path = 'http://127.0.0.1:{}/{}/upload'.format(self.gui.app.port, self.gui.receive_mode.web.slug) + else: + path = 'http://127.0.0.1:{}/upload'.format(self.gui.app.port) + response = requests.post(path, files=files) + QtTest.QTest.qWait(2000) + self.assertTrue(os.path.isfile(expected_file)) + + def run_all_receive_mode_tests(self, public_mode, receive_allow_receiver_shutdown): + self.click_mode(self.gui.receive_mode) + self.history_is_not_visible(self.gui.receive_mode) + self.click_toggle_history(self.gui.receive_mode) + self.history_is_visible(self.gui.receive_mode) + self.server_working_on_start_button_pressed(self.gui.receive_mode) + self.server_status_indicator_says_starting(self.gui.receive_mode) + self.settings_button_is_hidden() + self.a_server_is_started(self.gui.receive_mode) + self.a_web_server_is_running() + self.have_a_slug(self.gui.receive_mode, public_mode) + self.url_description_shown(self.gui.receive_mode) + self.have_copy_url_button(self.gui.receive_mode) + self.server_status_indicator_says_started(self.gui.receive_mode) + self.web_page(self.gui.receive_mode, 'Select the files you want to send, then click', public_mode) + self.upload_file(public_mode, '/tmp/OnionShare/test.txt') + self.history_widgets_present(self.gui.receive_mode) + self.counter_incremented(self.gui.receive_mode, 1) + self.upload_file(public_mode, '/tmp/OnionShare/test-2.txt') + self.counter_incremented(self.gui.receive_mode, 2) + self.history_indicator(self.gui.receive_mode, public_mode) + self.server_is_stopped(self.gui.receive_mode, False) + self.web_service_is_stopped() + self.server_status_indicator_says_closed(self.gui.receive_mode, False) + self.server_working_on_start_button_pressed(self.gui.receive_mode) + self.a_server_is_started(self.gui.receive_mode) + self.history_indicator(self.gui.receive_mode, public_mode) + diff --git a/tests_gui_local/GuiShareTest.py b/tests_gui_local/GuiShareTest.py new file mode 100644 index 00000000..8c3ea2b1 --- /dev/null +++ b/tests_gui_local/GuiShareTest.py @@ -0,0 +1,154 @@ +import socks +import zipfile +from PyQt5 import QtCore, QtTest +from .GuiBaseTest import GuiBaseTest + +class GuiShareTest(GuiBaseTest): + # Auto-stop timer tests + + def set_timeout(self, mode, timeout): + '''Test that the timeout can be set''' + timer = QtCore.QDateTime.currentDateTime().addSecs(timeout) + mode.server_status.shutdown_timeout.setDateTime(timer) + self.assertTrue(mode.server_status.shutdown_timeout.dateTime(), timer) + + + def timeout_widget_hidden(self, mode): + '''Test that the timeout widget is hidden when share has started''' + self.assertFalse(mode.server_status.shutdown_timeout_container.isVisible()) + + + def server_timed_out(self, mode, wait): + '''Test that the server has timed out after the timer ran out''' + QtTest.QTest.qWait(wait) + # We should have timed out now + self.assertEqual(mode.server_status.status, 0) + + # Share-specific tests + + def file_selection_widget_has_a_file(self): + '''Test that the number of files in the list is 1''' + self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 1) + + + def deleting_only_file_hides_delete_button(self): + '''Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button''' + rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(self.gui.share_mode.server_status.file_selection.file_list.item(0)) + QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.viewport(), QtCore.Qt.LeftButton, pos=rect.center()) + # Delete button should be visible + self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) + # Click delete, and since there's no more files, the delete button should be hidden + QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton) + self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) + + + def add_a_file_and_delete_using_its_delete_widget(self): + '''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) + + + 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) + + + def add_delete_buttons_hidden(self): + '''Test that the add and delete buttons are hidden when the server starts''' + self.assertFalse(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) + self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) + + + def download_share(self, public_mode): + '''Test that we can download the share''' + s = socks.socksocket() + s.settimeout(60) + s.connect(('127.0.0.1', self.gui.app.port)) + + if public_mode: + path = '/download' + else: + path = '{}/download'.format(self.gui.share_mode.web.slug) + + http_request = 'GET {} HTTP/1.0\r\n'.format(path) + http_request += 'Host: 127.0.0.1\r\n' + http_request += '\r\n' + s.sendall(http_request.encode('utf-8')) + + with open('/tmp/download.zip', 'wb') as file_to_write: + while True: + data = s.recv(1024) + if not data: + break + file_to_write.write(data) + file_to_write.close() + + zip = zipfile.ZipFile('/tmp/download.zip') + QtTest.QTest.qWait(2000) + self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8')) + + + def add_button_visible(self): + '''Test that the add button should be visible''' + self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) + + + def run_all_share_mode_setup_tests(self): + """Tests in share mode prior to starting a share""" + self.click_mode(self.gui.share_mode) + self.file_selection_widget_has_a_file() + self.history_is_not_visible(self.gui.share_mode) + self.click_toggle_history(self.gui.share_mode) + self.history_is_visible(self.gui.share_mode) + self.deleting_only_file_hides_delete_button() + self.add_a_file_and_delete_using_its_delete_widget() + self.file_selection_widget_readd_files() + + + def run_all_share_mode_started_tests(self, public_mode): + """Tests in share mode after starting a share""" + self.server_working_on_start_button_pressed(self.gui.share_mode) + self.server_status_indicator_says_starting(self.gui.share_mode) + self.add_delete_buttons_hidden() + self.settings_button_is_hidden() + self.a_server_is_started(self.gui.share_mode) + self.a_web_server_is_running() + self.have_a_slug(self.gui.share_mode, public_mode) + self.url_description_shown(self.gui.share_mode) + self.have_copy_url_button(self.gui.share_mode) + self.server_status_indicator_says_started(self.gui.share_mode) + self.web_page(self.gui.share_mode, 'Total size', public_mode) + + + def run_all_share_mode_download_tests(self, public_mode, stay_open): + """Tests in share mode after downloading a share""" + self.download_share(public_mode) + self.history_widgets_present(self.gui.share_mode) + self.server_is_stopped(self.gui.share_mode, stay_open) + self.web_service_is_stopped() + self.server_status_indicator_says_closed(self.gui.share_mode, stay_open) + self.add_button_visible() + self.server_working_on_start_button_pressed(self.gui.share_mode) + self.a_server_is_started(self.gui.share_mode) + self.history_indicator(self.gui.share_mode, public_mode) + + + def run_all_share_mode_tests(self, public_mode, stay_open): + """End-to-end share tests""" + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(public_mode) + self.run_all_share_mode_download_tests(public_mode, stay_open) + + + def run_all_share_mode_timer_tests(self, public_mode): + """Auto-stop timer tests in share mode""" + self.run_all_share_mode_setup_tests() + self.set_timeout(self.gui.share_mode, 5) + self.run_all_share_mode_started_tests(public_mode) + self.timeout_widget_hidden(self.gui.share_mode) + self.server_timed_out(self.gui.share_mode, 10000) + self.web_service_is_stopped() + diff --git a/tests_gui_local/__init__.py b/tests_gui_local/__init__.py index 7cf168eb..e69de29b 100644 --- a/tests_gui_local/__init__.py +++ b/tests_gui_local/__init__.py @@ -1 +0,0 @@ -from .GuiBaseTest import GuiBaseTest diff --git a/tests_gui_local/onionshare_receive_mode_upload_public_mode_test.py b/tests_gui_local/onionshare_receive_mode_upload_public_mode_test.py new file mode 100644 index 00000000..f41a2ce2 --- /dev/null +++ b/tests_gui_local/onionshare_receive_mode_upload_public_mode_test.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiReceiveTest import GuiReceiveTest + +class ReceiveModePublicModeTest(unittest.TestCase, GuiReceiveTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": True, + "receive_allow_receiver_shutdown": True + } + cls.gui = GuiReceiveTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiReceiveTest.tear_down() + + @pytest.mark.run(order=1) + def test_run_all_common_setup_tests(self): + GuiReceiveTest.run_all_common_setup_tests(self) + + @pytest.mark.run(order=2) + def test_run_all_receive_mode_tests(self): + GuiReceiveTest.run_all_receive_mode_tests(self, True, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests_gui_local/onionshare_receive_mode_upload_test.py b/tests_gui_local/onionshare_receive_mode_upload_test.py index 262c7aba..8a7661dd 100644 --- a/tests_gui_local/onionshare_receive_mode_upload_test.py +++ b/tests_gui_local/onionshare_receive_mode_upload_test.py @@ -1,38 +1,28 @@ #!/usr/bin/env python3 -import os -import sys -import unittest import pytest -import json +import unittest -from PyQt5 import QtWidgets +from .GuiReceiveTest import GuiReceiveTest -from onionshare.common import Common -from onionshare.web import Web -from onionshare import onion, strings -from onionshare_gui import * - -from .GuiBaseTest import GuiBaseTest - -class ReceiveModeTest(unittest.TestCase): +class ReceiveModeTest(unittest.TestCase, GuiReceiveTest): @classmethod def setUpClass(cls): test_settings = { "receive_allow_receiver_shutdown": True } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiReceiveTest.set_up(test_settings) @classmethod def tearDownClass(cls): - GuiBaseTest.tear_down() + GuiReceiveTest.tear_down() @pytest.mark.run(order=1) def test_run_all_common_setup_tests(self): - GuiBaseTest.run_all_common_setup_tests(self) + GuiReceiveTest.run_all_common_setup_tests(self) @pytest.mark.run(order=2) - def test_run_all_share_mode_tests(self): - GuiBaseTest.run_all_receive_mode_tests(self, False, True) + def test_run_all_receive_mode_tests(self): + GuiReceiveTest.run_all_receive_mode_tests(self, False, True) if __name__ == "__main__": unittest.main() diff --git a/tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py b/tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py deleted file mode 100644 index 201402c2..00000000 --- a/tests_gui_local/onionshare_receive_mode_upload_test_public_mode.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 -import os -import sys -import unittest -import pytest -import json - -from PyQt5 import QtWidgets - -from onionshare.common import Common -from onionshare.web import Web -from onionshare import onion, strings -from onionshare_gui import * - -from .GuiBaseTest import GuiBaseTest - -class ReceiveModePublicModeTest(unittest.TestCase): - @classmethod - def setUpClass(cls): - test_settings = { - "public_mode": True, - "receive_allow_receiver_shutdown": True - } - cls.gui = GuiBaseTest.set_up(test_settings) - - @classmethod - def tearDownClass(cls): - GuiBaseTest.tear_down() - - @pytest.mark.run(order=1) - def test_run_all_common_setup_tests(self): - GuiBaseTest.run_all_common_setup_tests(self) - - @pytest.mark.run(order=2) - def test_run_all_share_mode_tests(self): - GuiBaseTest.run_all_receive_mode_tests(self, True, True) - -if __name__ == "__main__": - unittest.main() diff --git a/tests_gui_local/onionshare_share_mode_download_public_mode_test.py b/tests_gui_local/onionshare_share_mode_download_public_mode_test.py new file mode 100644 index 00000000..53d1fb8c --- /dev/null +++ b/tests_gui_local/onionshare_share_mode_download_public_mode_test.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class ShareModePublicModeTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": True, + } + cls.gui = GuiShareTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + @pytest.mark.run(order=1) + def test_run_all_common_setup_tests(self): + GuiShareTest.run_all_common_setup_tests(self) + + @pytest.mark.run(order=2) + def test_run_all_share_mode_tests(self): + GuiShareTest.run_all_share_mode_tests(self, True, False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests_gui_local/onionshare_share_mode_download_stay_open_test.py b/tests_gui_local/onionshare_share_mode_download_stay_open_test.py new file mode 100644 index 00000000..b398f654 --- /dev/null +++ b/tests_gui_local/onionshare_share_mode_download_stay_open_test.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class ShareModeStayOpenTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "close_after_first_download": False, + } + cls.gui = GuiShareTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + @pytest.mark.run(order=1) + def test_run_all_common_setup_tests(self): + GuiShareTest.run_all_common_setup_tests(self) + + @pytest.mark.run(order=2) + def test_run_all_share_mode_tests(self): + GuiShareTest.run_all_share_mode_tests(self, False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests_gui_local/onionshare_share_mode_download_test.py b/tests_gui_local/onionshare_share_mode_download_test.py index b24a3a78..34532c59 100644 --- a/tests_gui_local/onionshare_share_mode_download_test.py +++ b/tests_gui_local/onionshare_share_mode_download_test.py @@ -1,37 +1,27 @@ #!/usr/bin/env python3 -import os -import sys -import unittest import pytest -import json +import unittest -from PyQt5 import QtWidgets +from .GuiShareTest import GuiShareTest -from onionshare.common import Common -from onionshare.web import Web -from onionshare import onion, strings -from onionshare_gui import * - -from .GuiBaseTest import GuiBaseTest - -class ShareModeTest(unittest.TestCase): +class ShareModeTest(unittest.TestCase, GuiShareTest): @classmethod def setUpClass(cls): test_settings = { } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiShareTest.set_up(test_settings) @classmethod def tearDownClass(cls): - GuiBaseTest.tear_down() + GuiShareTest.tear_down() @pytest.mark.run(order=1) def test_run_all_common_setup_tests(self): - GuiBaseTest.run_all_common_setup_tests(self) + GuiShareTest.run_all_common_setup_tests(self) @pytest.mark.run(order=2) def test_run_all_share_mode_tests(self): - GuiBaseTest.run_all_share_mode_tests(self, False, False) + GuiShareTest.run_all_share_mode_tests(self, False, False) if __name__ == "__main__": unittest.main() diff --git a/tests_gui_local/onionshare_share_mode_download_test_public_mode.py b/tests_gui_local/onionshare_share_mode_download_test_public_mode.py deleted file mode 100644 index e59b53f4..00000000 --- a/tests_gui_local/onionshare_share_mode_download_test_public_mode.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python3 -import os -import sys -import unittest -import pytest -import json - -from PyQt5 import QtWidgets - -from onionshare.common import Common -from onionshare.web import Web -from onionshare import onion, strings -from onionshare_gui import * - -from .GuiBaseTest import GuiBaseTest - -class ShareModePublicModeTest(unittest.TestCase): - @classmethod - def setUpClass(cls): - test_settings = { - "public_mode": True, - } - cls.gui = GuiBaseTest.set_up(test_settings) - - @classmethod - def tearDownClass(cls): - GuiBaseTest.tear_down() - - @pytest.mark.run(order=1) - def test_run_all_common_setup_tests(self): - GuiBaseTest.run_all_common_setup_tests(self) - - @pytest.mark.run(order=2) - def test_run_all_share_mode_tests(self): - GuiBaseTest.run_all_share_mode_tests(self, True, False) - -if __name__ == "__main__": - unittest.main() diff --git a/tests_gui_local/onionshare_share_mode_download_test_stay_open.py b/tests_gui_local/onionshare_share_mode_download_test_stay_open.py deleted file mode 100644 index 9394c34a..00000000 --- a/tests_gui_local/onionshare_share_mode_download_test_stay_open.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python3 -import os -import sys -import unittest -import pytest -import json - -from PyQt5 import QtWidgets - -from onionshare.common import Common -from onionshare.web import Web -from onionshare import onion, strings -from onionshare_gui import * - -from .GuiBaseTest import GuiBaseTest - -class ShareModeStayOpenTest(unittest.TestCase): - @classmethod - def setUpClass(cls): - test_settings = { - "close_after_first_download": False, - } - cls.gui = GuiBaseTest.set_up(test_settings) - - @classmethod - def tearDownClass(cls): - GuiBaseTest.tear_down() - - @pytest.mark.run(order=1) - def test_run_all_common_setup_tests(self): - GuiBaseTest.run_all_common_setup_tests(self) - - @pytest.mark.run(order=2) - def test_run_all_share_mode_tests(self): - GuiBaseTest.run_all_share_mode_tests(self, False, True) - -if __name__ == "__main__": - unittest.main() diff --git a/tests_gui_local/onionshare_slug_persistent_test.py b/tests_gui_local/onionshare_share_mode_slug_persistent_test.py similarity index 61% rename from tests_gui_local/onionshare_slug_persistent_test.py rename to tests_gui_local/onionshare_share_mode_slug_persistent_test.py index ab845f8e..b4c92f36 100644 --- a/tests_gui_local/onionshare_slug_persistent_test.py +++ b/tests_gui_local/onionshare_share_mode_slug_persistent_test.py @@ -1,20 +1,10 @@ #!/usr/bin/env python3 -import os -import sys -import unittest import pytest -import json +import unittest -from PyQt5 import QtWidgets +from .GuiShareTest import GuiShareTest -from onionshare.common import Common -from onionshare.web import Web -from onionshare import onion, strings -from onionshare_gui import * - -from .GuiBaseTest import GuiBaseTest - -class ShareModePersistentSlugTest(unittest.TestCase): +class ShareModePersistentSlugTest(unittest.TestCase, GuiShareTest): @classmethod def setUpClass(cls): test_settings = { @@ -23,19 +13,19 @@ class ShareModePersistentSlugTest(unittest.TestCase): "save_private_key": True, "close_after_first_download": False, } - cls.gui = GuiBaseTest.set_up(test_settings) + cls.gui = GuiShareTest.set_up(test_settings) @classmethod def tearDownClass(cls): - GuiBaseTest.tear_down() + GuiShareTest.tear_down() @pytest.mark.run(order=1) def test_run_all_common_setup_tests(self): - GuiBaseTest.run_all_common_setup_tests(self) + GuiShareTest.run_all_common_setup_tests(self) @pytest.mark.run(order=2) def test_run_all_share_mode_tests(self): - GuiBaseTest.run_all_share_mode_tests(self, False, True) + GuiShareTest.run_all_share_mode_tests(self, False, True) global slug slug = self.gui.share_mode.server_status.web.slug diff --git a/tests_gui_local/onionshare_share_mode_timer_test.py b/tests_gui_local/onionshare_share_mode_timer_test.py new file mode 100644 index 00000000..98902428 --- /dev/null +++ b/tests_gui_local/onionshare_share_mode_timer_test.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class ShareModeTimerTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": False, + "shutdown_timeout": True, + } + cls.gui = GuiShareTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + @pytest.mark.run(order=1) + def test_run_all_common_setup_tests(self): + GuiShareTest.run_all_common_setup_tests(self) + + @pytest.mark.run(order=2) + def test_run_all_share_mode_timer_tests(self): + GuiShareTest.run_all_share_mode_timer_tests(self, False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests_gui_local/onionshare_timer_test.py b/tests_gui_local/onionshare_timer_test.py deleted file mode 100644 index 60c616cc..00000000 --- a/tests_gui_local/onionshare_timer_test.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 -import os -import sys -import unittest -import pytest -import json - -from PyQt5 import QtWidgets - -from onionshare.common import Common -from onionshare.web import Web -from onionshare import onion, strings -from onionshare_gui import * - -from .GuiBaseTest import GuiBaseTest - -class ShareModeTimerTest(unittest.TestCase): - @classmethod - def setUpClass(cls): - test_settings = { - "public_mode": False, - "shutdown_timeout": True, - } - cls.gui = GuiBaseTest.set_up(test_settings) - - @classmethod - def tearDownClass(cls): - GuiBaseTest.tear_down() - - @pytest.mark.run(order=1) - def test_run_all_common_setup_tests(self): - GuiBaseTest.run_all_common_setup_tests(self) - - @pytest.mark.run(order=2) - def test_run_all_share_mode_timer_tests(self): - GuiBaseTest.run_all_share_mode_timer_tests(self, False) - -if __name__ == "__main__": - unittest.main()