diff options
author | Miguel Jacq <mig@mig5.net> | 2018-10-13 10:35:09 +1100 |
---|---|---|
committer | Miguel Jacq <mig@mig5.net> | 2018-10-13 10:35:09 +1100 |
commit | 10cdfa7631ea98f07f081d1942c9cdb4f1caf945 (patch) | |
tree | 6b96dd75641cd64874e59a285c0c47bdada6be8d /tests | |
parent | 1043be448339c2c343859417bcff6ab4f417674e (diff) | |
download | onionshare-10cdfa7631ea98f07f081d1942c9cdb4f1caf945.tar.gz onionshare-10cdfa7631ea98f07f081d1942c9cdb4f1caf945.zip |
Move GUI tests into tests/ dir and fix conftest related stuff so everything loads what it needs and passes
Diffstat (limited to 'tests')
27 files changed, 1287 insertions, 6 deletions
diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py new file mode 100644 index 00000000..79543468 --- /dev/null +++ b/tests/GuiBaseTest.py @@ -0,0 +1,275 @@ +import json +import os +import requests +import shutil +import socket +import socks + +from PyQt5 import QtCore, QtTest + +from onionshare import strings +from onionshare.common import Common +from onionshare.settings import Settings +from onionshare.onion import Onion +from onionshare.web import Web +from onionshare_gui import Application, OnionShare, OnionShareGui +from onionshare_gui.mode.share_mode import ShareMode +from onionshare_gui.mode.receive_mode import ReceiveMode + + +class GuiBaseTest(object): + @staticmethod + def set_up(test_settings, settings_filename): + '''Create GUI with given settings''' + # Create our test file + testfile = open('/tmp/test.txt', 'w') + testfile.write('onionshare') + testfile.close() + + common = Common() + common.settings = Settings(common) + common.define_css() + strings.load_strings(common) + + # Get all of the settings in test_settings + test_settings['downloads_dir'] = '/tmp/OnionShare' + for key, val in common.settings.default_settings.items(): + if key not in test_settings: + test_settings[key] = val + + # Start the Onion + testonion = Onion(common) + global qtapp + qtapp = Application(common) + app = OnionShare(common, testonion, True, 0) + + web = Web(common, False, True) + open('/tmp/{}.json'.format(settings_filename), 'w').write(json.dumps(test_settings)) + + gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt'], '/tmp/{}.json'.format(settings_filename), True) + return gui + + @staticmethod + def tear_down(): + try: + os.remove('/tmp/test.txt') + shutil.rmtree('/tmp/OnionShare') + except: + pass + + + def gui_loaded(self): + '''Test that the GUI actually is shown''' + self.assertTrue(self.gui.show) + + + def windowTitle_seen(self): + '''Test that the window title is OnionShare''' + self.assertEqual(self.gui.windowTitle(), 'OnionShare') + + + def settings_button_is_visible(self): + '''Test that the settings button is visible''' + self.assertTrue(self.gui.settings_button.isVisible()) + + + def server_status_bar_is_visible(self): + '''Test that the status bar is visible''' + self.assertTrue(self.gui.status_bar.isVisible()) + + + 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) + self.assertTrue(self.gui.mode, self.gui.MODE_RECEIVE) + if type(mode) == ShareMode: + QtTest.QTest.mouseClick(self.gui.share_mode_button, QtCore.Qt.LeftButton) + self.assertTrue(self.gui.mode, self.gui.MODE_SHARE) + + + 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 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(): + QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) + self.assertFalse(mode.history.isVisible()) + + # Indicator should not be visible yet + self.assertFalse(mode.toggle_history.indicator_label.isVisible()) + + if type(mode) == ReceiveMode: + # Upload a file + files = {'file[]': open('/tmp/test.txt', 'rb')} + if not public_mode: + path = 'http://127.0.0.1:{}/{}/upload'.format(self.gui.app.port, 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) + + if type(mode) == ShareMode: + # Download files + if public_mode: + url = "http://127.0.0.1:{}/download".format(self.gui.app.port) + else: + url = "http://127.0.0.1:{}/{}/download".format(self.gui.app.port, mode.web.slug) + r = requests.get(url) + QtTest.QTest.qWait(2000) + + # Indicator should be visible, have a value of "1" + self.assertTrue(mode.toggle_history.indicator_label.isVisible()) + self.assertEqual(mode.toggle_history.indicator_label.text(), "1") + + # Toggle history back on, indicator should be hidden again + QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) + self.assertFalse(mode.toggle_history.indicator_label.isVisible()) + + + def history_is_not_visible(self, mode): + '''Test that the History section is not visible''' + self.assertFalse(mode.history.isVisible()) + + + def history_is_visible(self, mode): + '''Test that the History section is visible''' + self.assertTrue(mode.history.isVisible()) + + + 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 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 settings_button_is_hidden(self): + '''Test that the settings button is hidden when the server starts''' + self.assertFalse(self.gui.settings_button.isVisible()) + + + 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 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 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 url_description_shown(self, mode): + '''Test that the URL label is showing''' + self.assertTrue(mode.server_status.url_description.isVisible()) + + + 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 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 web_page(self, mode, string, public_mode): + '''Test that the web page contains a string''' + s = socks.socksocket() + s.settimeout(60) + s.connect(('127.0.0.1', self.gui.app.port)) + + if not public_mode: + path = '/{}'.format(mode.server_status.web.slug) + else: + path = '/' + + 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/webpage', 'wb') as file_to_write: + while True: + data = s.recv(1024) + if not data: + break + file_to_write.write(data) + file_to_write.close() + + f = open('/tmp/webpage') + self.assertTrue(string in f.read()) + f.close() + + + 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 counter_incremented(self, mode, count): + '''Test that the counter has incremented''' + self.assertEquals(mode.history.completed_count, count) + + + 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 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) + + # We should be closed by now. Fail if not! + self.assertNotEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) + + + 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')) + if type(mode) == ShareMode: + if stay_open: + self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_stopped')) + else: + self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically')) + + def run_all_common_setup_tests(self): + self.gui_loaded() + self.windowTitle_seen() + self.settings_button_is_visible() + self.server_status_bar_is_visible() + + diff --git a/tests/GuiReceiveTest.py b/tests/GuiReceiveTest.py new file mode 100644 index 00000000..1fa5c4dc --- /dev/null +++ b/tests/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/GuiShareTest.py b/tests/GuiShareTest.py new file mode 100644 index 00000000..34149dec --- /dev/null +++ b/tests/GuiShareTest.py @@ -0,0 +1,168 @@ +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) + + # Persistence tests + def have_same_slug(self, slug): + '''Test that we have the same slug''' + self.assertEqual(self.gui.share_mode.server_status.web.slug, slug) + + # 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) + + + def run_all_share_mode_download_tests(self, public_mode, stay_open): + """Tests in share mode after downloading a share""" + self.web_page(self.gui.share_mode, 'Total size', public_mode) + 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_persistent_tests(self, public_mode, stay_open): + """Same as end-to-end share tests but also test the slug is the same on multiple shared""" + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(public_mode) + slug = self.gui.share_mode.server_status.web.slug + self.run_all_share_mode_download_tests(public_mode, stay_open) + self.have_same_slug(slug) + + + 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/TorGuiBaseTest.py b/tests/TorGuiBaseTest.py new file mode 100644 index 00000000..b182b1d4 --- /dev/null +++ b/tests/TorGuiBaseTest.py @@ -0,0 +1,161 @@ +import json +import os +import requests +import shutil +import socket +import socks + +from PyQt5 import QtCore, QtTest + +from onionshare import strings +from onionshare.common import Common +from onionshare.settings import Settings +from onionshare.onion import Onion +from onionshare.web import Web +from onionshare_gui import Application, OnionShare, OnionShareGui +from onionshare_gui.mode.share_mode import ShareMode +from onionshare_gui.mode.receive_mode import ReceiveMode + +from .GuiBaseTest import GuiBaseTest + +class TorGuiBaseTest(GuiBaseTest): + @staticmethod + def set_up(test_settings, settings_filename): + '''Create GUI with given settings''' + # Create our test file + testfile = open('/tmp/test.txt', 'w') + testfile.write('onionshare') + testfile.close() + + common = Common() + common.settings = Settings(common) + common.define_css() + strings.load_strings(common) + + # Get all of the settings in test_settings + test_settings['connection_type'] = 'automatic' + test_settings['downloads_dir'] = '/tmp/OnionShare' + for key, val in common.settings.default_settings.items(): + if key not in test_settings: + test_settings[key] = val + + # Start the Onion + testonion = Onion(common) + global qtapp + qtapp = Application(common) + app = OnionShare(common, testonion, False, 0) + + web = Web(common, False, False) + open('/tmp/{}.json'.format(settings_filename), 'w').write(json.dumps(test_settings)) + + gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt'], '/tmp/{}.json'.format(settings_filename), False) + return gui + + 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(): + QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) + self.assertFalse(mode.history.isVisible()) + + # Indicator should not be visible yet + self.assertFalse(mode.toggle_history.indicator_label.isVisible()) + + # Set up connecting to the onion + (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() + session = requests.session() + session.proxies = {} + session.proxies['http'] = 'socks5h://{}:{}'.format(socks_address, socks_port) + + if type(mode) == ReceiveMode: + # Upload a file + files = {'file[]': open('/tmp/test.txt', 'rb')} + if not public_mode: + path = 'http://{}/{}/upload'.format(self.gui.app.onion_host, mode.web.slug) + else: + path = 'http://{}/upload'.format(self.gui.app.onion_host) + response = session.post(path, files=files) + QtTest.QTest.qWait(4000) + + if type(mode) == ShareMode: + # Download files + if public_mode: + path = "http://{}/download".format(self.gui.app.onion_host) + else: + path = "http://{}/{}/download".format(self.gui.app.onion_host, mode.web.slug) + response = session.get(path) + QtTest.QTest.qWait(4000) + + # Indicator should be visible, have a value of "1" + self.assertTrue(mode.toggle_history.indicator_label.isVisible()) + self.assertEqual(mode.toggle_history.indicator_label.text(), "1") + + # Toggle history back on, indicator should be hidden again + QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) + self.assertFalse(mode.toggle_history.indicator_label.isVisible()) + + def a_server_is_started(self, mode): + '''Test that the server has started (overriding from local tests to wait for longer)''' + QtTest.QTest.qWait(45000) + # Should now be in SERVER_STARTED state + self.assertEqual(mode.server_status.status, 2) + + def have_an_onion_service(self): + '''Test that we have a valid Onion URL''' + self.assertRegex(self.gui.app.onion_host, r'[a-z2-7].onion') + + def web_page(self, mode, string, public_mode): + '''Test that the web page contains a string''' + (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() + socks.set_default_proxy(socks.SOCKS5, socks_address, socks_port) + s = socks.socksocket() + s.settimeout(60) + s.connect((self.gui.app.onion_host, 80)) + if not public_mode: + path = '/{}'.format(mode.server_status.web.slug) + else: + path = '/' + http_request = 'GET {} HTTP/1.0\r\n'.format(path) + http_request += 'Host: {}\r\n'.format(self.gui.app.onion_host) + http_request += '\r\n' + s.sendall(http_request.encode('utf-8')) + with open('/tmp/webpage', 'wb') as file_to_write: + while True: + data = s.recv(1024) + if not data: + break + file_to_write.write(data) + file_to_write.close() + f = open('/tmp/webpage') + self.assertTrue(string in f.read()) + f.close() + + def cancel_the_share(self, mode): + '''Test that we can cancel this share before it's started up ''' + 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() + QtTest.QTest.mousePress(mode.server_status.server_button, QtCore.Qt.LeftButton) + QtTest.QTest.qWait(1000) + QtTest.QTest.mouseRelease(mode.server_status.server_button, QtCore.Qt.LeftButton) + self.assertEqual(mode.server_status.status, 0) + self.server_is_stopped(self.gui.share_mode, False) + self.web_service_is_stopped() + + + # Stealth tests + def copy_have_hidserv_auth_button(self, mode): + '''Test that the Copy HidservAuth button is shown''' + self.assertTrue(mode.server_status.copy_hidservauth_button.isVisible()) + + def hidserv_auth_string(self): + '''Test the validity of the HidservAuth string''' + self.assertRegex(self.gui.app.auth_string, r'HidServAuth %s [a-zA-Z1-9]' % 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.cleanup(stop_tor=True) + QtTest.QTest.qWait(2500) + self.assertTrue(mode.status_bar.currentMessage(), strings._('gui_tor_connection_lost')) diff --git a/tests/TorGuiReceiveTest.py b/tests/TorGuiReceiveTest.py new file mode 100644 index 00000000..67b6a811 --- /dev/null +++ b/tests/TorGuiReceiveTest.py @@ -0,0 +1,51 @@ +import os +import requests +from PyQt5 import QtTest +from .TorGuiBaseTest import TorGuiBaseTest + +class TorGuiReceiveTest(TorGuiBaseTest): + + def upload_file(self, public_mode, expected_file): + '''Test that we can upload the file''' + (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() + session = requests.session() + session.proxies = {} + session.proxies['http'] = 'socks5h://{}:{}'.format(socks_address, socks_port) + files = {'file[]': open('/tmp/test.txt', 'rb')} + if not public_mode: + path = 'http://{}/{}/upload'.format(self.gui.app.onion_host, self.gui.receive_mode.web.slug) + else: + path = 'http://{}/upload'.format(self.gui.app.onion_host) + response = session.post(path, files=files) + QtTest.QTest.qWait(4000) + 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_an_onion_service() + 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/TorGuiShareTest.py b/tests/TorGuiShareTest.py new file mode 100644 index 00000000..8e17abad --- /dev/null +++ b/tests/TorGuiShareTest.py @@ -0,0 +1,70 @@ +import requests +import socks +import zipfile +from PyQt5 import QtCore, QtTest +from .TorGuiBaseTest import TorGuiBaseTest +from .GuiShareTest import GuiShareTest + +class TorGuiShareTest(TorGuiBaseTest, GuiShareTest): + def download_share(self, public_mode): + # Set up connecting to the onion + (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() + session = requests.session() + session.proxies = {} + session.proxies['http'] = 'socks5h://{}:{}'.format(socks_address, socks_port) + + # Download files + if public_mode: + path = "http://{}/download".format(self.gui.app.onion_host) + else: + path = "http://{}/{}/download".format(self.gui.app.onion_host, self.gui.share_mode.web.slug) + response = session.get(path, stream=True) + QtTest.QTest.qWait(4000) + + if response.status_code == 200: + with open('/tmp/download.zip', 'wb') as file_to_write: + for chunk in response.iter_content(chunk_size=128): + file_to_write.write(chunk) + file_to_write.close() + zip = zipfile.ZipFile('/tmp/download.zip') + QtTest.QTest.qWait(4000) + self.assertEquals('onionshare', zip.read('test.txt').decode('utf-8')) + + # Persistence tests + def have_same_onion(self, onion): + '''Test that we have the same onion''' + self.assertEqual(self.gui.app.onion_host, onion) + + 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_an_onion_service() + 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) + + def run_all_share_mode_persistent_tests(self, public_mode, stay_open): + """Same as end-to-end share tests but also test the slug is the same on multiple shared""" + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(public_mode) + slug = self.gui.share_mode.server_status.web.slug + onion = self.gui.app.onion_host + self.run_all_share_mode_download_tests(public_mode, stay_open) + self.have_same_onion(onion) + self.have_same_slug(slug) + + 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, 120) + 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, 125000) + self.web_service_is_stopped() + diff --git a/tests/conftest.py b/tests/conftest.py index 3ae6fd52..688b22d8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,22 @@ import pytest from onionshare import common, web, settings, strings +def pytest_addoption(parser): + parser.addoption( + "--runtor", action="store_true", default=False, help="run tor tests" + ) + + +def pytest_collection_modifyitems(config, items): + if config.getoption("--runtor"): + # --runtor given in cli: do not skip tor tests + return + skip_tor = pytest.mark.skip(reason="need --runtor option to run") + for item in items: + if "tor" in item.keywords: + item.add_marker(skip_tor) + + @pytest.fixture def temp_dir_1024(): """ Create a temporary directory that has a single file of a @@ -151,13 +167,11 @@ def time_strftime(monkeypatch): @pytest.fixture def common_obj(): - _common = common.Common() - _common.settings = settings.Settings(_common) - strings.load_strings(_common) - return _common + return common.Common() @pytest.fixture def settings_obj(sys_onionshare_dev_mode, platform_linux): _common = common.Common() _common.version = 'DUMMY_VERSION_1.2.3' + strings.load_strings(_common) return settings.Settings(_common) diff --git a/tests/local_onionshare_receive_mode_upload_public_mode_test.py b/tests/local_onionshare_receive_mode_upload_public_mode_test.py new file mode 100644 index 00000000..27bf9b2c --- /dev/null +++ b/tests/local_onionshare_receive_mode_upload_public_mode_test.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiReceiveTest import GuiReceiveTest + +class LocalReceiveModePublicModeTest(unittest.TestCase, GuiReceiveTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": True, + "receive_allow_receiver_shutdown": True + } + cls.gui = GuiReceiveTest.set_up(test_settings, 'LocalReceiveModePublicModeTest') + + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + def test_run_all_receive_mode_tests(self): + self.run_all_receive_mode_tests(True, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_receive_mode_upload_test.py b/tests/local_onionshare_receive_mode_upload_test.py new file mode 100644 index 00000000..f6594105 --- /dev/null +++ b/tests/local_onionshare_receive_mode_upload_test.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiReceiveTest import GuiReceiveTest + +class LocalReceiveModeTest(unittest.TestCase, GuiReceiveTest): + @classmethod + def setUpClass(cls): + test_settings = { + "receive_allow_receiver_shutdown": True + } + cls.gui = GuiReceiveTest.set_up(test_settings, 'LocalReceiveModeTest') + + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + def test_run_all_receive_mode_tests(self): + self.run_all_receive_mode_tests(False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_download_public_mode_test.py b/tests/local_onionshare_share_mode_download_public_mode_test.py new file mode 100644 index 00000000..2a3f9584 --- /dev/null +++ b/tests/local_onionshare_share_mode_download_public_mode_test.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModePublicModeTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": True, + } + cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModePublicModeTest') + + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_tests(True, False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_download_stay_open_test.py b/tests/local_onionshare_share_mode_download_stay_open_test.py new file mode 100644 index 00000000..1f734ae7 --- /dev/null +++ b/tests/local_onionshare_share_mode_download_stay_open_test.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeStayOpenTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "close_after_first_download": False, + } + cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModeStayOpenTest') + + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_tests(False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_download_test.py b/tests/local_onionshare_share_mode_download_test.py new file mode 100644 index 00000000..274cc311 --- /dev/null +++ b/tests/local_onionshare_share_mode_download_test.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModeTest') + + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_tests(False, False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_slug_persistent_test.py b/tests/local_onionshare_share_mode_slug_persistent_test.py new file mode 100644 index 00000000..3450ec3f --- /dev/null +++ b/tests/local_onionshare_share_mode_slug_persistent_test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModePersistentSlugTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": False, + "slug": "", + "save_private_key": True, + "close_after_first_download": False, + } + cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModePersistentSlugTest') + + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_persistent_tests(False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_timer_test.py b/tests/local_onionshare_share_mode_timer_test.py new file mode 100644 index 00000000..f9f36c48 --- /dev/null +++ b/tests/local_onionshare_share_mode_timer_test.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeTimerTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": False, + "shutdown_timeout": True, + } + cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModeTimerTest') + + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + def test_run_all_share_mode_timer_tests(self): + self.run_all_share_mode_timer_tests(False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_790_cancel_on_second_share_test.py b/tests/onionshare_790_cancel_on_second_share_test.py new file mode 100644 index 00000000..36725fb0 --- /dev/null +++ b/tests/onionshare_790_cancel_on_second_share_test.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +# Tests #790 regression +class ShareModeCancelSecondShareTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "close_after_first_download": True + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModeCancelSecondShareTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_share_mode_tests(self): + self.run_all_share_mode_tests(False, False) + + @pytest.mark.run(after='test_run_share_mode_tests') + @pytest.mark.tor + def test_cancel_the_share(self): + self.cancel_the_share(self.gui.share_mode) + + @pytest.mark.run(after='test_cancel_the_share') + @pytest.mark.tor + def test_server_is_stopped_round2(self): + self.server_is_stopped(self.gui.share_mode, False) + + @pytest.mark.run(after='test_server_is_stopped_round2') + @pytest.mark.tor + def test_web_service_is_stopped_round2(self): + self.web_service_is_stopped() + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_receive_mode_upload_public_mode_test.py b/tests/onionshare_receive_mode_upload_public_mode_test.py new file mode 100644 index 00000000..ee0c5c34 --- /dev/null +++ b/tests/onionshare_receive_mode_upload_public_mode_test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiReceiveTest import TorGuiReceiveTest + +class ReceiveModeTest(unittest.TestCase, TorGuiReceiveTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": True, + "receive_allow_receiver_shutdown": True + } + cls.gui = TorGuiReceiveTest.set_up(test_settings, 'ReceiveModeTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_all_receive_mode_tests(self): + self.run_all_receive_mode_tests(True, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_receive_mode_upload_test.py b/tests/onionshare_receive_mode_upload_test.py new file mode 100644 index 00000000..f1f683cc --- /dev/null +++ b/tests/onionshare_receive_mode_upload_test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiReceiveTest import TorGuiReceiveTest + +class ReceiveModeTest(unittest.TestCase, TorGuiReceiveTest): + @classmethod + def setUpClass(cls): + test_settings = { + "receive_allow_receiver_shutdown": True + } + cls.gui = TorGuiReceiveTest.set_up(test_settings, 'ReceiveModeTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_all_receive_mode_tests(self): + self.run_all_receive_mode_tests(False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_cancel_share_test.py b/tests/onionshare_share_mode_cancel_share_test.py new file mode 100644 index 00000000..97adf9ce --- /dev/null +++ b/tests/onionshare_share_mode_cancel_share_test.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class ShareModeCancelTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModeCancelTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_share_mode_setup_tests(self): + self.run_all_share_mode_setup_tests() + + @pytest.mark.run(after='test_run_share_mode_setup_tests') + @pytest.mark.tor + def test_cancel_the_share(self): + self.cancel_the_share(self.gui.share_mode) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_download_public_mode_test.py b/tests/onionshare_share_mode_download_public_mode_test.py new file mode 100644 index 00000000..a4a3bfb1 --- /dev/null +++ b/tests/onionshare_share_mode_download_public_mode_test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class ShareModePublicModeTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": True, + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModePublicModeTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_tests(True, False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_download_stay_open_test.py b/tests/onionshare_share_mode_download_stay_open_test.py new file mode 100644 index 00000000..33544217 --- /dev/null +++ b/tests/onionshare_share_mode_download_stay_open_test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class ShareModeStayOpenTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "close_after_first_download": False, + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModeStayOpenTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_tests(False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_download_test.py b/tests/onionshare_share_mode_download_test.py new file mode 100644 index 00000000..8d6d9655 --- /dev/null +++ b/tests/onionshare_share_mode_download_test.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class ShareModeTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModeTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_tests(False, False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_persistent_test.py b/tests/onionshare_share_mode_persistent_test.py new file mode 100644 index 00000000..665aecd5 --- /dev/null +++ b/tests/onionshare_share_mode_persistent_test.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class LocalShareModePersistentSlugTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "use_legacy_v2_onions": True, + "public_mode": False, + "slug": "", + "save_private_key": True, + "close_after_first_download": False, + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModePersistentSlugTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_all_share_mode_tests(self): + self.run_all_share_mode_persistent_tests(False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_stealth_test.py b/tests/onionshare_share_mode_stealth_test.py new file mode 100644 index 00000000..a6bbe08e --- /dev/null +++ b/tests/onionshare_share_mode_stealth_test.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class ShareModeStealthTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "use_legacy_v2_onions": True, + "use_stealth": True, + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModeStealthTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_share_mode_setup_tests(self): + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(False) + + @pytest.mark.run(after='test_run_share_mode_setup_tests') + @pytest.mark.tor + def test_copy_have_hidserv_auth_button(self): + self.copy_have_hidserv_auth_button(self.gui.share_mode) + + @pytest.mark.run(after='test_run_share_mode_setup_tests') + @pytest.mark.tor + def test_hidserv_auth_string(self): + self.hidserv_auth_string() + +if __name__ == "__main__": + unittest.main() diff --git a/tests/onionshare_share_mode_timer_test.py b/tests/onionshare_share_mode_timer_test.py new file mode 100644 index 00000000..32e28c00 --- /dev/null +++ b/tests/onionshare_share_mode_timer_test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .TorGuiShareTest import TorGuiShareTest + +class ShareModeTimerTest(unittest.TestCase, TorGuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "public_mode": False, + "shutdown_timeout": True, + } + cls.gui = TorGuiShareTest.set_up(test_settings, 'ShareModeTimerTest') + + @pytest.mark.tor + def test_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_all_share_mode_timer_tests(self): + self.run_all_share_mode_timer_tests(False) + +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..9112aedd --- /dev/null +++ b/tests/onionshare_share_mode_tor_connection_killed_test.py @@ -0,0 +1,41 @@ +#!/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_run_all_common_setup_tests(self): + self.run_all_common_setup_tests() + + @pytest.mark.run(after='test_run_all_common_setup_tests') + @pytest.mark.tor + def test_run_share_mode_setup_tests(self): + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(False) + + @pytest.mark.run(after='test_run_share_mode_setup_tests') + @pytest.mark.tor + def test_tor_killed_statusbar_message_shown(self): + self.tor_killed_statusbar_message_shown(self.gui.share_mode) + + @pytest.mark.run(after='test_tor_killed_statusbar_message_shown') + @pytest.mark.tor + def test_server_is_stopped(self): + self.server_is_stopped(self.gui.share_mode, False) + + @pytest.mark.run(after='test_tor_killed_statusbar_message_shown') + @pytest.mark.tor + def test_web_service_is_stopped(self): + self.web_service_is_stopped() + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_onionshare_strings.py b/tests/test_onionshare_strings.py index 6d39598c..ea57e3a9 100644 --- a/tests/test_onionshare_strings.py +++ b/tests/test_onionshare_strings.py @@ -23,7 +23,7 @@ import types import pytest from onionshare import strings - +from onionshare.settings import Settings # # Stub get_resource_path so it finds the correct path while running tests # def get_resource_path(filename): @@ -40,6 +40,7 @@ class TestLoadStrings: def test_load_strings_defaults_to_english( self, common_obj, locale_en, sys_onionshare_dev_mode): """ load_strings() loads English by default """ + common_obj.settings = Settings(common_obj) strings.load_strings(common_obj) assert strings._('preparing_files') == "Compressing files." @@ -47,6 +48,7 @@ class TestLoadStrings: def test_load_strings_loads_other_languages( self, common_obj, locale_fr, sys_onionshare_dev_mode): """ load_strings() loads other languages in different locales """ + common_obj.settings = Settings(common_obj) common_obj.settings.set('locale', 'fr') strings.load_strings(common_obj) assert strings._('preparing_files') == "Préparation des fichiers à partager." @@ -55,5 +57,6 @@ class TestLoadStrings: self, common_obj, locale_invalid, sys_onionshare_dev_mode): """ load_strings() raises a KeyError for an invalid locale """ with pytest.raises(KeyError): + common_obj.settings = Settings(common_obj) common_obj.settings.set('locale', 'XX') strings.load_strings(common_obj) diff --git a/tests/test_onionshare_web.py b/tests/test_onionshare_web.py index 24a0e163..d42adde4 100644 --- a/tests/test_onionshare_web.py +++ b/tests/test_onionshare_web.py @@ -31,6 +31,7 @@ import tempfile import pytest from onionshare.common import Common +from onionshare import strings from onionshare.web import Web from onionshare.settings import Settings @@ -41,7 +42,7 @@ RANDOM_STR_REGEX = re.compile(r'^[a-z2-7]+$') def web_obj(common_obj, mode, num_files=0): """ Creates a Web object, in either share mode or receive mode, ready for testing """ common_obj.load_settings() - + strings.load_strings(common_obj) web = Web(common_obj, False, mode) web.generate_slug() web.stay_open = True |