From de9bc975a4d1420a1bdfd52a90fc0b43fdc16975 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 24 Sep 2018 10:41:48 +1000 Subject: [PATCH] Refactor the unit tests to use common, abstracted tests --- unit_tests/commontests.py | 307 ++++++++++++++++++ .../onionshare_receive_mode_upload_test.py | 142 ++------ ...re_receive_mode_upload_test_public_mode.py | 144 ++------ .../onionshare_share_mode_download_test.py | 189 +++-------- ...re_share_mode_download_test_public_mode.py | 186 +++-------- ...hare_share_mode_download_test_stay_open.py | 171 ++++------ unit_tests/onionshare_slug_persistent_test.py | 108 +++--- unit_tests/onionshare_timer_test.py | 86 ++--- 8 files changed, 625 insertions(+), 708 deletions(-) create mode 100644 unit_tests/commontests.py diff --git a/unit_tests/commontests.py b/unit_tests/commontests.py new file mode 100644 index 00000000..1f6f5896 --- /dev/null +++ b/unit_tests/commontests.py @@ -0,0 +1,307 @@ +import os +import requests +import socket +import socks +import zipfile + +from PyQt5 import QtCore, QtTest +from onionshare import strings + +class CommonTests(object): + def test_gui_loaded(self): + '''Test that the GUI actually is shown''' + self.assertTrue(self.gui.show) + + def test_windowTitle_seen(self): + '''Test that the window title is OnionShare''' + self.assertEqual(self.gui.windowTitle(), 'OnionShare') + + def test_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): + '''Test that the status bar is visible''' + self.assertTrue(self.gui.status_bar.isVisible()) + + def test_info_widget_is_not_visible(self, mode): + '''Test that the info widget along top of screen is not shown''' + if mode == 'receive': + self.assertFalse(self.gui.receive_mode.info_widget.isVisible()) + if mode == 'share': + self.assertFalse(self.gui.share_mode.info_widget.isVisible()) + + def test_info_widget_is_visible(self, mode): + '''Test that the info widget along top of screen is shown''' + if mode == 'receive': + self.assertTrue(self.gui.receive_mode.info_widget.isVisible()) + if mode == 'share': + self.assertTrue(self.gui.share_mode.info_widget.isVisible()) + + def test_click_mode(self, mode): + '''Test that we can switch Mode by clicking the button''' + if mode == 'receive': + QtTest.QTest.mouseClick(self.gui.receive_mode_button, QtCore.Qt.LeftButton) + self.assertTrue(self.gui.mode, self.gui.MODE_RECEIVE) + if mode == 'share': + QtTest.QTest.mouseClick(self.gui.share_mode_button, QtCore.Qt.LeftButton) + self.assertTrue(self.gui.mode, self.gui.MODE_SHARE) + + def test_history_is_visible(self, mode): + '''Test that the History section is visible and that the relevant widget is present''' + if mode == 'receive': + self.assertTrue(self.gui.receive_mode.uploads.isVisible()) + self.assertTrue(self.gui.receive_mode.uploads.no_uploads_label.isVisible()) + if mode == 'share': + self.assertTrue(self.gui.share_mode.downloads.isVisible()) + self.assertTrue(self.gui.share_mode.downloads.no_downloads_label.isVisible()) + + def test_server_working_on_start_button_pressed(self, mode): + '''Test we can start the service''' + # Should be in SERVER_WORKING state + if mode == 'receive': + QtTest.QTest.mouseClick(self.gui.receive_mode.server_status.server_button, QtCore.Qt.LeftButton) + self.assertEqual(self.gui.receive_mode.server_status.status, 1) + if mode == 'share': + QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) + self.assertEqual(self.gui.share_mode.server_status.status, 1) + + def test_server_status_indicator_says_starting(self, mode): + '''Test that the Server Status indicator shows we are Starting''' + if mode == 'receive': + self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + if mode == 'share': + self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + + def test_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): + '''Test that the server has started''' + QtTest.QTest.qWait(2000) + # Should now be in SERVER_STARTED state + if mode == 'receive': + self.assertEqual(self.gui.receive_mode.server_status.status, 2) + if mode == 'share': + self.assertEqual(self.gui.share_mode.server_status.status, 2) + + def test_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): + '''Test that we have a valid slug''' + if mode == 'receive': + if not public_mode: + self.assertRegex(self.gui.receive_mode.server_status.web.slug, r'(\w+)-(\w+)') + else: + self.assertIsNone(self.gui.receive_mode.server_status.web.slug, r'(\w+)-(\w+)') + if mode == 'share': + if not public_mode: + self.assertRegex(self.gui.share_mode.server_status.web.slug, r'(\w+)-(\w+)') + else: + self.assertIsNone(self.gui.share_mode.server_status.web.slug, r'(\w+)-(\w+)') + + + def test_url_description_shown(self, mode): + '''Test that the URL label is showing''' + if mode == 'receive': + self.assertTrue(self.gui.receive_mode.server_status.url_description.isVisible()) + if mode == 'share': + self.assertTrue(self.gui.share_mode.server_status.url_description.isVisible()) + + def test_have_copy_url_button(self, mode): + '''Test that the Copy URL button is shown''' + if mode == 'receive': + self.assertTrue(self.gui.receive_mode.server_status.copy_url_button.isVisible()) + if mode == 'share': + self.assertTrue(self.gui.share_mode.server_status.copy_url_button.isVisible()) + + def test_server_status_indicator_says_started(self, mode): + '''Test that the Server Status indicator shows we are started''' + if mode == 'receive': + self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_started', True)) + if mode == 'share': + self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_share_started', True)) + + def test_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: + if mode == 'receive': + path = '/{}'.format(self.gui.receive_mode.server_status.web.slug) + if mode == 'share': + path = '/{}'.format(self.gui.share_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 test_history_widgets_present(self, mode): + '''Test that the relevant widgets are present in the history view after activity has taken place''' + if mode == 'receive': + self.assertFalse(self.gui.receive_mode.uploads.no_uploads_label.isVisible()) + self.assertTrue(self.gui.receive_mode.uploads.clear_history_button.isVisible()) + if mode == 'share': + self.assertFalse(self.gui.share_mode.downloads.no_downloads_label.isVisible()) + self.assertTrue(self.gui.share_mode.downloads.clear_history_button.isVisible()) + + def test_counter_incremented(self, mode, count): + '''Test that the counter has incremented''' + if mode == 'receive': + self.assertEquals(self.gui.receive_mode.uploads_completed, count) + if mode == 'share': + self.assertEquals(self.gui.share_mode.downloads_completed, count) + + def test_server_is_stopped(self, mode, stay_open): + '''Test that the server stops when we click Stop''' + if mode == 'receive': + QtTest.QTest.mouseClick(self.gui.receive_mode.server_status.server_button, QtCore.Qt.LeftButton) + self.assertEquals(self.gui.receive_mode.server_status.status, 0) + if mode == 'share': + if stay_open: + QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) + self.assertEquals(self.gui.share_mode.server_status.status, 0) + + def test_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 test_server_status_indicator_says_closed(self, mode, stay_open): + '''Test that the Server Status indicator shows we closed''' + if mode == 'receive': + self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_stopped', True)) + if mode == 'share': + if stay_open: + self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_stopped', True)) + else: + self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically', True)) + + # Auto-stop timer tests + def test_set_timeout(self, mode): + '''Test that the timeout can be set''' + timer = QtCore.QDateTime.currentDateTime().addSecs(120) + if mode == 'receive': + self.gui.receive_mode.server_status.shutdown_timeout.setDateTime(timer) + self.assertTrue(self.gui.receive_mode.server_status.shutdown_timeout.dateTime(), timer) + if mode == 'share': + self.gui.share_mode.server_status.shutdown_timeout.setDateTime(timer) + self.assertTrue(self.gui.share_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''' + if mode == 'receive': + self.assertFalse(self.gui.receive_mode.server_status.shutdown_timeout_container.isVisible()) + if mode == 'share': + self.assertFalse(self.gui.share_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 + if mode == 'receive': + self.assertEqual(self.gui.receive_mode.server_status.status, 0) + if mode == 'share': + self.assertEqual(self.gui.share_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.assertEquals(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.assertEquals('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()) + diff --git a/unit_tests/onionshare_receive_mode_upload_test.py b/unit_tests/onionshare_receive_mode_upload_test.py index b99faac0..bac622fb 100644 --- a/unit_tests/onionshare_receive_mode_upload_test.py +++ b/unit_tests/onionshare_receive_mode_upload_test.py @@ -2,20 +2,18 @@ import os import sys import unittest -import socket import pytest -import zipfile -import socks import json -import requests -from PyQt5 import QtCore, QtWidgets, QtTest +from PyQt5 import QtWidgets from onionshare.common import Common from onionshare.web import Web from onionshare import onion, strings from onionshare_gui import * +from .commontests import CommonTests + app = QtWidgets.QApplication(sys.argv) class OnionShareGuiTest(unittest.TestCase): @@ -83,176 +81,104 @@ class OnionShareGuiTest(unittest.TestCase): os.remove('/tmp/OnionShare/test-2.txt') @pytest.mark.run(order=1) - def test_gui_loaded_and_tor_bootstrapped(self): - '''Test that the GUI actually is shown''' - self.assertTrue(self.gui.show) + def test_gui_loaded(self): + CommonTests.test_gui_loaded(self) @pytest.mark.run(order=2) def test_windowTitle_seen(self): - '''Test that the window title is OnionShare''' - self.assertEqual(self.gui.windowTitle(), 'OnionShare') + CommonTests.test_windowTitle_seen(self) @pytest.mark.run(order=3) def test_settings_button_is_visible(self): - '''Test that the settings button is visible''' - self.assertTrue(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_visible(self) @pytest.mark.run(order=4) def test_server_status_bar_is_visible(self): - '''Test that the status bar is visible''' - self.assertTrue(self.gui.status_bar.isVisible()) + CommonTests.test_server_status_bar_is_visible(self) @pytest.mark.run(order=5) def test_info_widget_is_not_visible(self): - '''Test that the info widget along top of screen is not shown because we have a file''' - self.assertFalse(self.gui.receive_mode.info_widget.isVisible()) + CommonTests.test_info_widget_is_not_visible(self, 'receive') @pytest.mark.run(order=6) - def test_click_receive_mode(self): - '''Test that we can switch to Receive Mode by clicking the button''' - QtTest.QTest.mouseClick(self.gui.receive_mode_button, QtCore.Qt.LeftButton) - self.assertTrue(self.gui.mode, self.gui.MODE_RECEIVE) + def test_click_mode(self): + CommonTests.test_click_mode(self, 'receive') @pytest.mark.run(order=7) - def test_uploads_section_is_visible(self): - '''Test that the Uploads section is visible and that the No Uploads Yet label is present''' - self.assertTrue(self.gui.receive_mode.uploads.isVisible()) - self.assertTrue(self.gui.receive_mode.uploads.no_uploads_label.isVisible()) + def test_history_is_visible(self): + CommonTests.test_history_is_visible(self, 'receive') @pytest.mark.run(order=8) def test_server_working_on_start_button_pressed(self): - '''Test we can start the service''' - QtTest.QTest.mouseClick(self.gui.receive_mode.server_status.server_button, QtCore.Qt.LeftButton) - - # Should be in SERVER_WORKING state - self.assertEqual(self.gui.receive_mode.server_status.status, 1) + CommonTests.test_server_working_on_start_button_pressed(self, 'receive') @pytest.mark.run(order=9) def test_server_status_indicator_says_starting(self): - '''Test that the Server Status indicator shows we are Starting''' - self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + CommonTests.test_server_status_indicator_says_starting(self, 'receive') @pytest.mark.run(order=10) def test_settings_button_is_hidden(self): - '''Test that the settings button is hidden when the server starts''' - self.assertFalse(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_hidden(self) @pytest.mark.run(order=11) def test_a_server_is_started(self): - '''Test that the server has started''' - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.receive_mode.server_status.status, 2) + CommonTests.test_a_server_is_started(self, 'receive') @pytest.mark.run(order=12) def test_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) - - # Running in local mode, so we have no .onion - #@pytest.mark.run(order=13) - #def test_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') - # self.assertEqual(len(self.gui.app.onion_host), 62) + CommonTests.test_a_web_server_is_running(self) @pytest.mark.run(order=14) def test_have_a_slug(self): - '''Test that we have a valid slug''' - self.assertRegex(self.gui.receive_mode.server_status.web.slug, r'(\w+)-(\w+)') + CommonTests.test_have_a_slug(self, 'receive', False) @pytest.mark.run(order=15) def test_url_description_shown(self): - '''Test that the URL label is showing''' - self.assertTrue(self.gui.receive_mode.server_status.url_description.isVisible()) + CommonTests.test_url_description_shown(self, 'receive') @pytest.mark.run(order=16) def test_have_copy_url_button(self): - '''Test that the Copy URL button is shown''' - self.assertTrue(self.gui.receive_mode.server_status.copy_url_button.isVisible()) + CommonTests.test_have_copy_url_button(self, 'receive') @pytest.mark.run(order=17) - def test_server_status_indicator_says_sharing(self): - '''Test that the Server Status indicator shows we are Receiving''' - self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_started', True)) + def test_server_status_indicator_says_started(self): + CommonTests.test_server_status_indicator_says_started(self, 'receive') @pytest.mark.run(order=18) def test_web_page(self): - '''Test that the web page contains the term Select the files you want to send, then click''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - http_request = 'GET {} HTTP/1.0\r\n'.format(self.gui.receive_mode.server_status.web.slug) - 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('Select the files you want to send, then click "Send Files"' in f.read()) - f.close() + CommonTests.test_web_page(self, 'receive', 'Select the files you want to send, then click', False) @pytest.mark.run(order=19) def test_upload_file(self): - '''Test that we can upload the file''' - files = {'file[]': open('/tmp/test.txt', 'rb')} - response = requests.post('http://127.0.0.1:{}/{}/upload'.format(self.gui.app.port, self.gui.receive_mode.web.slug), files=files) - QtTest.QTest.qWait(2000) - self.assertTrue(os.path.isfile('/tmp/OnionShare/test.txt')) + CommonTests.test_upload_file(self, False, '/tmp/OnionShare/test.txt') @pytest.mark.run(order=20) - def test_uploads_widget_present(self): - '''Test that the No Uploads Yet label is hidden, that Clear History is present''' - self.assertFalse(self.gui.receive_mode.uploads.no_uploads_label.isVisible()) - self.assertTrue(self.gui.receive_mode.uploads.clear_history_button.isVisible()) + def test_history_widgets_present(self): + CommonTests.test_history_widgets_present(self, 'receive') @pytest.mark.run(order=21) - def test_upload_count_incremented(self): - '''Test that the Upload Count has incremented''' - self.assertEquals(self.gui.receive_mode.uploads_completed, 1) + def test_counter_incremented(self): + CommonTests.test_counter_incremented(self, 'receive', 1) @pytest.mark.run(order=22) def test_upload_same_file_is_renamed(self): - '''Test that we can upload the same file and that it gets renamed''' - files = {'file[]': open('/tmp/test.txt', 'rb')} - response = requests.post('http://127.0.0.1:{}/{}/upload'.format(self.gui.app.port, self.gui.receive_mode.web.slug), files=files) - QtTest.QTest.qWait(2000) - self.assertTrue(os.path.isfile('/tmp/OnionShare/test-2.txt')) + CommonTests.test_upload_file(self, False, '/tmp/OnionShare/test-2.txt') @pytest.mark.run(order=23) def test_upload_count_incremented_again(self): - '''Test that the Upload Count has incremented again''' - self.assertEquals(self.gui.receive_mode.uploads_completed, 2) + CommonTests.test_counter_incremented(self, 'receive', 2) @pytest.mark.run(order=24) def test_server_is_stopped(self): - '''Test that the server stops when we click Stop''' - QtTest.QTest.mouseClick(self.gui.receive_mode.server_status.server_button, QtCore.Qt.LeftButton) - self.assertEquals(self.gui.receive_mode.server_status.status, 0) + CommonTests.test_server_is_stopped(self, 'receive', False) @pytest.mark.run(order=25) def test_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) + CommonTests.test_web_service_is_stopped(self) @pytest.mark.run(order=26) def test_server_status_indicator_says_closed(self): - '''Test that the Server Status indicator shows we closed''' - self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_stopped', True)) + CommonTests.test_server_status_indicator_says_closed(self, 'receive', False) if __name__ == "__main__": unittest.main() diff --git a/unit_tests/onionshare_receive_mode_upload_test_public_mode.py b/unit_tests/onionshare_receive_mode_upload_test_public_mode.py index d309e5b1..8ed385f5 100644 --- a/unit_tests/onionshare_receive_mode_upload_test_public_mode.py +++ b/unit_tests/onionshare_receive_mode_upload_test_public_mode.py @@ -2,20 +2,18 @@ import os import sys import unittest -import socket import pytest -import zipfile -import socks import json -import requests -from PyQt5 import QtCore, QtWidgets, QtTest +from PyQt5 import QtWidgets from onionshare.common import Common from onionshare.web import Web from onionshare import onion, strings from onionshare_gui import * +from .commontests import CommonTests + app = QtWidgets.QApplication(sys.argv) class OnionShareGuiTest(unittest.TestCase): @@ -83,176 +81,104 @@ class OnionShareGuiTest(unittest.TestCase): os.remove('/tmp/OnionShare/test-2.txt') @pytest.mark.run(order=1) - def test_gui_loaded_and_tor_bootstrapped(self): - '''Test that the GUI actually is shown''' - self.assertTrue(self.gui.show) + def test_gui_loaded(self): + CommonTests.test_gui_loaded(self) @pytest.mark.run(order=2) def test_windowTitle_seen(self): - '''Test that the window title is OnionShare''' - self.assertEqual(self.gui.windowTitle(), 'OnionShare') + CommonTests.test_windowTitle_seen(self) @pytest.mark.run(order=3) def test_settings_button_is_visible(self): - '''Test that the settings button is visible''' - self.assertTrue(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_visible(self) @pytest.mark.run(order=4) def test_server_status_bar_is_visible(self): - '''Test that the status bar is visible''' - self.assertTrue(self.gui.status_bar.isVisible()) + CommonTests.test_server_status_bar_is_visible(self) @pytest.mark.run(order=5) def test_info_widget_is_not_visible(self): - '''Test that the info widget along top of screen is not shown because we have a file''' - self.assertFalse(self.gui.receive_mode.info_widget.isVisible()) + CommonTests.test_info_widget_is_not_visible(self, 'receive') @pytest.mark.run(order=6) - def test_click_receive_mode(self): - '''Test that we can switch to Receive Mode by clicking the button''' - QtTest.QTest.mouseClick(self.gui.receive_mode_button, QtCore.Qt.LeftButton) - self.assertTrue(self.gui.mode, self.gui.MODE_RECEIVE) + def test_click_mode(self): + CommonTests.test_click_mode(self, 'receive') @pytest.mark.run(order=7) - def test_uploads_section_is_visible(self): - '''Test that the Uploads section is visible and that the No Uploads Yet label is present''' - self.assertTrue(self.gui.receive_mode.uploads.isVisible()) - self.assertTrue(self.gui.receive_mode.uploads.no_uploads_label.isVisible()) + def test_history_is_visible(self): + CommonTests.test_history_is_visible(self, 'receive') @pytest.mark.run(order=8) def test_server_working_on_start_button_pressed(self): - '''Test we can start the service''' - QtTest.QTest.mouseClick(self.gui.receive_mode.server_status.server_button, QtCore.Qt.LeftButton) - - # Should be in SERVER_WORKING state - self.assertEqual(self.gui.receive_mode.server_status.status, 1) + CommonTests.test_server_working_on_start_button_pressed(self, 'receive') @pytest.mark.run(order=9) def test_server_status_indicator_says_starting(self): - '''Test that the Server Status indicator shows we are Starting''' - self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + CommonTests.test_server_status_indicator_says_starting(self, 'receive') @pytest.mark.run(order=10) def test_settings_button_is_hidden(self): - '''Test that the settings button is hidden when the server starts''' - self.assertFalse(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_hidden(self) @pytest.mark.run(order=11) def test_a_server_is_started(self): - '''Test that the server has started''' - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.receive_mode.server_status.status, 2) + CommonTests.test_a_server_is_started(self, 'receive') @pytest.mark.run(order=12) def test_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) - - # Running in local mode, so we have no .onion - #@pytest.mark.run(order=13) - #def test_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') - # self.assertEqual(len(self.gui.app.onion_host), 62) + CommonTests.test_a_web_server_is_running(self) @pytest.mark.run(order=14) - def test_have_no_slug(self): - '''Test that we have a valid slug''' - self.assertIsNone(self.gui.share_mode.server_status.web.slug) + def test_have_a_slug(self): + CommonTests.test_have_a_slug(self, 'receive', True) @pytest.mark.run(order=15) def test_url_description_shown(self): - '''Test that the URL label is showing''' - self.assertTrue(self.gui.receive_mode.server_status.url_description.isVisible()) + CommonTests.test_url_description_shown(self, 'receive') @pytest.mark.run(order=16) def test_have_copy_url_button(self): - '''Test that the Copy URL button is shown''' - self.assertTrue(self.gui.receive_mode.server_status.copy_url_button.isVisible()) + CommonTests.test_have_copy_url_button(self, 'receive') @pytest.mark.run(order=17) - def test_server_status_indicator_says_sharing(self): - '''Test that the Server Status indicator shows we are Receiving''' - self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_started', True)) + def test_server_status_indicator_says_started(self): + CommonTests.test_server_status_indicator_says_started(self, 'receive') @pytest.mark.run(order=18) def test_web_page(self): - '''Test that the web page contains the term Select the files you want to send, then click''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - http_request = 'GET / HTTP/1.0\r\n' - 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('Select the files you want to send, then click "Send Files"' in f.read()) - f.close() + CommonTests.test_web_page(self, 'receive', 'Select the files you want to send, then click', True) @pytest.mark.run(order=19) def test_upload_file(self): - '''Test that we can upload the file''' - files = {'file[]': open('/tmp/test.txt', 'rb')} - response = requests.post('http://127.0.0.1:{}/upload'.format(self.gui.app.port), files=files) - QtTest.QTest.qWait(2000) - self.assertTrue(os.path.isfile('/tmp/OnionShare/test.txt')) + CommonTests.test_upload_file(self, True, '/tmp/OnionShare/test.txt') @pytest.mark.run(order=20) - def test_uploads_widget_present(self): - '''Test that the No Uploads Yet label is hidden, that Clear History is present''' - self.assertFalse(self.gui.receive_mode.uploads.no_uploads_label.isVisible()) - self.assertTrue(self.gui.receive_mode.uploads.clear_history_button.isVisible()) + def test_history_widgets_present(self): + CommonTests.test_history_widgets_present(self, 'receive') @pytest.mark.run(order=21) - def test_upload_count_incremented(self): - '''Test that the Upload Count has incremented''' - self.assertEquals(self.gui.receive_mode.uploads_completed, 1) + def test_counter_incremented(self): + CommonTests.test_counter_incremented(self, 'receive', 1) @pytest.mark.run(order=22) def test_upload_same_file_is_renamed(self): - '''Test that we can upload the same file and that it gets renamed''' - files = {'file[]': open('/tmp/test.txt', 'rb')} - response = requests.post('http://127.0.0.1:{}/upload'.format(self.gui.app.port), files=files) - QtTest.QTest.qWait(2000) - self.assertTrue(os.path.isfile('/tmp/OnionShare/test-2.txt')) + CommonTests.test_upload_file(self, True, '/tmp/OnionShare/test-2.txt') @pytest.mark.run(order=23) def test_upload_count_incremented_again(self): - '''Test that the Upload Count has incremented again''' - self.assertEquals(self.gui.receive_mode.uploads_completed, 2) + CommonTests.test_counter_incremented(self, 'receive', 2) @pytest.mark.run(order=24) def test_server_is_stopped(self): - '''Test that the server stops when we click Stop''' - QtTest.QTest.mouseClick(self.gui.receive_mode.server_status.server_button, QtCore.Qt.LeftButton) - self.assertEquals(self.gui.receive_mode.server_status.status, 0) + CommonTests.test_server_is_stopped(self, 'receive', False) @pytest.mark.run(order=25) def test_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) + CommonTests.test_web_service_is_stopped(self) @pytest.mark.run(order=26) def test_server_status_indicator_says_closed(self): - '''Test that the Server Status indicator shows we closed''' - self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_stopped', True)) + CommonTests.test_server_status_indicator_says_closed(self, 'receive', False) if __name__ == "__main__": unittest.main() diff --git a/unit_tests/onionshare_share_mode_download_test.py b/unit_tests/onionshare_share_mode_download_test.py index c4c8bee7..0ef03e97 100644 --- a/unit_tests/onionshare_share_mode_download_test.py +++ b/unit_tests/onionshare_share_mode_download_test.py @@ -2,19 +2,18 @@ import os import sys import unittest -import socket import pytest -import zipfile -import socks import json -from PyQt5 import QtCore, QtWidgets, QtTest, QtGui +from PyQt5 import QtWidgets from onionshare.common import Common from onionshare.web import Web from onionshare import onion, strings from onionshare_gui import * +from .commontests import CommonTests + app = QtWidgets.QApplication(sys.argv) class OnionShareGuiTest(unittest.TestCase): @@ -80,210 +79,112 @@ class OnionShareGuiTest(unittest.TestCase): os.remove('/tmp/test.txt') @pytest.mark.run(order=1) - def test_gui_loaded_and_tor_bootstrapped(self): - '''Test that the GUI actually is shown''' - self.assertTrue(self.gui.show) + def test_gui_loaded(self): + CommonTests.test_gui_loaded(self) @pytest.mark.run(order=2) def test_windowTitle_seen(self): - '''Test that the window title is OnionShare''' - self.assertEqual(self.gui.windowTitle(), 'OnionShare') + CommonTests.test_windowTitle_seen(self) @pytest.mark.run(order=3) def test_settings_button_is_visible(self): - '''Test that the settings button is visible''' - self.assertTrue(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_visible(self) @pytest.mark.run(order=4) def test_server_status_bar_is_visible(self): - '''Test that the status bar is visible''' - self.assertTrue(self.gui.status_bar.isVisible()) + CommonTests.test_server_status_bar_is_visible(self) @pytest.mark.run(order=5) 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) + CommonTests.test_file_selection_widget_has_a_file(self) @pytest.mark.run(order=6) def test_info_widget_is_visible(self): - '''Test that the info widget along top of screen is shown because we have a file''' - self.assertTrue(self.gui.share_mode.info_widget.isVisible()) + CommonTests.test_info_widget_is_visible(self, 'share') @pytest.mark.run(order=7) - def test_downloads_section_is_visible(self): - '''Test that the Downloads section is visible and that the No Downloads Yet label is present''' - self.assertTrue(self.gui.share_mode.downloads.isVisible()) - self.assertTrue(self.gui.share_mode.downloads.no_downloads_label.isVisible()) + def test_history_is_visible(self): + CommonTests.test_history_is_visible(self, 'share') @pytest.mark.run(order=8) 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()) + CommonTests.test_deleting_only_file_hides_delete_button(self) @pytest.mark.run(order=9) 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.assertEquals(self.gui.share_mode.server_status.file_selection.get_num_files(), 0) + CommonTests.test_add_a_file_and_delete_using_its_delete_widget(self) @pytest.mark.run(order=10) 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) + CommonTests.test_file_selection_widget_readd_files(self) @pytest.mark.run(order=11) def test_server_working_on_start_button_pressed(self): - '''Test we can start the service''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) - - # Should be in SERVER_WORKING state - self.assertEqual(self.gui.share_mode.server_status.status, 1) + CommonTests.test_server_working_on_start_button_pressed(self, 'share') @pytest.mark.run(order=12) def test_server_status_indicator_says_starting(self): - '''Test that the Server Status indicator shows we are Starting''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + CommonTests.test_server_status_indicator_says_starting(self, 'share') @pytest.mark.run(order=13) - def test_add_delete_buttons_now_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_add_delete_buttons_hidden(self): + CommonTests.test_add_delete_buttons_hidden(self) @pytest.mark.run(order=14) def test_settings_button_is_hidden(self): - '''Test that the settings button is hidden when the server starts''' - self.assertFalse(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_hidden(self) @pytest.mark.run(order=15) def test_a_server_is_started(self): - '''Test that the server has started''' - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.share_mode.server_status.status, 2) + CommonTests.test_a_server_is_started(self, 'share') @pytest.mark.run(order=16) def test_a_web_server_is_running(self): - '''Test that the web server has started''' - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + CommonTests.test_a_web_server_is_running(self) - self.assertEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) - - # Running in local mode, so we have no .onion - #@pytest.mark.run(order=17) - #def test_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') - # self.assertEqual(len(self.gui.app.onion_host), 62) + @pytest.mark.run(order=17) + def test_have_a_slug(self): + CommonTests.test_have_a_slug(self, 'share', False) @pytest.mark.run(order=18) - def test_have_a_slug(self): - '''Test that we have a valid slug''' - self.assertRegex(self.gui.share_mode.server_status.web.slug, r'(\w+)-(\w+)') + def test_url_description_shown(self): + CommonTests.test_url_description_shown(self, 'share') @pytest.mark.run(order=19) - def test_url_description_shown(self): - '''Test that the URL label is showing''' - self.assertTrue(self.gui.share_mode.server_status.url_description.isVisible()) + def test_have_copy_url_button(self): + CommonTests.test_have_copy_url_button(self, 'share') @pytest.mark.run(order=20) - def test_have_copy_url_button(self): - '''Test that the Copy URL button is shown and can be copied to clipboard''' - self.assertTrue(self.gui.share_mode.server_status.copy_url_button.isVisible()) - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.copy_url_button, QtCore.Qt.LeftButton) - clipboard = self.gui.qtapp.clipboard() - self.assertEquals(clipboard.text(), 'http://127.0.0.1:{}/{}'.format(self.gui.app.port, self.gui.share_mode.server_status.web.slug)) + def test_server_status_indicator_says_started(self): + CommonTests.test_server_status_indicator_says_started(self, 'share') @pytest.mark.run(order=21) - def test_server_status_indicator_says_sharing(self): - '''Test that the Server Status indicator shows we are Sharing''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_started', True)) + def test_web_page(self): + CommonTests.test_web_page(self, 'share', 'Total size', False) @pytest.mark.run(order=22) - def test_web_page(self): - '''Test that the web page contains the term Total size''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - http_request = 'GET {} HTTP/1.0\r\n'.format(self.gui.share_mode.server_status.web.slug) - 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('Total size' in f.read()) - f.close() + def test_download_share(self): + CommonTests.test_download_share(self, False) @pytest.mark.run(order=23) - def test_download_share(self): - '''Test that we can download the share''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - http_request = 'GET {}/download HTTP/1.0\r\n'.format(self.gui.share_mode.server_status.web.slug) - 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') - self.assertEquals('onionshare', zip.read('test.txt').decode('utf-8')) + def test_history_widgets_present(self): + CommonTests.test_history_widgets_present(self, 'share') @pytest.mark.run(order=24) - def test_downloads_widget_present(self): - QtTest.QTest.qWait(1000) - '''Test that the No Downloads Yet label is hidden, that Clear History is present''' - self.assertFalse(self.gui.share_mode.downloads.no_downloads_label.isVisible()) - self.assertTrue(self.gui.share_mode.downloads.clear_history_button.isVisible()) + def test_server_is_stopped(self): + CommonTests.test_server_is_stopped(self, 'share', False) @pytest.mark.run(order=25) - def test_server_is_stopped(self): - '''Test that the server stopped automatically when we downloaded the share''' - self.assertEquals(self.gui.share_mode.server_status.status, 0) + def test_web_service_is_stopped(self): + CommonTests.test_web_service_is_stopped(self) @pytest.mark.run(order=26) - def test_web_service_is_stopped(self): - '''Test that the web server also stopped''' - 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 test_server_status_indicator_says_closed(self): + CommonTests.test_server_status_indicator_says_closed(self, 'share', False) @pytest.mark.run(order=27) - def test_server_status_indicator_says_closed(self): - '''Test that the Server Status indicator shows we closed because download occurred''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically', True)) - - @pytest.mark.run(order=28) - def test_add_button_visible_again(self): - '''Test that the add button should be visible again''' - self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) + def test_add_button_visible(self): + CommonTests.test_add_button_visible(self) if __name__ == "__main__": diff --git a/unit_tests/onionshare_share_mode_download_test_public_mode.py b/unit_tests/onionshare_share_mode_download_test_public_mode.py index 59905149..417eb8b9 100644 --- a/unit_tests/onionshare_share_mode_download_test_public_mode.py +++ b/unit_tests/onionshare_share_mode_download_test_public_mode.py @@ -2,19 +2,18 @@ import os import sys import unittest -import socket import pytest -import zipfile -import socks import json -from PyQt5 import QtCore, QtWidgets, QtTest +from PyQt5 import QtWidgets from onionshare.common import Common from onionshare.web import Web from onionshare import onion, strings from onionshare_gui import * +from .commontests import CommonTests + app = QtWidgets.QApplication(sys.argv) class OnionShareGuiTest(unittest.TestCase): @@ -80,207 +79,112 @@ class OnionShareGuiTest(unittest.TestCase): os.remove('/tmp/test.txt') @pytest.mark.run(order=1) - def test_gui_loaded_and_tor_bootstrapped(self): - '''Test that the GUI actually is shown''' - self.assertTrue(self.gui.show) + def test_gui_loaded(self): + CommonTests.test_gui_loaded(self) @pytest.mark.run(order=2) def test_windowTitle_seen(self): - '''Test that the window title is OnionShare''' - self.assertEqual(self.gui.windowTitle(), 'OnionShare') + CommonTests.test_windowTitle_seen(self) @pytest.mark.run(order=3) def test_settings_button_is_visible(self): - '''Test that the settings button is visible''' - self.assertTrue(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_visible(self) @pytest.mark.run(order=4) def test_server_status_bar_is_visible(self): - '''Test that the status bar is visible''' - self.assertTrue(self.gui.status_bar.isVisible()) + CommonTests.test_server_status_bar_is_visible(self) @pytest.mark.run(order=5) 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) + CommonTests.test_file_selection_widget_has_a_file(self) @pytest.mark.run(order=6) def test_info_widget_is_visible(self): - '''Test that the info widget along top of screen is shown because we have a file''' - self.assertTrue(self.gui.share_mode.info_widget.isVisible()) + CommonTests.test_info_widget_is_visible(self, 'share') @pytest.mark.run(order=7) - def test_downloads_section_is_visible(self): - '''Test that the Downloads section is visible and that the No Downloads Yet label is present''' - self.assertTrue(self.gui.share_mode.downloads.isVisible()) - self.assertTrue(self.gui.share_mode.downloads.no_downloads_label.isVisible()) + def test_history_is_visible(self): + CommonTests.test_history_is_visible(self, 'share') @pytest.mark.run(order=8) 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()) + CommonTests.test_deleting_only_file_hides_delete_button(self) @pytest.mark.run(order=9) 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.assertEquals(self.gui.share_mode.server_status.file_selection.get_num_files(), 0) + CommonTests.test_add_a_file_and_delete_using_its_delete_widget(self) @pytest.mark.run(order=10) 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) + CommonTests.test_file_selection_widget_readd_files(self) @pytest.mark.run(order=11) def test_server_working_on_start_button_pressed(self): - '''Test we can start the service''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) - - # Should be in SERVER_WORKING state - self.assertEqual(self.gui.share_mode.server_status.status, 1) + CommonTests.test_server_working_on_start_button_pressed(self, 'share') @pytest.mark.run(order=12) def test_server_status_indicator_says_starting(self): - '''Test that the Server Status indicator shows we are Starting''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + CommonTests.test_server_status_indicator_says_starting(self, 'share') @pytest.mark.run(order=13) - def test_add_delete_buttons_now_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_add_delete_buttons_hidden(self): + CommonTests.test_add_delete_buttons_hidden(self) @pytest.mark.run(order=14) def test_settings_button_is_hidden(self): - '''Test that the settings button is hidden when the server starts''' - self.assertFalse(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_hidden(self) @pytest.mark.run(order=15) def test_a_server_is_started(self): - '''Test that the server has started''' - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.share_mode.server_status.status, 2) + CommonTests.test_a_server_is_started(self, 'share') @pytest.mark.run(order=16) def test_a_web_server_is_running(self): - '''Test that the web server has started''' - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + CommonTests.test_a_web_server_is_running(self) - self.assertEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) - - # Running in local mode, so we have no .onion - #@pytest.mark.run(order=17) - #def test_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') - # self.assertEqual(len(self.gui.app.onion_host), 62) + @pytest.mark.run(order=17) + def test_have_a_slug(self): + CommonTests.test_have_a_slug(self, 'share', True) @pytest.mark.run(order=18) - def test_have_no_slug(self): - '''Test that we have a valid slug''' - self.assertIsNone(self.gui.share_mode.server_status.web.slug) + def test_url_description_shown(self): + CommonTests.test_url_description_shown(self, 'share') @pytest.mark.run(order=19) - def test_url_description_shown(self): - '''Test that the URL label is showing''' - self.assertTrue(self.gui.share_mode.server_status.url_description.isVisible()) + def test_have_copy_url_button(self): + CommonTests.test_have_copy_url_button(self, 'share') @pytest.mark.run(order=20) - def test_have_copy_url_button(self): - '''Test that the Copy URL button is shown''' - self.assertTrue(self.gui.share_mode.server_status.copy_url_button.isVisible()) + def test_server_status_indicator_says_started(self): + CommonTests.test_server_status_indicator_says_started(self, 'share') @pytest.mark.run(order=21) - def test_server_status_indicator_says_sharing(self): - '''Test that the Server Status indicator shows we are Sharing''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_started', True)) + def test_web_page(self): + CommonTests.test_web_page(self, 'share', 'Total size', True) @pytest.mark.run(order=22) - def test_web_page(self): - '''Test that the web page contains the term Total size''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - http_request = 'GET / HTTP/1.0\r\n' - 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('Total size' in f.read()) - f.close() + def test_download_share(self): + CommonTests.test_download_share(self, True) @pytest.mark.run(order=23) - def test_download_share(self): - '''Test that we can download the share''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - http_request = 'GET /download HTTP/1.0\r\n' - 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') - self.assertEquals('onionshare', zip.read('test.txt').decode('utf-8')) + def test_history_widgets_present(self): + CommonTests.test_history_widgets_present(self, 'share') @pytest.mark.run(order=24) - def test_downloads_widget_present(self): - QtTest.QTest.qWait(1000) - '''Test that the No Downloads Yet label is hidden, that Clear History is present''' - self.assertFalse(self.gui.share_mode.downloads.no_downloads_label.isVisible()) - self.assertTrue(self.gui.share_mode.downloads.clear_history_button.isVisible()) + def test_server_is_stopped(self): + CommonTests.test_server_is_stopped(self, 'share', False) @pytest.mark.run(order=25) - def test_server_is_stopped(self): - '''Test that the server stopped automatically when we downloaded the share''' - self.assertEquals(self.gui.share_mode.server_status.status, 0) + def test_web_service_is_stopped(self): + CommonTests.test_web_service_is_stopped(self) @pytest.mark.run(order=26) - def test_web_service_is_stopped(self): - '''Test that the web server also stopped''' - 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 test_server_status_indicator_says_closed(self): + CommonTests.test_server_status_indicator_says_closed(self, 'share', False) @pytest.mark.run(order=27) - def test_server_status_indicator_says_closed(self): - '''Test that the Server Status indicator shows we closed because download occurred''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically', True)) - - @pytest.mark.run(order=28) - def test_add_button_visible_again(self): - '''Test that the add button should be visible again''' - self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) + def test_add_button_visible(self): + CommonTests.test_add_button_visible(self) if __name__ == "__main__": diff --git a/unit_tests/onionshare_share_mode_download_test_stay_open.py b/unit_tests/onionshare_share_mode_download_test_stay_open.py index 82a0ac87..c3177a38 100644 --- a/unit_tests/onionshare_share_mode_download_test_stay_open.py +++ b/unit_tests/onionshare_share_mode_download_test_stay_open.py @@ -2,19 +2,18 @@ import os import sys import unittest -import socket import pytest -import zipfile -import socks import json -from PyQt5 import QtCore, QtWidgets, QtTest +from PyQt5 import QtWidgets from onionshare.common import Common from onionshare.web import Web from onionshare import onion, strings from onionshare_gui import * +from .commontests import CommonTests + app = QtWidgets.QApplication(sys.argv) class OnionShareGuiTest(unittest.TestCase): @@ -51,7 +50,7 @@ class OnionShareGuiTest(unittest.TestCase): "hidservauth_string": "", "no_bridges": True, "private_key": "", - "public_mode": False, + "public_mode": True, "receive_allow_receiver_shutdown": True, "save_private_key": False, "shutdown_timeout": False, @@ -80,176 +79,124 @@ class OnionShareGuiTest(unittest.TestCase): os.remove('/tmp/test.txt') @pytest.mark.run(order=1) - def test_gui_loaded_and_tor_bootstrapped(self): - '''Test that the GUI actually is shown''' - self.assertTrue(self.gui.show) + def test_gui_loaded(self): + CommonTests.test_gui_loaded(self) @pytest.mark.run(order=2) def test_windowTitle_seen(self): - '''Test that the window title is OnionShare''' - self.assertEqual(self.gui.windowTitle(), 'OnionShare') + CommonTests.test_windowTitle_seen(self) @pytest.mark.run(order=3) def test_settings_button_is_visible(self): - '''Test that the settings button is visible''' - self.assertTrue(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_visible(self) @pytest.mark.run(order=4) def test_server_status_bar_is_visible(self): - '''Test that the status bar is visible''' - self.assertTrue(self.gui.status_bar.isVisible()) + CommonTests.test_server_status_bar_is_visible(self) @pytest.mark.run(order=5) 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) + CommonTests.test_file_selection_widget_has_a_file(self) @pytest.mark.run(order=6) def test_info_widget_is_visible(self): - '''Test that the info widget along top of screen is shown because we have a file''' - self.assertTrue(self.gui.share_mode.info_widget.isVisible()) + CommonTests.test_info_widget_is_visible(self, 'share') @pytest.mark.run(order=7) - def test_downloads_section_is_visible(self): - '''Test that the Downloads section is visible and that the No Downloads Yet label is present''' - self.assertTrue(self.gui.share_mode.downloads.isVisible()) - self.assertTrue(self.gui.share_mode.downloads.no_downloads_label.isVisible()) + def test_history_is_visible(self): + CommonTests.test_history_is_visible(self, 'share') @pytest.mark.run(order=8) 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()) + CommonTests.test_deleting_only_file_hides_delete_button(self) @pytest.mark.run(order=9) 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.assertEquals(self.gui.share_mode.server_status.file_selection.get_num_files(), 0) + CommonTests.test_add_a_file_and_delete_using_its_delete_widget(self) @pytest.mark.run(order=10) 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) + CommonTests.test_file_selection_widget_readd_files(self) @pytest.mark.run(order=11) def test_server_working_on_start_button_pressed(self): - '''Test we can start the service''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) - - # Should be in SERVER_WORKING state - self.assertEqual(self.gui.share_mode.server_status.status, 1) + CommonTests.test_server_working_on_start_button_pressed(self, 'share') @pytest.mark.run(order=12) def test_server_status_indicator_says_starting(self): - '''Test that the Server Status indicator shows we are Starting''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + CommonTests.test_server_status_indicator_says_starting(self, 'share') @pytest.mark.run(order=13) - def test_add_delete_buttons_now_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_add_delete_buttons_hidden(self): + CommonTests.test_add_delete_buttons_hidden(self) @pytest.mark.run(order=14) def test_settings_button_is_hidden(self): - '''Test that the settings button is hidden when the server starts''' - self.assertFalse(self.gui.settings_button.isVisible()) + CommonTests.test_settings_button_is_hidden(self) @pytest.mark.run(order=15) def test_a_server_is_started(self): - '''Test that the server has started''' - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.share_mode.server_status.status, 2) + CommonTests.test_a_server_is_started(self, 'share') @pytest.mark.run(order=16) def test_a_web_server_is_running(self): - '''Test that the web server has started''' - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + CommonTests.test_a_web_server_is_running(self) - self.assertEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) - - # Running in local mode, so we have no .onion - #@pytest.mark.run(order=17) - #def test_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') - # self.assertEqual(len(self.gui.app.onion_host), 62) + @pytest.mark.run(order=17) + def test_have_a_slug(self): + CommonTests.test_have_a_slug(self, 'share', True) @pytest.mark.run(order=18) - def test_have_a_slug(self): - '''Test that we have a valid slug''' - self.assertRegex(self.gui.share_mode.server_status.web.slug, r'(\w+)-(\w+)') + def test_url_description_shown(self): + CommonTests.test_url_description_shown(self, 'share') @pytest.mark.run(order=19) - def test_url_description_shown(self): - '''Test that the URL label is showing''' - self.assertTrue(self.gui.share_mode.server_status.url_description.isVisible()) + def test_have_copy_url_button(self): + CommonTests.test_have_copy_url_button(self, 'share') @pytest.mark.run(order=20) - def test_have_copy_url_button(self): - '''Test that the Copy URL button is shown''' - self.assertTrue(self.gui.share_mode.server_status.copy_url_button.isVisible()) + def test_server_status_indicator_says_started(self): + CommonTests.test_server_status_indicator_says_started(self, 'share') @pytest.mark.run(order=21) - def test_server_status_indicator_says_sharing(self): - '''Test that the Server Status indicator shows we are Sharing''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_started', True)) + def test_web_page(self): + CommonTests.test_web_page(self, 'share', 'Total size', True) @pytest.mark.run(order=22) def test_download_share(self): - '''Test that we can download the share''' - s = socks.socksocket() - s.settimeout(60) - s.connect(('127.0.0.1', self.gui.app.port)) - - http_request = 'GET {}/download HTTP/1.0\r\n'.format(self.gui.share_mode.server_status.web.slug) - 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') - self.assertEquals('onionshare', zip.read('test.txt').decode('utf-8')) + CommonTests.test_download_share(self, True) @pytest.mark.run(order=23) - def test_downloads_widget_present(self): - QtTest.QTest.qWait(1000) - '''Test that the No Downloads Yet label is hidden, that Clear History is present''' - self.assertFalse(self.gui.share_mode.downloads.no_downloads_label.isVisible()) - self.assertTrue(self.gui.share_mode.downloads.clear_history_button.isVisible()) + def test_history_widgets_present(self): + CommonTests.test_history_widgets_present(self, 'share') @pytest.mark.run(order=24) - def test_server_is_not_stopped(self): - '''Test that the server stayed open after we downloaded the share''' - self.assertEquals(self.gui.share_mode.server_status.status, 2) + def test_counter_incremented(self): + CommonTests.test_counter_incremented(self, 'share', 1) @pytest.mark.run(order=25) - def test_web_service_is_running(self): - '''Test that the web server is still running''' - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.assertEquals(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) + def test_download_share_again(self): + CommonTests.test_download_share(self, True) @pytest.mark.run(order=26) - def test_download_count_incremented(self): - '''Test that the Download Count has incremented''' - self.assertEquals(self.gui.share_mode.downloads_completed, 1) + def test_counter_incremented_again(self): + CommonTests.test_counter_incremented(self, 'share', 2) + + @pytest.mark.run(order=27) + def test_server_is_stopped(self): + CommonTests.test_server_is_stopped(self, 'share', True) + + @pytest.mark.run(order=28) + def test_web_service_is_stopped(self): + CommonTests.test_web_service_is_stopped(self) + + @pytest.mark.run(order=29) + def test_server_status_indicator_says_closed(self): + CommonTests.test_server_status_indicator_says_closed(self, 'share', True) + + @pytest.mark.run(order=30) + def test_add_button_visible(self): + CommonTests.test_add_button_visible(self) if __name__ == "__main__": diff --git a/unit_tests/onionshare_slug_persistent_test.py b/unit_tests/onionshare_slug_persistent_test.py index d0dcd08a..05c9719a 100644 --- a/unit_tests/onionshare_slug_persistent_test.py +++ b/unit_tests/onionshare_slug_persistent_test.py @@ -2,19 +2,18 @@ import os import sys import unittest -import socket import pytest -import zipfile -import socks import json -from PyQt5 import QtCore, QtWidgets, QtTest +from PyQt5 import QtWidgets from onionshare.common import Common from onionshare.web import Web from onionshare import onion, strings from onionshare_gui import * +from .commontests import CommonTests + app = QtWidgets.QApplication(sys.argv) class OnionShareGuiTest(unittest.TestCase): @@ -83,79 +82,86 @@ class OnionShareGuiTest(unittest.TestCase): @pytest.mark.run(order=1) def test_gui_loaded(self): - '''Test that the GUI actually is shown''' - self.assertTrue(self.gui.show) + CommonTests.test_gui_loaded(self) @pytest.mark.run(order=2) - def test_server_working_on_start_button_pressed(self): - '''Test we can start the service''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) - - # Should be in SERVER_WORKING state - self.assertEqual(self.gui.share_mode.server_status.status, 1) + def test_windowTitle_seen(self): + CommonTests.test_windowTitle_seen(self) @pytest.mark.run(order=3) - def test_server_status_indicator_says_starting(self): - '''Test that the Server Status indicator shows we are Starting''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + def test_settings_button_is_visible(self): + CommonTests.test_settings_button_is_visible(self) @pytest.mark.run(order=4) - def test_a_server_is_started(self): - '''Test that the server has started''' - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.share_mode.server_status.status, 2) + def test_server_status_bar_is_visible(self): + CommonTests.test_server_status_bar_is_visible(self) @pytest.mark.run(order=5) - def test_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_info_widget_is_visible(self): + CommonTests.test_info_widget_is_visible(self, 'share') @pytest.mark.run(order=6) + def test_history_is_visible(self): + CommonTests.test_history_is_visible(self, 'share') + + @pytest.mark.run(order=7) + def test_server_working_on_start_button_pressed(self): + CommonTests.test_server_working_on_start_button_pressed(self, 'share') + + @pytest.mark.run(order=8) + def test_server_status_indicator_says_starting(self): + CommonTests.test_server_status_indicator_says_starting(self, 'share') + + @pytest.mark.run(order=9) + def test_settings_button_is_hidden(self): + CommonTests.test_settings_button_is_hidden(self) + + @pytest.mark.run(order=10) + def test_a_server_is_started(self): + CommonTests.test_a_server_is_started(self, 'share') + + @pytest.mark.run(order=11) + def test_a_web_server_is_running(self): + CommonTests.test_a_web_server_is_running(self) + + @pytest.mark.run(order=12) def test_have_a_slug(self): - '''Test that we have a valid slug''' - self.assertRegex(self.gui.share_mode.server_status.web.slug, r'(\w+)-(\w+)') + CommonTests.test_have_a_slug(self, 'share', False) global slug slug = self.gui.share_mode.server_status.web.slug - @pytest.mark.run(order=7) - def test_server_can_be_stopped(self): - '''Test we can stop the service''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) + @pytest.mark.run(order=13) + def test_server_status_indicator_says_started(self): + CommonTests.test_server_status_indicator_says_started(self, 'share') - # Should be in SERVER_STOPPED state - self.assertEqual(self.gui.share_mode.server_status.status, 0) + @pytest.mark.run(order=14) + def test_server_is_stopped(self): + CommonTests.test_server_is_stopped(self, 'share', True) - @pytest.mark.run(order=8) + @pytest.mark.run(order=15) def test_web_service_is_stopped(self): - '''Test that the web server also stopped''' - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - QtTest.QTest.qWait(4000) + CommonTests.test_web_service_is_stopped(self) - # We should be closed by now. Fail if not! - self.assertNotEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) + @pytest.mark.run(order=16) + def test_server_status_indicator_says_closed(self): + CommonTests.test_server_status_indicator_says_closed(self, 'share', True) - @pytest.mark.run(order=9) + @pytest.mark.run(order=17) def test_server_started_again(self): - '''Test we can start the service again''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.share_mode.server_status.status, 2) + CommonTests.test_server_working_on_start_button_pressed(self, 'share') + CommonTests.test_server_status_indicator_says_starting(self, 'share') + CommonTests.test_a_server_is_started(self, 'share') - @pytest.mark.run(order=10) + @pytest.mark.run(order=18) def test_have_same_slug(self): '''Test that we have the same slug''' self.assertEqual(self.gui.share_mode.server_status.web.slug, slug) - @pytest.mark.run(order=11) + @pytest.mark.run(order=19) def test_server_is_stopped_again(self): - '''Test that we can stop the server''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) - QtTest.QTest.qWait(1000) - self.assertEqual(self.gui.share_mode.server_status.status, 0) + CommonTests.test_server_is_stopped(self, 'share', True) + CommonTests.test_web_service_is_stopped(self) + if __name__ == "__main__": unittest.main() diff --git a/unit_tests/onionshare_timer_test.py b/unit_tests/onionshare_timer_test.py index ed20c1c0..34c77f12 100644 --- a/unit_tests/onionshare_timer_test.py +++ b/unit_tests/onionshare_timer_test.py @@ -2,19 +2,18 @@ import os import sys import unittest -import socket import pytest -import zipfile -import socks import json -from PyQt5 import QtCore, QtWidgets, QtTest +from PyQt5 import QtWidgets from onionshare.common import Common from onionshare.web import Web from onionshare import onion, strings from onionshare_gui import * +from .commontests import CommonTests + app = QtWidgets.QApplication(sys.argv) class OnionShareGuiTest(unittest.TestCase): @@ -81,62 +80,63 @@ class OnionShareGuiTest(unittest.TestCase): @pytest.mark.run(order=1) def test_gui_loaded(self): - '''Test that the GUI actually is shown''' - self.assertTrue(self.gui.show) + CommonTests.test_gui_loaded(self) @pytest.mark.run(order=2) - def test_set_timeout(self): - '''Test that the timeout can be set''' - timer = QtCore.QDateTime.currentDateTime().addSecs(120) - self.gui.share_mode.server_status.shutdown_timeout.setDateTime(timer) - self.assertTrue(self.gui.share_mode.server_status.shutdown_timeout.dateTime(), timer) + def test_windowTitle_seen(self): + CommonTests.test_windowTitle_seen(self) @pytest.mark.run(order=3) - def test_server_working_on_start_button_pressed(self): - '''Test we can start the service''' - QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton) - - # Should be in SERVER_WORKING state - self.assertEqual(self.gui.share_mode.server_status.status, 1) + def test_settings_button_is_visible(self): + CommonTests.test_settings_button_is_visible(self) @pytest.mark.run(order=4) - def test_server_status_indicator_says_starting(self): - '''Test that the Server Status indicator shows we are Starting''' - self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_working', True)) + def test_server_status_bar_is_visible(self): + CommonTests.test_server_status_bar_is_visible(self) @pytest.mark.run(order=5) - def test_a_server_is_started(self): - '''Test that the server has started''' - QtTest.QTest.qWait(2000) - # Should now be in SERVER_STARTED state - self.assertEqual(self.gui.share_mode.server_status.status, 2) + def test_file_selection_widget_has_a_file(self): + CommonTests.test_file_selection_widget_has_a_file(self) @pytest.mark.run(order=6) - def test_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_info_widget_is_visible(self): + CommonTests.test_info_widget_is_visible(self, 'share') @pytest.mark.run(order=7) - def test_timeout_widget_hidden(self): - '''Test that the timeout widget is hidden when share has started''' - self.assertFalse(self.gui.share_mode.server_status.shutdown_timeout_container.isVisible()) + def test_history_is_visible(self): + CommonTests.test_history_is_visible(self, 'share') @pytest.mark.run(order=8) - def test_server_timed_out(self): - '''Test that the server has timed out after the timer ran out''' - QtTest.QTest.qWait(100000) - # We should have timed out now - self.assertEqual(self.gui.share_mode.server_status.status, 0) + def test_set_timeout(self): + CommonTests.test_set_timeout(self, 'share') @pytest.mark.run(order=9) - def test_web_service_is_stopped(self): - '''Test that the web server also stopped''' - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + def test_server_working_on_start_button_pressed(self): + CommonTests.test_server_working_on_start_button_pressed(self, 'share') - # We should be closed by now. Fail if not! - self.assertNotEqual(sock.connect_ex(('127.0.0.1',self.gui.app.port)), 0) + @pytest.mark.run(order=10) + def test_server_status_indicator_says_starting(self): + CommonTests.test_server_status_indicator_says_starting(self, 'share') + + @pytest.mark.run(order=11) + def test_a_server_is_started(self): + CommonTests.test_a_server_is_started(self, 'share') + + @pytest.mark.run(order=12) + def test_a_web_server_is_running(self): + CommonTests.test_a_web_server_is_running(self) + + @pytest.mark.run(order=13) + def test_timeout_widget_hidden(self): + CommonTests.test_timeout_widget_hidden(self, 'share') + + @pytest.mark.run(order=14) + def test_timeout(self): + CommonTests.test_server_timed_out(self, 'share', 120000) + + @pytest.mark.run(order=15) + def test_web_service_is_stopped(self): + CommonTests.test_web_service_is_stopped(self) if __name__ == "__main__": unittest.main()